Passed
Push — master ( 5fd064...896ccb )
by Fabio
04:59
created

TUserOwnerRule::isUserAllowed()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 4
c 1
b 0
f 0
nc 3
nop 4
dl 0
loc 6
rs 10
1
<?php
2
/**
3
 * TUserOwnerRule class file
4
 *
5
 * @author Brad Anderson <[email protected]>
6
 * @link https://github.com/pradosoft/prado
7
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
8
 * @package Prado\Security\Permissions
9
 */
10
11
namespace Prado\Security\Permissions;
12
13
use Prado\Exceptions\TInvalidDataValueException;
14
use Prado\Security\IUser;
15
use Prado\Security\TAuthorizationRule;
16
17
/**
18
 * TUserOwnerRule class
19
 *
20
 * TUserOwnerRule will check if the extra data sent to isUserAllowed
21
 * has a user name that matches the parameter user name.
22
 *
23
 * @author Brad Anderson <[email protected]>
24
 * @package Prado\Security\Permissions
25
 * @since 4.2.0
26
 */
27
class TUserOwnerRule extends TAuthorizationRule
28
{
29
	/**
30
	 * @param \Prado\Security\IUser $user the user object
31
	 * @param string $verb the request verb (GET, PUT)
32
	 * @param string $ip the request IP address
33
	 * @param null|array $extra extra data username to validate
34
	 * @return int 1 if the user is allowed, -1 if the user is denied, 0 if the rule does not apply to the user
35
	 */
36
	public function isUserAllowed(IUser $user, $verb, $ip, $extra = null)
37
	{
38
		if (parent::isUserAllowed($user, $verb, $ip, $extra) !== 0 && strcasecmp($user->getName(), $extra['username'] ?? '') === 0) {
39
			return ($this->getAction() === 'allow') ? 1 : -1;
40
		} else {
41
			return 0;
42
		}
43
	}
44
}
45