1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* TPermissionsConfigurationBehavior 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\Util\TBehavior; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* TPermissionsConfigurationBehavior class. |
16
|
|
|
* |
17
|
|
|
* TPermissionsConfigurationBehavior is designed specifically to attach to the |
18
|
|
|
* {@link TPageConfiguration} class objects. It reads and parses the |
19
|
|
|
* permissions role hierarchy and permissions rules from a page configuration |
20
|
|
|
* file. Within the config.xml for a page, for example, add the following: |
21
|
|
|
* <code> |
22
|
|
|
* <permissions> |
23
|
|
|
* <role name="pageRole" children="otherRole, permission_name" /> |
24
|
|
|
* <permissionrule name="permission_name" action="allow" roles="manager"/> |
25
|
|
|
* </permissions> |
26
|
|
|
* </code> |
27
|
|
|
* |
28
|
|
|
* See <@link TPermissionsManager> for information on php configurations. |
29
|
|
|
* |
30
|
|
|
* @author Brad Anderson <[email protected]> |
31
|
|
|
* @since 4.2.0 |
32
|
|
|
*/ |
33
|
|
|
class TPermissionsConfigurationBehavior extends TBehavior |
34
|
|
|
{ |
35
|
|
|
/** @var \Prado\Security\Permissions\TPermissionsManager manager object for the behavior */ |
36
|
|
|
private $_manager; |
37
|
|
|
|
38
|
|
|
/** @var array|\Prado\Xml\TXmlElement permissions data to parse */ |
39
|
|
|
private $_permissions = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param null|\Prado\Security\Permissions\TPermissionsManager $manager |
43
|
|
|
*/ |
44
|
|
|
public function __construct($manager = null) |
45
|
|
|
{ |
46
|
|
|
if ($manager) { |
47
|
|
|
$this->setPermissionsManager($manager); |
48
|
|
|
} |
49
|
|
|
parent::__construct(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Loads the configuration specific for page service. This may be called multiple |
54
|
|
|
* times. |
55
|
|
|
* @param array $config config array |
56
|
|
|
* @param string $configPath base path corresponding to this php element |
57
|
|
|
* @param string $configPagePath the page path that the config php is associated with. The page path doesn't include the page name. |
58
|
|
|
* @param \Prado\Util\TCallChain $callchain |
59
|
|
|
*/ |
60
|
|
|
public function dyLoadPageConfigurationFromPhp($config, $configPath, $configPagePath, $callchain) |
61
|
|
|
{ |
62
|
|
|
// authorization |
63
|
|
|
if (isset($config['permissions']) && is_array($config['permissions'])) { |
64
|
|
|
$this->_permissions[] = $config['permissions']; |
65
|
|
|
} |
66
|
|
|
return $callchain->dyLoadPageConfigurationFromPhp($config, $configPath, $configPagePath); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Loads the configuration specific for page service. This may be called multiple |
71
|
|
|
* times. |
72
|
|
|
* @param \Prado\Xml\TXmlElement $dom config xml element |
73
|
|
|
* @param string $configPath base path corresponding to this xml element |
74
|
|
|
* @param string $configPagePath the page path that the config XML is associated with. The page path doesn't include the page name. |
75
|
|
|
* @param \Prado\Util\TCallChain $callchain |
76
|
|
|
*/ |
77
|
|
|
public function dyLoadPageConfigurationFromXml($dom, $configPath, $configPagePath, $callchain) |
78
|
|
|
{ |
79
|
|
|
// authorization |
80
|
|
|
if (($permissionsNode = $dom->getElementByTagName('permissions')) !== null) { |
|
|
|
|
81
|
|
|
$this->_permissions[] = $permissionsNode; |
82
|
|
|
} |
83
|
|
|
return $callchain->dyLoadPageConfigurationFromXml($dom, $configPath, $configPagePath); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Applies the permissions hierarchy and permission rules |
88
|
|
|
* @param \Prado\Util\TCallChain $callchain |
89
|
|
|
*/ |
90
|
|
|
public function dyApplyConfiguration($callchain) |
91
|
|
|
{ |
92
|
|
|
$manager = $this->getPermissionsManager(); |
93
|
|
|
foreach ($this->_permissions as $permission) { |
94
|
|
|
$manager->loadPermissionsData($permission); |
95
|
|
|
} |
96
|
|
|
return $callchain->dyApplyConfiguration(); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return \Prado\Security\Permissions\TPermissionsManager manages application permissions |
101
|
|
|
*/ |
102
|
|
|
public function getPermissionsManager() |
103
|
|
|
{ |
104
|
|
|
return $this->_manager; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param \Prado\Security\Permissions\TPermissionsManager|\WeakReference $manager manages application permissions |
109
|
|
|
*/ |
110
|
|
|
public function setPermissionsManager($manager) |
111
|
|
|
{ |
112
|
|
|
if (class_exists('\WeakReference', false) && $manager instanceof \WeakReference) { |
113
|
|
|
$manager = $manager->get(); |
114
|
|
|
} |
115
|
|
|
$this->_manager = $manager; |
|
|
|
|
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|