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