Completed
Branch master (b375cb)
by
unknown
05:49
created

Auth::_getAuth()   A

↳ Parent: Auth

Complexity

Conditions 1
Paths 1

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 5
Code Lines 3

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
namespace Redaxscript;
3
4
/**
5
 * parent class to authenticate the user
6
 *
7
 * @since 3.0.0
8
 *
9
 * @package Redaxscript
10
 * @category Auth
11
 * @author Henry Ruhs
12
 *
13
 * @method getPermissionNew(string $type)
14
 * @method getPermissionInstall(string $type)
15
 * @method getPermissionEdit(string $type)
16
 * @method getPermissionDelete(string $type)
17
 * @method getPermissionUninstall(string $type)
18
 * @method getFilter()
19
 */
20
21
class Auth
22
{
23
	/**
24
	 * instance of the request class
25
	 *
26
	 * @var object
27
	 */
28
29
	protected $_request;
30
31
	/**
32
	 * array of the user
33
	 *
34
	 * @var array
35
	 */
36
37
	protected $_userArray = [];
38
39
	/**
40
	 * array of the permission
41
	 *
42
	 * @var array
43
	 */
44
45
	protected $_permissionArray = [];
46
47
	/**
48
	 * array of the type
49
	 *
50
	 * @var array
51
	 */
52
53
	protected $_typeArray =
54
	[
55
		'categories',
56
		'articles',
57
		'extras',
58
		'comments',
59
		'groups',
60
		'users',
61
		'modules',
62
		'settings',
63
		'filter'
64
	];
65
66
	/**
67
	 * array of the call
68
	 *
69
	 * @var array
70
	 */
71
72
	protected $_callArray =
73
	[
74
		'categories' =>
75
		[
76
			'getPermissionNew' => 1,
77
			'getPermissionEdit' => 2,
78
			'getPermissionDelete' => 3
79
		],
80
		'articles' =>
81
		[
82
			'getPermissionNew' => 1,
83
			'getPermissionEdit' => 2,
84
			'getPermissionDelete' => 3
85
		],
86
		'extras' =>
87
		[
88
			'getPermissionNew' => 1,
89
			'getPermissionEdit' => 2,
90
			'getPermissionDelete' => 3
91
		],
92
		'comments' =>
93
		[
94
			'getPermissionNew' => 1,
95
			'getPermissionEdit' => 2,
96
			'getPermissionDelete' => 3
97
		],
98
		'groups' =>
99
		[
100
			'getPermissionNew' => 1,
101
			'getPermissionEdit' => 2,
102
			'getPermissionDelete' => 3
103
		],
104
		'users' =>
105
		[
106
			'getPermissionNew' => 1,
107
			'getPermissionEdit' => 2,
108
			'getPermissionDelete' => 3
109
		],
110
		'modules' =>
111
		[
112
			'getPermissionInstall' => 1,
113
			'getPermissionEdit' => 2,
114
			'getPermissionUninstall' => 3
115
		],
116
		'settings' =>
117
		[
118
			'getPermissionEdit' => 1
119
		],
120
		'filter' =>
121
		[
122
			'getFilter' => 0
123
		]
124
	];
125
126
	/**
127
	 * constructor of the class
128
	 *
129
	 * @since 3.0.0
130
	 *
131
	 * @param Request $request instance of the request class
132
	 */
133
134 31
	public function __construct(Request $request)
135
	{
136 31
		$this->_request = $request;
137 31
	}
138
139
	/**
140
	 * call method as needed
141
	 *
142
	 * @since 3.0.0
143
	 *
144
	 * @param string $method name of the method
145
	 * @param array $argumentArray arguments of the method
146
	 *
147
	 * @return mixed
148
	 */
149
150 24
	public function __call($method = null, $argumentArray = [])
151
	{
152 24
		$type = $argumentArray[0];
153 24
		if (array_key_exists($method, $this->_callArray[$type]))
154 24
		{
155 20
			return in_array($this->_callArray[$type][$method], $this->getPermission($type));
156
		}
157 24
		if ($method === 'getFilter')
158 24
		{
159 4
			return !in_array($this->_callArray['filter'][$method], $this->getPermission('filter'));
160
		}
161 20
	}
162
163
	/**
164
	 * init the class
165
	 *
166
	 * @since 3.0.0
167
	 */
168
169 3
	public function init()
170
	{
171 3
		$authArray = $this->_getAuth();
172 3
		if (array_key_exists('user', $authArray))
173 3
		{
174 2
			$this->_userArray = $authArray['user'];
175 2
		}
176 3
		if (array_key_exists('permission', $authArray))
177 3
		{
178 2
			$this->_permissionArray = $authArray['permission'];
179 2
		}
180 3
	}
181
182
	/**
183
	 * login the user
184
	 *
185
	 * @since 3.0.0
186
	 *
187
	 * @param integer $userId identifier of the user
188
	 *
189
	 * @return boolean
190
	 */
191
192 30
	public function login($userId = null)
193
	{
194 30
		$user = Db::forTablePrefix('users')->whereIdIs($userId)->where('status', 1)->findOne();
195
		if ($user)
196 30
		{
197 30
			$groupArray = array_map('intval', explode(',', $user->groups));
198 30
			$group = Db::forTablePrefix('groups')->whereIdIn($groupArray)->where('status', 1)->select($this->_typeArray)->findArray();
0 ignored issues
show
Bug introduced by redaxmedia
The method where cannot be called on \Redaxscript\Db::forTabl...>whereIdIn($groupArray) (of type array|string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
199
200
			/* set filter */
201
202 30
			$this->setPermission('filter',
203
			[
0 ignored issues
show
Documentation introduced by redaxmedia
array(1) is of type array<integer,integer,{"...nteger,{"0":"integer"}>, but the function expects a integer|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
204
				1
205 30
			]);
206
207
			/* process groups */
208
209 30
			foreach ($group as $key => $value)
210
			{
211 24
				foreach ($value as $keySub => $valueSub)
212
				{
213 24
					$valueArray = array_map('intval', explode(',', $valueSub));
214 24
					$this->setPermission($keySub, $valueArray);
0 ignored issues
show
Documentation introduced by redaxmedia
$valueArray is of type array, but the function expects a integer|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
215 24
				}
216 30
			}
217
218
			/* set user */
219
220 30
			$this->setUser('id', $user->id);
221 30
			$this->setUser('name', $user->name);
222 30
			$this->setUser('user', $user->user);
223 30
			$this->setUser('email', $user->email);
224 30
			$this->setUser('language', $user->language);
225 30
			$this->setUser('groups', $user->groups);
226
227
			/* save user and permission */
228
229 30
			$this->save();
230 30
		}
231 30
		return $this->getStatus();
232
	}
233
234
	/**
235
	 * logout the user
236
	 *
237
	 * @since 3.0.0
238
	 *
239
	 * @return boolean
240
	 */
241
242 3
	public function logout()
243
	{
244 3
		if ($this->getStatus())
245 3
		{
246 3
			$this->_setAuth(null);
247 3
			return !$this->getStatus();
248
		}
249 2
		return false;
250
	}
251
252
	/**
253
	 * get the user
254
	 *
255
	 * @since 3.0.0
256
	 *
257
	 * @param string $key key of the user
258
	 *
259
	 * @return mixed
260
	 */
261
262 31
	public function getUser($key = null)
263
	{
264 31
		if (array_key_exists($key, $this->_userArray))
265 31
		{
266 1
			return $this->_userArray[$key];
267
		}
268 30
		else if (!$key)
0 ignored issues
show
Bug Best Practice introduced by redaxmedia
The expression $key of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
269 30
		{
270 30
			return $this->_userArray;
271
		}
272 1
		return false;
273
	}
274
275
	/**
276
	 * set the user
277
	 *
278
	 * @since 3.0.0
279
	 *
280
	 * @param string $key key of the user
281
	 * @param string $value value of the user
282
	 */
283
284 30
	public function setUser($key = null, $value = null)
285
	{
286 30
		$this->_userArray[$key] = $value;
287 30
	}
288
289
	/**
290
	 * get the permission
291
	 *
292
	 * @since 3.0.0
293
	 *
294
	 * @param string $key key of the permission
295
	 *
296
	 * @return mixed
297
	 */
298
299 31
	public function getPermission($key = null)
300
	{
301 31
		if (array_key_exists($key, $this->_permissionArray))
302 31
		{
303 20
			return $this->_permissionArray[$key];
304
		}
305 30
		else if (!$key)
0 ignored issues
show
Bug Best Practice introduced by redaxmedia
The expression $key of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
306 30
		{
307 30
			return $this->_permissionArray;
308
		}
309 5
		return false;
310
	}
311
312
	/**
313
	 * set the permission
314
	 *
315
	 * @since 3.0.0
316
	 *
317
	 * @param string $key key of the permission
318
	 * @param integer $value value of the permission
319
	 */
320
321 30
	public function setPermission($key = null, $value = null)
322
	{
323 30
		if (is_array($this->_permissionArray[$key]))
324 30
		{
325 24
			$value = array_merge($this->_permissionArray[$key], $value);
326 24
		}
327 30
		$this->_permissionArray[$key] = $value;
328 30
	}
329
330
	/**
331
	 * get the auth status
332
	 *
333
	 * @since 3.0.0
334
	 *
335
	 * @return boolean
336
	 */
337
338 30
	public function getStatus()
339
	{
340 30
		$authArray = $this->_getAuth();
341 30
		return array_key_exists('user', $authArray) && array_key_exists('permission', $authArray);
342
	}
343
344
	/**
345
	 * save user and permission
346
	 *
347
	 * @since 3.0.0
348
	 */
349
350 30
	public function save()
351
	{
352 30
		$userArray = $this->getUser();
353 30
		$permissionArray = $this->getPermission();
354
355
		/* set to session */
356
357 30
		if ($userArray && $permissionArray)
358 30
		{
359 30
			$this->_setAuth(
360
			[
361 30
				'user' => $userArray,
362
				'permission' => $permissionArray
363 30
			]);
364 30
			if ($userArray['language'])
365 30
			{
366 30
				$this->_request->setSession('language', $userArray['language']);
367 30
			}
368 30
		}
369 30
	}
370
371
	/**
372
	 * get auth from session
373
	 *
374
	 * @since 3.0.0
375
	 *
376
	 * @return mixed
377
	 */
378
379 31
	protected function _getAuth()
380
	{
381 31
		$root = new Server\Root($this->_request);
382 31
		return $this->_request->getSession($root->getOutput() . '/auth');
383
	}
384
385
	/**
386
	 * set auth to session
387
	 *
388
	 * @since 3.0.0
389
	 *
390
	 * @param mixed $value
391
	 */
392
393 30
	protected function _setAuth($value = null)
394
	{
395 30
		$root = new Server\Root($this->_request);
396 30
		return $this->_request->setSession($root->getOutput() . '/auth', $value);
397
	}
398
}
399