Passed
Push — php8 ( bd75dc...d8afb4 )
by Fabio
08:15
created

TUserPermissionsBehavior::setPermissionsManager()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 2
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
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
	/** @var \Prado\Security\Permissions\TPermissionsManager manager object for the behavior */
31
	private $_manager;
32
33
	/**
34
	 * @param null|\Prado\Security\Permissions\TPermissionsManager $manager
35
	 */
36
	public function __construct($manager = null)
37
	{
38
		if ($manager) {
39
			$this->setPermissionsManager($manager);
40
		}
41
		parent::__construct();
42
	}
43
44
	/**
45
	 * Gets all the rules for the permission and checks against the TUser.
46
	 * @param string $permission
47
	 * @param null|mixed $extraData
48
	 */
49
	public function can($permission, $extraData = null)
50
	{
51
		$rules = $this->getPermissionsManager()->getPermissionRules($permission);
52
		if (!$rules) {
53
			return true; //Default from TAuthorizationRuleCollection::isUserAllowed
54
		}
55
		$request = Prado::getApplication()->getRequest();
56
		return $rules->isUserAllowed($this->getOwner(), $request->getRequestType(), $request->getUserHostAddress(), $extraData);
57
	}
58
59
	/**
60
	 * @param string[] $roles The default roles of all users
61
	 * @param \Prado\Util\TCallChain $callchain
62
	 * @return string[] the default roles of all users
63
	 */
64
	public function dyDefaultRoles($roles, $callchain)
65
	{
66
		$roles = array_merge($roles, $this->getPermissionsManager()->getDefaultRoles() ?? []);
67
		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

67
		return $callchain->/** @scrutinizer ignore-call */ dyDefaultRoles($roles);
Loading history...
68
	}
69
70
	/**
71
	 * This handles the dynamic event where the $role does not match the user
72
	 * roles.  It checks the hierarchy of roles/permissions
73
	 * @param bool $return the return value, initially false
74
	 * @param string $role
75
	 * @param \Prado\Util\TCallChain $callchain
76
	 */
77
	public function dyIsInRole($return, $role, $callchain)
78
	{
79
		$inRole = $this->getPermissionsManager()->isInHierarchy($this->getOwner()->getRoles(), $role);
80
		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

80
		return $callchain->/** @scrutinizer ignore-call */ dyIsInRole($return, $role) || $inRole;
Loading history...
81
	}
82
83
	/**
84
	 * @return \Prado\Security\Permissions\TPermissionsManager application permissions manager
85
	 */
86
	public function getPermissionsManager()
87
	{
88
		return $this->_manager;
89
	}
90
91
	/**
92
	 * @param \Prado\Security\Permissions\TPermissionsManager|\WeakReference $manager manages application permissions
93
	 */
94
	public function setPermissionsManager($manager)
95
	{
96
		if (class_exists('\WeakReference', false) && $manager instanceof \WeakReference) {
97
			$manager = $manager->get();
98
		}
99
		$this->_manager = $manager;
0 ignored issues
show
Documentation Bug introduced by
It seems like $manager can also be of type WeakReference. However, the property $_manager is declared as type Prado\Security\Permissions\TPermissionsManager. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
100
	}
101
}
102