Source for file rules.php

Documentation is available at rules.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.  * JAccessRules 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('action' => array(-42 => true, 3 => true, 4 => false))
  32.      * or an equivalent JSON encoded string, or an object where properties are arrays.
  33.      *
  34.      * @param   mixed  $input  A JSON format string (probably from the database) or a nested array.
  35.      *
  36.      * @since   11.1
  37.      */
  38.     public function __construct($input '')
  39.     {
  40.         // Convert in input to an array.
  41.         if (is_string($input))
  42.         {
  43.             $input json_decode($inputtrue);
  44.         }
  45.         elseif (is_object($input))
  46.         {
  47.             $input = (array) $input;
  48.         }
  49.  
  50.         if (is_array($input))
  51.         {
  52.             // Top level keys represent the actions.
  53.             foreach ($input as $action => $identities)
  54.             {
  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 JAccessRule objects.
  64.      *
  65.      * @since   11.1
  66.      */
  67.     public function getData()
  68.     {
  69.         return $this->data;
  70.     }
  71.  
  72.     /**
  73.      * Method to merge a collection of JAccessRules.
  74.      *
  75.      * @param   mixed  $input  JAccessRule or array of JAccessRules
  76.      *
  77.      * @return  void 
  78.      *
  79.      * @since   11.1
  80.      */
  81.     public function mergeCollection($input)
  82.     {
  83.         // Check if the input is an array.
  84.         if (is_array($input))
  85.         {
  86.             foreach ($input as $actions)
  87.             {
  88.                 $this->merge($actions);
  89.             }
  90.         }
  91.     }
  92.  
  93.     /**
  94.      * Method to merge actions with this object.
  95.      *
  96.      * @param   mixed  $actions  JAccessRule object, an array of actions or a JSON string array of actions.
  97.      *
  98.      * @return  void 
  99.      *
  100.      * @since   11.1
  101.      */
  102.     public function merge($actions)
  103.     {
  104.         if (is_string($actions))
  105.         {
  106.             $actions json_decode($actionstrue);
  107.         }
  108.  
  109.         if (is_array($actions))
  110.         {
  111.             foreach ($actions as $action => $identities)
  112.             {
  113.                 $this->mergeAction($action$identities);
  114.             }
  115.         }
  116.         elseif ($actions instanceof JAccessRules)
  117.         {
  118.             $data $actions->getData();
  119.  
  120.             foreach ($data as $name => $identities)
  121.             {
  122.                 $this->mergeAction($name$identities);
  123.             }
  124.         }
  125.     }
  126.  
  127.     /**
  128.      * Merges an array of identities for an action.
  129.      *
  130.      * @param   string  $action      The name of the action.
  131.      * @param   array   $identities  An array of identities
  132.      *
  133.      * @return  void 
  134.      *
  135.      * @since   11.1
  136.      */
  137.     public function mergeAction($action$identities)
  138.     {
  139.         if (isset($this->data[$action]))
  140.         {
  141.             // If exists, merge the action.
  142.             $this->data[$action]->mergeIdentities($identities);
  143.         }
  144.         else
  145.         {
  146.             // If new, add the action.
  147.             $this->data[$actionnew JAccessRule($identities);
  148.         }
  149.     }
  150.  
  151.     /**
  152.      * Checks that an action can be performed by an identity.
  153.      *
  154.      * The identity is an integer where +ve represents a user group,
  155.      * and -ve represents a user.
  156.      *
  157.      * @param   string  $action    The name of the action.
  158.      * @param   mixed   $identity  An integer representing the identity, or an array of identities
  159.      *
  160.      * @return  mixed   Object or null if there is no information about the action.
  161.      *
  162.      * @since   11.1
  163.      */
  164.     public function allow($action$identity)
  165.     {
  166.         // Check we have information about this action.
  167.         if (isset($this->data[$action]))
  168.         {
  169.             return $this->data[$action]->allow($identity);
  170.         }
  171.  
  172.         return null;
  173.     }
  174.  
  175.     /**
  176.      * Get the allowed actions for an identity.
  177.      *
  178.      * @param   mixed  $identity  An integer representing the identity or an array of identities
  179.      *
  180.      * @return  JObject  Allowed actions for the identity or identities
  181.      *
  182.      * @since   11.1
  183.      */
  184.     public function getAllowed($identity)
  185.     {
  186.         // Sweep for the allowed actions.
  187.         $allowed new JObject;
  188.  
  189.         foreach ($this->data as $name => &$action)
  190.         {
  191.             if ($action->allow($identity))
  192.             {
  193.                 $allowed->set($nametrue);
  194.             }
  195.         }
  196.         return $allowed;
  197.     }
  198.  
  199.     /**
  200.      * Magic method to convert the object to JSON string representation.
  201.      *
  202.      * @return  string  JSON representation of the actions array
  203.      *
  204.      * @since   11.1
  205.      */
  206.     public function __toString()
  207.     {
  208.         $temp array();
  209.  
  210.         foreach ($this->data as $name => $rule)
  211.         {
  212.             // Convert the action to JSON, then back into an array otherwise
  213.             // re-encoding will quote the JSON for the identities in the action.
  214.             $temp[$namejson_decode((string) $rule);
  215.         }
  216.  
  217.         return json_encode($temp);
  218.     }
  219. }

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