Fix state propagation to the server registry

Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
This commit is contained in:
Christoph Wurst 2018-07-31 08:35:56 +02:00
Родитель 9259ca5622
Коммит 24d2da94e4
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: CC42AC2A7F0E56D8
3 изменённых файлов: 121 добавлений и 0 удалений

Просмотреть файл

@ -17,6 +17,7 @@ namespace OCA\TwoFactorU2F\AppInfo;
use OCA\TwoFactorU2F\Event\StateChanged;
use OCA\TwoFactorU2F\Listener\IListener;
use OCA\TwoFactorU2F\Listener\StateChangeActivity;
use OCA\TwoFactorU2F\Listener\StateChangeRegistryUpdater;
use OCP\AppFramework\App;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
@ -32,6 +33,7 @@ class Application extends App {
/** @var IListener[] $listeners */
$listeners = [
$container->query(StateChangeActivity::class),
$container->query(StateChangeRegistryUpdater::class),
];
foreach ($listeners as $listener) {

Просмотреть файл

@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
/**
* Nextcloud - U2F 2FA
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @copyright Christoph Wurst 2018
*/
namespace OCA\TwoFactorU2F\Listener;
use OCA\TwoFactorU2F\Event\StateChanged;
use OCA\TwoFactorU2F\Provider\U2FProvider;
use OCP\Authentication\TwoFactorAuth\IRegistry;
use Symfony\Component\EventDispatcher\Event;
class StateChangeRegistryUpdater implements IListener {
/** @var IRegistry */
private $providerRegistry;
/** @var U2FProvider */
private $provider;
public function __construct(IRegistry $providerRegistry, U2FProvider $provider) {
$this->providerRegistry = $providerRegistry;
$this->provider = $provider;
}
public function handle(Event $event) {
if ($event instanceof StateChanged) {
if ($event->isEnabled()) {
$this->providerRegistry->enableProviderFor($this->provider, $event->getUser());
} else {
$this->providerRegistry->disableProviderFor($this->provider, $event->getUser());
}
}
}
}

Просмотреть файл

@ -0,0 +1,75 @@
<?php
/**
* Created by PhpStorm.
* User: christoph
* Date: 31.07.18
* Time: 08:11
*/
namespace OCA\TwoFactorU2F\Tests\Unit\Listener;
use OCA\TwoFactorU2F\Event\StateChanged;
use OCA\TwoFactorU2F\Listener\StateChangeRegistryUpdater;
use OCA\TwoFactorU2F\Provider\U2FProvider;
use OCP\Authentication\TwoFactorAuth\IRegistry;
use OCP\IUser;
use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_MockObject_MockObject;
use Symfony\Component\EventDispatcher\Event;
class StateChangeRegistryUpdaterTest extends TestCase {
/** @var IRegistry|PHPUnit_Framework_MockObject_MockObject */
private $providerRegistry;
/** @var U2FProvider|PHPUnit_Framework_MockObject_MockObjec */
private $provider;
/** @var StateChangeRegistryUpdater */
private $listener;
protected function setUp() {
parent::setUp();
$this->providerRegistry = $this->createMock(IRegistry::class);
$this->provider = $this->createMock(U2FProvider::class);
$this->listener = new StateChangeRegistryUpdater($this->providerRegistry, $this->provider);
}
public function testHandleGenericEvent() {
$event = $this->createMock(Event::class);
$this->providerRegistry->expects($this->never())
->method('enableProviderFor');
$this->providerRegistry->expects($this->never())
->method('disableProviderFor');
$this->listener->handle($event);
}
public function testHandleEnableEvent() {
$user = $this->createMock(IUser::class);
$event = new StateChanged($user, true);
$this->providerRegistry->expects($this->once())
->method('enableProviderFor')
->with(
$this->provider,
$user
);
$this->listener->handle($event);
}
public function testHandleDisableEvent() {
$user = $this->createMock(IUser::class);
$event = new StateChanged($user, false);
$this->providerRegistry->expects($this->once())
->method('disableProviderFor')
->with(
$this->provider,
$user
);
$this->listener->handle($event);
}
}