Commit 61d92528 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Déclenchement d'un nouvel événement UserAuthenticatedEvent::POST_PERSIST après...

Déclenchement d'un nouvel événement UserAuthenticatedEvent::POST_PERSIST après que l'entité est persistée
parent af3c6d6f
...@@ -28,11 +28,24 @@ abstract class AuthenticatedUserSavedAbstractListener implements ListenerAggrega ...@@ -28,11 +28,24 @@ abstract class AuthenticatedUserSavedAbstractListener implements ListenerAggrega
protected $listeners = []; protected $listeners = [];
/** /**
* Renseigne les relations 'intervenant' et 'personnel' avant que l'objet soit persisté. * Méthode appelée juste avant que l'entité utilisateur soit persistée.
* *
* @param Event $e * @param UserAuthenticatedEvent $e
*/ */
abstract public function onUserAuthenticatedPrePersist(UserAuthenticatedEvent $e); public function onUserAuthenticatedPrePersist(UserAuthenticatedEvent $e)
{
}
/**
* Méthode appelée juste après que l'entité utilisateur soit persistée.
*
* @param UserAuthenticatedEvent $e
*/
public function onUserAuthenticatedPostPersist(UserAuthenticatedEvent $e)
{
}
/** /**
* Attach one or more listeners * Attach one or more listeners
...@@ -41,25 +54,28 @@ abstract class AuthenticatedUserSavedAbstractListener implements ListenerAggrega ...@@ -41,25 +54,28 @@ abstract class AuthenticatedUserSavedAbstractListener implements ListenerAggrega
* implementation will pass this to the aggregate. * implementation will pass this to the aggregate.
* *
* @param EventManagerInterface $events * @param EventManagerInterface $events
*
* @return void
*/ */
public function attach(EventManagerInterface $events) public function attach(EventManagerInterface $events)
{ {
$sharedEvents = $events->getSharedManager(); $sharedEvents = $events->getSharedManager();
$this->listeners[] = $sharedEvents->attach( $this->listeners[] = $sharedEvents->attach(
'UnicaenAuth\Service\User', 'UnicaenAuth\Service\User',
UserAuthenticatedEvent::PRE_PERSIST, UserAuthenticatedEvent::PRE_PERSIST,
[$this, 'onUserAuthenticatedPrePersist'], [$this, 'onUserAuthenticatedPrePersist'],
100); 100);
$this->listeners[] = $sharedEvents->attach(
'UnicaenAuth\Service\User',
UserAuthenticatedEvent::POST_PERSIST,
[$this, 'onUserAuthenticatedPostPersist'],
100);
} }
/** /**
* Detach all previously attached listeners * Detach all previously attached listeners
* *
* @param EventManagerInterface $events * @param EventManagerInterface $events
*
* @return void
*/ */
public function detach(EventManagerInterface $events) public function detach(EventManagerInterface $events)
{ {
......
...@@ -14,7 +14,8 @@ use ZfcUser\Entity\UserInterface; ...@@ -14,7 +14,8 @@ use ZfcUser\Entity\UserInterface;
*/ */
class UserAuthenticatedEvent extends Event class UserAuthenticatedEvent extends Event
{ {
const PRE_PERSIST = 'prePersist'; const PRE_PERSIST = 'prePersist'; // avant que l'entité soit persistée
const POST_PERSIST = 'postPersist'; // après que l'entité est persistée
const PARAM_DB_USER = 'db_user'; const PARAM_DB_USER = 'db_user';
const PARAM_LDAP_USER = 'ldap_user'; const PARAM_LDAP_USER = 'ldap_user';
......
...@@ -114,10 +114,15 @@ class User implements ServiceLocatorAwareInterface, EventManagerAwareInterface ...@@ -114,10 +114,15 @@ class User implements ServiceLocatorAwareInterface, EventManagerAwareInterface
$entity->setState($state); $entity->setState($state);
// pre-persist // pre-persist
$this->triggerUserAuthenticatedEvent($entity, $userData); $event = new UserAuthenticatedEvent(UserAuthenticatedEvent::PRE_PERSIST);
$this->triggerEvent($event, $entity, $userData);
// persist // persist
$mapper->$method($entity); $mapper->$method($entity);
// post-persist
$event = new UserAuthenticatedEvent(UserAuthenticatedEvent::POST_PERSIST);
$this->triggerEvent($event, $entity, $userData);
} }
catch (PDOException $pdoe) { catch (PDOException $pdoe) {
throw new RuntimeException("Impossible d'enregistrer l'utilisateur authentifié dans la base de données.", null, $pdoe); throw new RuntimeException("Impossible d'enregistrer l'utilisateur authentifié dans la base de données.", null, $pdoe);
...@@ -127,15 +132,11 @@ class User implements ServiceLocatorAwareInterface, EventManagerAwareInterface ...@@ -127,15 +132,11 @@ class User implements ServiceLocatorAwareInterface, EventManagerAwareInterface
} }
/** /**
* Déclenche l'événement donnant aux applications clientes l'opportunité de modifier l'entité * @param UserInterface $entity
* utilisateur avant qu'elle ne soit persistée.
*
* @param mixed $entity
* @param People|ShibUser $userData * @param People|ShibUser $userData
*/ */
private function triggerUserAuthenticatedEvent($entity, $userData) private function triggerEvent(UserAuthenticatedEvent $event, $entity, $userData)
{ {
$event = new UserAuthenticatedEvent(UserAuthenticatedEvent::PRE_PERSIST);
$event->setTarget($this); $event->setTarget($this);
$event->setDbUser($entity); $event->setDbUser($entity);
if ($userData instanceof People) { if ($userData instanceof People) {
......
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