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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
To Configure
<?php
Access::config(array(
'permissions' => array(
'adapter' => 'Permissions',
'model' => 'app\models\Perms',
'defaultNoUser' => array(),
'defaultUser' => array(
'route' => array(
'users' => array(
'logout', 'account'
)
)
),
'userIdentifier' => 'PrincipalID'
)
));
Perms::applyFilter('find', function($self, $params, $chain) {
if($params['type'] != 'first') {
return $chain->next($self, $params, $chain);
}
$cacheKey = 'permissions_' . $params['options']['conditions']['id'];
$cache = Cache::read('default', $cacheKey);
if($cache) {
return $cache;
}
$result = $chain->next($self, $params, $chain);
Cache::write('default', $cacheKey, $result, '+1 day');
return $result;
});
?>
<?php
namespace li3_access\extensions\adapter\security\access;
use lithium\core\ConfigException;
use lithium\util\Set;
class Permissions extends \lithium\core\Object {
const pathRoute = 'route';
const pathCustom = 'custom';
protected $_model = null;
public function __construct(array $config = array()) {
$defaults = array(
'model' => 'app\models\perms',
'defaultNoUser' => array(),
'defaultUser' => array(),
'userIdentifier' => 'id'
);
parent::__construct($config + $defaults);
$this->_model = $this->_config['model'];
}
/**
* @throws \lithium\core\ConfigException
* @param $user
* @param $request
* @param array $options
* @return array
*/
public function check($user, $request, array $options = array()) {
$config = $this->_config;
$model = $this->_model;
$params = compact('user', 'request', 'options');
return $this->_filter(__METHOD__, $params, function($self, $params) use($config, $model) {
$user = $params['user'];
$request = $params['request'];
$options = $params['options'];
$reqIsObject = is_object($request);
$path = array();
switch (true) {
case $reqIsObject:
$path = array(
Permissions::pathRoute,
$request->params['controller'],
$request->params['action']
);
break;
case (!$reqIsObject && is_string($request)):
$path = explode('.', $request);
array_unshift($path, Permissions::pathCustom);
break;
case (!$reqIsObject && is_array($request)):
$path = $request;
break;
}
switch(true) {
case !$user:
$hasAccess = $self->_processPath($path, $config['defaultNoUser']);
return $hasAccess ? false : $options;
case ($result = $self->_processPath($path, $config['defaultUser'])):
return $result ? false : $options;
default:
$userId = $config['userIdentifier'];
if(!isset($user[$userId])) {
$message = "The user identifier '{$userId}' is not available.";
throw new ConfigException($message);
}
$perms = $model::find('first', array(
'conditions' => array(
'id' => $user[$userId]
)
));
if(!$perms) {
return false;
}
$userPath = unserialize($perms->perms);
$result = $self->_processPath($path, $userPath);
return $result ? array() : $options;
}
});
}
/**
* Adds a custom route to the users permission list.
*
* $customRoute is formatted as a dot path string, this is done as 'foo.bar.baz' for example.
* Asterisks are usable at the end of the path however not in the middle. A user with access
* to 'foo.bar.*' will have access to foo.bar.baz, foo.bar.aaa etc.
*
* @param $user
* @param $customRoute
* @return bool
*/
public function addCustomPath($user, $customRoute) {
if(!is_string($customRoute)) {
return false;
}
$parts = explode('.', $customRoute);
$value = array_pop($parts);
$parts = array_merge((array)self::pathCustom, $parts, (array)0);
return $this->add($user, Set::expand(array(implode('.', $parts) => $value)));
}
/**
* Adds an action to the users permission list. If the action is set to * the user will have
* access to all of the controllers actions.
*/
public function addAction($user, $controller, $action) {
return $this->add($user, array(
self::pathRoute => array(
$controller => array(
$action
)
)
));
}
/**
* $user must contain the 'userIdentifier' key defined in config
* $paths are the paths which are to be added this is an array representation of the path and
* is from the origin, so 'route' or 'custom' must be specified. Multiple paths can be defined
* using this function
*
* @throws \lithium\core\ConfigException
* @param $user
* @param array $paths
* @return bool
*/
public function add($user, array $paths = array()) {
$model = $this->_model;
$userId = $this->_config['userIdentifier'];
$params = compact('model', 'userId', 'user', 'paths');
return $this->_filter(__METHOD__, $params, function($self, $params) {
$model = $params['model'];
$userId = $params['userId'];
$user = $params['user'];
$paths = $params['paths'];
if(!isset($user[$userId])) {
throw new ConfigException("The user identifier '{$userId}' is not available.");
}
$result = $model::find('first', array(
'conditions' => array(
'id' => $user[$userId]
)
));
if(!$result) {
$perms = $model::create(array(
'id' => $user[$userId],
'perms' => serialize($paths)
));
return $perms->save();
}
$allowedPaths = unserialize($result->perms);
$allowedPaths = array_merge_recursive($allowedPaths, $paths);
$result->perms = serialize($allowedPaths);
return $result->save();
});
}
public function removeCustomPath($user, $customRoute) {
if(!is_string($customRoute)) {
return false;
}
$parts = explode('.', $customRoute);
$value = array_pop($parts);
$parts = array_merge((array)self::pathCustom, $parts, (array)0);
return $this->remove($user, Set::expand(array(implode('.', $parts) => $value)));
}
/**
* Removes an action from a users permission list. Setting action to * removes all actions
* in the controller thus removing the controller from the users permission list.
*/
public function removeAction($user, $controller, $action) {
return $this->remove($user, array(
self::pathRoute => array(
$controller => array(
$action
)
)
));
}
/**
* use this to remove permissions from a user, multiple permissions can be defined in the paths
* array. The user must have the configured userIdentifier available.
*
* @throws \lithium\core\ConfigException
* @param $user
* @param array $paths
* @return bool
*/
public function remove($user, array $paths = array()) {
$model = $this->_model;
$userId = $this->_config['userIdentifier'];
$params = compact('model', 'userId', 'user', 'paths');
return $this->_filter(__METHOD__, $params, function($self, $params) {
$model = $params['model'];
$userId = $params['userId'];
$user = $params['user'];
$paths = $params['paths'];
if (!isset($user[$userId])) {
throw new ConfigException("The user identifier '{$userId}' is not available.");
}
$result = $model::find('first', array(
'conditions' => array(
'id' => $user[$userId]
)
));
if (!$result) {
return true;
}
$allowedPaths = unserialize($result->perms);
$pathsFlat = Set::flatten($paths);
foreach ($pathsFlat as $path => $value) {
$pointer = &$allowedPaths;
$pathParts = explode('.', $path);
array_pop($pathParts);
foreach ($pathParts as $pathPart) {
if (!isset($pointer[$pathPart])) {
unset($pointer);
$pointer = null;
break;
}
$pointer = &$pointer[$pathPart];
}
switch(true) {
case !$pointer:
break;
case $value == '*':
$pointer = null;
break;
case (($index = array_search($value, $pointer)) !== false):
unset($pointer[$index]);
break;
}
}
$result->perms = serialize($self->_cleanPaths($allowedPaths));
return $result->save();
});
}
public function listPerms($user) {
$userId = $user[$this->_config['userIdentifier']];
$model = $this->_model;
$result = $model::find('first', array(
'conditions' => array(
'id' => $userId
)
));
return $result ? unserialize($result->perms) : array();
}
public function _cleanPaths($paths) {
foreach ($paths as &$path) {
if (is_array($path)) {
$path = $this->_cleanPaths($path);
}
}
return array_filter($paths);
}
public function _processPath($path, &$allowedPaths) {
$pointer = &$allowedPaths;
foreach($path as $item) {
switch(true) {
case (in_array('*', $pointer)):
return true;
case (in_array($item, $pointer)):
$pointer = array();
continue;
case (!isset($pointer[$item])):
return false;
}
$pointer = &$pointer[$item];
}
return true;
}
}
?>
|