Source for file rule.php

Documentation is available at rule.php

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

Documentation generated on Tue, 19 Nov 2013 15:12:31 +0100 by phpDocumentor 1.4.3