Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use typed events #90

Merged
merged 2 commits into from
Nov 27, 2019
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
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ Currently the app checks passwords for public link shares and for user passwords
You can easily check passwords for your own app by adding following code to your app:

````
$eventDispatcher = \OC::$server->getEventDispatcher();
$event = new Symfony\Component\EventDispatcher\GenericEvent($password);
$eventDispatcher->dispatch('OCP\PasswordPolicy::validate', $event);
$eventDispatcher = \OC::$server->query(IEventDispatcher::class);
$event = new \OCP\Security\Events\GenerateSecurePasswordEvent();
$eventDispatcher->dispatchTyped($event);
$password = $event->getPassword() ?? 'fallback when this app is not enabled';
````
52 changes: 47 additions & 5 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
use OCA\Password_Policy\Generator;
use OCA\Password_Policy\PasswordValidator;
use OCP\AppFramework\App;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\ILogger;
use OCP\Security\Events\GenerateSecurePasswordEvent;
use OCP\Security\Events\ValidatePasswordPolicyEvent;
use Symfony\Component\EventDispatcher\GenericEvent;

class Application extends App {
Expand All @@ -36,21 +41,58 @@ public function __construct() {
$container = $this->getContainer();

$server = $container->getServer();
$eventDispatcher = $server->getEventDispatcher();
/** @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;
}

/** @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());
}
);

// TODO: remove these two legacy event listeners
$symfonyDispatcher = $server->getEventDispatcher();
$symfonyDispatcher->addListener(
'OCP\PasswordPolicy::validate',
function (GenericEvent $event) use ($container) {
/** @var ILogger $logger */
$logger = $container->query(ILogger::class);
$logger->debug('OCP\PasswordPolicy::validate is deprecated. Listen to ' . ValidatePasswordPolicyEvent::class . ' instead');

$eventDispatcher->addListener('OCP\PasswordPolicy::validate',
function(GenericEvent $event) use ($container) {
/** @var PasswordValidator $validator */
$validator = $container->query(PasswordValidator::class);
$validator->validate($event->getSubject());
}
);
$eventDispatcher->addListener('OCP\PasswordPolicy::generate',
function(GenericEvent $event) use ($container) {
$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());
Expand Down