RoleProvider.php 8.12 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
91
92
        $roles                  = [];
        $r                      = new Role();
        $roles[$r->getRoleId()] = $r;
        $r                      = new \Application\Acl\ComposanteRole();
        $roles[$r->getRoleId()] = $r;
        $r                      = new \Application\Acl\DrhRole();
        $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
93

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

lecluse's avatar
#1344    
lecluse committed
97

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

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

        // chargement des rôles métiers
        $qb = $this->getEntityManager()->createQueryBuilder()
            ->from("Application\Entity\Db\Role", "r")
112
            ->select("r, a, s, p")
lecluse's avatar
#1344    
lecluse committed
113
            ->distinct()
114
            ->join("r.perimetre", "p")
lecluse's avatar
#1344    
lecluse committed
115
116
117
118
119
            ->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
120
121
        foreach ($qb->getQuery()->getResult() as $dbRole) {
            /* @var $dbRole \Application\Entity\Db\Role */
lecluse's avatar
#1344    
lecluse committed
122
            $roleId = $dbRole->getRoleId();
lecluse's avatar
#1344    
lecluse committed
123
124

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

lecluse's avatar
lecluse committed
151
            $role = new $roleClass($roleId, $parent, $dbRole->getLibelle());
152
            /* @var $role Role */
lecluse's avatar
#1344    
lecluse committed
153
            $role->setPersonnel($personnel);
154
            $role->setPerimetre($dbRole->getPerimetre());
lecluse's avatar
#1344    
lecluse committed
155

156
            // 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
157
158
            if ($this->structureSelectionnee && $dbRole->getPerimetre()->isEtablissement()) {
                $role->setStructure($this->structureSelectionnee);
159
160
            }

lecluse's avatar
#1344    
lecluse committed
161
162
163
            $roles[$roleId] = $role;

            $affectations = $dbRole->getAffectation();
lecluse's avatar
lecluse committed
164
165
166
167
168
169
170
171
172
            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
173
174
                        $roles[$affRoleId] = $affRole;
                    }
lecluse's avatar
#1344    
lecluse committed
175
                }
176
            }
177
        }
lecluse's avatar
#1344    
lecluse committed
178
179
180


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

lecluse's avatar
lecluse committed
186
187
188
            if ($intervenant) {
                if ($intervenant->getStatut()->getId() == $statut['statut-id']) {
                    $role->setIntervenant($intervenant);
lecluse's avatar
#1344    
lecluse committed
189
190
                }
            }
lecluse's avatar
lecluse committed
191
            $roles[$statut['role-id']] = $role;
lecluse's avatar
#1344    
lecluse committed
192
193
        }

194
195
        return $roles;
    }
lecluse's avatar
#1344    
lecluse committed
196

lecluse's avatar
lecluse committed
197
198


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

lecluse's avatar
lecluse committed
224
        return $session->statutsInfo;
lecluse's avatar
lecluse committed
225
226
    }

lecluse's avatar
lecluse committed
227
228


229
230
231
    public function setStructureSelectionnee(StructureEntity $structureSelectionnee = null)
    {
        $this->structureSelectionnee = $structureSelectionnee;
lecluse's avatar
lecluse committed
232

233
234
        return $this;
    }
235
}