forked from phalcon/incubator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMongo.php
471 lines (415 loc) · 13.4 KB
/
Mongo.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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
<?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 license@phalconphp.com so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Andres Gutierrez <andres@phalconphp.com> |
| Eduar Carvajal <eduar@phalconphp.com> |
+------------------------------------------------------------------------+
*/
namespace Phalcon\Acl\Adapter;
use Phalcon\Acl\Adapter;
use Phalcon\Acl\Exception;
use Phalcon\Acl\Resource;
use Phalcon\Acl;
use Phalcon\Acl\Role;
use Phalcon\Acl\RoleInterface;
/**
* Phalcon\Acl\Adapter\Mongo
* Manages ACL lists using Mongo Collections
*/
class Mongo extends Adapter
{
/**
* @var array
*/
protected $options;
/**
* Class constructor.
*
* @param array $options
* @throws \Phalcon\Acl\Exception
*/
public function __construct($options)
{
if (!is_array($options)) {
throw new Exception("Acl options must be an array");
}
if (!isset($options['db'])) {
throw new Exception("Parameter 'db' is required");
}
if (!isset($options['roles'])) {
throw new Exception("Parameter 'roles' is required");
}
if (!isset($options['resources'])) {
throw new Exception("Parameter 'resources' is required");
}
if (!isset($options['resourcesAccesses'])) {
throw new Exception("Parameter 'resourcesAccesses' is required");
}
if (!isset($options['accessList'])) {
throw new Exception("Parameter 'accessList' is required");
}
$this->options = $options;
}
/**
* {@inheritdoc}
*
* Example:
* <code>$acl->addRole(new Phalcon\Acl\Role('administrator'), 'consultor');</code>
* <code>$acl->addRole('administrator', 'consultor');</code>
*
* @param string $role
* @param array $accessInherits
* @return boolean
* @throws \Phalcon\Acl\Exception
*/
public function addRole($role, $accessInherits = null)
{
if (is_string($role)) {
$role = new Role($role, ucwords($role) . ' Role');
}
if (!$role instanceof RoleInterface) {
throw new Exception('Role must be either an string or implement RoleInterface');
}
$roles = $this->getCollection('roles');
$exists = $roles->count(['name' => $role->getName()]);
if (!$exists) {
$roles->insert([
'name' => $role->getName(),
'description' => $role->getDescription()
]);
$this->getCollection('accessList')->insert([
'roles_name' => $role->getName(),
'resources_name' => '*',
'access_name' => '*',
'allowed' => $this->_defaultAccess
]);
}
if ($accessInherits) {
return $this->addInherit($role->getName(), $accessInherits);
}
return true;
}
/**
* {@inheritdoc}
*
* @param string $roleName
* @param string $roleToInherit
* @throws \BadMethodCallException
*/
public function addInherit($roleName, $roleToInherit)
{
throw new \BadMethodCallException('Not implemented yet.');
}
/**
* {@inheritdoc}
*
* @param string $roleName
* @return boolean
*/
public function isRole($roleName)
{
return $this->getCollection('roles')->count(['name' => $roleName]) > 0;
}
/**
* {@inheritdoc}
*
* @param string $resourceName
* @return boolean
*/
public function isResource($resourceName)
{
return $this->getCollection('resources')->count(['name' => $resourceName]) > 0;
}
/**
* {@inheritdoc}
*
* Example:
* <code>
* //Add a resource to the the list allowing access to an action
* $acl->addResource(new Phalcon\Acl\Resource('customers'), 'search');
* $acl->addResource('customers', 'search');
* //Add a resource with an access list
* $acl->addResource(new Phalcon\Acl\Resource('customers'), array('create', 'search'));
* $acl->addResource('customers', array('create', 'search'));
* </code>
*
* @param \Phalcon\Acl\Resource $resource
* @param array|string $accessList
* @return boolean
*/
public function addResource($resource, $accessList = null)
{
if (!is_object($resource)) {
$resource = new Resource($resource);
}
$resources = $this->getCollection('resources');
$exists = $resources->count(['name' => $resource->getName()]);
if (!$exists) {
$resources->insert([
'name' => $resource->getName(),
'description' => $resource->getDescription()
]);
}
if ($accessList) {
return $this->addResourceAccess($resource->getName(), $accessList);
}
return true;
}
/**
* {@inheritdoc}
*
* @param string $resourceName
* @param array|string $accessList
* @return boolean
* @throws \Phalcon\Acl\Exception
*/
public function addResourceAccess($resourceName, $accessList)
{
if (!$this->isResource($resourceName)) {
throw new Exception("Resource '" . $resourceName . "' does not exist in ACL");
}
$resourcesAccesses = $this->getCollection('resourcesAccesses');
if (!is_array($accessList)) {
$accessList = [$accessList];
}
foreach ($accessList as $accessName) {
$exists = $resourcesAccesses->count([
'resources_name' => $resourceName,
'access_name' => $accessName
]);
if (!$exists) {
$resourcesAccesses->insert([
'resources_name' => $resourceName,
'access_name' => $accessName
]);
}
}
return true;
}
/**
* {@inheritdoc}
*
* @return \Phalcon\Acl\Resource[]
*/
public function getResources()
{
$resources = [];
foreach ($this->getCollection('resources')->find() as $row) {
$resources[] = new Resource($row['name'], $row['description']);
}
return $resources;
}
/**
* {@inheritdoc}
*
* @return RoleInterface[]
*/
public function getRoles()
{
$roles = [];
foreach ($this->getCollection('roles')->find() as $row) {
$roles[] = new Role($row['name'], $row['description']);
}
return $roles;
}
/**
* {@inheritdoc}
*
* @param string $resourceName
* @param array|string $accessList
*/
public function dropResourceAccess($resourceName, $accessList)
{
throw new \BadMethodCallException('Not implemented yet.');
}
/**
* {@inheritdoc}
*
* You can use '*' as wildcard
* Example:
* <code>
* //Allow access to guests to search on customers
* $acl->allow('guests', 'customers', 'search');
* //Allow access to guests to search or create on customers
* $acl->allow('guests', 'customers', array('search', 'create'));
* //Allow access to any role to browse on products
* $acl->allow('*', 'products', 'browse');
* //Allow access to any role to browse on any resource
* $acl->allow('*', '*', 'browse');
* </code>
*
* @param string $roleName
* @param string $resourceName
* @param mixed $access
*/
public function allow($roleName, $resourceName, $access)
{
$this->allowOrDeny($roleName, $resourceName, $access, Acl::ALLOW);
}
/**
* {@inheritdoc}
*
* You can use '*' as wildcard
* Example:
* <code>
* //Deny access to guests to search on customers
* $acl->deny('guests', 'customers', 'search');
* //Deny access to guests to search or create on customers
* $acl->deny('guests', 'customers', array('search', 'create'));
* //Deny access to any role to browse on products
* $acl->deny('*', 'products', 'browse');
* //Deny access to any role to browse on any resource
* $acl->deny('*', '*', 'browse');
* </code>
*
* @param string $roleName
* @param string $resourceName
* @param mixed $access
* @return boolean
*/
public function deny($roleName, $resourceName, $access)
{
$this->allowOrDeny($roleName, $resourceName, $access, Acl::DENY);
}
/**
* {@inheritdoc}
*
* Example:
* <code>
* //Does Andres have access to the customers resource to create?
* $acl->isAllowed('Andres', 'Products', 'create');
* //Do guests have access to any resource to edit?
* $acl->isAllowed('guests', '*', 'edit');
* </code>
*
* @param string $role
* @param string $resource
* @param string $access
* @return boolean
*/
public function isAllowed($role, $resource, $access)
{
$accessList = $this->getCollection('accessList');
$access = $accessList->findOne([
'roles_name' => $role,
'resources_name' => $resource,
'access_name' => $access
]);
if (is_array($access)) {
return (bool) $access['allowed'];
}
/**
* Check if there is an common rule for that resource
*/
$access = $accessList->findOne([
'roles_name' => $role,
'resources_name' => $resource,
'access_name' => '*'
]);
if (is_array($access)) {
return (bool) $access['allowed'];
}
return $this->_defaultAccess;
}
/**
* Returns a mongo collection
*
* @param string $name
* @return \MongoCollection
*/
protected function getCollection($name)
{
return $this->options['db']->selectCollection($this->options[$name]);
}
/**
* Inserts/Updates a permission in the access list
*
* @param string $roleName
* @param string $resourceName
* @param string $accessName
* @param integer $action
* @return boolean
* @throws \Phalcon\Acl\Exception
*/
protected function insertOrUpdateAccess($roleName, $resourceName, $accessName, $action)
{
/**
* Check if the access is valid in the resource
*/
$exists = $this->getCollection('resourcesAccesses')->count([
'resources_name' => $resourceName,
'access_name' => $accessName
]);
if (!$exists) {
throw new Exception(
"Access '" . $accessName . "' does not exist in resource '" . $resourceName . "' in ACL"
);
}
$accessList = $this->getCollection('accessList');
$access = $accessList->findOne([
'roles_name' => $roleName,
'resources_name' => $resourceName,
'access_name' => $accessName
]);
if (!$access) {
$accessList->insert([
'roles_name' => $roleName,
'resources_name' => $resourceName,
'access_name' => $accessName,
'allowed' => $action
]);
} else {
$access['allowed'] = $action;
$accessList->save($access);
}
/**
* Update the access '*' in access_list
*/
$exists = $accessList->count([
'roles_name' => $roleName,
'resources_name' => $resourceName,
'access_name' => '*'
]);
if (!$exists) {
$accessList->insert([
'roles_name' => $roleName,
'resources_name' => $resourceName,
'access_name' => '*',
'allowed' => $this->_defaultAccess
]);
}
return true;
}
/**
* Inserts/Updates a permission in the access list
*
* @param string $roleName
* @param string $resourceName
* @param string $access
* @param integer $action
* @throws \Phalcon\Acl\Exception
*/
protected function allowOrDeny($roleName, $resourceName, $access, $action)
{
if (!$this->isRole($roleName)) {
throw new Exception('Role "' . $roleName . '" does not exist in the list');
}
if (!is_array($access)) {
$access = [$access];
}
foreach ($access as $accessName) {
$this->insertOrUpdateAccess($roleName, $resourceName, $accessName, $action);
}
}
}