RoleProvider.php 6.87 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 UnicaenAuth\Provider\Privilege\PrivilegeProviderAwareTrait;
12
use Zend\Permissions\Acl\Role\RoleInterface;
lecluse's avatar
#1344    
lecluse committed
13
use Application\Acl\Role;
lecluse's avatar
lecluse committed
14
15
use Zend\ServiceManager\ServiceLocatorAwareTrait;
use Application\Service\Traits\StatutIntervenantAwareTrait;
lecluse's avatar
lecluse committed
16
use UnicaenApp\Traits\SessionContainerTrait;
lecluse's avatar
lecluse committed
17
use Application\Service\Traits\IntervenantAwareTrait;
18
19

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

35
36
37
    /**
     * @var array
     */
38
39
    protected $config = [];

40
41
42
43
    /**
     * @var array
     */
    protected $roles;
44

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

50
51
52
53
54
    /**
     * @var array
     */
    private $rolesPrivileges;

lecluse's avatar
#1344    
lecluse committed
55

56

57
    /**
58
     * Constructeur.
lecluse's avatar
lecluse committed
59
     *
60
61
     * @param array $config
     */
lecluse's avatar
lecluse committed
62
    public function __construct($config = [])
63
    {
64
        $this->config = $config;
65
    }
66

lecluse's avatar
lecluse committed
67
68


69
    /**
70
     * @return RoleInterface[]
71
72
73
     */
    public function getRoles()
    {
74
        if (null === $this->roles) {
75
76
            $this->roles = $this->makeRoles();
        }
lecluse's avatar
lecluse committed
77

78
79
        return $this->roles;
    }
80

lecluse's avatar
lecluse committed
81
82


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
    protected function getRolesPrivileges()
    {
        if (!$this->rolesPrivileges){
            $pr = $this->getPrivilegeProvider()->getPrivilegesRoles();
            foreach( $pr as $priv => $roles ){
                foreach( $roles as $role ){
                    if (!isset($this->rolesPrivileges[$role])){
                        $this->rolesPrivileges[$role] = [];
                    }
                    $this->rolesPrivileges[$role][] = $priv;
                }
            }
        }
        return $this->rolesPrivileges;
    }



