|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* TPermissionEvent 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\Exceptions\TConfigurationException; |
|
14
|
|
|
use Prado\TPropertyValue; |
|
15
|
|
|
use Prado\Security\TAuthorizationRule; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* TPermissionEvent class |
|
19
|
|
|
* |
|
20
|
|
|
* TPermissionEvent links a permission to dynamic events to check permission. |
|
21
|
|
|
* This class acts as a data container object for the Permission, Description, |
|
22
|
|
|
* and module preset rules. Preset Rules can be turned off from the |
|
23
|
|
|
* TPermissionManager with setting setAutoPresetRules to false. |
|
24
|
|
|
* This class works with {@link \Prado\Security\Permissions\TPermissionsBehavior} |
|
25
|
|
|
* to register the Permission, Description, and Preset Rules and also to |
|
26
|
|
|
* link the dynamic events to check user permission. |
|
27
|
|
|
* |
|
28
|
|
|
* @author Brad Anderson <[email protected]> |
|
29
|
|
|
* @package Prado\Security\Permissions |
|
30
|
|
|
* @since 4.2.0 |
|
31
|
|
|
*/ |
|
32
|
|
|
class TPermissionEvent extends \Prado\TComponent |
|
33
|
|
|
{ |
|
34
|
|
|
/** @var string permission */ |
|
35
|
|
|
private $_name; |
|
36
|
|
|
|
|
37
|
|
|
/** @var string short description of the permission */ |
|
38
|
|
|
private $_description; |
|
39
|
|
|
|
|
40
|
|
|
/** @var string[] dynamic events */ |
|
41
|
|
|
private $_events; |
|
42
|
|
|
|
|
43
|
|
|
/** @var \Prado\Security\TAuthorizationRule[] the preset rules from the module */ |
|
44
|
|
|
private $_rules; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Constructs the class. All permission names are made lower case. |
|
48
|
|
|
* @param string $permissionName the permission name linked to the dynamic events. |
|
49
|
|
|
* @param string $description short description of the permission |
|
50
|
|
|
* @param string|string[] $events the events that the permission is linked. |
|
51
|
|
|
* @param null|Prado\Security\TAuthorizationRule[] $rules default rules from the module. |
|
52
|
|
|
*/ |
|
53
|
|
|
public function __construct($permissionName = '', $description = '', $events = [], $rules = null) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->setName($permissionName); |
|
56
|
|
|
$this->setDescription($description); |
|
57
|
|
|
$this->setEvents($events); |
|
58
|
|
|
$this->setRules($rules); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return string the permission name |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getName() |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->_name; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param string $permissionName the permission name |
|
71
|
|
|
*/ |
|
72
|
|
|
public function setName($permissionName) |
|
73
|
|
|
{ |
|
74
|
|
|
$this->_name = strtolower(TPropertyValue::ensureString($permissionName)); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return string the permission description |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getDescription() |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->_description; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param string $description the permission description |
|
87
|
|
|
*/ |
|
88
|
|
|
public function setDescription($description) |
|
89
|
|
|
{ |
|
90
|
|
|
$this->_description = TPropertyValue::ensureString($description); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return string[] the dynamic events tied to the permission |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getEvents() |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->_events; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* this will take an array of string dynamic events, or a ',' comma separated |
|
103
|
|
|
* list of dynamic events |
|
104
|
|
|
* @param string|string[] $events the dynamic events tied to the permission |
|
105
|
|
|
*/ |
|
106
|
|
|
public function setEvents($events) |
|
107
|
|
|
{ |
|
108
|
|
|
if (is_string($events)) { |
|
109
|
|
|
$events = array_map('trim', explode(',', $events)); |
|
110
|
|
|
} elseif ($events !== null && !is_array($events)) { |
|
|
|
|
|
|
111
|
|
|
throw new TConfigurationException('permission_events_invalid', $events); |
|
112
|
|
|
} |
|
113
|
|
|
$this->_events = array_map('strtolower', array_filter($events ?? [])); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* these are the preset rules for when registering the permission name. |
|
118
|
|
|
* @return \Prado\Security\TAuthorizationRule[] the preset permission rules |
|
119
|
|
|
*/ |
|
120
|
|
|
public function getRules() |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->_rules; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* these are the preset rules for when registering the permission name. |
|
127
|
|
|
* @param null|Prado\Security\TAuthorizationRule|Prado\Security\TAuthorizationRule[] $rules the preset permission rules |
|
|
|
|
|
|
128
|
|
|
*/ |
|
129
|
|
|
public function setRules($rules) |
|
130
|
|
|
{ |
|
131
|
|
|
if ($rules instanceof TAuthorizationRule) { |
|
132
|
|
|
$rules = [$rules]; |
|
133
|
|
|
} |
|
134
|
|
|
if ($rules !== null && !is_array($rules)) { |
|
|
|
|
|
|
135
|
|
|
throw new TConfigurationException('permission_rules_invalid', $rules); |
|
136
|
|
|
} |
|
137
|
|
|
$this->_rules = $rules ?? []; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|