diff --git a/module/Application/src/Application/Acl/IntervenantRole.php b/module/Application/src/Application/Acl/IntervenantRole.php index d12ce2d5a21f3dcb60575e72ec87d2f2fe03cafc..5d275da8d19de11d2709620c1f9bd8514b13c3f5 100644 --- a/module/Application/src/Application/Acl/IntervenantRole.php +++ b/module/Application/src/Application/Acl/IntervenantRole.php @@ -2,41 +2,19 @@ namespace Application\Acl; -use UnicaenAuth\Acl\NamedRole; -use Application\Interfaces\StructureAwareInterface; -use Application\Interfaces\IntervenantAwareInterface; -use Application\Traits\IntervenantAwareTrait; -use Application\Traits\StructureAwareTrait; - /** * Description of IntervenantRole * - * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr> + * @author Laurent LÉCLUSE <laurent.lecluse at unicaen.fr> */ -class IntervenantRole extends Role implements StructureAwareInterface, IntervenantAwareInterface +class IntervenantRole extends Role { - use StructureAwareTrait; - use IntervenantAwareTrait; - const ROLE_ID = "intervenant"; public function __construct($id = self::ROLE_ID, $parent = Role::ROLE_ID, $name = 'Intervenant', $description = null, $selectable = true) { parent::__construct($id, $parent, $name, $description, $selectable); } - - /** - * - * @param Resource|string $resource - * @param Privilege|string $privilege - */ - function hasPrivilege( $resource, $privilege ) - { - if ($statut = $this->getIntervenant()->getStatut()){ - return $statut->hasPrivilege($resource, $privilege); - } - return false; - } } diff --git a/module/Application/src/Application/Acl/Role.php b/module/Application/src/Application/Acl/Role.php index 9e0de1536895a8f776cf02f230ad7f179930acdb..e322502ff86a6c6b807fee5bef0cba5c91da690a 100644 --- a/module/Application/src/Application/Acl/Role.php +++ b/module/Application/src/Application/Acl/Role.php @@ -4,8 +4,6 @@ namespace Application\Acl; use UnicaenAuth\Acl\NamedRole; use Application\Entity\Db\Role as DbRole; -use Zend\Permissions\Acl\Resource; -use Application\Entity\Db\Privilege; /** * Rôle père de tous les rôles "administrateur". @@ -14,6 +12,9 @@ use Application\Entity\Db\Privilege; */ class Role extends NamedRole { + use \Application\Traits\StructureAwareTrait, + \Application\Traits\PersonnelAwareTrait, + \Application\Traits\IntervenantAwareTrait; const ROLE_ID = 'role'; @@ -35,27 +36,14 @@ class Role extends NamedRole * * @return RoleEntity */ - function getDbRole() + public function getDbRole() { - return $this->role; + return $this->dbRole; } - function setDbRole(DbRole $dbRole) + public function setDbRole(DbRole $dbRole) { $this->dbRole = $dbRole; return $this; } - - /** - * - * @param Resource|string $resource - * @param Privilege|string $privilege - */ - function hasPrivilege( $resource, $privilege ) - { - if ($typeRole = $this->getTypeRole()){ - return $typeRole->hasPrivilege($resource, $privilege); - } - return false; - } } \ No newline at end of file diff --git a/module/Application/src/Application/Provider/Identity/IdentityProvider.php b/module/Application/src/Application/Provider/Identity/IdentityProvider.php index 6b615f96ec331903adcde41310e6b5202d58af40..129a93f1ef431efe4ef9513094d90b0fd5fbf6a8 100644 --- a/module/Application/src/Application/Provider/Identity/IdentityProvider.php +++ b/module/Application/src/Application/Provider/Identity/IdentityProvider.php @@ -2,16 +2,11 @@ namespace Application\Provider\Identity; use Application\Acl; -use Application\Entity\Db\IntervenantExterieur; -use Application\Entity\Db\IntervenantPermanent; use Application\Entity\Db\Affectation; -use Application\Entity\Db\Utilisateur; -use Common\Exception\RuntimeException; use UnicaenApp\Service\EntityManagerAwareInterface; use UnicaenApp\Service\EntityManagerAwareTrait; use UnicaenAuth\Provider\Identity\ChainableProvider; use UnicaenAuth\Provider\Identity\ChainEvent; -use Zend\Permissions\Acl\Role\RoleInterface; use Zend\ServiceManager\ServiceLocatorAwareInterface; use Zend\ServiceManager\ServiceLocatorAwareTrait; @@ -30,11 +25,6 @@ class IdentityProvider implements ServiceLocatorAwareInterface, ChainableProvide */ protected $roles; - public function init() - { - $this->getEntityManager()->getFilters()->enable('historique'); - } - /** * {@inheritDoc} */ @@ -49,106 +39,45 @@ class IdentityProvider implements ServiceLocatorAwareInterface, ChainableProvide public function getIdentityRoles() { if (null === $this->roles) { + $this->getEntityManager()->getFilters()->enable('historique')->init( + [ + 'Application\Entity\Db\Role', + 'Application\Entity\Db\Affectation', + ], + new \DateTime + ); + $this->roles = []; - if (!$this->getServiceLocator()->get('AuthUserContext')->getIdentity()) { - return $this->roles; - } + $serviceAuthUserContext = $this->getServiceLocator()->get('AuthUserContext'); + /* @var $serviceAuthUserContext \UnicaenAuth\Service\UserContext */ + $utilisateur = $serviceAuthUserContext->getDbUser(); + /* @var $utilisateur \Application\Entity\Db\Utilisateur */ + + if (! $utilisateur) return $this->roles; // pas connecté /** * Rôles que possède l'utilisateur dans la base de données. */ - $this->roles = array_merge($this->roles, $this->getDbRoles()); + if ($utilisateur->getPersonnel()) { + foreach ($utilisateur->getPersonnel()->getAffectation() as $affectation) { + /* @var $affectation Affectation */ + $roleId = $affectation->getRole()->getCode(); + if ($structure = $affectation->getStructure()){ + $roleId .= '-'.$structure->getSourceCode(); + } + $this->roles[] = $roleId; + } + } /** * Rôle correspondant au type d'intervenant auquel appartient l'utilisateur */ - $intervenantRole = $this->getIntervenantRole(); - if ($intervenantRole){ - $this->roles[] = $intervenantRole; + $intervenant = $utilisateur->getIntervenant(); + if ($intervenant){ + $this->roles[] = Acl\IntervenantRole::ROLE_ID; } - } - - //var_dump($this->roles); - return $this->roles; } - - /** - * Fetch dans la base de données les rôles que possède l'utilisateur sur une structure précise. - * - * @return array Id des rôles trouvés - */ - protected function getDbRoles() - { - $utilisateur = $this->getDbUser(); - $roles = []; - - if (!$utilisateur) { - return $roles; - } - - /** - * Responsabilités métier importées (tables affectation et TYPE_ROLE) - */ - if ($utilisateur->getPersonnel()) { - foreach ($utilisateur->getPersonnel()->getAffectation() as $affectation) { /* @var $affectation Affectation */ - $roleId = $affectation->getRole()->getCode(); - if ($structure = $affectation->getStructure()){ - $roleId .= '-'.$structure->getSourceCode(); - } - $roles[] = $roleId; - } - } - - return $roles; - } - - /** - * Retourne le rôle correspondant au type d'intervenant auquel appartient l'utilisateur. - * - * @return RoleInterface|null - */ - protected function getIntervenantRole() - { - $utilisateur = $this->getDbUser(); - - if (!$utilisateur) { - return null; - } - - $intervenant = $utilisateur->getIntervenant(); - - if (!$intervenant) { - return Acl\IntervenantRole::ROLE_ID; - } - - $statut = $intervenant->getStatut()->getSourceCode(); - if ($statut === \Application\Entity\Db\StatutIntervenant::NON_AUTORISE){ - return null; - } - - if ($intervenant instanceof IntervenantPermanent) { - $role = Acl\IntervenantPermanentRole::ROLE_ID; - } - elseif ($intervenant instanceof IntervenantExterieur) { - $role = Acl\IntervenantExterieurRole::ROLE_ID; - } - else { - throw new RuntimeException("Type d'intervenant inattendu : " . get_class($intervenant)); - } - - return $role; - } - - /** - * Retourne l'utilisateur connecté. - * - * @return Utilisateur - */ - private function getDbUser() - { - return $this->getServiceLocator()->get('AuthUserContext')->getDbUser(); /* @var $dbUser Utilisateur */ - } } diff --git a/module/Application/src/Application/Provider/Identity/IdentityProviderFactory.php b/module/Application/src/Application/Provider/Identity/IdentityProviderFactory.php index f06c977d8957ffb71336fac20d8f1da966c7d658..1ed5169ee4c7f7d753ceea12bd4e63990be647c7 100644 --- a/module/Application/src/Application/Provider/Identity/IdentityProviderFactory.php +++ b/module/Application/src/Application/Provider/Identity/IdentityProviderFactory.php @@ -16,7 +16,7 @@ class IdentityProviderFactory implements FactoryInterface * Create service * * @param ServiceLocatorInterface $serviceLocator - * @return Acteur + * @return IdentityProvider */ public function createService(ServiceLocatorInterface $serviceLocator) { @@ -24,7 +24,6 @@ class IdentityProviderFactory implements FactoryInterface $identityProvider = new IdentityProvider; $identityProvider->setEntityManager($em); - $identityProvider->init(); return $identityProvider; } } \ No newline at end of file diff --git a/module/Application/src/Application/Provider/IdentityProvider.php b/module/Application/src/Application/Provider/IdentityProvider.php new file mode 100644 index 0000000000000000000000000000000000000000..5f2c8538f0d4e1d5fcfc5484e2936f660b41c293 --- /dev/null +++ b/module/Application/src/Application/Provider/IdentityProvider.php @@ -0,0 +1,79 @@ +<?php +namespace Application\Provider\Identity; + +use Application\Acl; +use Application\Entity\Db\IntervenantExterieur; +use Application\Entity\Db\IntervenantPermanent; +use Application\Entity\Db\Affectation; +use Application\Entity\Db\Utilisateur; +use Common\Exception\RuntimeException; +use UnicaenApp\Service\EntityManagerAwareInterface; +use UnicaenApp\Service\EntityManagerAwareTrait; +use UnicaenAuth\Provider\Identity\ChainableProvider; +use UnicaenAuth\Provider\Identity\ChainEvent; +use Zend\Permissions\Acl\Role\RoleInterface; +use Zend\ServiceManager\ServiceLocatorAwareInterface; +use Zend\ServiceManager\ServiceLocatorAwareTrait; + +/** + * Classe chargée de fournir les rôles que possède l'identité authentifiée. + * + * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr> + */ +class IdentityProvider implements ServiceLocatorAwareInterface, ChainableProvider, EntityManagerAwareInterface +{ + use ServiceLocatorAwareTrait; + use EntityManagerAwareTrait; + + /** + * @var array + */ + protected $roles; + + /** + * {@inheritDoc} + */ + public function injectIdentityRoles(ChainEvent $event) + { + $event->addRoles($this->getIdentityRoles()); + } + + /** + * {@inheritDoc} + */ + public function getIdentityRoles() + { + if (null === $this->roles) { + $this->roles = []; + + $serviceAuthUserContext = $this->getServiceLocator()->get('AuthUserContext'); + /* @var $serviceAuthUserContext \UnicaenAuth\Service\UserContext */ + $utilisateur = $serviceAuthUserContext->getDbUser(); + + if (! $utilisateur) return $this->roles; // pas connecté + + /** + * Rôles que possède l'utilisateur dans la base de données. + */ + if ($utilisateur->getPersonnel()) { + foreach ($utilisateur->getPersonnel()->getAffectation() as $affectation) { + /* @var $affectation Affectation */ + $roleId = $affectation->getRole()->getCode(); + if ($structure = $affectation->getStructure()){ + $roleId .= '-'.$structure->getSourceCode(); + } + $this->roles[] = $roleId; + } + } + + /** + * Rôle correspondant au type d'intervenant auquel appartient l'utilisateur + */ + $intervenant = $utilisateur->getIntervenant(); + if ($intervenant){ + $this->roles[] = Acl\IntervenantRole::ROLE_ID; + } + } + return $this->roles; + } +} diff --git a/module/Application/src/Application/Provider/Role/RoleProvider.php b/module/Application/src/Application/Provider/Role/RoleProvider.php index 0659eb7af00260e456e8afe9c26c8c20b653d5aa..ff48f63ae675fce60f49584a6a8fd52b59a75429 100644 --- a/module/Application/src/Application/Provider/Role/RoleProvider.php +++ b/module/Application/src/Application/Provider/Role/RoleProvider.php @@ -2,16 +2,15 @@ namespace Application\Provider\Role; -use Application\Acl\AdministrateurRole; use Application\Entity\Db\Affectation; use Application\Entity\Db\Structure as StructureEntity; -use Application\Interfaces\StructureAwareInterface; use BjyAuthorize\Provider\Role\ProviderInterface; -use Exception; use UnicaenApp\Exception\LogicException; use UnicaenApp\Service\EntityManagerAwareInterface; use UnicaenApp\Service\EntityManagerAwareTrait; use Zend\Permissions\Acl\Role\RoleInterface; +use Application\Acl\Role; +use Application\Acl\IntervenantRole; /** * Fournisseur des rôles utilisateurs de l'application : @@ -34,6 +33,12 @@ class RoleProvider implements ProviderInterface, EntityManagerAwareInterface */ protected $roles; + /** + * @var StructureEntity + */ + protected $structureSelectionnee; + + /** * Constructeur. @@ -64,7 +69,7 @@ class RoleProvider implements ProviderInterface, EntityManagerAwareInterface { $roles = []; - // Chargement des rôles de base + /* deprecated */ foreach( $this->config as $classname ){ if (class_exists( $classname )){ $role = new $classname; /* @var $role RoleInterface */ @@ -73,7 +78,22 @@ class RoleProvider implements ProviderInterface, EntityManagerAwareInterface throw new LogicException('La classe "'.$classname.'" déclarée dans la configuration du fournisseur de rôles n\'a pas été trouvée.'); } } - if (($utilisateur = $this->getUtilisateur()) && ($personnel = $utilisateur->getPersonnel())){ + /* fin de deprecated */ + + $serviceAuthUserContext = $this->getServiceLocator()->get('AuthUserContext'); + /* @var $serviceAuthUserContext \UnicaenAuth\Service\UserContext */ + $utilisateur = $serviceAuthUserContext->getDbUser(); + + + /* Cas spécifique du rôle intervenant */ + if ($utilisateur && $utilisateur->getIntervenant()){ + $role = new IntervenantRole; + $role->setIntervenant( $utilisateur->getIntervenant() ); + $roles[$role->getRoleId()] = $role; + } + + /* Rôles du personnel */ + if ($utilisateur && ($personnel = $utilisateur->getPersonnel())){ // chargement des rôles métiers $qb = $this->getEntityManager()->createQueryBuilder() ->from("Application\Entity\Db\Affectation", "a") @@ -85,61 +105,21 @@ class RoleProvider implements ProviderInterface, EntityManagerAwareInterface ->andWhere('1=compriseEntre(r.histoCreation,r.histoDestruction)') ->andWhere("a.personnel = :personnel")->setParameter(':personnel', $personnel); foreach ($qb->getQuery()->getResult() as $affectation) { /* @var $affectation Affectation */ - $roleId = $affectation->getRole()->getCode(); - if (! isset($roles[$roleId])){ - throw new Exception('Le rôle "'.$roleId.'" est inconnu.'); - } - $classname = get_class($roles[$roleId]); - if ($roles[$roleId] instanceof StructureAwareInterface && $affectation->getStructure()){ - $roleId .= '-'.$affectation->getStructure()->getSourceCode(); - $roles[$roleId] = new $classname($roleId); - $roles[$roleId]->setStructure( $affectation->getStructure() ); + $dbRole = $affectation->getRole(); + $role = new Role( $dbRole->getCode(), 'user', $dbRole->getLibelle()); + $role->setDbRole( $dbRole ); + $role->setPersonnel( $personnel ); + + if ($this->structureSelectionnee){ + $role->setStructure( $this->structureSelectionnee ); }else{ - $roles[$roleId] = new $classname($roleId); + $role->setStructure( $affectation->getStructure() ); } - $roles[$roleId]->setDbRole( $affectation->getRole() ); - - $this->injectSelectedStructureInRole($roles[$roleId]); + $roles[$role->getRoleId()] = $role; } } return $roles; } - - /** - * - * @return \Application\Entity\Db\Utilisateur - */ - public function getUtilisateur() - { - $identity = $this->getServiceLocator()->get('AuthUserContext')->getIdentity(); - if (isset($identity['db'])){ - return $identity['db']; - }else{ - return null; - } - } - - /** - * Inject la structure sélectionnée en session dans le rôle Administrateur. - * - * @param \Application\Acl\Role $role - * @return self - */ - public function injectSelectedStructureInRole($role) - { - if (! $role instanceof AdministrateurRole) { - return $this; - } - - $role->setStructure($this->structureSelectionnee); - - return $this; - } - - /** - * @var StructureEntity - */ - protected $structureSelectionnee; public function setStructureSelectionnee(StructureEntity $structureSelectionnee = null) { diff --git a/module/Application/src/Application/Service/Context.php b/module/Application/src/Application/Service/Context.php index e1b5a455f0ee3452a69e5d67603d9cb5db4d2d81..35b625af51693137d114591cbccce3f0d6bb7fe3 100644 --- a/module/Application/src/Application/Service/Context.php +++ b/module/Application/src/Application/Service/Context.php @@ -74,7 +74,7 @@ class Context extends AbstractService protected $sessionContainer; - + /** @@ -89,23 +89,17 @@ class Context extends AbstractService if ($authUserContext->getIdentity()) { $this->selectedIdentityRole = $authUserContext->getSelectedIdentityRole(); - $utilisateur = $authUserContext->getDbUser(); - if ($utilisateur){ - if ($this->selectedIdentityRole instanceof IntervenantAwareInterface) { - $intervenant = $utilisateur->getIntervenant(); - if (! $intervenant){ // sinon import automatique - $iSourceCode = (int)$authUserContext->getLdapUser()->getSupannEmpId(); - $intervenant = $this->getServiceIntervenant()->importer($iSourceCode); - } - $this->selectedIdentityRole->setIntervenant( $intervenant ); - } - if ($this->selectedIdentityRole instanceof PersonnelAwareInterface){ - $this->selectedIdentityRole->setPersonnel( $utilisateur->getPersonnel() ); - } + if ( + $this->selectedIdentityRole instanceof \Application\Acl\IntervenantRole + && ! $this->selectedIdentityRole->getIntervenant() + ){ + // import automatique + $iSourceCode = (int)$authUserContext->getLdapUser()->getSupannEmpId(); + $intervenant = $this->getServiceIntervenant()->importer($iSourceCode); + $this->selectedIdentityRole->setIntervenant( $intervenant ); // injection à la volée de l'intervenant } } } - return $this->selectedIdentityRole; } @@ -268,7 +262,7 @@ class Context extends AbstractService $sc->structure = null; }else{ $role = $this->getSelectedIdentityRole(); - if ($role instanceof StructureAwareInterface && $role->getStructure()){ + if ($role->getStructure()){ $sc->structure = $role->getStructure()->getId(); }else{ $sc->structure = null;