1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* KumbiaPHP web & app Framework |
4
|
|
|
* |
5
|
|
|
* LICENSE |
6
|
|
|
* |
7
|
|
|
* This source file is subject to the new BSD license that is bundled |
8
|
|
|
* with this package in the file LICENSE. |
9
|
|
|
* |
10
|
|
|
* @category Kumbia |
11
|
|
|
* @package Acl |
12
|
|
|
* |
13
|
|
|
* @copyright Copyright (c) 2005 - 2023 KumbiaPHP Team (http://www.kumbiaphp.com) |
14
|
|
|
* @license https://github.com/KumbiaPHP/KumbiaPHP/blob/master/LICENSE New BSD License |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Clase Base para gestión de ACL |
19
|
|
|
* |
20
|
|
|
* Nueva Clase Base para gestión de ACL (Access Control List) permisos |
21
|
|
|
* |
22
|
|
|
* @category Kumbia |
23
|
|
|
* @package Acl |
24
|
|
|
*/ |
25
|
|
|
abstract class Acl2 |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Adaptador por defecto |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected static $_defaultAdapter = 'simple'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Verifica si el usuario puede acceder al recurso |
37
|
|
|
* |
38
|
|
|
* @param string $resource recurso al cual se verificará acceso |
39
|
|
|
* @param string $user usuario de la acl |
40
|
|
|
* @return boolean |
41
|
|
|
*/ |
42
|
|
|
public function check($resource, $user) |
43
|
|
|
{ |
44
|
|
|
// Itera en los roles de usuario |
45
|
|
|
foreach ($this->_getUserRoles($user) as $role) { |
46
|
|
|
if ($this->_checkRole($role, $resource)) { |
47
|
|
|
return TRUE; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// Por defecto se niega el acceso |
52
|
|
|
return FALSE; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Verifica si un rol puede acceder al recurso |
57
|
|
|
* |
58
|
|
|
* @param string $role |
59
|
|
|
* @param string $resource |
60
|
|
|
* @return boolean |
61
|
|
|
*/ |
62
|
|
|
private function _checkRole($role, $resource) |
63
|
|
|
{ |
64
|
|
|
// Verificar si el rol puede acceder al recurso |
65
|
|
|
if (in_array($resource, $this->_getRoleResources($role))) { |
66
|
|
|
return TRUE; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// Verifica si ha heredado el acceso, verificando los recursos de los padres |
70
|
|
|
foreach ($this->_getRoleParents($role) as $parent) { |
71
|
|
|
if ($this->_checkRole($parent, $resource)) { |
72
|
|
|
return TRUE; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return FALSE; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Obtiene los roles del usuario al que se le valida si puede acceder al recurso |
81
|
|
|
* |
82
|
|
|
* @param string $user usuario al que se le valida acceso |
83
|
|
|
* @return array roles de usuario |
84
|
|
|
*/ |
85
|
|
|
abstract protected function _getUserRoles($user); |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Obtiene los recursos al cual el rol puede acceder |
89
|
|
|
* |
90
|
|
|
* @param string $role nombre de rol |
91
|
|
|
* @return array recursos al cual el rol puede acceder |
92
|
|
|
*/ |
93
|
|
|
abstract protected function _getRoleResources($role); |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Obtiene los padres del rol |
97
|
|
|
* |
98
|
|
|
* @param string $role nombre de rol |
99
|
|
|
* @return array padres del rol |
100
|
|
|
*/ |
101
|
|
|
abstract protected function _getRoleParents($role); |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Obtiene el adaptador para ACL |
105
|
|
|
* |
106
|
|
|
* @param string $adapter (simple, model, xml, ini) |
107
|
|
|
*/ |
108
|
|
|
public static function factory($adapter = '') |
109
|
|
|
{ |
110
|
|
|
if (!$adapter) { |
111
|
|
|
$adapter = self::$_defaultAdapter; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
require_once __DIR__ . "/adapters/{$adapter}_acl.php"; |
115
|
|
|
$class = $adapter . 'acl'; |
116
|
|
|
|
117
|
|
|
return new $class; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Cambia el adaptador por defecto |
122
|
|
|
* |
123
|
|
|
* @param string $adapter nombre del adaptador por defecto |
124
|
|
|
*/ |
125
|
|
|
public static function setDefault($adapter) |
126
|
|
|
{ |
127
|
|
|
self::$_defaultAdapter = $adapter; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
} |
131
|
|
|
|