AbstractAssertion.php 7.91 KB
Newer Older
1
2
3
4
<?php

namespace UnicaenAuth\Assertion;

Laurent Lécluse's avatar
Laurent Lécluse committed
5
use BjyAuthorize\Service\Authorize;
6
7
use UnicaenApp\ServiceManager\ServiceLocatorAwareInterface;
use UnicaenApp\ServiceManager\ServiceLocatorAwareTrait;
Laurent Lécluse's avatar
Laurent Lécluse committed
8
use UnicaenAuth\Service\Traits\UserContextServiceAwareTrait;
9
use Zend\Mvc\MvcEvent;
10
use Zend\Mvc\Plugin\FlashMessenger\FlashMessenger;
11
12
13
14
15
16
17
18
19
20
21
22
23
use Zend\Permissions\Acl\Acl;
use Zend\Permissions\Acl\Assertion\AssertionInterface;
use Zend\Permissions\Acl\Resource\ResourceInterface;
use Zend\Permissions\Acl\Role\RoleInterface;

/**
 * Description of AbstractAssertion
 *
 * @author Laurent LÉCLUSE <laurent.lecluse at unicaen.fr>
 */
abstract class AbstractAssertion implements AssertionInterface, ServiceLocatorAwareInterface
{
    use ServiceLocatorAwareTrait;
Laurent Lécluse's avatar
Laurent Lécluse committed
24
    use UserContextServiceAwareTrait;
25

26
27
28
29
30
31
32
33
    /**
     * @var Acl
     */
    private $acl;

    /**
     * @var RoleInterface
     */
Laurent Lécluse's avatar
Laurent Lécluse committed
34
    private $role = false;
35

36
37
38
39
40
    /**
     * @var FlashMessenger
     */
    private $fm;

41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70


    /**
     * !!!! Pour éviter l'erreur "Serialization of 'Closure' is not allowed"... !!!!
     *
     * @return array
     */
    public function __sleep()
    {
        return [];
    }



    /**
     * Returns true if and only if the assertion conditions are met
     *
     * This method is passed the ACL, Role, Resource, and privilege to which the authorization query applies. If the
     * $role, $this->resource, or $privilege parameters are null, it means that the query applies to all Roles, Resources, or
     * privileges, respectively.
     *
     * @param  Acl               $acl
     * @param  RoleInterface     $role
     * @param  ResourceInterface $resource
     * @param  string            $privilege
     *
     * @return bool
     */
    public final function assert(Acl $acl, RoleInterface $role = null, ResourceInterface $resource = null, $privilege = null)
    {
71
72
        $this->setAcl($acl);
        $this->setRole($role);
73
        $this->init();
74
75
        switch (true) {
            case $this->detectPrivilege($resource):
76
//                var_dump('assertPrivilege '.get_class($this).' '.$resource->getResourceId().' '.$privilege);
77
                return $this->assertPrivilege(ltrim(strstr($resource, '/'), '/'), $privilege);
78
79
80
81
82
83
84
85
86

            case $this->detectController($resource):

                $resource   = (string)$resource;
                $spos       = strpos($resource, '/') + 1;
                $dpos       = strrpos($resource, ':') + 1;
                $controller = substr($resource, $spos, $dpos - $spos - 1);
                $action     = substr($resource, $dpos);

87
//                var_dump('assertController '.get_class($this).' '.$controller.'.'.$action.' '.$privilege);
88
                return $this->assertController($controller, $action, $privilege);
89
90

            case $this->detectEntity($resource):
91
//                var_dump('assertEntity '.get_class($this).' '.$resource->getResourceId().' '.$privilege);
92
                return $this->assertEntity($resource, $privilege);
93
94

            default:
95
//                var_dump('assertOther '.get_class($this).' '.$resource->getResourceId().' '.$privilege);
96
                return $this->assertOther($resource, $privilege);
97
98
99
100
101
        }
    }



102
103
104
105
106
107
108
109
110
111
112
113
114
    /**
     * @param string|ResourceInterface $resource
     * @param string                   $privilege
     *
     * @return bool
     */
    public function isAllowed($resource, $privilege = null)
    {
        return $this->getServiceAuthorize()->isAllowed($resource, $privilege);
    }



115
    /**
116
117
118
119
     * @return Acl
     */
    public function getAcl()
    {
Laurent Lécluse's avatar
Laurent Lécluse committed
120
121
122
        if (!$this->acl){
            $this->acl = $this->getServiceAuthorize()->getAcl();
        }
123
124
125
126
127
128
129
        return $this->acl;
    }



    /**
     * @param Acl $acl
130
     *
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
     * @return AbstractAssertion
     */
    public function setAcl(Acl $acl = null)
    {
        $this->acl = $acl;

        return $this;
    }



