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