SimpleRbacAuthorize   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 163
Duplicated Lines 14.72 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 29
lcom 1
cbo 6
dl 24
loc 163
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A authorize() 0 21 5
B authorizeByControllerAndAction() 10 23 6
A _isAllowedRole() 0 14 5
A authorizeByPrefix() 0 11 5
A getControllerNameAndAction() 0 10 2
A getActionMap() 7 7 3
A getPrefixMap() 7 7 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
$this->getControllerNameAndAction($request) cannot be passed to extract() as the parameter $var_array expects a reference.
Loading history...
75
		$actionMap = $this->getActionMap();
76
77 View Code Duplication
		if (isset($actionMap[$name]['*'])) {
0 ignored issues
show
Duplication introduced by
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.

Loading history...
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
Duplication introduced by
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.

Loading history...
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
Bug introduced by
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.

Loading history...
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
Duplication introduced by
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.

Loading history...
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
Duplication introduced by
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.

Loading history...
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