RoleProvider.php 7.95 KB
Newer Older
1
2
3
4
<?php

namespace Application\Provider\Role;

lecluse's avatar
#1344    
lecluse committed
5
use Application\Entity\Db\Affectation;
6
use Application\Entity\Db\Structure as StructureEntity;
lecluse's avatar
lecluse committed
7
use Application\Service\Traits\PersonnelAwareTrait;
8
use BjyAuthorize\Provider\Role\ProviderInterface;
9
10
use UnicaenApp\Service\EntityManagerAwareInterface;
use UnicaenApp\Service\EntityManagerAwareTrait;
11
use Zend\Permissions\Acl\Role\RoleInterface;
lecluse's avatar
#1344    
lecluse committed
12
use Application\Acl\Role;
lecluse's avatar
lecluse committed
13
14
15
16
use Zend\ServiceManager\ServiceLocatorAwareTrait;
use Application\Service\Traits\StatutIntervenantAwareTrait;
use Application\Traits\SessionContainerTrait;
use Application\Service\Traits\IntervenantAwareTrait;
17
18

/**
19
 * Fournisseur des rôles utilisateurs de l'application :
20
21
 * - ceux définis dans la configuration du fournisseur
 *
22
 *
23
 */
24
class RoleProvider implements ProviderInterface, EntityManagerAwareInterface
25
{
lecluse's avatar
lecluse committed
26
27
28
29
30
31
    use EntityManagerAwareTrait;
    use ServiceLocatorAwareTrait;
    use StatutIntervenantAwareTrait;
    use SessionContainerTrait;
    use IntervenantAwareTrait;
    use PersonnelAwareTrait;
32

33
34
35
    /**
     * @var array
     */
36
37
    protected $config = [];

38
39
40
41
    /**
     * @var array
     */
    protected $roles;
42

lecluse's avatar
#1344    
lecluse committed
43
44
45
46
47
48
    /**
     * @var StructureEntity
     */
    protected $structureSelectionnee;


49

50
    /**
51
     * Constructeur.
lecluse's avatar
lecluse committed
52
     *
53
54
     * @param array $config
     */
lecluse's avatar
lecluse committed
55
    public function __construct($config = [])
56
    {
57
        $this->config = $config;
58
    }
59

lecluse's avatar
lecluse committed
60
61


62
    /**
63
     * @return RoleInterface[]
64
65
66
     */
    public function getRoles()
    {
67
        if (null === $this->roles) {
68
69
            $this->roles = $this->makeRoles();
        }
lecluse's avatar
lecluse committed
70

71
72
        return $this->roles;
    }
73

lecluse's avatar
lecluse committed
74
75


76
77
    protected function makeRoles()
    {
lecluse's avatar
lecluse committed
78
79
80
81
82
83
84
85
86
87
88
89
90
        $roles                  = [];
        $r                      = new Role();
        $roles[$r->getRoleId()] = $r;
        $r                      = new \Application\Acl\ComposanteRole();
        $roles[$r->getRoleId()] = $r;
        $r                      = new \Application\Acl\EtablissementRole();
        $roles[$r->getRoleId()] = $r;
        $r                      = new \Application\Acl\IntervenantRole();
        $roles[$r->getRoleId()] = $r;
        $r                      = new \Application\Acl\IntervenantExterieurRole();
        $roles[$r->getRoleId()] = $r;
        $r                      = new \Application\Acl\IntervenantPermanentRole();
        $roles[$r->getRoleId()] = $r;
lecluse's avatar
#1344    
lecluse committed
91

lecluse's avatar
#1344    
lecluse committed
92
93
        $serviceAuthUserContext = $this->getServiceLocator()->get('AuthUserContext');
        /* @var $serviceAuthUserContext \UnicaenAuth\Service\UserContext */
94

lecluse's avatar
#1344    
lecluse committed
95

lecluse's avatar
lecluse committed
96
97
        if ($ldapUser = $serviceAuthUserContext->getLdapUser()) {
            $numeroPersonnel = (integer)$ldapUser->getSupannEmpId();
lecluse's avatar
lecluse committed
98
            $intervenant     = $this->getServiceIntervenant()->getBySourceCode($numeroPersonnel);
lecluse's avatar
lecluse committed
99
100
            $personnel       = $this->getServicePersonnel()->getBySourceCode($numeroPersonnel);
        } else {
101
            $intervenant = null;
lecluse's avatar
lecluse committed
102
            $personnel = null;
103
104
        }

lecluse's avatar
#1344    
lecluse committed
105
        /* Rôles du personnel */
lecluse's avatar
#1344    
lecluse committed
106
107
108
109

        // chargement des rôles métiers
        $qb = $this->getEntityManager()->createQueryBuilder()
            ->from("Application\Entity\Db\Role", "r")
110
            ->select("r, a, s, p")
lecluse's avatar
#1344    
lecluse committed
111
            ->distinct()
112
            ->join("r.perimetre", "p")
lecluse's avatar
#1344    
lecluse committed
113
114
115
116
117
            ->leftJoin("r.affectation", "a", \Doctrine\ORM\Query\Expr\Join::WITH, '1=compriseEntre(a.histoCreation,a.histoDestruction) AND a.personnel = :personnel')
            ->leftJoin("a.structure", "s")
            ->andWhere('1=compriseEntre(r.histoCreation,r.histoDestruction)')
            ->setParameter(':personnel', $personnel);

lecluse's avatar
lecluse committed
118
119
        foreach ($qb->getQuery()->getResult() as $dbRole) {
            /* @var $dbRole \Application\Entity\Db\Role */
lecluse's avatar
#1344    
lecluse committed
120
            $roleId = $dbRole->getRoleId();
lecluse's avatar
#1344    
lecluse committed
121

lecluse's avatar
lecluse committed
122
            $roleClass = \Application\Acl\Role::class;
lecluse's avatar
lecluse committed
123
            $parent    = 'user';
lecluse's avatar
#1344    
lecluse committed
124
            /** @deprecated */
lecluse's avatar
lecluse committed
125
            if ($roleId == 'gestionnaire-composante') {
lecluse's avatar
lecluse committed
126
                $roleClass = \Application\Acl\GestionnaireComposanteRole::class;
lecluse's avatar
lecluse committed
127
128
129
                $parent    = 'composante';
            }
            if ($roleId == 'directeur-composante') {
lecluse's avatar
lecluse committed
130
                $roleClass = \Application\Acl\DirecteurComposanteRole::class;
lecluse's avatar
lecluse committed
131
132
133
                $parent    = 'composante';
            }
            if ($roleId == 'administrateur') {
lecluse's avatar
lecluse committed
134
                $roleClass = \Application\Acl\AdministrateurRole::class;
lecluse's avatar
lecluse committed
135
136
            }
            if ($roleId == 'responsable-composante') {
lecluse's avatar
lecluse committed
137
                $roleClass = \Application\Acl\ResponsableComposanteRole::class;
lecluse's avatar
lecluse committed
138
139
140
                $parent    = 'composante';
            }
            if ($roleId == 'superviseur-etablissement') {
lecluse's avatar
lecluse committed
141
                $roleClass = \Application\Acl\EtablissementRole::class;
lecluse's avatar
lecluse committed
142
143
                $parent    = 'etablissement';
            }
lecluse's avatar
#1344    
lecluse committed
144
145
            /* FIN de deprecated */

lecluse's avatar
lecluse committed
146
            $role = new $roleClass($roleId, $parent, $dbRole->getLibelle());
147
            /* @var $role Role */
lecluse's avatar
#1344    
lecluse committed
148
            $role->setPersonnel($personnel);
149
            $role->setPerimetre($dbRole->getPerimetre());
lecluse's avatar
#1344    
lecluse committed
150

151
            // Si le rôle est de périmètre établissement, alors il se peut que l'on veuille zoomer sur une composante en particulier...
lecluse's avatar
lecluse committed
152
153
            if ($this->structureSelectionnee && $dbRole->getPerimetre()->isEtablissement()) {
                $role->setStructure($this->structureSelectionnee);
154
155
            }

lecluse's avatar
#1344    
lecluse committed
156
157
158
            $roles[$roleId] = $role;

            $affectations = $dbRole->getAffectation();
lecluse's avatar
lecluse committed
159
160
161
162
163
164
165
166
167
            foreach ($affectations as $affectation) {
                /* @var $affectation Affectation */
                if ($structure = $affectation->getStructure()) {
                    $affRoleId = $roleId . '-' . $structure->getSourceCode();
                    if (!isset($roles[$affRoleId])) {
                        $affRoleLibelle = $dbRole->getLibelle() . ' (' . $structure->getLibelleCourt() . ')';
                        $affRole        = new $roleClass($affRoleId, $roleId, $affRoleLibelle);
                        $affRole->setPersonnel($personnel);
                        $affRole->setStructure($structure);
lecluse's avatar
#1344    
lecluse committed
168
169
                        $roles[$affRoleId] = $affRole;
                    }
lecluse's avatar
#1344    
lecluse committed
170
                }
171
            }
172
        }
lecluse's avatar
#1344    
lecluse committed
173
174
175


        // Chargement des rôles par statut d'intervenant
lecluse's avatar
lecluse committed
176
        $si = $this->getStatutsInfo();
lecluse's avatar
lecluse committed
177
        foreach ($si as $statut) {
lecluse's avatar
lecluse committed
178
            $roleClass = $statut['role-class'];
lecluse's avatar
lecluse committed
179
            $role      = new $roleClass($statut['role-id'], $statut['parent'], $roles[$statut['parent']]->getRoleName());
lecluse's avatar
#1344    
lecluse committed
180

lecluse's avatar
lecluse committed
181
182
183
            if ($intervenant) {
                if ($intervenant->getStatut()->getId() == $statut['statut-id']) {
                    $role->setIntervenant($intervenant);
lecluse's avatar
#1344    
lecluse committed
184
185
                }
            }
lecluse's avatar
lecluse committed
186
            $roles[$statut['role-id']] = $role;
lecluse's avatar
#1344    
lecluse committed
187
188
        }

189
190
        return $roles;
    }
lecluse's avatar
#1344    
lecluse committed
191

lecluse's avatar
lecluse committed
192
193


lecluse's avatar
lecluse committed
194
195
    public function getStatutsInfo()
    {
lecluse's avatar
lecluse committed
196
        $session = $this->getSessionContainer();
lecluse's avatar
lecluse committed
197
198
        if (!isset($session->statutsInfo)) {
            $si      = [];
lecluse's avatar
lecluse committed
199
            $statuts = $this->getServiceStatutIntervenant()->getList();
lecluse's avatar
lecluse committed
200
            foreach ($statuts as $statut) {
lecluse's avatar
lecluse committed
201
                /** @deprecated */
lecluse's avatar
lecluse committed
202
203
                if ($statut->getTypeIntervenant()->getCode() === \Application\Entity\Db\TypeIntervenant::CODE_PERMANENT) {
                    $parent    = \Application\Acl\IntervenantPermanentRole::ROLE_ID;
lecluse's avatar
lecluse committed
204
                    $roleClass = \Application\Acl\IntervenantPermanentRole::class;
lecluse's avatar
lecluse committed
205
206
                } else {
                    $parent    = \Application\Acl\IntervenantExterieurRole::ROLE_ID;
lecluse's avatar
lecluse committed
207
                    $roleClass = \Application\Acl\IntervenantExterieurRole::class;
lecluse's avatar
lecluse committed
208
209
                }
                $si[] = [
lecluse's avatar
lecluse committed
210
211
212
                    'statut-id'  => $statut->getId(),
                    'role-id'    => $statut->getRoleId(),
                    'parent'     => $parent,
lecluse's avatar
lecluse committed
213
214
                    'role-class' => $roleClass,
                ];
lecluse's avatar
lecluse committed
215
216
217
            }
            $session->statutsInfo = $si;
        }
lecluse's avatar
lecluse committed
218

lecluse's avatar
lecluse committed
219
        return $session->statutsInfo;
lecluse's avatar
lecluse committed
220
221
    }

lecluse's avatar
lecluse committed
222
223


224
225
226
    public function setStructureSelectionnee(StructureEntity $structureSelectionnee = null)
    {
        $this->structureSelectionnee = $structureSelectionnee;
lecluse's avatar
lecluse committed
227

228
229
        return $this;
    }
230
}