This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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)); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
75 | $actionMap = $this->getActionMap(); |
||
76 | |||
77 | View Code Duplication | if (isset($actionMap[$name]['*'])) { |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
78 | if ($this->_isAllowedRole($user[$roleField], $actionMap[$name]['*'])) { |
||
79 | return true; |
||
80 | } |
||
81 | } |
||
82 | |||
83 | View Code Duplication | if (isset($actionMap[$name][$action])) { |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
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; |
||
0 ignored issues
–
show
The property
action does not seem to exist in Cake\Network\Request .
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name. If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading. ![]() |
|||
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() { |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
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() { |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
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 |