diff --git a/src/UnicaenAuth/Service/UserContext.php b/src/UnicaenAuth/Service/UserContext.php index c09cac240644857c172c9fbaff57bf1a64ef4abb..7785666ef1bdd16b57c4bbc807193901326263a7 100644 --- a/src/UnicaenAuth/Service/UserContext.php +++ b/src/UnicaenAuth/Service/UserContext.php @@ -5,8 +5,10 @@ namespace UnicaenAuth\Service; use Zend\ServiceManager\ServiceLocatorAwareInterface; use Zend\ServiceManager\ServiceLocatorAwareTrait; 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; /** * Service centralisant des méthodes utiles concernant l'utilisateur authentifié. @@ -44,6 +46,7 @@ class UserContext implements ServiceLocatorAwareInterface return $identity['db']; } } + return null; } @@ -59,15 +62,16 @@ class UserContext implements ServiceLocatorAwareInterface return $identity['ldap']; } } + return null; } /** - * Retourne l'identité correspondant à l'utilisateur courant. + * Retourne les données d'identité correspondant à l'utilisateur courant. * * @return mixed */ - protected function getIdentity() + public function getIdentity() { if (null === $this->identity) { $authenticationService = $this->getServiceLocator()->get('Zend\Authentication\AuthenticationService'); @@ -75,6 +79,7 @@ class UserContext implements ServiceLocatorAwareInterface $this->identity = $authenticationService->getIdentity(); } } + return $this->identity; } @@ -90,6 +95,7 @@ class UserContext implements ServiceLocatorAwareInterface $identityProvider = $authorize->getIdentityProvider(); $this->identityRoles = $identityProvider->getIdentityRoles(); } + return $this->identityRoles; } @@ -102,20 +108,24 @@ class UserContext implements ServiceLocatorAwareInterface { return array_filter( $this->getIdentityRoles(), - function($r) { return !($r instanceof \UnicaenAuth\Acl\NamedRole && !$r->getSelectable()); }); + function($r) { return !($r instanceof NamedRole && !$r->getSelectable()); }); } /** - * Retourne le rôle utilisateur sélectionné, ou le premier sélectionnable si aucun n'a été sléectionné. + * Si un utilisateur est authentifié, retourne le rôle utilisateur sélectionné, + * ou alors le premier sélectionnable si aucun n'a été sélectionné. * * @return mixed */ public function getSelectedIdentityRole() { if (null === $this->getSessionContainer()->selectedIdentityRole) { - $roles = $this->getSelectableIdentityRoles(); - $this->getSessionContainer()->selectedIdentityRole = reset($roles) ?: null; + if ($this->getIdentity()) { + $roles = $this->getSelectableIdentityRoles(); + $this->getSessionContainer()->selectedIdentityRole = reset($roles) ?: null; + } } + return $this->getSessionContainer()->selectedIdentityRole; } @@ -131,14 +141,39 @@ class UserContext implements ServiceLocatorAwareInterface if (!$this->isRoleValid($role)) { throw new \Common\Exception\RuntimeException("Rôle spécifié invalide."); } - $this->getSessionContainer()->selectedIdentityRole = $role; + $this->getSessionContainer()->selectedIdentityRole = $this->normalizedIdentityRole($role); } else { unset($this->getSessionContainer()->selectedIdentityRole); } + return $this; } + /** + * Recherche le role spécifié parmi les rôles connus au format objets. + * + * @param mixed $role + * @return mixed Role trouvé au format objet dans la mesure du possible + */ + protected function normalizedIdentityRole($role) + { + if (is_object($role)) { + return $role; + } + + foreach ($this->getIdentityRoles() as $r) { + if ($r instanceof RoleInterface && $role === $r->getRoleId()) { + return $r; + } + if ($role === $r) { + return $r; + } + } + + return $role; + } + /** * Teste si le rôle spécifié fait partie des rôles disponibles. * @@ -148,13 +183,14 @@ class UserContext implements ServiceLocatorAwareInterface protected function isRoleValid($role) { foreach ($this->getIdentityRoles() as $r) { - if ($r instanceof \Zend\Permissions\Acl\Role\RoleInterface) { + if ($r instanceof RoleInterface) { $r = $r->getRoleId(); } if ($role === $r) { return true; } } + return false; } @@ -169,6 +205,7 @@ class UserContext implements ServiceLocatorAwareInterface if (null === $this->sessionContainer) { $this->sessionContainer = new SessionContainer(get_class()); } + return $this->sessionContainer; } } \ No newline at end of file