diff --git a/src/UnicaenAuth/Controller/UtilisateurController.php b/src/UnicaenAuth/Controller/UtilisateurController.php index 18bccc48d3dc9eae2113c5cd38dae6767dd741d6..e7b019dc45dbc753b46d0e9c9f2bd459e1b7a420 100644 --- a/src/UnicaenAuth/Controller/UtilisateurController.php +++ b/src/UnicaenAuth/Controller/UtilisateurController.php @@ -50,10 +50,8 @@ class UtilisateurController extends AbstractActionController } if ($addFlashMessage) { - $message = sprintf( - "Vous endossez à présent le profil utilisateur <strong>%s</strong>.", - $this->getAuthUserContextService()->getSelectedIdentityRole()->getRoleId() - ); + $selectedRole = $this->getAuthUserContextService()->getSelectedIdentityRoleToString(); + $message = sprintf("Vous endossez à présent le profil utilisateur <strong>%s</strong>.", $selectedRole); $this->flashMessenger()->setNamespace('UnicaenAuth/success')->addMessage($message); } diff --git a/src/UnicaenAuth/Formatter/RoleFormatter.php b/src/UnicaenAuth/Formatter/RoleFormatter.php new file mode 100644 index 0000000000000000000000000000000000000000..0f5d966352eb0fdfc30ac42d04aff5c73fd744e0 --- /dev/null +++ b/src/UnicaenAuth/Formatter/RoleFormatter.php @@ -0,0 +1,42 @@ +<?php + +namespace UnicaenAuth\Formatter; + +use Zend\Permissions\Rbac\RoleInterface; + +class RoleFormatter +{ + /** + * Retourne le rôle utilisateur spécifié au format littéral. + * + * @param $role + * @return string + */ + public function format($role) + { + return $this->roleToString($role); + } + + /** + * Retourne le rôle utilisateur spécifié au format littéral. + * + * @param object|RoleInterface|string $role + * @return string + */ + private function roleToString($role) + { + $string = '?'; + + if (is_object($role) && method_exists($role, '__toString')) { + $string = (string) $role; + } + elseif ($role instanceof RoleInterface) { + $string = $role->getRoleId(); + } + elseif (is_string($role)) { + $string = $role; + } + + return $string; + } +} \ No newline at end of file diff --git a/src/UnicaenAuth/Provider/Identity/Chain.php b/src/UnicaenAuth/Provider/Identity/Chain.php index 67c2c61565ebe0a8597027c707b3ef09264a94f8..6d042707e2f344c70be8bd9e3a4c61f5177d751c 100644 --- a/src/UnicaenAuth/Provider/Identity/Chain.php +++ b/src/UnicaenAuth/Provider/Identity/Chain.php @@ -1,4 +1,5 @@ <?php + namespace UnicaenAuth\Provider\Identity; use BjyAuthorize\Provider\Identity\ProviderInterface; @@ -76,7 +77,7 @@ class Chain implements ProviderInterface, ServiceLocatorAwareInterface, EventMan // collecte des rôles $this->getEventManager()->trigger('getIdentityRoles', $e); - $roles = $e->getRoles(); + $roles = $e->getRoles(); /** @var RoleInterface[] $roles */ $authorizeService = $this->getServiceLocator()->get('BjyAuthorize\Service\Authorize'); /* @var $authorizeService Authorize */ @@ -88,14 +89,14 @@ class Chain implements ProviderInterface, ServiceLocatorAwareInterface, EventMan } // évite les doublons if (!$registry->has($role)) { - $role = $authorizeService->getAcl()->getRole($role); + if (is_string($role)) { + $role = $authorizeService->getAcl()->getRole($role); + } $registry->add($role); $this->roles[$role->getRoleId()] = $role; } } -// var_dump($this->roles); - return $this->roles; } diff --git a/src/UnicaenAuth/Service/UserContext.php b/src/UnicaenAuth/Service/UserContext.php index 0fc768d899a4205cd2e15582f902ff1985e40a60..d2a321475baf6a0fab56d11a29ca56a193b2c094 100644 --- a/src/UnicaenAuth/Service/UserContext.php +++ b/src/UnicaenAuth/Service/UserContext.php @@ -5,21 +5,21 @@ namespace UnicaenAuth\Service; use BjyAuthorize\Acl\Role; use UnicaenApp\Exception\RuntimeException; use UnicaenApp\Traits\SessionContainerTrait; +use UnicaenAuth\Acl\NamedRole; +use UnicaenAuth\Entity\Ldap\People; use UnicaenAuth\Entity\Shibboleth\ShibUser; use UnicaenAuth\Event\UserRoleSelectedEvent; +use UnicaenAuth\Formatter\RoleFormatter; use UnicaenAuth\Provider\Identity\Chain; use Zend\EventManager\EventManagerAwareInterface; -use Zend\Session\Container as SessionContainer; +use Zend\EventManager\EventManagerAwareTrait; 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> + * @author Unicaen */ class UserContext extends AbstractService implements EventManagerAwareInterface { @@ -36,8 +36,6 @@ class UserContext extends AbstractService implements EventManagerAwareInterface */ protected $identityRoles; - - /** * Retourne l'utilisateur BDD courant * @@ -54,8 +52,6 @@ class UserContext extends AbstractService implements EventManagerAwareInterface return null; } - - /** * Retourne l'utilisateur LDAP courant * @@ -72,8 +68,6 @@ class UserContext extends AbstractService implements EventManagerAwareInterface return null; } - - /** * Retourne l'éventuel utilisateur Shibboleth courant. * @@ -90,8 +84,6 @@ class UserContext extends AbstractService implements EventManagerAwareInterface return null; } - - /** * Retourne les données d'identité correspondant à l'utilisateur courant. * @@ -109,7 +101,6 @@ class UserContext extends AbstractService implements EventManagerAwareInterface return $this->identity; } - /** * Retourne l'identifiant de connexion de l'utilisateur courant. * @@ -130,8 +121,6 @@ class UserContext extends AbstractService implements EventManagerAwareInterface return null; } - - /** * @param string $roleId * @@ -147,8 +136,6 @@ class UserContext extends AbstractService implements EventManagerAwareInterface return null; } - - /** * Retourne tous les rôles de l'utilisateur courant, pas seulement le rôle courant sélectionné. * @@ -164,15 +151,16 @@ class UserContext extends AbstractService implements EventManagerAwareInterface $roles = $this->getServiceAuthorize()->getRoles(); $identityProvider = $this->getIdentityProvider(); if ($identityProvider instanceof Chain) { - $iRoles = $identityProvider->getAllIdentityRoles(); + $identityRoles = $identityProvider->getAllIdentityRoles(); } else { - $iRoles = $identityProvider->getIdentityRoles(); + $identityRoles = $identityProvider->getIdentityRoles(); } - foreach ($iRoles as $role) { - if ($role instanceof Role) { + foreach ($identityRoles as $role) { + if ($role instanceof RoleInterface) { $this->identityRoles[$role->getRoleId()] = $role; - } elseif (isset($roles[$role])) { - $role = $roles[$role]; + } elseif (is_string($role) && isset($roles[$role])) { + $role = $roles[$role]; + /** @var RoleInterface $role */ $this->identityRoles[$role->getRoleId()] = $role; } } @@ -181,7 +169,23 @@ class UserContext extends AbstractService implements EventManagerAwareInterface return $this->identityRoles; } + /** + * Retourne tous les rôles de l'utilisateur courant au format littéral. + * + * @return array + * @see getIdentityRoles() + */ + public function getIdentityRolesToString() + { + $f = new RoleFormatter(); + $rolesToStrings = []; + foreach ($this->getIdentityRoles() as $identityRole) { + $rolesToStrings[$identityRole->getRoleId()] = $f->format($identityRole); + } + + return $rolesToStrings; + } /** * Retourne parmi tous les rôles de l'utilisateur courant ceux qui peuvent être sélectionnés. @@ -198,8 +202,6 @@ class UserContext extends AbstractService implements EventManagerAwareInterface return $roles; } - - /** * 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é. @@ -207,11 +209,10 @@ class UserContext extends AbstractService implements EventManagerAwareInterface * NB: Si un rôle est spécifié en session comme devant être le prochain rôle sélectionné, * c'est lui qui est pris en compte. * - * @return mixed + * @return RoleInterface */ public function getSelectedIdentityRole() { - if ($this->getNextSelectedIdentityRole()) { $this->getSessionContainer()->selectedIdentityRole = $this->getNextSelectedIdentityRole(); } @@ -222,10 +223,9 @@ class UserContext extends AbstractService implements EventManagerAwareInterface } $roleId = $this->getSessionContainer()->selectedIdentityRole; - if ($roleId) { - - $roles = $this->getServiceAuthorize()->getRoles(); // Récupération de tous les rôles du provider +// $roles = $this->getServiceAuthorize()->getRoles(); // Récupération de tous les rôles du provider + $roles = $this->getIdentityRoles(); if (isset($roles[$roleId])) { $role = $roles[$roleId]; } else { @@ -240,7 +240,24 @@ class UserContext extends AbstractService implements EventManagerAwareInterface return null; } + /** + * Retourne le rôle utilisateur sélectionné éventuel au format littéral. + * + * @return string + * @see getSelectedIdentityRole() + */ + public function getSelectedIdentityRoleToString() + { + $role = $this->getSelectedIdentityRole(); + + if (! $role) { + return null; + } + + $f = new RoleFormatter(); + return $f->format($role); + } /** * Mémorise en session le rôle spécifié comme étant le rôle courant de l'utilisateur. @@ -271,8 +288,6 @@ class UserContext extends AbstractService implements EventManagerAwareInterface return $this; } - - /** * Retourne l'éventuel rôle spécifié en session devant être le prochain rôle sélectionné. * @@ -283,8 +298,6 @@ class UserContext extends AbstractService implements EventManagerAwareInterface return $this->getSessionContainer()->nextSelectedIdentityRole; } - - /** * Mémorise en session le rôle devant être le prochain rôle sélectionné. * @@ -352,16 +365,15 @@ class UserContext extends AbstractService implements EventManagerAwareInterface return false; } - - /** * - * @return \UnicaenAuth\Provider\Identity\Chain + * @return Chain */ private function getIdentityProvider() { - return $this->getServiceAuthorize()->getIdentityProvider(); - /* @var $identityProvider \UnicaenAuth\Provider\Identity\Chain */ - } + /* @var $identityProvider Chain */ + $identityProvider = $this->getServiceAuthorize()->getIdentityProvider(); + return $identityProvider; + } } \ No newline at end of file diff --git a/src/UnicaenAuth/View/Helper/UserProfile.php b/src/UnicaenAuth/View/Helper/UserProfile.php index 0e577ad9b192799b1efd9c89135886e2c97ce3c5..f00c0ee374a12fb5c58437b4740f876d1c6b7ba3 100644 --- a/src/UnicaenAuth/View/Helper/UserProfile.php +++ b/src/UnicaenAuth/View/Helper/UserProfile.php @@ -1,7 +1,6 @@ <?php -namespace UnicaenAuth\View\Helper; -use Zend\Permissions\Acl\Role\RoleInterface; +namespace UnicaenAuth\View\Helper; /** * Aide de vue permettant d'afficher le profil de l'utilisateur connecté. @@ -15,17 +14,12 @@ class UserProfile extends UserAbstract */ protected $userProfileSelectable = false; - /** - * @var array - */ - protected $identityRoles; - /** * Point d'entrée. * * @param bool $userProfileSelectable Spécifie s'il faut afficher les profils - * de l'utilisateur sous forme d'une liste déroulante ou de boutons radios, permettant - * ainsi à l'utilisateur de changer de profil courant. + * de l'utilisateur sous forme d'une liste déroulante ou de boutons radios, + * permettant ainsi à l'utilisateur de changer de profil courant. * @return self */ public function __invoke($userProfileSelectable = false) @@ -62,8 +56,7 @@ class UserProfile extends UserAbstract if ($this->userProfileSelectable) { $html .= $this->getView()->userProfileSelect(false); - } - else { + } else { $html .= $this->getView()->htmlList($roles); } @@ -80,19 +73,6 @@ class UserProfile extends UserAbstract return $this->render(); } - /** - * Retourne les rôles de l'utilisateur courant. - * - * @return array - */ - protected function getIdentityRoles() - { - if (null === $this->identityRoles) { - $this->identityRoles = $this->getUserContext()->getIdentityRoles(); - } - return $this->identityRoles; - } - /** * Retourne les rôles de l'utilisateur courant. * @@ -100,27 +80,7 @@ class UserProfile extends UserAbstract */ protected function getIdentityRolesAsOptions() { - $identityRoles = $this->getIdentityRoles(); - $roles = []; - - foreach ($identityRoles as $id => $role) { - $lib = '?'; - if (is_object($role) && method_exists($role, '__toString')) { - $lib = (string) $role; - } - elseif ($role instanceof RoleInterface) { - $lib = $role->getRoleId(); - } - elseif (is_string($role)) { - $lib = $role; - } - if ($this->getTranslator()) { - $lib = $this->getTranslator()->translate($lib, $this->getTranslatorTextDomain()); - } - $roles[$id] = $lib; - } - - return $roles; + return $this->getUserContext()->getIdentityRolesToString(); } /** @@ -134,6 +94,7 @@ class UserProfile extends UserAbstract public function setUserProfileSelectable($userProfileSelectable = true) { $this->userProfileSelectable = $userProfileSelectable; + return $this; } } \ No newline at end of file