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

namespace UnicaenAuth\Assertion;

5
use Zend\Mvc\Application;
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
use Zend\Mvc\MvcEvent;
use Zend\Permissions\Acl\Acl;
use Zend\Permissions\Acl\Assertion\AssertionInterface;
use Zend\Permissions\Acl\Resource\ResourceInterface;
use Zend\Permissions\Acl\Role\RoleInterface;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorAwareTrait;

/**
 * Description of AbstractAssertion
 *
 * @author Laurent LÉCLUSE <laurent.lecluse at unicaen.fr>
 */
abstract class AbstractAssertion implements AssertionInterface, ServiceLocatorAwareInterface
{
    use ServiceLocatorAwareTrait;

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

    /**
     * @var RoleInterface
     */
    private $role;

33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62


    /**
     * !!!! 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)
    {
63
64
        $this->setAcl($acl);
        $this->setRole($role);
65
66
67
        switch (true) {
            case $this->detectPrivilege($resource):

68
                return $this->assertPrivilege(ltrim(strstr($resource, '/'), '/'), $privilege);
69
70
71
72
73
74
75
76
77

            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);

78
                return $this->assertController($controller, $action, $privilege);
79
80
81

            case $this->detectEntity($resource):

82
                return $this->assertEntity($resource, $privilege);
83
84
85

            default:

86
                return $this->assertOther($resource, $privilege);
87
88
89
90
91
92
        }
    }



    /**
93
94
95
96
97
98
99
100
101
102
103
     * @return Acl
     */
    public function getAcl()
    {
        return $this->acl;
    }



    /**
     * @param Acl $acl
104
     *
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
     * @return AbstractAssertion
     */
    public function setAcl(Acl $acl = null)
    {
        $this->acl = $acl;

        return $this;
    }



    /**
     * @return RoleInterface
     */
    public function getRole()
    {
        return $this->role;
    }



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

        return $this;
    }



    /**
141
142
143
144
145
146
147
148
149
150
151
152
153
154
     * @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/');
    }



    /**
155
156
     * @param string $privilege
     * @param string $subPrivilege
157
158
159
     *
     * @return boolean
     */
160
    protected function assertPrivilege($privilege, $subPrivilege = null)
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
    {
        return true;
    }



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

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



    /**
182
183
184
     * @param string $controller
     * @param string $action
     * @param string $privilege
185
186
187
     *
     * @return boolean
     */
188
    protected function assertController($controller, $action = null, $privilege = null)
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
    {
        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
     */
215
    protected function assertEntity(ResourceInterface $entity, $privilege = null)
216
217
218
219
220
221
222
    {
        return true;
    }



    /**
223
     * @param ResourceInterface $resource
224
225
226
227
     * @param string            $privilege
     *
     * @return boolean
     */
228
    protected function assertOther(ResourceInterface $resource = null, $privilege = null)
229
230
231
232
233
234
235
236
237
238
239
240
241
    {
        return true;
    }



    /**
     * @return MvcEvent
     */
    protected function getMvcEvent()
    {
        $application = $this->getServiceLocator()->get('Application');

242
243
        /* @var $application Application */

244
245
246
247
        return $application->getMvcEvent();
    }

}