1
|
|
|
<?php |
2
|
|
|
namespace Burzum\SimpleRbac\Auth; |
3
|
|
|
|
4
|
|
|
use Cake\Auth\BaseAuthorize; |
5
|
|
|
use Cake\Core\Configure; |
6
|
|
|
use Cake\Network\Request; |
7
|
|
|
use Cake\Utility\Inflector; |
8
|
|
|
use RuntimeException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Copyright 2011 - 2015, Florian Krämer |
12
|
|
|
* |
13
|
|
|
* Licensed under The MIT License |
14
|
|
|
* Redistributions of files must retain the above copyright notice. |
15
|
|
|
* |
16
|
|
|
* @copyright Copyright 2011 - 2015, Florian Krämer |
17
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
18
|
|
|
*/ |
19
|
|
|
class SimpleRbacAuthorize extends BaseAuthorize { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Default config for this object. |
23
|
|
|
* |
24
|
|
|
* - `roleField` - The name of the role field in the user data array that is passed to authorize() |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
public $_defaultConfig = array( |
29
|
|
|
'roleField' => 'role', |
30
|
|
|
'allowEmptyActionMap' => false, |
31
|
|
|
'allowEmptyPrefixMap' => true, |
32
|
|
|
'undefinedActionsAreAllowed' => false |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Authorize a user based on his roles |
37
|
|
|
* |
38
|
|
|
* @param array $user The user to authorize |
39
|
|
|
* @param Request $request The request needing authorization. |
40
|
|
|
* @return boolean |
41
|
|
|
* @throws RuntimeException when the role field does not exist |
42
|
|
|
*/ |
43
|
|
|
public function authorize($user, Request $request) { |
44
|
|
|
$roleField = $this->_config['roleField']; |
45
|
|
|
|
46
|
|
|
if (!isset($user[$roleField])) { |
47
|
|
|
throw new RuntimeException(sprintf('The role field `%s` does not exist!', $roleField)); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if (is_string($user[$roleField])) { |
51
|
|
|
$user[$roleField] = array($user[$roleField]); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if ($this->authorizeByPrefix($user[$roleField], $request)) { |
55
|
|
|
return true; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if ($this->authorizeByControllerAndAction($user, $request)) { |
59
|
|
|
return true; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return false; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Checks if a role is granted access to a controller and action |
67
|
|
|
* |
68
|
|
|
* @param array $user |
69
|
|
|
* @param Request $request |
70
|
|
|
* @return boolean |
71
|
|
|
*/ |
72
|
|
|
public function authorizeByControllerAndAction($user, Request $request) { |
73
|
|
|
$roleField = $this->_config['roleField']; |
74
|
|
|
extract($this->getControllerNameAndAction($request)); |
|
|
|
|
75
|
|
|
$actionMap = $this->getActionMap(); |
76
|
|
|
|
77
|
|
View Code Duplication |
if (isset($actionMap[$name]['*'])) { |
|
|
|
|
78
|
|
|
if ($this->_isAllowedRole($user[$roleField], $actionMap[$name]['*'])) { |
79
|
|
|
return true; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
View Code Duplication |
if (isset($actionMap[$name][$action])) { |
|
|
|
|
84
|
|
|
if ($this->_isAllowedRole($user[$roleField], $actionMap[$name][$action])) { |
85
|
|
|
return true; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if ($this->config('undefinedActionsAreAllowed') === true) { |
90
|
|
|
return true; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Checks if a role is allowed. |
98
|
|
|
* |
99
|
|
|
* @param array|string $userRoles |
100
|
|
|
* @param array $allowedRoles |
101
|
|
|
* @return boolean |
102
|
|
|
*/ |
103
|
|
|
protected function _isAllowedRole($userRoles, array $allowedRoles) { |
104
|
|
|
if (in_array('*', $allowedRoles)) { |
105
|
|
|
return true; |
106
|
|
|
} |
107
|
|
|
if (is_string($userRoles)) { |
108
|
|
|
$userRoles = [$userRoles]; |
109
|
|
|
} |
110
|
|
|
foreach ($userRoles as $userRole) { |
111
|
|
|
if (in_array($userRole, $allowedRoles)) { |
112
|
|
|
return true; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
return false; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Checks if a role is granted access to a prefix route like /admin. |
120
|
|
|
* |
121
|
|
|
* @param array $roles |
122
|
|
|
* @param Request $request |
123
|
|
|
* @return boolean |
124
|
|
|
*/ |
125
|
|
|
public function authorizeByPrefix(array $roles, Request $request) { |
126
|
|
|
$prefixeMap = $this->getPrefixMap(); |
127
|
|
|
if (isset($request->params['prefix']) && isset($prefixeMap[$request->params['prefix']])) { |
128
|
|
|
foreach ($roles as $role) { |
129
|
|
|
if (in_array($role, $prefixeMap[$request->params['prefix']])) { |
130
|
|
|
return true; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
return false; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Gets the controller and action, prefixes the controller with the plugin if there is one |
139
|
|
|
* |
140
|
|
|
* @param Request $request |
141
|
|
|
* @return array |
142
|
|
|
*/ |
143
|
|
|
public function getControllerNameAndAction(Request $request) { |
144
|
|
|
$controller = $this->_registry->getController(); |
145
|
|
|
$name = $controller->name; |
146
|
|
|
$action = $request->action; |
|
|
|
|
147
|
|
|
|
148
|
|
|
if (!empty($request->params['plugin'])) { |
149
|
|
|
$name = Inflector::camelize($request->params['plugin']) . '.' . $name; |
150
|
|
|
} |
151
|
|
|
return compact('name', 'action'); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Can be overridden if inherited with a method to fetch this from anywhere, a database for example. |
156
|
|
|
* |
157
|
|
|
* @return array |
158
|
|
|
* @throws RuntimeException |
159
|
|
|
*/ |
160
|
|
View Code Duplication |
public function getActionMap() { |
|
|
|
|
161
|
|
|
$actionMap = (array) Configure::read('SimpleRbac.actionMap'); |
162
|
|
|
if (empty($actionMap) && $this->_config['allowEmptyActionMap'] === false) { |
163
|
|
|
throw new \RuntimeException('SimpleRbac.actionMap configuration is empty!'); |
164
|
|
|
} |
165
|
|
|
return $actionMap; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Can be overriden if inherited with a method to fetch this from anywhere, a database for exaple |
170
|
|
|
* |
171
|
|
|
* @return array |
172
|
|
|
* @throws \RuntimeException |
173
|
|
|
*/ |
174
|
|
View Code Duplication |
public function getPrefixMap() { |
|
|
|
|
175
|
|
|
$prefixMap = (array) Configure::read('SimpleRbac.prefixMap'); |
176
|
|
|
if (empty($prefixMap) && $this->_config['allowEmptyPrefixMap'] === false) { |
177
|
|
|
throw new \RuntimeException('SimpleRbac.prefixMap configuration is empty!'); |
178
|
|
|
} |
179
|
|
|
return $prefixMap; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|