Commit e6292f3e authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Déclenchement d'une nouvel événement (classe UserRoleSelectedEvent) donnant à...

Déclenchement d'une nouvel événement (classe UserRoleSelectedEvent) donnant à l'application l'opportunité de réagir au changement de rôle de l'utilisateur courant.
parent 57f3297b
Loading
Loading
Loading
Loading
+70 −0
Original line number Diff line number Diff line
<?php

namespace UnicaenAuth\Event\Listener;

use Zend\EventManager\EventManagerInterface;
use Zend\EventManager\ListenerAggregateInterface;
use UnicaenApp\Service\EntityManagerAwareInterface;
use UnicaenApp\Service\EntityManagerAwareTrait;
use UnicaenAuth\Event\UserRoleSelectedEvent;

/**
 * Classe abstraites pour les classes désirant scruter un événement déclenché lors de la sélection d'un
 * rôle utilisateur.
 *
 * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
 * @see UserAuthenticatedEvent
 */
abstract class UserRoleSelectedEventAbstractListener implements ListenerAggregateInterface, EntityManagerAwareInterface
{
    use EntityManagerAwareTrait;

    /**
     * @var \Zend\Stdlib\CallbackHandler[]
     */
    protected $listeners = [];

    /**
     * Renseigne les relations 'intervenant' et 'personnel' avant que l'objet soit persisté.
     *
     * @param UserRoleSelectedEvent $e
     * @return
     */
    abstract public function postSelection(UserRoleSelectedEvent $e);

    /**
     * Attach one or more listeners
     *
     * Implementors may add an optional $priority argument; the EventManager
     * 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\UserContext',
            UserRoleSelectedEvent::POST_SELECTION,
            [$this, 'postSelection'],
            100);
    }

    /**
     * Detach all previously attached listeners
     *
     * @param EventManagerInterface $events
     *
     * @return void
     */
    public function detach(EventManagerInterface $events)
    {
        foreach ($this->listeners as $index => $listener) {
            if ($events->detach($listener)) {
                unset($this->listeners[$index]);
            }
        }
    }
}
 No newline at end of file
+45 −0
Original line number Diff line number Diff line
<?php

namespace UnicaenAuth\Event;

use Zend\EventManager\Event;
use Zend\Permissions\Acl\Role\RoleInterface;

/**
 * Classe des événements déclenchés lors l'utilisateur a sélectionné rôle.
 *
 * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
 */
class UserRoleSelectedEvent extends Event
{
    const POST_SELECTION = 'postSelection';

    private $role;

    /**
     * @return RoleInterface|string
     */
    public function getRole()
    {
        return $this->role;
    }

    /**
     * @param RoleInterface|string $role
     * @return self
     */
    public function setRole($role)
    {
        $this->role = $role;

        return $this;
    }

    /**
     * @return static
     */
    static public function postSelection()
    {
        return new static(static::POST_SELECTION);
    }
}
 No newline at end of file
+23 −2
Original line number Diff line number Diff line
@@ -5,20 +5,24 @@ namespace UnicaenAuth\Service;
use BjyAuthorize\Acl\Role;
use UnicaenApp\Exception\RuntimeException;
use UnicaenApp\Traits\SessionContainerTrait;
use UnicaenAuth\Event\UserRoleSelectedEvent;
use UnicaenAuth\Provider\Identity\Chain;
use Zend\EventManager\EventManagerAwareInterface;
use Zend\Session\Container as SessionContainer;
use Zend\Permissions\Acl\Role\RoleInterface;
use ZfcUser\Entity\UserInterface;
use UnicaenAuth\Entity\Ldap\People;
use UnicaenAuth\Acl\NamedRole;
use Zend\EventManager\EventManagerAwareTrait;

/**
 * Service centralisant des méthodes utiles concernant l'utilisateur authentifié.
 *
 * @author Laurent LÉCLUSE <laurent.lecluse at unicaen.fr>
 */
class UserContext extends AbstractService
class UserContext extends AbstractService implements EventManagerAwareInterface
{
    use EventManagerAwareTrait;
    use SessionContainerTrait;

    /**
@@ -222,6 +226,8 @@ class UserContext extends AbstractService
            unset($this->getSessionContainer()->selectedIdentityRole);
        }

        $this->triggerUserRoleSelectedEvent(UserRoleSelectedEvent::POST_SELECTION, $role);

        return $this;
    }

@@ -261,10 +267,25 @@ class UserContext extends AbstractService
            unset($this->getSessionContainer()->nextSelectedIdentityRole);
        }

        $this->triggerUserRoleSelectedEvent(UserRoleSelectedEvent::POST_SELECTION, $role);

        return $this;
    }


    /**
     * Déclenche l'événement donnant à l'application l'opportunité de réagir à la sélection d'un rôle.
     *
     * @param string $name Ex: UserRoleSelectedEvent::POST_SELECTION
     * @param RoleInterface|string|null $role Rôle sélectionné
     */
    private function triggerUserRoleSelectedEvent($name, $role)
    {
        $event = new UserRoleSelectedEvent($name);
        $event
            ->setRole($role)
            ->setTarget($this);
        $this->getEventManager()->trigger($event);
    }

    /**
     * Teste si le rôle spécifié fait partie des rôles disponibles.