[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Access/ -> Rule.php (source)

   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  // phpcs:disable PSR1.Files.SideEffects
  13  \defined('JPATH_PLATFORM') or die;
  14  // phpcs:enable PSR1.Files.SideEffects
  15  
  16  /**
  17   * Rule class.
  18   *
  19   * @since  2.5.0
  20   */
  21  class Rule
  22  {
  23      /**
  24       * A named array
  25       *
  26       * @var    array
  27       * @since  1.7.0
  28       */
  29      protected $data = array();
  30  
  31      /**
  32       * Constructor.
  33       *
  34       * The input array must be in the form: array(-42 => true, 3 => true, 4 => false)
  35       * or an equivalent JSON encoded string.
  36       *
  37       * @param   mixed  $identities  A JSON format string (probably from the database) or a named array.
  38       *
  39       * @since   1.7.0
  40       */
  41      public function __construct($identities)
  42      {
  43          // Convert string input to an array.
  44          if (\is_string($identities)) {
  45              $identities = json_decode($identities, true);
  46          }
  47  
  48          $this->mergeIdentities($identities);
  49      }
  50  
  51      /**
  52       * Get the data for the action.
  53       *
  54       * @return  array  A named array
  55       *
  56       * @since   1.7.0
  57       */
  58      public function getData()
  59      {
  60          return $this->data;
  61      }
  62  
  63      /**
  64       * Merges the identities
  65       *
  66       * @param   mixed  $identities  An integer or array of integers representing the identities to check.
  67       *
  68       * @return  void
  69       *
  70       * @since   1.7.0
  71       */
  72      public function mergeIdentities($identities)
  73      {
  74          if ($identities instanceof Rule) {
  75              $identities = $identities->getData();
  76          }
  77  
  78          if (\is_array($identities)) {
  79              foreach ($identities as $identity => $allow) {
  80                  $this->mergeIdentity($identity, $allow);
  81              }
  82          }
  83      }
  84  
  85      /**
  86       * Merges the values for an identity.
  87       *
  88       * @param   integer  $identity  The identity.
  89       * @param   boolean  $allow     The value for the identity (true == allow, false == deny).
  90       *
  91       * @return  void
  92       *
  93       * @since   1.7.0
  94       */
  95      public function mergeIdentity($identity, $allow)
  96      {
  97          $identity = (int) $identity;
  98          $allow = (int) ((bool) $allow);
  99  
 100          // Check that the identity exists.
 101          if (isset($this->data[$identity])) {
 102              // Explicit deny always wins a merge.
 103              if ($this->data[$identity] !== 0) {
 104                  $this->data[$identity] = $allow;
 105              }
 106          } else {
 107              $this->data[$identity] = $allow;
 108          }
 109      }
 110  
 111      /**
 112       * Checks that this action can be performed by an identity.
 113       *
 114       * The identity is an integer where +ve represents a user group,
 115       * and -ve represents a user.
 116       *
 117       * @param   mixed  $identities  An integer or array of integers representing the identities to check.
 118       *
 119       * @return  mixed  True if allowed, false for an explicit deny, null for an implicit deny.
 120       *
 121       * @since   1.7.0
 122       */
 123      public function allow($identities)
 124      {
 125          // Implicit deny by default.
 126          $result = null;
 127  
 128          // Check that the inputs are valid.
 129          if (!empty($identities)) {
 130              if (!\is_array($identities)) {
 131                  $identities = array($identities);
 132              }
 133  
 134              foreach ($identities as $identity) {
 135                  // Technically the identity just needs to be unique.
 136                  $identity = (int) $identity;
 137  
 138                  // Check if the identity is known.
 139                  if (isset($this->data[$identity])) {
 140                      $result = (bool) $this->data[$identity];
 141  
 142                      // An explicit deny wins.
 143                      if ($result === false) {
 144                          break;
 145                      }
 146                  }
 147              }
 148          }
 149  
 150          return $result;
 151      }
 152  
 153      /**
 154       * Convert this object into a JSON encoded string.
 155       *
 156       * @return  string  JSON encoded string
 157       *
 158       * @since   1.7.0
 159       */
 160      public function __toString()
 161      {
 162          return json_encode($this->data);
 163      }
 164  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer