Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
rule.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  * JAccessRule 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(-42 => true, 3 => true, 4 => false)
33  * or an equivalent JSON encoded string.
34  *
35  * @param mixed $identities A JSON format string (probably from the database) or a named array.
36  *
37  * @since 11.1
38  */
39  public function __construct($identities)
40  {
41  // Convert string input to an array.
42  if (is_string($identities))
43  {
44  $identities = json_decode($identities, true);
45  }
46 
47  $this->mergeIdentities($identities);
48  }
49 
50  /**
51  * Get the data for the action.
52  *
53  * @return array A named array
54  *
55  * @since 11.1
56  */
57  public function getData()
58  {
59  return $this->data;
60  }
61 
62  /**
63  * Merges the identities
64  *
65  * @param mixed $identities An integer or array of integers representing the identities to check.
66  *
67  * @return void
68  *
69  * @since 11.1
70  */
71  public function mergeIdentities($identities)
72  {
73  if ($identities instanceof JAccessRule)
74  {
75  $identities = $identities->getData();
76  }
77 
78  if (is_array($identities))
79  {
80  foreach ($identities as $identity => $allow)
81  {
82  $this->mergeIdentity($identity, $allow);
83  }
84  }
85  }
86 
87  /**
88  * Merges the values for an identity.
89  *
90  * @param integer $identity The identity.
91  * @param boolean $allow The value for the identity (true == allow, false == deny).
92  *
93  * @return void
94  *
95  * @since 11.1
96  */
97  public function mergeIdentity($identity, $allow)
98  {
99  $identity = (int) $identity;
100  $allow = (int) ((boolean) $allow);
101 
102  // Check that the identity exists.
103  if (isset($this->data[$identity]))
104  {
105  // Explicit deny always wins a merge.
106  if ($this->data[$identity] !== 0)
107  {
108  $this->data[$identity] = $allow;
109  }
110  }
111  else
112  {
113  $this->data[$identity] = $allow;
114  }
115  }
116 
117  /**
118  * Checks that this action can be performed by an identity.
119  *
120  * The identity is an integer where +ve represents a user group,
121  * and -ve represents a user.
122  *
123  * @param mixed $identities An integer or array of integers representing the identities to check.
124  *
125  * @return mixed True if allowed, false for an explicit deny, null for an implicit deny.
126  *
127  * @since 11.1
128  */
129  public function allow($identities)
130  {
131  // Implicit deny by default.
132  $result = null;
133 
134  // Check that the inputs are valid.
135  if (!empty($identities))
136  {
137  if (!is_array($identities))
138  {
139  $identities = array($identities);
140  }
141 
142  foreach ($identities as $identity)
143  {
144  // Technically the identity just needs to be unique.
145  $identity = (int) $identity;
146 
147  // Check if the identity is known.
148  if (isset($this->data[$identity]))
149  {
150  $result = (boolean) $this->data[$identity];
151 
152  // An explicit deny wins.
153  if ($result === false)
154  {
155  break;
156  }
157  }
158 
159  }
160  }
161 
162  return $result;
163  }
164 
165  /**
166  * Convert this object into a JSON encoded string.
167  *
168  * @return string JSON encoded string
169  *
170  * @since 11.1
171  */
172  public function __toString()
173  {
174  return json_encode($this->data);
175  }
176 }