Skip to content
Snippets Groups Projects
Commit ea78ac99 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Correction : aucun rôle sélectionné si aucun utilisateur authentifié.

parent 1d9253b3
No related branches found
No related tags found
No related merge requests found
...@@ -5,8 +5,10 @@ namespace UnicaenAuth\Service; ...@@ -5,8 +5,10 @@ namespace UnicaenAuth\Service;
use Zend\ServiceManager\ServiceLocatorAwareInterface; use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorAwareTrait; use Zend\ServiceManager\ServiceLocatorAwareTrait;
use Zend\Session\Container as SessionContainer; use Zend\Session\Container as SessionContainer;
use Zend\Permissions\Acl\Role\RoleInterface;
use ZfcUser\Entity\UserInterface; use ZfcUser\Entity\UserInterface;
use UnicaenAuth\Entity\Ldap\People; use UnicaenAuth\Entity\Ldap\People;
use UnicaenAuth\Acl\NamedRole;
/** /**
* Service centralisant des méthodes utiles concernant l'utilisateur authentifié. * Service centralisant des méthodes utiles concernant l'utilisateur authentifié.
...@@ -44,6 +46,7 @@ class UserContext implements ServiceLocatorAwareInterface ...@@ -44,6 +46,7 @@ class UserContext implements ServiceLocatorAwareInterface
return $identity['db']; return $identity['db'];
} }
} }
return null; return null;
} }
...@@ -59,15 +62,16 @@ class UserContext implements ServiceLocatorAwareInterface ...@@ -59,15 +62,16 @@ class UserContext implements ServiceLocatorAwareInterface
return $identity['ldap']; return $identity['ldap'];
} }
} }
return null; return null;
} }
/** /**
* Retourne l'identité correspondant à l'utilisateur courant. * Retourne les données d'identité correspondant à l'utilisateur courant.
* *
* @return mixed * @return mixed
*/ */
protected function getIdentity() public function getIdentity()
{ {
if (null === $this->identity) { if (null === $this->identity) {
$authenticationService = $this->getServiceLocator()->get('Zend\Authentication\AuthenticationService'); $authenticationService = $this->getServiceLocator()->get('Zend\Authentication\AuthenticationService');
...@@ -75,6 +79,7 @@ class UserContext implements ServiceLocatorAwareInterface ...@@ -75,6 +79,7 @@ class UserContext implements ServiceLocatorAwareInterface
$this->identity = $authenticationService->getIdentity(); $this->identity = $authenticationService->getIdentity();
} }
} }
return $this->identity; return $this->identity;
} }
...@@ -90,6 +95,7 @@ class UserContext implements ServiceLocatorAwareInterface ...@@ -90,6 +95,7 @@ class UserContext implements ServiceLocatorAwareInterface
$identityProvider = $authorize->getIdentityProvider(); $identityProvider = $authorize->getIdentityProvider();
$this->identityRoles = $identityProvider->getIdentityRoles(); $this->identityRoles = $identityProvider->getIdentityRoles();
} }
return $this->identityRoles; return $this->identityRoles;
} }
...@@ -102,20 +108,24 @@ class UserContext implements ServiceLocatorAwareInterface ...@@ -102,20 +108,24 @@ class UserContext implements ServiceLocatorAwareInterface
{ {
return array_filter( return array_filter(
$this->getIdentityRoles(), $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 * @return mixed
*/ */
public function getSelectedIdentityRole() public function getSelectedIdentityRole()
{ {
if (null === $this->getSessionContainer()->selectedIdentityRole) { if (null === $this->getSessionContainer()->selectedIdentityRole) {
if ($this->getIdentity()) {
$roles = $this->getSelectableIdentityRoles(); $roles = $this->getSelectableIdentityRoles();
$this->getSessionContainer()->selectedIdentityRole = reset($roles) ?: null; $this->getSessionContainer()->selectedIdentityRole = reset($roles) ?: null;
} }
}
return $this->getSessionContainer()->selectedIdentityRole; return $this->getSessionContainer()->selectedIdentityRole;
} }
...@@ -131,14 +141,39 @@ class UserContext implements ServiceLocatorAwareInterface ...@@ -131,14 +141,39 @@ class UserContext implements ServiceLocatorAwareInterface
if (!$this->isRoleValid($role)) { if (!$this->isRoleValid($role)) {
throw new \Common\Exception\RuntimeException("Rôle spécifié invalide."); throw new \Common\Exception\RuntimeException("Rôle spécifié invalide.");
} }
$this->getSessionContainer()->selectedIdentityRole = $role; $this->getSessionContainer()->selectedIdentityRole = $this->normalizedIdentityRole($role);
} }
else { else {
unset($this->getSessionContainer()->selectedIdentityRole); unset($this->getSessionContainer()->selectedIdentityRole);
} }
return $this; 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. * Teste si le rôle spécifié fait partie des rôles disponibles.
* *
...@@ -148,13 +183,14 @@ class UserContext implements ServiceLocatorAwareInterface ...@@ -148,13 +183,14 @@ class UserContext implements ServiceLocatorAwareInterface
protected function isRoleValid($role) protected function isRoleValid($role)
{ {
foreach ($this->getIdentityRoles() as $r) { foreach ($this->getIdentityRoles() as $r) {
if ($r instanceof \Zend\Permissions\Acl\Role\RoleInterface) { if ($r instanceof RoleInterface) {
$r = $r->getRoleId(); $r = $r->getRoleId();
} }
if ($role === $r) { if ($role === $r) {
return true; return true;
} }
} }
return false; return false;
} }
...@@ -169,6 +205,7 @@ class UserContext implements ServiceLocatorAwareInterface ...@@ -169,6 +205,7 @@ class UserContext implements ServiceLocatorAwareInterface
if (null === $this->sessionContainer) { if (null === $this->sessionContainer) {
$this->sessionContainer = new SessionContainer(get_class()); $this->sessionContainer = new SessionContainer(get_class());
} }
return $this->sessionContainer; return $this->sessionContainer;
} }
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment