Commit 4f5c6f4d authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Ajout possibilité d'écouter l'événement...

Ajout possibilité d'écouter l'événement \UnicaenAuth\Service\User::EVENT_USER_AUTHENTICATED_PRE_PERSIST pour modifier l'entité avant qu'elle de soit persistée dans la table des utilisateurs.
parent 5d7e0bc5
......@@ -54,10 +54,10 @@ class AbstractFactory implements AbstractFactoryInterface
}
if ($adapter instanceof \Zend\EventManager\EventManagerAwareInterface) {
$userService = $serviceLocator->get('unicaen-auth_user_service');
$eventManager = $serviceLocator->get('event_manager');
$eventManager->attach('userAuthenticated', array($userService, 'userAuthenticated'), 1);
$adapter->setEventManager($eventManager);
$userService = $serviceLocator->get('unicaen-auth_user_service'); /* @var $userService \UnicaenAuth\Service\User */
$eventManager->attach('userAuthenticated', array($userService, 'userAuthenticated'), 100);
}
return $adapter;
......
......@@ -7,6 +7,8 @@ use UnicaenApp\Mapper\Ldap\People as LdapPeopleMapper;
use UnicaenAuth\Options\ModuleOptions;
use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\ServiceManagerAwareInterface;
use Zend\EventManager\EventManagerAwareInterface;
use Zend\EventManager\EventManagerInterface;
use ZfcUser\Authentication\Adapter\AdapterChainEvent as AuthEvent;
use ZfcUser\Options\AuthenticationOptionsInterface;
use ZfcUser\Options\ModuleOptions as ZfcUserModuleOptions;
......@@ -21,13 +23,20 @@ use ZfcUser\Options\ModuleOptions as ZfcUserModuleOptions;
* @see \UnicaenAuth\Authentication\Adapter\AbstractFactory
* @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
*/
class User implements ServiceManagerAwareInterface
class User implements ServiceManagerAwareInterface, EventManagerAwareInterface
{
const EVENT_USER_AUTHENTICATED_PRE_PERSIST = 'userAuthenticated.prePersist';
/**
* @var ServiceManager
*/
protected $serviceManager;
/**
* @var EventManagerInterface
*/
protected $eventManager;
/**
* @var ModuleOptions
*/
......@@ -89,6 +98,14 @@ class User implements ServiceManagerAwareInterface
$entity->setDisplayName($ldapPeople->getDisplayName());
$entity->setPassword('ldap');
$entity->setState(in_array('deactivated', ldap_explode_dn($ldapPeople->getDn(), 1)) ? 0 : 1);
// trigger pre-persist event
$event = new \Zend\EventManager\Event(self::EVENT_USER_AUTHENTICATED_PRE_PERSIST);
$event->setTarget($this)
->setParams(array('entity' => $entity, 'ldapPeople' => $ldapPeople));
$this->getEventManager()->trigger($event);
// persist
$mapper->$method($entity);
}
catch (PDOException $pdoe) {
......@@ -118,6 +135,34 @@ class User implements ServiceManagerAwareInterface
$this->serviceManager = $serviceManager;
return $this;
}
/**
* Retrieve the event manager
*
* Lazy-loads an EventManager instance if none registered.
*
* @return EventManagerInterface
*/
public function getEventManager()
{
return $this->eventManager;
}
/**
* Inject an EventManager instance
*
* @param EventManagerInterface $eventManager
* @return void
*/
public function setEventManager(EventManagerInterface $eventManager)
{
$eventManager->setIdentifiers(array(
__CLASS__,
get_called_class(),
));
$this->eventManager = $eventManager;
return $this;
}
/**
* get ldap people mapper
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment