RoleProvider.php 6.63 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;
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;
14
use Application\Service\Traits\StatutIntervenantServiceAwareTrait;
lecluse's avatar
lecluse committed
15
use UnicaenApp\Traits\SessionContainerTrait;
16
use Application\Service\Traits\IntervenantServiceAwareTrait;
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
    use EntityManagerAwareTrait;
27
    use StatutIntervenantServiceAwareTrait;
lecluse's avatar
lecluse committed
28
    use SessionContainerTrait;
29
    use IntervenantServiceAwareTrait;
30
    use PrivilegeProviderAwareTrait;
31
    use ContextServiceAwareTrait;
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
     * @var Structure
lecluse's avatar
#1344    
lecluse committed
45
46
47
     */
    protected $structureSelectionnee;

48
49
50
51
52
    /**
     * @var array
     */
    private $rolesPrivileges;

lecluse's avatar
#1344    
lecluse committed
53

54

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

lecluse's avatar
lecluse committed
65
66


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

76
77
        return $this->roles;
    }
78

lecluse's avatar
lecluse committed
79
80


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

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



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

106
        $intervenant = $this->getServiceContext()->getIntervenant();
107
        $utilisateur = $this->getServiceContext()->getUtilisateur();
lecluse's avatar
#1344    
lecluse committed
108
109

        // chargement des rôles métiers
110

lecluse's avatar
lecluse committed
111
        $query = $this->getEntityManager()->createQuery(
112
            'SELECT
lecluse's avatar
lecluse committed
113
114
115
116
            r, a, s, p
        FROM
            Application\Entity\Db\Role r
            JOIN r.perimetre p
117
            LEFT JOIN r.affectation a WITH a.histoDestruction IS NULL AND a.utilisateur = :utilisateur
lecluse's avatar
lecluse committed
118
119
            LEFT JOIN a.structure s
        WHERE
120
            r.histoDestruction IS NULL'
121
        )->setParameter('utilisateur', $utilisateur);
122
123
124
        $query->useResultCache(true);
        $query->setResultCacheId(__CLASS__.'/affectations');

lecluse's avatar
lecluse committed
125

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

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

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

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

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

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

171

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

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

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

lecluse's avatar
lecluse committed
191
192


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

lecluse's avatar
lecluse committed
209
        return $session->statutsInfo;
lecluse's avatar
lecluse committed
210
211
    }

lecluse's avatar
lecluse committed
212
213


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

218
219
        return $this;
    }
220
}