    /**
     * @return RoleInterface
     */
    public function getRole()
    {
Laurent Lécluse's avatar
Laurent Lécluse committed
147
        if (false === $this->role){
148
            $sUserContext = $this->serviceUserContext;
Laurent Lécluse's avatar
Laurent Lécluse committed
149
150
151
152
            if ($sUserContext->getIdentity()) {
                $this->role = $sUserContext->getSelectedIdentityRole();
            }
        }
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
        return $this->role;
    }



    /**
     * @param RoleInterface $role
     *
     * @return AbstractAssertion
     */
    public function setRole(RoleInterface $role = null)
    {
        $this->role = $role;

        return $this;
    }



    /**
173
174
175
176
177
178
179
180
181
182
183
184
185
186
     * @param string $resource
     *
     * @return boolean
     */
    private function detectPrivilege($resource = null)
    {
        if ($resource instanceof ResourceInterface) $resource = $resource->getResourceId();

        return is_string($resource) && 0 === strpos($resource, 'privilege/');
    }



    /**
187
188
     * @param string $privilege
     * @param string $subPrivilege
189
190
191
     *
     * @return boolean
     */
192
    protected function assertPrivilege($privilege, $subPrivilege = null)
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
    {
        return true;
    }



    /**
     * @param string $resource
     *
     * @return boolean
     */
    private function detectController($resource = null)
    {
        if ($resource instanceof ResourceInterface) $resource = $resource->getResourceId();

        return 0 === strpos($resource, 'controller/');
    }



213
214
215
216
217
218
219
220
221
222
    /**
     * Ititialisation des paramètres de l'assertion (si nécessaire)
     */
    public function init()
    {

    }



223
    /**
224
225
226
     * @param string $controller
     * @param string $action
     * @param string $privilege
227
228
229
     *
     * @return boolean
     */
230
    protected function assertController($controller, $action = null, $privilege = null)
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
    {
        return true;
    }



    /**
     * @param string $resource
     *
     * @return boolean
     */
    private function detectEntity($resource = null)
    {
        return
            is_object($resource)
            && method_exists($resource, 'getId');
    }



    /**
     * @param ResourceInterface $entity
     * @param string            $privilege
     *
     * @return boolean
     */
257
    protected function assertEntity(ResourceInterface $entity, $privilege = null)
258
259
260
261
262
263
264
    {
        return true;
    }



    /**
265
     * @param ResourceInterface $resource
266
267
268
269
     * @param string            $privilege
     *
     * @return boolean
     */
270
    protected function assertOther(ResourceInterface $resource = null, $privilege = null)
271
272
273
274
275
276
    {
        return true;
    }



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
    /**
     * Parcours la liste des résultats des assertions transmises (liste de booleans)
     * Si l'une d'entres elles est fausse alors false est retourné. true sinon.
     *
     * @param array $assertions
     *
     * @return bool
     */
    protected function asserts($assertions)
    {
        if (!is_array($assertions)){
            $assertions = [$assertions];
        }

        foreach( $assertions as $assertion ){
            if (!$assertion) return false;
        }

        return true;
    }

298
299
300
301
    /**
     * @var MvcEvent
     */
    private $mvcEvent;
302

303
304
305
306
307
308
309
    /**
     * @param MvcEvent $mvcEvent
     */
    public function setMvcEvent(MvcEvent $mvcEvent)
    {
        $this->mvcEvent = $mvcEvent;
    }
310

311
312
313
314
315
    /**
     * @return MvcEvent
     */
    protected function getMvcEvent()
    {
316
        return $this->mvcEvent;
317
318
    }

319
320
321
322
    /**
     * @var Authorize
     */
    private $serviceAuthorize;
Laurent Lécluse's avatar
Laurent Lécluse committed
323

324
325
326
327
328
329
330
    /**
     * @param Authorize $serviceAuthorize
     */
    public function setServiceAuthorize(Authorize $serviceAuthorize)
    {
        $this->serviceAuthorize = $serviceAuthorize;
    }
Laurent Lécluse's avatar
Laurent Lécluse committed
331
332
333
334
335
336

    /**
     * @return Authorize
     */
    private function getServiceAuthorize()
    {
337
        return $this->serviceAuthorize;
Laurent Lécluse's avatar
Laurent Lécluse committed
338
339
    }

340
341
342
343
344
345
346
347
    /**
     * @param FlashMessenger $fm
     * @deprecated Merci d'abandonner cette méthode : SoC violation !
     */
    public function setFlashMessenger(FlashMessenger $fm)
    {
        $this->fm = $fm;
    }
348
349
350

    /**
     * @return FlashMessenger
351
     * @deprecated Merci d'abandonner cette méthode : SoC violation !
352
353
354
355
356
     */
    protected function flashMessenger()
    {
        return $this->fm;
    }
357
}