forked from phalcon/incubator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemory.php
241 lines (214 loc) · 6.67 KB
/
Memory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
/*
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2016 Phalcon Team (http://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file docs/LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to [email protected] so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Nemanja Ognjanovic <[email protected]> |
+------------------------------------------------------------------------+
*/
namespace Phalcon\Acl\Factory;
use Phalcon\Config;
use Phalcon\Acl\Adapter\Memory as MemoryAdapter;
use Phalcon\Acl\Exception;
use Phalcon\Acl\Resource;
use Phalcon\Acl\Role;
/**
* Class Memory
*
* This factory is intended to be used to ease setup of \Phalcon\Acl\Adapter\Memory
* in case \Phalcon\Config is used for configuration.
*
* @package Phalcon\Acl\Factory
*/
class Memory
{
/**
* @var Config
*/
private $config;
/**
* @var MemoryAdapter
*/
private $acl;
/**
* Array of defined role objects.
*
* @var Role[]
*/
private $roles = [];
/**
* Creates configured instance of acl.
*
* @param Config $config config
* @return MemoryAdapter acl
* @throws Exception If configuration is wrong
*/
public function create(Config $config)
{
$this->acl = new MemoryAdapter();
$this->config = $config;
$defaultAction = $this->config->get('defaultAction');
if (!is_int($defaultAction) && !ctype_digit($defaultAction)) {
throw new Exception('Key "defaultAction" must exist and must be of numeric value.');
}
$this->acl->setDefaultAction((int) $defaultAction);
$this->addResources();
$this->addRoles();
return $this->acl;
}
/**
* Adds resources from config to acl object.
*
* @return $this
* @throws Exception
*/
protected function addResources()
{
if (!(array)$this->config->get('resource')) {
throw new Exception('Key "resource" must exist and must be traversable.');
}
// resources
foreach ($this->config->get('resource') as $name => $resource) {
$actions = (array) $resource->get('actions');
if (!$actions) {
$actions = null;
}
$this->acl->addResource(
$this->makeResource($name, $resource->description),
$actions
);
}
return $this;
}
/**
* Adds role from config to acl object.
*
* @return $this
* @throws Exception
*/
protected function addRoles()
{
if (!(array)$this->config->get('role')) {
throw new Exception('Key "role" must exist and must be traversable.');
}
foreach ($this->config->get('role') as $role => $rules) {
$this->roles[$role] = $this->makeRole($role, $rules->get('description'));
$this->addRole($role, $rules);
$this->addAccessRulesToRole($role, $rules);
}
return $this;
}
/**
* Adds access rules to role.
*
* @param string $role role
* @param Config $rules rules
*
* @return $this
*
* @throws Exception
*/
protected function addAccessRulesToRole($role, Config $rules)
{
foreach ($rules as $method => $rules) {
// skip not wanted rules
if (in_array($method, ['inherit', 'description'])) {
continue;
}
foreach ($rules as $controller => $actionRules) {
$actions = $this->castAction($actionRules->get('actions'));
if (!in_array($method, ['allow', 'deny'])) {
throw new Exception(sprintf(
'Wrong access method given. Expected "allow" or "deny" but "%s" was set.',
$method
));
}
$this->acl->{$method}($role, $controller, $actions);
}
}
return $this;
}
/**
* Cast actions
*
* @param mixed $actions Actions
* @return array|null
* @throws Exception
*/
protected function castAction($actions)
{
if ($actions instanceof Config) {
$actions = $actions->toArray();
} elseif (is_string($actions)) {
$actions = [$actions];
}
if (!is_array($actions)) {
throw new Exception(
'Key "actions" must exist and must be traversable.'
);
}
return $actions;
}
/**
* Add role to acl.
*
* @param string $role role
* @param Config $rules rules
*
* @return $this
*
* @throws Exception
*/
protected function addRole($role, Config $rules)
{
// role has inheritance ?
if ($rules->get('inherit')) {
// role exists?
if (!array_key_exists($rules->inherit, $this->roles)) {
throw new Exception(sprintf(
'Role "%s" cannot inherit non-existent role "%s".
Either such role does not exist or it is set to be inherited before it is actually defined.',
$role,
$rules->inherit
));
}
$this->acl->addRole($this->roles[$role], $this->roles[$rules->inherit]);
} else {
$this->acl->addRole($this->roles[$role]);
}
return $this;
}
/**
* Creates acl resource.
*
* @param string $name Resource name
* @param string|null $description Resource description [Optional]
*
* @return Resource
*/
protected function makeResource($name, $description = null)
{
return new Resource($name, $description);
}
/**
* Creates acl role.
*
* @param string $role Role name
* @param string|null $description Description [Optional]
*
* @return Role
*/
protected function makeRole($role, $description = null)
{
return new Role($role, $description);
}
}