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); |
|
|
|
|
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; |
|
|
|
|
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; |
|
|
|
|
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|