Skip to content
Snippets Groups Projects
Select Git revision
  • 8039b2de0f72059ccd9ea50bdd1e8954c8af3932
  • master default protected
  • b24
  • ll-workflow
  • alc-scindage-donnees-pj
  • FJ_LL_Tbl_Contrat
  • alc-docker-node
  • ll-apiplatform
  • php84
  • ll-rgpd
  • b23
  • alc-filtre-type-intervenant
  • ll-sans-mdb5
  • formules-ancienne-infra
  • ll-formules
  • alc-intervenant-dmep
  • ll-suppr-v_vol-s
  • b20
  • ll-postgresql
  • b23.0.1
  • b22
  • 24.8
  • 24.7
  • 24.6
  • 24.5
  • 24.4
  • 24.3
  • 24.2
  • 24.1
  • 24.0
  • 23.15
  • 24.0-beta19
  • 24.0-beta18
  • 24.0-beta17
  • 24.0-beta16
  • 24.0-beta15
  • 24.0-beta14
  • 24.0-beta13
  • 23.14
  • 24.0-beta12
  • 24.0-beta11
41 results

RoleProvider.php

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    RoleProvider.php 5.10 KiB
    <?php
    
    namespace Application\Provider\Role;
    
    use Application\Entity\Db\Affectation;
    use Application\Entity\Db\Structure as StructureEntity;
    use BjyAuthorize\Provider\Role\ProviderInterface;
    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 :
     * - ceux définis dans la configuration du fournisseur
     *
     *  * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
     */
    class RoleProvider implements ProviderInterface, EntityManagerAwareInterface
    {
        use EntityManagerAwareTrait;
        use \Zend\ServiceManager\ServiceLocatorAwareTrait;
    
        /**
         * @var array
         */
        protected $config = [];
    
        /**
         * @var array
         */
        protected $roles;
    
        /**
         * @var StructureEntity
         */
        protected $structureSelectionnee;
    
    
    
        /**
         * Constructeur.
         * @param array $config
         */
        public function __construct( $config = [] )
        {
            $this->config = $config;
        }
    
        public function init()
        {
            $this->getEntityManager()->getFilters()->enable('historique');
        }
    
        /**
         * @return RoleInterface[]
         */
        public function getRoles()
        {
            if (null === $this->roles) {
                $this->roles = $this->makeRoles();
            }
            return $this->roles;
        }
    
        protected function makeRoles()
        {
            $roles = [];
    
            /* deprecated */
            foreach( $this->config as $classname ){
                if (class_exists( $classname )){
                    $role = new $classname; /* @var $role RoleInterface */
                    $roles[$role->getRoleId()] = $role;
                }else{
                    throw new LogicException('La classe "'.$classname.'" déclarée dans la configuration du fournisseur de rôles n\'a pas été trouvée.');
                }
            }
            /* 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")
                    ->select("a, r, s")
                    ->distinct()
                    ->join("a.role", "r")
                    ->leftJoin("a.structure", "s")
                    ->andWhere('1=compriseEntre(a.histoCreation,a.histoDestruction)')
                    ->andWhere('1=compriseEntre(r.histoCreation,r.histoDestruction)')
                    ->andWhere("a.personnel = :personnel")->setParameter(':personnel', $personnel);
                foreach ($qb->getQuery()->getResult() as $affectation) { /* @var $affectation Affectation */
                     $dbRole = $affectation->getRole();
    
                    $roleId = $dbRole->getCode();
                    $roleLibelle = $dbRole->getLibelle();
                    if ($structure = $affectation->getStructure()){
                        $roleId .= '-'.$structure->getSourceCode();
                        $roleLibelle .= ' ('.$structure->getLibelleCourt().')';
                    }
    
                    /** @deprecated */
                    $parents = [
                        'gestionnaire-composante',
                        'responsable-recherche-labo',
                        'directeur-composante',
                        'administrateur',
                        'responsable-composante',
                        'superviseur-etablissement',
                    ];
                    if (in_array($dbRole->getCode(), $parents)){
                        $parent = $dbRole->getCode();
                    }else{
                        $parent = 'user';
                    }
    
                    if (isset($roles[$roleId])){
                        $role = $roles[$roleId];
                    }else{
                        $role = new Role( $roleId, $parent, $roleLibelle);
                    }
    
                    /* fin de deprecated */
    
                    //$role = new Role( $roleId, $parent, $roleLibelle);
                    $role->setDbRole( $dbRole );
                    $role->setPersonnel( $personnel );
    
                    if ($this->structureSelectionnee){
                        $role->setStructure( $this->structureSelectionnee );
                    }else{
                        $role->setStructure( $affectation->getStructure() );
                    }
                    
    
                    $roles[$roleId] = $role;
                }
            }
            return $roles;
        }
        
        public function setStructureSelectionnee(StructureEntity $structureSelectionnee = null)
        {
            $this->structureSelectionnee = $structureSelectionnee;
            
            return $this;
        }
    }