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

TUserOwnerRule   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 15
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 15
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A isUserAllowed() 0 6 4
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