Skip to content

Commit

Permalink
Merge pull request #374 from nextcloud/implement-IDeactivatableByAdmin
Browse files Browse the repository at this point in the history
Implement IDeactivatableByAdmin interface
  • Loading branch information
ChristophWurst authored Jan 21, 2025
2 parents 4dfcc85 + c2a9ea7 commit 6e5ba6f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 21 deletions.
36 changes: 15 additions & 21 deletions lib/Provider/AdminProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,20 @@
namespace OCA\TwoFactorAdmin\Provider;

use OCA\TwoFactorAdmin\Service\CodeStorage;
use OCP\Authentication\TwoFactorAuth\IDeactivatableByAdmin;
use OCP\Authentication\TwoFactorAuth\IProvider;
use OCP\Authentication\TwoFactorAuth\IProvidesIcons;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\Template;

class AdminProvider implements IProvider, IProvidesIcons {
class AdminProvider implements IProvider, IProvidesIcons, IDeactivatableByAdmin {

/** @var string */
private $appName;

/** @var IL10N */
private $l10n;

/** @var IURLGenerator */
private $urlGenerator;

/** @var CodeStorage */
private $codeStorage;

public function __construct(string $appName,
IL10N $l10n,
IURLGenerator $urlGenerator,
CodeStorage $codeStorage) {
$this->appName = $appName;
$this->l10n = $l10n;
$this->urlGenerator = $urlGenerator;
$this->codeStorage = $codeStorage;
public function __construct(private string $appName,
private IL10N $l10n,
private IURLGenerator $urlGenerator,
private CodeStorage $codeStorage) {
}

/**
Expand Down Expand Up @@ -102,6 +87,15 @@ public function isTwoFactorAuthEnabledForUser(IUser $user): bool {
return $this->codeStorage->hasCode($user);
}

/**
* Disable this provider for the given user.
*
* @param IUser $user the user to deactivate this provider for
*/
public function disableFor(IUser $user): void {
$this->codeStorage->removeCodesForUser($user);
}

public function getLightIcon(): String {
return $this->urlGenerator->imagePath('core', 'actions/more-white.svg');
}
Expand Down
5 changes: 5 additions & 0 deletions lib/Service/CodeStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,9 @@ public function validateCode(IUser $user, string $code): bool {
}
}

public function removeCodesForUser(IUser $user): void {
$this->codeMapper->deleteAll($user);
$this->eventDispatcher->dispatch(StateChanged::class, new StateChanged($user, false));
}

}
9 changes: 9 additions & 0 deletions tests/Unit/Provider/AdminProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ public function testIsTwoFactorAuthEnabled() {
$this->assertTrue($enabled);
}

public function testDisableForUser() {
$user = $this->createMock(IUser::class);
$this->codeStorage->expects($this->once())
->method('removeCodesForUser')
->with($user);

$this->provider->disableFor($user);
}

public function testGetDarkIcon() {
$this->urlGenerator->expects($this->once())
->method('imagePath')
Expand Down
13 changes: 13 additions & 0 deletions tests/Unit/Service/CodeStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use ChristophWurst\Nextcloud\Testing\TestCase;
use OCA\TwoFactorAdmin\Db\Code;
use OCA\TwoFactorAdmin\Db\CodeMapper;
use OCA\TwoFactorAdmin\Event\StateChanged;
use OCA\TwoFactorAdmin\Service\CodeStorage;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
Expand Down Expand Up @@ -153,4 +154,16 @@ public function testValidateValidCode() {
$this->assertTrue($valid);
}

public function testRemoveCodesForUser() {
$user = $this->createMock(IUser::class);
$this->codeMapper->expects($this->once())
->method("deleteAll")
->with($user);
$this->eventDispatcher->expects($this->once())
->method('dispatch')
->with(StateChanged::class, new StateChanged($user, false));

$this->codeStorage->removeCodesForUser($user);
}

}

0 comments on commit 6e5ba6f

Please sign in to comment.