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

Ajout possibilité de nommer les rôles spécifiés via une...

Ajout possibilité de nommer les rôles spécifiés via une 'UnicaenAuth\Provider\Role\Config' (sous-classe de 'BjyAuthorize\Provider\Role\Config'). Exemple : array('guest' => array('name' => "Profil de base", 'children' => array())).
Correction aide de vue affichant le profil de l'utilisateur connecté.
parent 4183602a
Branches
Tags
No related merge requests found
......@@ -132,7 +132,7 @@ class Module implements ConfigProviderInterface, ViewHelperProviderInterface, Se
},
'userProfile' => function (HelperPluginManager $sm) {
$helper = new View\Helper\UserProfile($sm->getServiceLocator()->get('zfcuser_auth_service'));
// $helper->setIdentityProvider($sm->getServiceLocator()->get('BjyAuthorize\Service\Authorize')->getIdentityProvider());
$helper->setIdentityProvider($sm->getServiceLocator()->get('BjyAuthorize\Service\Authorize')->getIdentityProvider());
return $helper;
},
'userInfo' => function (HelperPluginManager $sm) {
......@@ -153,41 +153,9 @@ class Module implements ConfigProviderInterface, ViewHelperProviderInterface, Se
{
return array(
'factories' => array(
// 'unicaen-auth_module_options' => function($sm) {
// $config = $sm->get('Configuration');
// return new Options\ModuleOptions(isset($config['unicaen-auth']) ? $config['unicaen-auth'] : array());
// },
// 'UnicaenAuth\Authentication\Adapter\Db' => function() {
// return new Authentication\Adapter\Db();
// },
// 'UnicaenAuth\Authentication\Adapter\Ldap' => function() {
// return new Authentication\Adapter\Ldap();
// },
// 'UnicaenAuth\Authentication\Adapter\Cas' => function() {
// return new Authentication\Adapter\Cas();
// },
// 'UnicaenAuth\Authentication\Storage\Db' => function() {
// return new Authentication\Storage\Db();
// },
// 'UnicaenAuth\Authentication\Storage\Ldap' => function() {
// return new Authentication\Storage\Ldap();
// },
'unicaen-auth_user_service' => function () {
return new Service\User();
},
// 'zfcuser_auth_service' => function ($sm) {
// return new \Zend\Authentication\AuthenticationService(
// $sm->get('UnicaenAuth\Authentication\Storage\Mixed'),
// $sm->get('ZfcUser\Authentication\Adapter\AdapterChain')
// );
// },
// 'UnicaenAuth\Authentication\Storage\Mixed' => function($sm) {
// $storage = new Authentication\Storage\Mixed();
// $storage->setLdapStorage($sm->get('UnicaenAuth\Authentication\Storage\Ldap'))
// ->setDbStorage($sm->get('UnicaenAuth\Authentication\Storage\Db'));
// return $storage;
// },
// 'ZfcUser\Authentication\Adapter\AdapterChain' => 'UnicaenAuth\Authentication\Adapter\AdapterChainServiceFactory',
'UnicaenAuth\Provider\Identity\LdapPeople' => function($sm) {
$authService = $sm->get('zfcuser_auth_service');
$provider = new \UnicaenAuth\Provider\Identity\LdapPeople($authService);
......
<?php
namespace UnicaenAuth\Acl;
use Zend\Permissions\Acl\Role\RoleInterface;
/**
* Rôle avec nom.
*
* @author Ben Youngblood <bx.youngblood@gmail.com>
*/
class NamedRole extends \BjyAuthorize\Acl\Role
{
/**
* @var string
*/
protected $roleName;
/**
* @param string|null $roleId
* @param RoleInterface|string|null $parent
* @param string $roleName
*/
public function __construct($roleId = null, $parent = null, $roleName = null)
{
parent::__construct($roleId, $parent);
$this->setRoleName($roleName ?: $roleId);
}
/**
* Retourne l'intitulé du rôle.
*
* @return string
*/
public function getRoleName()
{
return $this->roleName;
}
/**
* Spécifie l'intitulé du rôle.
*
* @param string $roleName
* @return self
*/
public function setRoleName($roleName)
{
$this->roleName = (string) $roleName;
return $this;
}
}
\ No newline at end of file
......@@ -28,7 +28,7 @@ class LdapPeople implements ProviderInterface, ServiceLocatorAwareInterface
/**
* @var string|RoleInterface
*/
protected $defaultRole = 'guest';
protected $defaultRole;
/**
* @param AuthenticationService $authService
......@@ -54,17 +54,22 @@ class LdapPeople implements ProviderInterface, ServiceLocatorAwareInterface
$authorizeService = $this->getServiceLocator()->get('BjyAuthorize\Service\Authorize');
/* @var $authorizeService \BjyAuthorize\Service\Authorize */
foreach ($identity->getMemberOf() as $group) {
if ($authorizeService->getAcl()->hasRole($group)) {
$roles[] = $group;
try {
$role = $authorizeService->getAcl()->getRole($group);
}
catch (\Zend\Permissions\Acl\Exception\InvalidArgumentException $exc) {
continue;
}
if ($role->getRoleId() === $group) {
$roles[] = $role;
}
}
}
if (!$roles) {
$roles = array($this->getDefaultRole());
$roles[] = $this->getDefaultRole();
}
// var_dump($roles);
return $roles;
}
......@@ -75,6 +80,9 @@ class LdapPeople implements ProviderInterface, ServiceLocatorAwareInterface
*/
public function getDefaultRole()
{
if (null === $this->defaultRole) {
$this->defaultRole = new \UnicaenAuth\Acl\NamedRole('guest', null, "Invité");
}
return $this->defaultRole;
}
......@@ -90,7 +98,6 @@ class LdapPeople implements ProviderInterface, ServiceLocatorAwareInterface
if ( ! ($defaultRole instanceof RoleInterface || is_string($defaultRole))) {
throw InvalidRoleException::invalidRoleInstance($defaultRole);
}
$this->defaultRole = $defaultRole;
}
......@@ -98,6 +105,7 @@ class LdapPeople implements ProviderInterface, ServiceLocatorAwareInterface
* Set service locator
*
* @param \Zend\ServiceManager\ServiceLocatorInterface $serviceLocator
* @return self
*/
public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
{
......
<?php
namespace UnicaenAuth\Provider\Role;
use UnicaenAuth\Acl\NamedRole;
/**
* Array config based Role provider
*
* L'ajout par rapport à la classe mère est l'instanciation de rôles "nommés" :
* la clé 'name' peut être ajoutée pour spécifier le nom du rôle.
* Exemple :
* <code>array('guest' => array('name' => "Profil de base", 'children' => array()))</code>
*
* @see \BjyAuthorize\Provider\Role\Config
* @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
*/
class Config extends \BjyAuthorize\Provider\Role\Config
{
/**
* @param string $name
* @param array $options Ex: array('guest' => array('name' => "Profil de base", 'children' => array()))
* @param string|null $parent
*
* @return array
*/
protected function loadRole($name, $options = array(), $parent = null)
{
if (isset($options['name']) && !is_array($options['name'])) {
$roleName = $options['name'];
} else {
$roleName = null;
}
if (isset($options['children']) && count($options['children']) > 0) {
$children = $options['children'];
} else {
$children = array();
}
$roles = array();
$role = new NamedRole($name, $parent, $roleName);
$roles[] = $role;
foreach ($children as $key => $value) {
if (is_numeric($key)) {
$roles = array_merge($roles, $this->loadRole($value, array(), $role));
} else {
$roles = array_merge($roles, $this->loadRole($key, $value, $role));
}
}
return $roles;
}
}
\ No newline at end of file
......@@ -8,10 +8,15 @@ namespace UnicaenAuth\View\Helper;
*/
class UserProfile extends UserAbstract
{
/**
* @var \BjyAuthorize\Provider\Identity\ProviderInterface
*/
protected $identityProvider;
/**
* Point d'entrée.
*
* @return UserProfile
* @return self
*/
public function __invoke()
{
......@@ -25,6 +30,42 @@ class UserProfile extends UserAbstract
*/
public function __toString()
{
return '';
$roles = array();
foreach ($this->getIdentityProvider()->getIdentityRoles() as $role) {
if ($role instanceof \UnicaenAuth\Acl\NamedRole) {
$roles[] = $role->getRoleName();
}
elseif ($role instanceof \BjyAuthorize\Acl\Role) {
$roles[] = $role->getRoleId();
}
else {
$roles[] = (string)$role;
}
}
$html = "<strong>Profil utilisateur :</strong>";
if ($roles) {
$html .= $this->getView()->htmlList($roles);
}
return $html;
}
/**
*
* @return \BjyAuthorize\Provider\Identity\ProviderInterface
*/
public function getIdentityProvider()
{
return $this->identityProvider;
}
/**
*
* @param \BjyAuthorize\Provider\Identity\ProviderInterface $identityProvider
* @return self
*/
public function setIdentityProvider(\BjyAuthorize\Provider\Identity\ProviderInterface $identityProvider)
{
$this->identityProvider = $identityProvider;
return $this;
}
}
\ 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