101
102
    protected function makeRoles()
    {
lecluse's avatar
lecluse committed
103
104
105
        $roles                  = [];
        $r                      = new Role();
        $roles[$r->getRoleId()] = $r;
lecluse's avatar
#1344    
lecluse committed
106

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

lecluse's avatar
#1344    
lecluse committed
110

lecluse's avatar
lecluse committed
111
        if ($ldapUser = $serviceAuthUserContext->getLdapUser()) {
112
            $supannEmpId = (integer)$ldapUser->getSupannEmpId();
lecluse's avatar
lecluse committed
113
            $intervenant     = $this->getServiceIntervenant()->getBySourceCode($supannEmpId, null, false);
114
            $personnel       = $this->getServicePersonnel()->getBySourceCode($supannEmpId);
lecluse's avatar
lecluse committed
115
        } else {
116
            $intervenant = null;
lecluse's avatar
lecluse committed
117
            $personnel = null;
118
119
        }

lecluse's avatar
#1344    
lecluse committed
120
        /* Rôles du personnel */
lecluse's avatar
#1344    
lecluse committed
121
122

        // chargement des rôles métiers
lecluse's avatar
lecluse committed
123
124
125
126
127
128
129
130
131
132
133
134
135
        $query = $this->getEntityManager()->createQuery(
        'SELECT DISTINCT
            r, a, s, p
        FROM
            Application\Entity\Db\Role r
            JOIN r.perimetre p
            LEFT JOIN r.affectation a WITH 1=compriseEntre(a.histoCreation,a.histoDestruction) AND a.personnel = :personnel
            LEFT JOIN a.structure s
        WHERE
            1=compriseEntre(r.histoCreation,r.histoDestruction)'
        )->setParameter(':personnel', $personnel);

        $result = $query->getResult();
136
        $rolesPrivileges = $this->getRolesPrivileges();
lecluse's avatar
lecluse committed
137
        foreach ($result as $dbRole) {
lecluse's avatar
lecluse committed
138
            /* @var $dbRole \Application\Entity\Db\Role */
lecluse's avatar
#1344    
lecluse committed
139
            $roleId = $dbRole->getRoleId();
lecluse's avatar
#1344    
lecluse committed
140

141
            $role = new Role($roleId, 'user', $dbRole->getLibelle());
142
143
144
145
            if (isset($rolesPrivileges[$roleId])){
                $role->initPrivileges($rolesPrivileges[$roleId]);
            }

146
147
148
            if ($dbRole->getPeutChangerStructure()){
                $role->setPeutChangerStructure(true);
            }
149
            /* @var $role Role */
lecluse's avatar
lecluse committed
150
            $role->setDbRole( $dbRole );
lecluse's avatar
#1344    
lecluse committed
151
            $role->setPersonnel($personnel);
152
            $role->setPerimetre($dbRole->getPerimetre());
lecluse's avatar
#1344    
lecluse committed
153

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

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

            $affectations = $dbRole->getAffectation();
lecluse's avatar
lecluse committed
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() . ')';
168
                        $affRole        = new \Application\Acl\Role($affRoleId, $roleId, $affRoleLibelle);
lecluse's avatar
lecluse committed
169
                        $affRole->setDbRole( $dbRole );
lecluse's avatar
lecluse committed
170
171
                        $affRole->setPersonnel($personnel);
                        $affRole->setStructure($structure);
lecluse's avatar
#1344    
lecluse committed
172
173
                        $roles[$affRoleId] = $affRole;
                    }
lecluse's avatar
#1344    
lecluse committed
174
                }
175
            }
176
        }
lecluse's avatar
#1344    
lecluse committed
177
178

        // Chargement des rôles par statut d'intervenant
lecluse's avatar
lecluse committed
179
        $si = $this->getStatutsInfo();
lecluse's avatar
lecluse committed
180
        foreach ($si as $statut) {
181
            $role = new Role($statut['role-id'], 'user', $statut['role-name']);
lecluse's avatar
#1344    
lecluse committed
182

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

192
193
        return $roles;
    }
lecluse's avatar
#1344    
lecluse committed
194

lecluse's avatar
lecluse committed
195
196


lecluse's avatar
lecluse committed
197
198
    public function getStatutsInfo()
    {
lecluse's avatar
lecluse committed
199
        $session = $this->getSessionContainer();
lecluse's avatar
lecluse committed
200
201
        if (!isset($session->statutsInfo)) {
            $si      = [];
lecluse's avatar
lecluse committed
202
            $statuts = $this->getServiceStatutIntervenant()->getList();
lecluse's avatar
lecluse committed
203
            foreach ($statuts as $statut) {
lecluse's avatar
lecluse committed
204
                $si[] = [
lecluse's avatar
lecluse committed
205
206
                    'statut-id'  => $statut->getId(),
                    'role-id'    => $statut->getRoleId(),
207
                    'role-name'  => $statut->getTypeIntervenant()->getLibelle(),
lecluse's avatar
lecluse committed
208
                ];
lecluse's avatar
lecluse committed
209
210
211
            }
            $session->statutsInfo = $si;
        }
lecluse's avatar
lecluse committed
212

lecluse's avatar
lecluse committed
213
        return $session->statutsInfo;
lecluse's avatar
lecluse committed
214
215
    }

lecluse's avatar
lecluse committed
216
217


218
219
220
    public function setStructureSelectionnee(StructureEntity $structureSelectionnee = null)
    {
        $this->structureSelectionnee = $structureSelectionnee;
lecluse's avatar
lecluse committed
221

222
223
        return $this;
    }
224
}