|
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); |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param string[] $roles The default roles of all users |
|
62
|
|
|
* @param Prado\Util\TCallChain $callchain |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
81
|
|
|
return $callchain->dyIsInRole($return, $role) || $inRole; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param TPerm\Prado\Security\Permissions\TPermissionsManagerissionsManager $manager manages application permissions |
|
|
|
|
|
|
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
|
|
|
|