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