Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion lib/Controller/AdminCodeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
use OCP\Group\ISubAdmin;
use OCP\IGroupManager;
use OCP\IRequest;
use OCP\IUserManager;
use OCP\IUserSession;

class AdminCodeController extends Controller {

Expand All @@ -41,26 +44,54 @@ class AdminCodeController extends Controller {
/** @var CodeStorage */
private $codeStorage;

/** @var IGroupManager */
private $groupManager;

/** @var ISubAdmin */
private $subAdmin;

/** @var IUserSession */
private $userSession;

public function __construct(IRequest $request,
IUserManager $userManager,
CodeStorage $codeStorage) {
IGroupManager $groupManager,
ISubAdmin $subAdmin,
CodeStorage $codeStorage,
IUserSession $userSession) {
parent::__construct(Application::APP_ID, $request);
$this->codeStorage = $codeStorage;
$this->userManager = $userManager;
$this->groupManager = $groupManager;
$this->subAdmin = $subAdmin;
$this->userSession = $userSession;
}

/**
* @param string $uid
*
* @SubAdminRequired
*
* @return JSONResponse
*/
public function create(string $uid): JSONResponse {
$currentUser = $this->userSession->getUser();
$user = $this->userManager->get($uid);

if ($currentUser === null) {
// This is pretty much impossible
return new JSONResponse(null, Http::STATUS_BAD_REQUEST);
}
if ($user === null) {
return new JSONResponse(null, Http::STATUS_NOT_FOUND);
}

if (!$this->groupManager->isAdmin($currentUser->getUID())
&& !$this->subAdmin->isUserAccessible($currentUser, $user)) {
// Nope
return new JSONResponse(null, Http::STATUS_FORBIDDEN);
}

return new JSONResponse([
'code' => $this->codeStorage->generateCode($user),
'validFor' => CodeStorage::CODE_TTL,
Expand Down
3 changes: 2 additions & 1 deletion lib/Settings/AdminSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@
use OCA\TwoFactorAdmin\AppInfo\Application;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\Settings\ISettings;
use OCP\Settings\ISubAdminSettings;

class AdminSettings implements ISettings {
class AdminSettings implements ISubAdminSettings {

public function getForm() {
return new TemplateResponse(Application::APP_ID, 'settings-admin');
Expand Down
2 changes: 2 additions & 0 deletions src/components/CodeGenerator.vue
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@
.catch(e => {
if (e.response && e.response.status === 404) {
this.error = t('twofactor_admin', 'user {uid} does not exist', {uid: this.uid})
} else if (e.response && e.response.status === 403) {
this.error = t('twofactor_admin', 'you are not allowed to generate codes for this user')
} else {
this.error = t('twofactor_admin', 'unknown error', {uid: this.uid})
}
Expand Down