Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
This commit is contained in:
Roeland Jago Douma 2019-08-27 21:33:28 +02:00 коммит произвёл Christoph Wurst
Родитель 5aef56d7d4
Коммит df34aae355
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: CC42AC2A7F0E56D8
7 изменённых файлов: 26 добавлений и 67 удалений

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

@ -20,6 +20,7 @@ use OCA\TwoFactorU2F\Listener\IListener;
use OCA\TwoFactorU2F\Listener\StateChangeActivity;
use OCA\TwoFactorU2F\Listener\StateChangeRegistryUpdater;
use OCP\AppFramework\App;
use OCP\EventDispatcher\IEventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class Application extends App {
@ -30,29 +31,12 @@ class Application extends App {
parent::__construct(self::APP_ID, $urlParams);
$container = $this->getContainer();
/** @var EventDispatcherInterface $eventDispatcher */
$eventDispatcher = $container->getServer()->getEventDispatcher();
$eventDispatcher->addListener(StateChanged::class, function (StateChanged $event) use ($container) {
/** @var IListener[] $listeners */
$listeners = [
$container->query(StateChangeActivity::class),
$container->query(StateChangeRegistryUpdater::class),
];
foreach ($listeners as $listener) {
$listener->handle($event);
}
});
$eventDispatcher->addListener(DisabledByAdmin::class, function (DisabledByAdmin $event) use ($container) {
/** @var IListener[] $listeners */
$listeners = [
$container->query(StateChangeActivity::class),
];
foreach ($listeners as $listener) {
$listener->handle($event);
}
});
/** @var IEventDispatcher $eventDispatcher */
$eventDispatcher = $container->query(IEventDispatcher::class);
$eventDispatcher->addServiceListener(StateChanged::class, StateChangeActivity::class);
$eventDispatcher->addServiceListener(StateChanged::class, StateChangeRegistryUpdater::class);
$eventDispatcher->addServiceListener(DisabledByAdmin::class, StateChangeActivity::class);
}
}

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

@ -14,8 +14,8 @@ declare(strict_types=1);
namespace OCA\TwoFactorU2F\Event;
use OCP\EventDispatcher\Event;
use OCP\IUser;
use Symfony\Component\EventDispatcher\Event;
class StateChanged extends Event {
@ -26,6 +26,8 @@ class StateChanged extends Event {
private $enabled;
public function __construct(IUser $user, bool $enabled) {
parent::__construct();
$this->user = $user;
$this->enabled = $enabled;
}

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

@ -1,23 +0,0 @@
<?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 Symfony\Component\EventDispatcher\Event;
interface IListener {
public function handle(Event $event);
}

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

@ -17,9 +17,10 @@ namespace OCA\TwoFactorU2F\Listener;
use OCA\TwoFactorU2F\Event\DisabledByAdmin;
use OCA\TwoFactorU2F\Event\StateChanged;
use OCP\Activity\IManager;
use Symfony\Component\EventDispatcher\Event;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
class StateChangeActivity implements IListener {
class StateChangeActivity implements IEventListener {
/** @var IManager */
private $activityManager;
@ -28,7 +29,7 @@ class StateChangeActivity implements IListener {
$this->activityManager = $activityManager;
}
public function handle(Event $event) {
public function handle(Event $event): void {
if ($event instanceof StateChanged) {
if ($event instanceof DisabledByAdmin) {
$subject = 'u2f_disabled_by_admin';

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

@ -18,9 +18,10 @@ use OCA\TwoFactorU2F\Event\StateChanged;
use OCA\TwoFactorU2F\Provider\U2FProvider;
use OCA\TwoFactorU2F\Service\U2FManager;
use OCP\Authentication\TwoFactorAuth\IRegistry;
use Symfony\Component\EventDispatcher\Event;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
class StateChangeRegistryUpdater implements IListener {
class StateChangeRegistryUpdater implements IEventListener {
/** @var IRegistry */
private $providerRegistry;
@ -37,7 +38,7 @@ class StateChangeRegistryUpdater implements IListener {
$this->manager = $manager;
}
public function handle(Event $event) {
public function handle(Event $event): void {
if ($event instanceof StateChanged) {
$devices = $this->manager->getDevices($event->getUser());
if ($event->isEnabled() && count($devices) === 1) {
@ -49,4 +50,4 @@ class StateChangeRegistryUpdater implements IListener {
}
}
}
}
}

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

@ -1,10 +1,4 @@
<?php
/**
* Created by PhpStorm.
* User: christoph
* Date: 31.07.18
* Time: 07:48
*/
namespace OCA\TwoFactorU2F\Tests\Unit\Listener;
@ -13,10 +7,10 @@ use OCA\TwoFactorU2F\Event\StateChanged;
use OCA\TwoFactorU2F\Listener\StateChangeActivity;
use OCP\Activity\IEvent;
use OCP\Activity\IManager;
use OCP\EventDispatcher\Event;
use OCP\IUser;
use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_MockObject_MockObject;
use Symfony\Component\EventDispatcher\Event;
class StateChangeActivityTest extends TestCase {
@ -35,7 +29,7 @@ class StateChangeActivityTest extends TestCase {
}
public function testHandleGenericEvent() {
$event = $this->createMock(Event::class);
$event = new Event();
$this->activityManager->expects($this->never())
->method('publish');

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

@ -13,20 +13,20 @@ use OCA\TwoFactorU2F\Listener\StateChangeRegistryUpdater;
use OCA\TwoFactorU2F\Provider\U2FProvider;
use OCA\TwoFactorU2F\Service\U2FManager;
use OCP\Authentication\TwoFactorAuth\IRegistry;
use OCP\EventDispatcher\Event;
use OCP\IUser;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_MockObject_MockObject;
use Symfony\Component\EventDispatcher\Event;
class StateChangeRegistryUpdaterTest extends TestCase {
/** @var IRegistry|PHPUnit_Framework_MockObject_MockObject */
/** @var IRegistry|MockObject */
private $providerRegistry;
/** @var U2FManager|PHPUnit_Framework_MockObject_MockObjec */
/** @var U2FManager|MockObject */
private $manager;
/** @var U2FProvider|PHPUnit_Framework_MockObject_MockObjec */
/** @var U2FProvider|MockObject */
private $provider;
/** @var StateChangeRegistryUpdater */
@ -43,7 +43,7 @@ class StateChangeRegistryUpdaterTest extends TestCase {
}
public function testHandleGenericEvent() {
$event = $this->createMock(Event::class);
$event = new Event();
$this->providerRegistry->expects($this->never())
->method('enableProviderFor');
$this->providerRegistry->expects($this->never())