RoleProvider.php 6.68 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;
7
use Application\Service\Traits\ContextServiceAwareTrait;
lecluse's avatar
lecluse committed
8
use Application\Service\Traits\PersonnelAwareTrait;
9
use BjyAuthorize\Provider\Role\ProviderInterface;
10
11
use UnicaenApp\Service\EntityManagerAwareInterface;
use UnicaenApp\Service\EntityManagerAwareTrait;
12
use UnicaenAuth\Provider\Privilege\PrivilegeProviderAwareTrait;
13
use Zend\Permissions\Acl\Role\RoleInterface;
lecluse's avatar
#1344    
lecluse committed
14
use Application\Acl\Role;
15
use Application\Service\Traits\StatutIntervenantServiceAwareTrait;
lecluse's avatar
lecluse committed
16
use UnicaenApp\Traits\SessionContainerTrait;
17
use Application\Service\Traits\IntervenantServiceAwareTrait;
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
    use EntityManagerAwareTrait;
28
    use StatutIntervenantServiceAwareTrait;
lecluse's avatar
lecluse committed
29
    use SessionContainerTrait;
30
    use IntervenantServiceAwareTrait;
lecluse's avatar
lecluse committed
31
    use PersonnelAwareTrait;
32
    use PrivilegeProviderAwareTrait;
33
    use ContextServiceAwareTrait;
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
     * @var Structure
lecluse's avatar
#1344    
lecluse committed
47
48
49
     */
    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


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

lecluse's avatar
lecluse committed
97
        return $this->rolesPrivileges;
98
99
100
101
    }



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

108
109
        $intervenant = $this->getServiceContext()->getIntervenant();
        $personnel   = $this->getServiceContext()->getPersonnel();
110

lecluse's avatar
#1344    
lecluse committed
111
        /* Rôles du personnel */
lecluse's avatar
#1344    
lecluse committed
112
113

        // chargement des rôles métiers
114

lecluse's avatar
lecluse committed
115
        $query = $this->getEntityManager()->createQuery(
lecluse's avatar
lecluse committed
116
            'SELECT DISTINCT
lecluse's avatar
lecluse committed
117
118
119
120
            r, a, s, p
        FROM
            Application\Entity\Db\Role r
            JOIN r.perimetre p
121
            LEFT JOIN r.affectation a WITH a.histoDestruction IS NULL AND a.personnel = :personnel
lecluse's avatar
lecluse committed
122
123
            LEFT JOIN a.structure s
        WHERE
124
            r.histoDestruction IS NULL'
125
        )->setParameter('personnel', $personnel);
lecluse's avatar
lecluse committed
126

127
        $result          = $query->getResult();
lecluse's avatar
lecluse committed
128
        $rolesPrivileges = $this->getRolesPrivileges();
lecluse's avatar
lecluse committed
129
        foreach ($result as $dbRole) {
lecluse's avatar
lecluse committed
130
            /* @var $dbRole \Application\Entity\Db\Role */
lecluse's avatar
#1344    
lecluse committed
131
            $roleId = $dbRole->getRoleId();
lecluse's avatar
#1344    
lecluse committed
132

133
            $role = new Role($roleId, 'user', $dbRole->getLibelle());
134
            if (isset($rolesPrivileges[$roleId])) {
lecluse's avatar
lecluse committed
135
136
                $role->initPrivileges($rolesPrivileges[$roleId]);
            }
137

138
            if ($dbRole->getPeutChangerStructure()) {
139
140
                $role->setPeutChangerStructure(true);
            }
141
            /* @var $role Role */
142
            $role->setDbRole($dbRole);
lecluse's avatar
#1344    
lecluse committed
143
            $role->setPersonnel($personnel);
144
            $role->setPerimetre($dbRole->getPerimetre());
lecluse's avatar
#1344    
lecluse committed
145

146
            // 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
147
148
            if ($this->structureSelectionnee && $dbRole->getPerimetre()->isEtablissement()) {
                $role->setStructure($this->structureSelectionnee);
149
150
            }

lecluse's avatar
#1344    
lecluse committed
151
152
153
            $roles[$roleId] = $role;

            $affectations = $dbRole->getAffectation();
lecluse's avatar
lecluse committed
154
155
156
157
158
159
            foreach ($affectations as $affectation) {
                /* @var $affectation Affectation */
                if ($structure = $affectation->getStructure()) {
                    $affRoleId = $roleId . '-' . $structure->getSourceCode();
                    if (!isset($roles[$affRoleId])) {
                        $affRoleLibelle = $dbRole->getLibelle() . ' (' . $structure->getLibelleCourt() . ')';
160
                        $affRole        = new \Application\Acl\Role($affRoleId, $roleId, $affRoleLibelle);
161
                        if (isset($rolesPrivileges[$roleId])) {
lecluse's avatar
lecluse committed
162
163
                            $affRole->initPrivileges($rolesPrivileges[$roleId]);
                        }
164
                        $affRole->setDbRole($dbRole);
lecluse's avatar
lecluse committed
165
166
                        $affRole->setPersonnel($personnel);
                        $affRole->setStructure($structure);
lecluse's avatar
#1344    
lecluse committed
167
168
                        $roles[$affRoleId] = $affRole;
                    }
lecluse's avatar
#1344    
lecluse committed
169
                }
170
            }
171
        }
lecluse's avatar
#1344    
lecluse committed
172

173

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

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

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

lecluse's avatar
lecluse committed
193
194


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

lecluse's avatar
lecluse committed
211
        return $session->statutsInfo;
lecluse's avatar
lecluse committed
212
213
    }

lecluse's avatar
lecluse committed
214
215


216
    public function setStructureSelectionnee(Structure $structureSelectionnee = null)
217
218
    {
        $this->structureSelectionnee = $structureSelectionnee;
lecluse's avatar
lecluse committed
219

220
221
        return $this;
    }
222
}