Rules.php
4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\Access;
defined('JPATH_PLATFORM') or die;
/**
* Access rules class.
*
* @since 2.5.0
*/
class Rules
{
/**
* A named array.
*
* @var array
* @since 1.7.0
*/
protected $data = array();
/**
* Constructor.
*
* The input array must be in the form: array('action' => array(-42 => true, 3 => true, 4 => false))
* or an equivalent JSON encoded string, or an object where properties are arrays.
*
* @param mixed $input A JSON format string (probably from the database) or a nested array.
*
* @since 1.7.0
*/
public function __construct($input = '')
{
// Convert in input to an array.
if (is_string($input))
{
$input = json_decode($input, true);
}
elseif (is_object($input))
{
$input = (array) $input;
}
if (is_array($input))
{
// Top level keys represent the actions.
foreach ($input as $action => $identities)
{
$this->mergeAction($action, $identities);
}
}
}
/**
* Get the data for the action.
*
* @return array A named array of Rule objects.
*
* @since 1.7.0
*/
public function getData()
{
return $this->data;
}
/**
* Method to merge a collection of Rules.
*
* @param mixed $input Rule or array of Rules
*
* @return void
*
* @since 1.7.0
*/
public function mergeCollection($input)
{
// Check if the input is an array.
if (is_array($input))
{
foreach ($input as $actions)
{
$this->merge($actions);
}
}
}
/**
* Method to merge actions with this object.
*
* @param mixed $actions Rule object, an array of actions or a JSON string array of actions.
*
* @return void
*
* @since 1.7.0
*/
public function merge($actions)
{
if (is_string($actions))
{
$actions = json_decode($actions, true);
}
if (is_array($actions))
{
foreach ($actions as $action => $identities)
{
$this->mergeAction($action, $identities);
}
}
elseif ($actions instanceof Rules)
{
$data = $actions->getData();
foreach ($data as $name => $identities)
{
$this->mergeAction($name, $identities);
}
}
}
/**
* Merges an array of identities for an action.
*
* @param string $action The name of the action.
* @param array $identities An array of identities
*
* @return void
*
* @since 1.7.0
*/
public function mergeAction($action, $identities)
{
if (isset($this->data[$action]))
{
// If exists, merge the action.
$this->data[$action]->mergeIdentities($identities);
}
else
{
// If new, add the action.
$this->data[$action] = new Rule($identities);
}
}
/**
* Checks that an action can be performed by an identity.
*
* The identity is an integer where +ve represents a user group,
* and -ve represents a user.
*
* @param string $action The name of the action.
* @param mixed $identity An integer representing the identity, or an array of identities
*
* @return mixed Object or null if there is no information about the action.
*
* @since 1.7.0
*/
public function allow($action, $identity)
{
// Check we have information about this action.
if (isset($this->data[$action]))
{
return $this->data[$action]->allow($identity);
}
return;
}
/**
* Get the allowed actions for an identity.
*
* @param mixed $identity An integer representing the identity or an array of identities
*
* @return \JObject Allowed actions for the identity or identities
*
* @since 1.7.0
*/
public function getAllowed($identity)
{
// Sweep for the allowed actions.
$allowed = new \JObject;
foreach ($this->data as $name => &$action)
{
if ($action->allow($identity))
{
$allowed->set($name, true);
}
}
return $allowed;
}
/**
* Magic method to convert the object to JSON string representation.
*
* @return string JSON representation of the actions array
*
* @since 1.7.0
*/
public function __toString()
{
$temp = array();
foreach ($this->data as $name => $rule)
{
if ($data = $rule->getData())
{
$temp[$name] = $data;
}
}
return json_encode($temp, JSON_FORCE_OBJECT);
}
}