diff options
author | Morris Jobke <hey@morrisjobke.de> | 2020-07-09 23:11:28 +0300 |
---|---|---|
committer | Morris Jobke <hey@morrisjobke.de> | 2020-07-09 23:27:42 +0300 |
commit | 305683caad95b182749b4fc924509d7b8135569d (patch) | |
tree | 987774e49f8706f823d505e98b5078d8d63d93c9 /lib | |
parent | 94fd328113a04a13417e6b333266ad9190f01a0f (diff) |
Use IBootstrap for the app bootstrap
Signed-off-by: Morris Jobke <hey@morrisjobke.de>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/AppInfo/Application.php | 113 | ||||
-rw-r--r-- | lib/Listener/BeforePasswordUpdatedEventListener.php | 46 | ||||
-rw-r--r-- | lib/Listener/BeforeUserLoggedInEventListener.php | 46 | ||||
-rw-r--r-- | lib/Listener/FailedLoginListener.php | 2 | ||||
-rw-r--r-- | lib/Listener/GenerateSecurePasswordEventListener.php | 47 | ||||
-rw-r--r-- | lib/Listener/PasswordUpdatedEventListener.php | 46 | ||||
-rw-r--r-- | lib/Listener/SuccesfullLoginListener.php | 1 | ||||
-rw-r--r-- | lib/Listener/ValidatePasswordPolicyEventListener.php | 47 |
8 files changed, 258 insertions, 90 deletions
diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 7d6c68e..7dc6a04 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -25,15 +25,19 @@ declare(strict_types=1); namespace OCA\Password_Policy\AppInfo; use OCA\Password_Policy\Capabilities; -use OCA\Password_Policy\ComplianceService; -use OCA\Password_Policy\Generator; +use OCA\Password_Policy\Listener\BeforePasswordUpdatedEventListener; +use OCA\Password_Policy\Listener\BeforeUserLoggedInEventListener; use OCA\Password_Policy\Listener\FailedLoginListener; +use OCA\Password_Policy\Listener\GenerateSecurePasswordEventListener; +use OCA\Password_Policy\Listener\PasswordUpdatedEventListener; use OCA\Password_Policy\Listener\SuccesfullLoginListener; +use OCA\Password_Policy\Listener\ValidatePasswordPolicyEventListener; use OCA\Password_Policy\PasswordValidator; use OCP\AppFramework\App; +use OCP\AppFramework\Bootstrap\IBootContext; +use OCP\AppFramework\Bootstrap\IBootstrap; +use OCP\AppFramework\Bootstrap\IRegistrationContext; use OCP\Authentication\Events\LoginFailedEvent; -use OCP\EventDispatcher\Event; -use OCP\EventDispatcher\IEventDispatcher; use OCP\ILogger; use OCP\Security\Events\GenerateSecurePasswordEvent; use OCP\Security\Events\ValidatePasswordPolicyEvent; @@ -43,104 +47,39 @@ use OCP\User\Events\PasswordUpdatedEvent; use OCP\User\Events\UserLoggedInEvent; use Symfony\Component\EventDispatcher\GenericEvent; -class Application extends App { +class Application extends App implements IBootstrap { public function __construct() { parent::__construct('password_policy'); - $container = $this->getContainer(); - - $server = $container->getServer(); - /** @var IEventDispatcher $eventDispatcher */ - $eventDispatcher = $server->query(IEventDispatcher::class); - - /** register capabilities */ - $container->registerCapability(Capabilities::class); + } - $eventDispatcher->addListener( - ValidatePasswordPolicyEvent::class, - function (Event $event) use ($container) { - if (!($event instanceof ValidatePasswordPolicyEvent)) { - return; - } + public function register(IRegistrationContext $context): void { + $context->registerCapability(Capabilities::class); - /** @var PasswordValidator $validator */ - $validator = $container->query(PasswordValidator::class); - $validator->validate($event->getPassword()); - } - ); - $eventDispatcher->addListener( - GenerateSecurePasswordEvent::class, - function (Event $event) use ($container) { - if (!($event instanceof GenerateSecurePasswordEvent)) { - return; - } - - /** @var Generator */ - $generator = $container->query(Generator::class); - $event->setPassword($generator->generate()); - } - ); - $eventDispatcher->addListener( - BeforePasswordUpdatedEvent::class, - function (Event $event) use ($container) { - if(!($event instanceof BeforePasswordUpdatedEvent)) { - return; - } - /** @var ComplianceService $complianceUpdater */ - $complianceUpdater = $container->query(ComplianceService::class); - $complianceUpdater->audit($event->getUser(), $event->getPassword()); - } - ); - $eventDispatcher->addListener( - PasswordUpdatedEvent::class, - function (Event $event) use ($container) { - if(!($event instanceof PasswordUpdatedEvent)) { - return; - } - /** @var ComplianceService $complianceUpdater */ - $complianceUpdater = $container->query(ComplianceService::class); - $complianceUpdater->update($event->getUser(), $event->getPassword()); - } - ); - $eventDispatcher->addListener( - BeforeUserLoggedInEvent::class, - function (Event $event) use ($container) { - if(!$event instanceof BeforeUserLoggedInEvent) { - return; - } - /** @var ComplianceService $complianceUpdater */ - $complianceUpdater = $container->query(ComplianceService::class); - $complianceUpdater->entryControl($event->getUsername(), $event->getPassword()); - } - ); + $context->registerEventListener(ValidatePasswordPolicyEvent::class, ValidatePasswordPolicyEventListener::class); + $context->registerEventListener(GenerateSecurePasswordEvent::class, GenerateSecurePasswordEventListener::class); + $context->registerEventListener(BeforePasswordUpdatedEvent::class, BeforePasswordUpdatedEventListener::class); + $context->registerEventListener(PasswordUpdatedEvent::class, PasswordUpdatedEventListener::class); + $context->registerEventListener(BeforeUserLoggedInEvent::class, BeforeUserLoggedInEventListener::class); + $context->registerEventListener(LoginFailedEvent::class, FailedLoginListener::class); + $context->registerEventListener(UserLoggedInEvent::class, SuccesfullLoginListener::class); + } - $eventDispatcher->addServiceListener(LoginFailedEvent::class, FailedLoginListener::class); - $eventDispatcher->addServiceListener(UserLoggedInEvent::class, SuccesfullLoginListener::class); + public function boot(IBootContext $context): void { + $server = $context->getServerContainer(); - // TODO: remove these two legacy event listeners + // TODO: remove this legacy event listener once https://github.com/nextcloud/user_sql/pull/146 is in $symfonyDispatcher = $server->getEventDispatcher(); $symfonyDispatcher->addListener( 'OCP\PasswordPolicy::validate', - function (GenericEvent $event) use ($container) { + function (GenericEvent $event) use ($server) { /** @var ILogger $logger */ - $logger = $container->query(ILogger::class); + $logger = $server->query(ILogger::class); $logger->debug('OCP\PasswordPolicy::validate is deprecated. Listen to ' . ValidatePasswordPolicyEvent::class . ' instead'); /** @var PasswordValidator $validator */ - $validator = $container->query(PasswordValidator::class); + $validator = $server->query(PasswordValidator::class); $validator->validate($event->getSubject()); } ); - $symfonyDispatcher->addListener( - 'OCP\PasswordPolicy::generate', - function (GenericEvent $event) use ($container) { - /** @var ILogger $logger */ - $logger = $container->query(ILogger::class); - $logger->debug('OCP\PasswordPolicy::generate is deprecated. Listen to ' . GenerateSecurePasswordEvent::class . ' instead'); - - /** @var Generator */ - $generator = $container->query(Generator::class); - $event->setArgument('password', $generator->generate()); - } - ); } } diff --git a/lib/Listener/BeforePasswordUpdatedEventListener.php b/lib/Listener/BeforePasswordUpdatedEventListener.php new file mode 100644 index 0000000..e54bec9 --- /dev/null +++ b/lib/Listener/BeforePasswordUpdatedEventListener.php @@ -0,0 +1,46 @@ +<?php +declare(strict_types=1); +/** + * @copyright Copyright (c) 2020 Morris Jobke <hey@morrisjobke.de> + * + * @author Morris Jobke <hey@morrisjobke.de> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\Password_Policy\Listener; + +use OCA\Password_Policy\ComplianceService; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\User\Events\BeforePasswordUpdatedEvent; + +class BeforePasswordUpdatedEventListener implements IEventListener { + /** @var ComplianceService */ + private $complianceUpdater; + + public function __construct(ComplianceService $complianceUpdater) { + $this->complianceUpdater = $complianceUpdater; + } + + public function handle(Event $event): void { + if(!($event instanceof BeforePasswordUpdatedEvent)) { + return; + } + $this->complianceUpdater->audit($event->getUser(), $event->getPassword()); + } +} diff --git a/lib/Listener/BeforeUserLoggedInEventListener.php b/lib/Listener/BeforeUserLoggedInEventListener.php new file mode 100644 index 0000000..3544b35 --- /dev/null +++ b/lib/Listener/BeforeUserLoggedInEventListener.php @@ -0,0 +1,46 @@ +<?php +declare(strict_types=1); +/** + * @copyright Copyright (c) 2020 Morris Jobke <hey@morrisjobke.de> + * + * @author Morris Jobke <hey@morrisjobke.de> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\Password_Policy\Listener; + +use OCA\Password_Policy\ComplianceService; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\User\Events\BeforeUserLoggedInEvent; + +class BeforeUserLoggedInEventListener implements IEventListener { + /** @var ComplianceService */ + private $complianceUpdater; + + public function __construct(ComplianceService $complianceUpdater) { + $this->complianceUpdater = $complianceUpdater; + } + + public function handle(Event $event): void { + if(!$event instanceof BeforeUserLoggedInEvent) { + return; + } + $this->complianceUpdater->entryControl($event->getUsername(), $event->getPassword()); + } +} diff --git a/lib/Listener/FailedLoginListener.php b/lib/Listener/FailedLoginListener.php index 33a9d4d..87def7a 100644 --- a/lib/Listener/FailedLoginListener.php +++ b/lib/Listener/FailedLoginListener.php @@ -30,7 +30,6 @@ use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; class FailedLoginListener implements IEventListener { - /** @var FailedLoginCompliance */ private $compliance; @@ -45,5 +44,4 @@ class FailedLoginListener implements IEventListener { $this->compliance->onFailedLogin($event->getUid()); } - } diff --git a/lib/Listener/GenerateSecurePasswordEventListener.php b/lib/Listener/GenerateSecurePasswordEventListener.php new file mode 100644 index 0000000..b88f32c --- /dev/null +++ b/lib/Listener/GenerateSecurePasswordEventListener.php @@ -0,0 +1,47 @@ +<?php +declare(strict_types=1); +/** + * @copyright Copyright (c) 2020 Morris Jobke <hey@morrisjobke.de> + * + * @author Morris Jobke <hey@morrisjobke.de> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\Password_Policy\Listener; + +use OCA\Password_Policy\Generator; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\Security\Events\GenerateSecurePasswordEvent; + +class GenerateSecurePasswordEventListener implements IEventListener { + /** @var Generator */ + private $generator; + + public function __construct(Generator $generator) { + $this->generator = $generator; + } + + public function handle(Event $event): void { + if (!($event instanceof GenerateSecurePasswordEvent)) { + return; + } + + $event->setPassword($this->generator->generate()); + } +} diff --git a/lib/Listener/PasswordUpdatedEventListener.php b/lib/Listener/PasswordUpdatedEventListener.php new file mode 100644 index 0000000..e4ae863 --- /dev/null +++ b/lib/Listener/PasswordUpdatedEventListener.php @@ -0,0 +1,46 @@ +<?php +declare(strict_types=1); +/** + * @copyright Copyright (c) 2020 Morris Jobke <hey@morrisjobke.de> + * + * @author Morris Jobke <hey@morrisjobke.de> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\Password_Policy\Listener; + +use OCA\Password_Policy\ComplianceService; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\User\Events\PasswordUpdatedEvent; + +class PasswordUpdatedEventListener implements IEventListener { + /** @var ComplianceService */ + private $complianceUpdater; + + public function __construct(ComplianceService $complianceUpdater) { + $this->complianceUpdater = $complianceUpdater; + } + + public function handle(Event $event): void { + if(!($event instanceof PasswordUpdatedEvent)) { + return; + } + $this->complianceUpdater->update($event->getUser(), $event->getPassword()); + } +} diff --git a/lib/Listener/SuccesfullLoginListener.php b/lib/Listener/SuccesfullLoginListener.php index 55a93b0..f8b42ed 100644 --- a/lib/Listener/SuccesfullLoginListener.php +++ b/lib/Listener/SuccesfullLoginListener.php @@ -44,5 +44,4 @@ class SuccesfullLoginListener implements IEventListener { $this->compliance->onSucessfullLogin($event->getUser()); } - } diff --git a/lib/Listener/ValidatePasswordPolicyEventListener.php b/lib/Listener/ValidatePasswordPolicyEventListener.php new file mode 100644 index 0000000..5596528 --- /dev/null +++ b/lib/Listener/ValidatePasswordPolicyEventListener.php @@ -0,0 +1,47 @@ +<?php +declare(strict_types=1); +/** + * @copyright Copyright (c) 2020 Morris Jobke <hey@morrisjobke.de> + * + * @author Morris Jobke <hey@morrisjobke.de> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\Password_Policy\Listener; + +use OCA\Password_Policy\PasswordValidator; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\Security\Events\ValidatePasswordPolicyEvent; + +class ValidatePasswordPolicyEventListener implements IEventListener { + /** @var PasswordValidator */ + private $passwordValidator; + + public function __construct(PasswordValidator $passwordValidator) { + $this->passwordValidator = $passwordValidator; + } + + public function handle(Event $event): void { + if (!($event instanceof ValidatePasswordPolicyEvent)) { + return; + } + + $this->passwordValidator->validate($event->getPassword()); + } +} |