From b7ca55caba929078d7280a4e7d6aeb02c5e5d406 Mon Sep 17 00:00:00 2001 From: Roel van Duijnhoven Date: Thu, 23 Nov 2017 13:39:01 +0100 Subject: [PATCH 1/4] Make SharedEventManager usable Works now similar to how https://github.com/zendframework/zend-eventmanager/blob/master/src/EventManagerAwareTrait.php works. --- src/ZfcUser/EventManager/EventProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ZfcUser/EventManager/EventProvider.php b/src/ZfcUser/EventManager/EventProvider.php index 46ef8599..cc6c277d 100644 --- a/src/ZfcUser/EventManager/EventProvider.php +++ b/src/ZfcUser/EventManager/EventProvider.php @@ -47,7 +47,7 @@ public function setEventManager(EventManagerInterface $events) public function getEventManager() { if (!$this->events instanceof EventManagerInterface) { - $this->setEventManager(new EventManager(new SharedEventManager())); + $this->setEventManager(new EventManager()); } return $this->events; } From ffe3fc80a0fad5a54c1b1763c24d7f8a7a31cd78 Mon Sep 17 00:00:00 2001 From: Roel van Duijnhoven Date: Mon, 27 Nov 2017 11:36:09 +0100 Subject: [PATCH 2/4] Inject EventManager ourselves. Such that attached listeners set in constructor will not be overwritten later by Zend/Mvc's initializer. --- .../Authentication/Adapter/AdapterChainServiceFactory.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ZfcUser/Authentication/Adapter/AdapterChainServiceFactory.php b/src/ZfcUser/Authentication/Adapter/AdapterChainServiceFactory.php index 06c89e16..a5af75e7 100644 --- a/src/ZfcUser/Authentication/Adapter/AdapterChainServiceFactory.php +++ b/src/ZfcUser/Authentication/Adapter/AdapterChainServiceFactory.php @@ -16,6 +16,7 @@ class AdapterChainServiceFactory implements FactoryInterface public function __invoke(ContainerInterface $serviceLocator, $requestedName, array $options = null) { $chain = new AdapterChain(); + $chain->setEventManager($serviceLocator->get('EventManager')); $options = $this->getOptions($serviceLocator); From aaff500b4c5a6f94f241472a01d29f055694c48b Mon Sep 17 00:00:00 2001 From: Roel van Duijnhoven Date: Mon, 27 Nov 2017 11:53:00 +0100 Subject: [PATCH 3/4] Use proper way to pass custom event. This way the listeners in ZF2 style do not have to be adapted. --- .../Authentication/Adapter/AdapterChain.php | 19 +++++++++++++------ .../Adapter/ChainableAdapter.php | 4 ++-- src/ZfcUser/Authentication/Adapter/Db.php | 9 ++++----- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/ZfcUser/Authentication/Adapter/AdapterChain.php b/src/ZfcUser/Authentication/Adapter/AdapterChain.php index a7502386..b15857ed 100644 --- a/src/ZfcUser/Authentication/Adapter/AdapterChain.php +++ b/src/ZfcUser/Authentication/Adapter/AdapterChain.php @@ -5,6 +5,7 @@ use Zend\Authentication\Adapter\AdapterInterface; use Zend\Authentication\Result as AuthenticationResult; use Zend\EventManager\Event; +use Zend\EventManager\EventInterface; use Zend\EventManager\EventManagerAwareTrait; use Zend\Stdlib\RequestInterface as Request; use Zend\Stdlib\ResponseInterface as Response; @@ -51,11 +52,13 @@ public function prepareForAuthentication(Request $request) $e = $this->getEvent(); $e->setRequest($request); - $this->getEventManager()->trigger('authenticate.pre', $e); + $e->setName('authenticate.pre'); + $this->getEventManager()->triggerEvent($e); - $result = $this->getEventManager()->trigger('authenticate', $e, function ($test) { + $e->setName('authenticate'); + $result = $this->getEventManager()->triggerEventUntil(function ($test) { return ($test instanceof Response); - }); + }, $e); if ($result->stopped()) { if ($result->last() instanceof Response) { @@ -71,11 +74,13 @@ public function prepareForAuthentication(Request $request) } if ($e->getIdentity()) { - $this->getEventManager()->trigger('authenticate.success', $e); + $e->setName('authenticate.success'); + $this->getEventManager()->triggerEvent($e); return true; } - $this->getEventManager()->trigger('authenticate.fail', $e); + $e->setName('authenticate.fail'); + $this->getEventManager()->triggerEvent($e); return false; } @@ -109,7 +114,9 @@ public function resetAdapters() public function logoutAdapters() { //Adapters might need to perform additional cleanup after logout - $this->getEventManager()->trigger('logout', $this->getEvent()); + $e = $this->getEvent(); + $e->setName('logout'); + $this->getEventManager()->triggerEvent($e); } /** diff --git a/src/ZfcUser/Authentication/Adapter/ChainableAdapter.php b/src/ZfcUser/Authentication/Adapter/ChainableAdapter.php index 748d7e1d..8f297f0f 100644 --- a/src/ZfcUser/Authentication/Adapter/ChainableAdapter.php +++ b/src/ZfcUser/Authentication/Adapter/ChainableAdapter.php @@ -8,10 +8,10 @@ interface ChainableAdapter { /** - * @param EventInterface $e + * @param AdapterChainEvent $e * @return bool */ - public function authenticate(EventInterface $e); + public function authenticate(AdapterChainEvent $e); /** * @return StorageInterface diff --git a/src/ZfcUser/Authentication/Adapter/Db.php b/src/ZfcUser/Authentication/Adapter/Db.php index 891e3e91..b6187464 100644 --- a/src/ZfcUser/Authentication/Adapter/Db.php +++ b/src/ZfcUser/Authentication/Adapter/Db.php @@ -36,20 +36,19 @@ class Db extends AbstractAdapter /** * Called when user id logged out - * @param EventInterface $e + * @param AdapterChainEvent $e */ - public function logout(EventInterface $e) + public function logout(AdapterChainEvent $e) { $this->getStorage()->clear(); } /** - * @param EventInterface $e + * @param AdapterChainEvent $e * @return bool */ - public function authenticate(EventInterface $e) + public function authenticate(AdapterChainEvent $e) { - $e = $e->getTarget(); if ($this->isSatisfied()) { $storage = $this->getStorage()->read(); $e->setIdentity($storage['identity']) From 1c4c15e005fa918f93bfb50a4bf5a966457bd065 Mon Sep 17 00:00:00 2001 From: Roel van Duijnhoven Date: Mon, 27 Nov 2017 11:54:51 +0100 Subject: [PATCH 4/4] Remove triggers that are unsuable. The only way to use them was via the shared service manager. But in ZF3 this manager is only instantiated after construction. Thus this has no function anymore. --- src/ZfcUser/Form/Base.php | 2 -- src/ZfcUser/Form/ChangeEmail.php | 2 -- src/ZfcUser/Form/ChangePassword.php | 2 -- src/ZfcUser/Form/Login.php | 2 -- src/ZfcUser/Form/LoginFilter.php | 2 -- src/ZfcUser/Form/RegisterFilter.php | 2 -- 6 files changed, 12 deletions(-) diff --git a/src/ZfcUser/Form/Base.php b/src/ZfcUser/Form/Base.php index 5972eae0..88d1b2ae 100644 --- a/src/ZfcUser/Form/Base.php +++ b/src/ZfcUser/Form/Base.php @@ -85,8 +85,6 @@ public function __construct($name = null) //$csrf = new Element\Csrf('csrf'); //$csrf->getValidator()->setTimeout($this->getRegistrationOptions()->getUserFormTimeout()); //$this->add($csrf); - - $this->getEventManager()->trigger('init', $this); } public function init() diff --git a/src/ZfcUser/Form/ChangeEmail.php b/src/ZfcUser/Form/ChangeEmail.php index 556432a0..98798a35 100644 --- a/src/ZfcUser/Form/ChangeEmail.php +++ b/src/ZfcUser/Form/ChangeEmail.php @@ -65,8 +65,6 @@ public function __construct($name, AuthenticationOptionsInterface $options) 'type' => 'submit' ), )); - - $this->getEventManager()->trigger('init', $this); } /** diff --git a/src/ZfcUser/Form/ChangePassword.php b/src/ZfcUser/Form/ChangePassword.php index 52dada45..0dabd0bc 100644 --- a/src/ZfcUser/Form/ChangePassword.php +++ b/src/ZfcUser/Form/ChangePassword.php @@ -66,8 +66,6 @@ public function __construct($name, AuthenticationOptionsInterface $options) 'type' => 'submit' ), )); - - $this->getEventManager()->trigger('init', $this); } /** diff --git a/src/ZfcUser/Form/Login.php b/src/ZfcUser/Form/Login.php index 46966778..740c314f 100644 --- a/src/ZfcUser/Form/Login.php +++ b/src/ZfcUser/Form/Login.php @@ -66,8 +66,6 @@ public function __construct($name, AuthenticationOptionsInterface $options) $this->add($submitElement, array( 'priority' => -100, )); - - $this->getEventManager()->trigger('init', $this); } /** diff --git a/src/ZfcUser/Form/LoginFilter.php b/src/ZfcUser/Form/LoginFilter.php index 3f98e3e6..37eae3f2 100644 --- a/src/ZfcUser/Form/LoginFilter.php +++ b/src/ZfcUser/Form/LoginFilter.php @@ -38,7 +38,5 @@ public function __construct(AuthenticationOptionsInterface $options) array('name' => 'StringTrim'), ), )); - - $this->getEventManager()->trigger('init', $this); } } diff --git a/src/ZfcUser/Form/RegisterFilter.php b/src/ZfcUser/Form/RegisterFilter.php index d9fe348d..1e1319bb 100644 --- a/src/ZfcUser/Form/RegisterFilter.php +++ b/src/ZfcUser/Form/RegisterFilter.php @@ -99,8 +99,6 @@ public function __construct($emailValidator, $usernameValidator, RegistrationOpt ), ), )); - - $this->getEventManager()->trigger('init', $this); } public function getEmailValidator()