Passed
Push — master ( f5ec54...94ecb9 )
by Fabio
05:41
created

TUserPermissionsBehavior::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 6
rs 10
1
<?php
2
/**
3
 * TUserPermissionsBehavior 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
 */
9
10
namespace Prado\Security\Permissions;
11
12
use Prado\Prado;
13
use Prado\Util\TBehavior;
14
15
/**
16
 * TUserPermissionsBehavior class.
17
 *
18
 * TUserPermissionsBehavior is designed to attach to {@link TUser}.
19
 * This class adds {@link can} permissions functionality. It also
20
 * handles {@link dyDefaultRoles} and {@link dyIsInRole} of TUser.
21
 *
22
 * This passes through dyDefaultRoles and dyIsInRole to the {@link TPermissionsManager}.
23
 *
24
 * @author Brad Anderson <[email protected]>
25
 * @since 4.2.0
26
 * @method \Prado\Security\TUser getOwner()
27
 */
28
class TUserPermissionsBehavior extends TBehavior
29
{
30
	use TPermissionsManagerPropertyTrait;
31
32
	/**
33
	 * Gets all the rules for the permission and checks against the TUser.
34
	 * @param string $permission
35
	 * @param null|mixed $extraData
36
	 */
37
	public function can($permission, $extraData = null)
38
	{
39
		$rules = $this->getPermissionsManager()->getPermissionRules($permission);
40
		if (!$rules) {
41
			return true; //Default from TAuthorizationRuleCollection::isUserAllowed
42
		}
43
		$request = Prado::getApplication()->getRequest();
44
		return $rules->isUserAllowed($this->getOwner(), $request->getRequestType(), $request->getUserHostAddress(), $extraData);
45
	}
46
47
	/**
48
	 * @param string[] $roles The default roles of all users
49
	 * @param \Prado\Util\TCallChain $callchain
50
	 * @return string[] the default roles of all users
51
	 */
52
	public function dyDefaultRoles($roles, $callchain)
53
	{
54
		$roles = array_merge($roles, $this->getPermissionsManager()->getDefaultRoles() ?? []);
55
		return $callchain->dyDefaultRoles($roles);
0 ignored issues
show
Bug introduced by
The method dyDefaultRoles() does not exist on Prado\Util\TCallChain. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
		return $callchain->/** @scrutinizer ignore-call */ dyDefaultRoles($roles);
Loading history...
56
	}
57
58
	/**
59
	 * This handles the dynamic event where the $role does not match the user
60
	 * roles.  It checks the hierarchy of roles/permissions
61
	 * @param bool $return the return value, initially false
62
	 * @param string $role
63
	 * @param \Prado\Util\TCallChain $callchain
64
	 */
65
	public function dyIsInRole($return, $role, $callchain)
66
	{
67
		$inRole = $this->getPermissionsManager()->isInHierarchy($this->getOwner()->getRoles(), $role);
68
		return $callchain->dyIsInRole($return, $role) || $inRole;
0 ignored issues
show
Bug introduced by
The method dyIsInRole() does not exist on Prado\Util\TCallChain. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

68
		return $callchain->/** @scrutinizer ignore-call */ dyIsInRole($return, $role) || $inRole;
Loading history...
69
	}
70
}
71