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
Loading
Loading
Loading
Loading
+28 −12
Original line number Diff line number Diff line
@@ -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)
    {
+2 −1
Original line number Diff line number Diff line
@@ -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';
+8 −7
Original line number Diff line number Diff line
@@ -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) {