This repository has been archived by the owner on Jan 15, 2021. It is now read-only.
/
SimpleRbacAuthorize.php
165 lines (143 loc) · 4.18 KB
/
SimpleRbacAuthorize.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
<?php
/**
* Copyright 2011 - 2014, Florian Krämer
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2011 - 2014, Florian Krämer
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::uses('BaseAuthorize', 'Controller/Component/Auth');
/**
* Very simple rbac authorize
*
* @package BzUtils
* @subpackage BzUtils.Controller.Component.Auth
*/
class SimpleRbacAuthorize extends BaseAuthorize {
/**
* Settings for authorize objects.
*
* - `userModel` - Model name that user records can be found under. Defaults to 'User'.
* - `roleField` - The name of the role field in the user data array that is passed to authorize()
*
* @var array
*/
public $settings = array(
'userModel' => 'User',
'roleField' => 'role',
'allowEmptyActionMap' => false,
'allowEmptyPrefixMap' => true,
);
/**
* Authorize a user based on his roles
*
* @param array $user The user to authorize
* @param CakeRequest $request The request needing authorization.
* @return boolean
* @throws RuntimeException when the role field does not exist
*/
public function authorize($user, CakeRequest $request) {
$roleField = $this->settings['roleField'];
if (!isset($user[$roleField])) {
throw new RuntimeException(__d('bz_utils', 'The role field %s does not exist!'));
}
if (is_string($user[$roleField])) {
$user[$roleField] = array($user[$roleField]);
}
if ($this->authorizeByPrefix($user[$roleField], $request)) {
return true;
}
if ($this->authorizeByControllerAndAction($user, $request)) {
return true;
}
return false;
}
/**
* Checks if a role is granted access to a controller and action
*
* @param array $user
* @param CakeRequest $request
* @return boolean
*/
public function authorizeByControllerAndAction($user, CakeRequest $request) {
$roleField = $this->settings['roleField'];
extract($this->getConrollerNameAndAction($request));
$actionMap = $this->getActionMap();
if (isset($actionMap[$name])) {
if (in_array('*', $actionMap[$name])) {
return true;
}
}
if (isset($actionMap[$name][$action])) {
if (in_array('*', $actionMap[$name][$action])) {
return true;
}
foreach ($user[$roleField] as $role) {
if (in_array($role, $actionMap[$name][$action])) {
return true;
}
}
}
return false;
}
/**
* Checks if a role is granted access to a prefix route like /admin
*
* @param array $roles
* @param CakeRequest $request
* @return boolean
*/
public function authorizeByPrefix($roles, CakeRequest $request) {
$prefixeMap = $this->getPrefixMap();
if (isset($request->params['prefix']) && isset($prefixeMap[$request->params['prefix']])) {
foreach ($roles as $role) {
if (in_array($role, $prefixeMap[$request->params['prefix']])) {
return true;
}
}
}
return false;
}
/**
* Gets the controller and action, prefixes the controller with the plugin if there is one
*
* @param CakeRequest $request
* @return array
*/
public function getConrollerNameAndAction(CakeRequest $request) {
$name = $this->_Controller->name;
$action = $this->_Controller->action;
if (!empty($request->params['plugin'])) {
$name = Inflector::camelize($request->params['plugin']) . '.' . $name;
}
return compact('name', 'action');
}
/**
* Can be overriden if inherited with a method to fetch this from anywhere, a database for exaple
*
* @return array
* @throws RuntimeException
*/
public function getActionMap() {
$actionMap = (array) Configure::read('SimpleRbac.actionMap');
if (empty($actionMap) && $this->settings['allowEmptyActionMap'] === false) {
throw new RuntimeException(__d('bz_utils', 'SimpleRbac.actionMap configuration is empty!'));
}
return $actionMap;
}
/**
* Can be overriden if inherited with a method to fetch this from anywhere, a database for exaple
*
* @return array
* @throws RuntimeException
*/
public function getPrefixMap() {
$prefixMap = (array) Configure::read('SimpleRbac.prefixMap');
if (empty($prefixMap) && $this->settings['allowEmptyPrefixMap'] === false) {
throw new RuntimeException(__d('bz_utils', 'SimpleRbac.prefixMap configuration is empty!'));
}
return $prefixMap;
}
}