Passed
Push — master ( 11e3a6...398838 )
by Fabio
06:26
created

TUserPermissionsBehavior   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 72
rs 10
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setManager() 0 6 2
A dyIsInRole() 0 4 2
A __construct() 0 6 2
A dyDefaultRoles() 0 4 1
A getManager() 0 3 1
A can() 0 8 2
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
 * @package Prado\Security\Permissions
9
 */
10
11
namespace Prado\Security\Permissions;
12
13
use Prado\Prado;
14
use Prado\Util\TBehavior;
15
16
/**
17
 * TUserPermissionsBehavior class.
18
 *
19
 * TUserPermissionsBehavior is designed to attach to {@link TUser}.
20
 * This class adds {@link can} permissions functionality. It also
21
 * handles {@link dyDefaultRoles} and {@link dyIsInRole} of TUser.
22
 *
23
 * This passes through dyDefaultRoles and dyIsInRole to the {@link TPermissionsManager}.
24
 *
25
 * @author Brad Anderson <[email protected]>
26
 * @package Prado\Security\Permissions
27
 * @since 4.2.0
28
 */
29
class TUserPermissionsBehavior extends TBehavior
30
{
31
	/** @var \Prado\Security\Permissions\TPermissionsManager manager object for the behavior */
32
	private $_manager;
33
	
34
	/**
35
	 * @param null|\Prado\Security\Permissions\TPermissionsManager $manager
36
	 */
37
	public function __construct($manager = null)
38
	{
39
		if ($manager) {
40
			$this->setManager($manager);
41
		}
42
		parent::__construct();
43
	}
44
	
45
	/**
46
	 * Gets all the rules for the permission and checks against the TUser.
47
	 * @param string $permission
48
	 * @param null|mixed $extraData
49
	 */
50
	public function can($permission, $extraData = null)
51
	{
52
		$rules = $this->getManager()->getPermissionRules($permission);
53
		if (!$rules) {
54
			return true; //Default from TAuthorizationRuleCollection::isUserAllowed
55
		}
56
		$request = Prado::getApplication()->getRequest();
57
		return $rules->isUserAllowed($this->getOwner(), $request->getRequestType(), $request->getUserHostAddress(), $extraData);
0 ignored issues
show
Bug introduced by
$this->getOwner() of type Prado\TComponent is incompatible with the type Prado\Security\IUser expected by parameter $user of Prado\Security\TAuthoriz...ection::isUserAllowed(). ( Ignorable by Annotation )

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

57
		return $rules->isUserAllowed(/** @scrutinizer ignore-type */ $this->getOwner(), $request->getRequestType(), $request->getUserHostAddress(), $extraData);
Loading history...
58
	}
59
	
60
	/**
61
	 * @param string[] $roles The default roles of all users
62
	 * @param Prado\Util\TCallChain $callchain
0 ignored issues
show
Bug introduced by
The type Prado\Prado\Util\TCallChain was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
63
	 * @return string[] the default roles of all users
64
	 */
65
	public function dyDefaultRoles($roles, $callchain)
66
	{
67
		$roles = array_merge($roles, $this->getManager()->getDefaultRoles() ?? []);
68
		return $callchain->dyDefaultRoles($roles);
69
	}
70
	
71
	/**
72
	 * This handles the dynamic event where the $role does not match the user
73
	 * roles.  It checks the hierarchy of roles/permissions
74
	 * @param bool $return the return value, initially false
75
	 * @param string $role
76
	 * @param Prado\Util\TCallChain $callchain
77
	 */
78
	public function dyIsInRole($return, $role, $callchain)
79
	{
80
		$inRole = $this->getManager()->isInHierarchy($this->getOwner()->getRoles(), $role);
0 ignored issues
show
Bug introduced by
The method getRoles() does not exist on Prado\TComponent. 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

80
		$inRole = $this->getManager()->isInHierarchy($this->getOwner()->/** @scrutinizer ignore-call */ getRoles(), $role);
Loading history...
81
		return $callchain->dyIsInRole($return, $role) || $inRole;
82
	}
83
	
84
	/**
85
	 * @param TPerm\Prado\Security\Permissions\TPermissionsManagerissionsManager $manager manages application permissions
0 ignored issues
show
Bug introduced by
The type Prado\Security\Permissio...nsManagerissionsManager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
86
	 */
87
	public function getManager()
88
	{
89
		return $this->_manager;
90
	}
91
	
92
	/**
93
	 * @param \Prado\Security\Permissions\TPermissionsManager|\WeakReference $manager manages application permissions
94
	 */
95
	public function setManager($manager)
96
	{
97
		if ($manager instanceof \WeakReference) {
98
			$manager = $manager->get();
99
		}
100
		$this->_manager = $manager;
101
	}
102
}
103