Skip to content
Snippets Groups Projects
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
No related branches found
No related tags found
No related merge requests found
......@@ -28,11 +28,24 @@ abstract class AuthenticatedUserSavedAbstractListener implements ListenerAggrega
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
......@@ -41,25 +54,28 @@ abstract class AuthenticatedUserSavedAbstractListener implements ListenerAggrega
* implementation will pass this to the aggregate.
*
* @param EventManagerInterface $events
*
* @return void
*/
public function attach(EventManagerInterface $events)
{
$sharedEvents = $events->getSharedManager();
$this->listeners[] = $sharedEvents->attach(
'UnicaenAuth\Service\User',
UserAuthenticatedEvent::PRE_PERSIST,
[$this, 'onUserAuthenticatedPrePersist'],
100);
$this->listeners[] = $sharedEvents->attach(
'UnicaenAuth\Service\User',
UserAuthenticatedEvent::POST_PERSIST,
[$this, 'onUserAuthenticatedPostPersist'],
100);
}
/**
* Detach all previously attached listeners
*
* @param EventManagerInterface $events
*
* @return void
*/
public function detach(EventManagerInterface $events)
{
......
......@@ -14,7 +14,8 @@ use ZfcUser\Entity\UserInterface;
*/
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_LDAP_USER = 'ldap_user';
......
......@@ -114,10 +114,15 @@ class User implements ServiceLocatorAwareInterface, EventManagerAwareInterface
$entity->setState($state);
// pre-persist
$this->triggerUserAuthenticatedEvent($entity, $userData);
$event = new UserAuthenticatedEvent(UserAuthenticatedEvent::PRE_PERSIST);
$this->triggerEvent($event, $entity, $userData);
// persist
$mapper->$method($entity);
// post-persist
$event = new UserAuthenticatedEvent(UserAuthenticatedEvent::POST_PERSIST);
$this->triggerEvent($event, $entity, $userData);
}
catch (PDOException $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
}
/**
* Déclenche l'événement donnant aux applications clientes l'opportunité de modifier l'entité
* utilisateur avant qu'elle ne soit persistée.
*
* @param mixed $entity
* @param UserInterface $entity
* @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->setDbUser($entity);
if ($userData instanceof People) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment