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;
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) {
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment