File indexing completed on 2024-06-02 06:09:17

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Acl
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 
0023 /**
0024  * @see Zend_Acl_Role_Interface
0025  */
0026 // require_once 'Zend/Acl/Role/Interface.php';
0027 
0028 
0029 /**
0030  * @category   Zend
0031  * @package    Zend_Acl
0032  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0033  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0034  */
0035 class Zend_Acl_Role_Registry
0036 {
0037     /**
0038      * Internal Role registry data storage
0039      *
0040      * @var array
0041      */
0042     protected $_roles = array();
0043 
0044     /**
0045      * Adds a Role having an identifier unique to the registry
0046      *
0047      * The $parents parameter may be a reference to, or the string identifier for,
0048      * a Role existing in the registry, or $parents may be passed as an array of
0049      * these - mixing string identifiers and objects is ok - to indicate the Roles
0050      * from which the newly added Role will directly inherit.
0051      *
0052      * In order to resolve potential ambiguities with conflicting rules inherited
0053      * from different parents, the most recently added parent takes precedence over
0054      * parents that were previously added. In other words, the first parent added
0055      * will have the least priority, and the last parent added will have the
0056      * highest priority.
0057      *
0058      * @param  Zend_Acl_Role_Interface              $role
0059      * @param  Zend_Acl_Role_Interface|string|array $parents
0060      * @throws Zend_Acl_Role_Registry_Exception
0061      * @return Zend_Acl_Role_Registry Provides a fluent interface
0062      */
0063     public function add(Zend_Acl_Role_Interface $role, $parents = null)
0064     {
0065         $roleId = $role->getRoleId();
0066 
0067         if ($this->has($roleId)) {
0068             /**
0069              * @see Zend_Acl_Role_Registry_Exception
0070              */
0071             // require_once 'Zend/Acl/Role/Registry/Exception.php';
0072             throw new Zend_Acl_Role_Registry_Exception("Role id '$roleId' already exists in the registry");
0073         }
0074 
0075         $roleParents = array();
0076 
0077         if (null !== $parents) {
0078             if (!is_array($parents)) {
0079                 $parents = array($parents);
0080             }
0081             /**
0082              * @see Zend_Acl_Role_Registry_Exception
0083              */
0084             // require_once 'Zend/Acl/Role/Registry/Exception.php';
0085             foreach ($parents as $parent) {
0086                 try {
0087                     if ($parent instanceof Zend_Acl_Role_Interface) {
0088                         $roleParentId = $parent->getRoleId();
0089                     } else {
0090                         $roleParentId = $parent;
0091                     }
0092                     $roleParent = $this->get($roleParentId);
0093                 } catch (Zend_Acl_Role_Registry_Exception $e) {
0094                     throw new Zend_Acl_Role_Registry_Exception("Parent Role id '$roleParentId' does not exist", 0, $e);
0095                 }
0096                 $roleParents[$roleParentId] = $roleParent;
0097                 $this->_roles[$roleParentId]['children'][$roleId] = $role;
0098             }
0099         }
0100 
0101         $this->_roles[$roleId] = array(
0102             'instance' => $role,
0103             'parents'  => $roleParents,
0104             'children' => array()
0105             );
0106 
0107         return $this;
0108     }
0109 
0110     /**
0111      * Returns the identified Role
0112      *
0113      * The $role parameter can either be a Role or a Role identifier.
0114      *
0115      * @param  Zend_Acl_Role_Interface|string $role
0116      * @throws Zend_Acl_Role_Registry_Exception
0117      * @return Zend_Acl_Role_Interface
0118      */
0119     public function get($role)
0120     {
0121         if ($role instanceof Zend_Acl_Role_Interface) {
0122             $roleId = $role->getRoleId();
0123         } else {
0124             $roleId = (string) $role;
0125         }
0126 
0127         if (!$this->has($role)) {
0128             /**
0129              * @see Zend_Acl_Role_Registry_Exception
0130              */
0131             // require_once 'Zend/Acl/Role/Registry/Exception.php';
0132             throw new Zend_Acl_Role_Registry_Exception("Role '$roleId' not found");
0133         }
0134 
0135         return $this->_roles[$roleId]['instance'];
0136     }
0137 
0138     /**
0139      * Returns true if and only if the Role exists in the registry
0140      *
0141      * The $role parameter can either be a Role or a Role identifier.
0142      *
0143      * @param  Zend_Acl_Role_Interface|string $role
0144      * @return boolean
0145      */
0146     public function has($role)
0147     {
0148         if ($role instanceof Zend_Acl_Role_Interface) {
0149             $roleId = $role->getRoleId();
0150         } else {
0151             $roleId = (string) $role;
0152         }
0153 
0154         return isset($this->_roles[$roleId]);
0155     }
0156 
0157     /**
0158      * Returns an array of an existing Role's parents
0159      *
0160      * The array keys are the identifiers of the parent Roles, and the values are
0161      * the parent Role instances. The parent Roles are ordered in this array by
0162      * ascending priority. The highest priority parent Role, last in the array,
0163      * corresponds with the parent Role most recently added.
0164      *
0165      * If the Role does not have any parents, then an empty array is returned.
0166      *
0167      * @param  Zend_Acl_Role_Interface|string $role
0168      * @uses   Zend_Acl_Role_Registry::get()
0169      * @return array
0170      */
0171     public function getParents($role)
0172     {
0173         $roleId = $this->get($role)->getRoleId();
0174 
0175         return $this->_roles[$roleId]['parents'];
0176     }
0177 
0178     /**
0179      * Returns true if and only if $role inherits from $inherit
0180      *
0181      * Both parameters may be either a Role or a Role identifier. If
0182      * $onlyParents is true, then $role must inherit directly from
0183      * $inherit in order to return true. By default, this method looks
0184      * through the entire inheritance DAG to determine whether $role
0185      * inherits from $inherit through its ancestor Roles.
0186      *
0187      * @param  Zend_Acl_Role_Interface|string $role
0188      * @param  Zend_Acl_Role_Interface|string $inherit
0189      * @param  boolean                        $onlyParents
0190      * @throws Zend_Acl_Role_Registry_Exception
0191      * @return boolean
0192      */
0193     public function inherits($role, $inherit, $onlyParents = false)
0194     {
0195         /**
0196          * @see Zend_Acl_Role_Registry_Exception
0197          */
0198         // require_once 'Zend/Acl/Role/Registry/Exception.php';
0199         try {
0200             $roleId     = $this->get($role)->getRoleId();
0201             $inheritId = $this->get($inherit)->getRoleId();
0202         } catch (Zend_Acl_Role_Registry_Exception $e) {
0203             throw new Zend_Acl_Role_Registry_Exception($e->getMessage(), $e->getCode(), $e);
0204         }
0205 
0206         $inherits = isset($this->_roles[$roleId]['parents'][$inheritId]);
0207 
0208         if ($inherits || $onlyParents) {
0209             return $inherits;
0210         }
0211 
0212         foreach ($this->_roles[$roleId]['parents'] as $parentId => $parent) {
0213             if ($this->inherits($parentId, $inheritId)) {
0214                 return true;
0215             }
0216         }
0217 
0218         return false;
0219     }
0220 
0221     /**
0222      * Removes the Role from the registry
0223      *
0224      * The $role parameter can either be a Role or a Role identifier.
0225      *
0226      * @param  Zend_Acl_Role_Interface|string $role
0227      * @throws Zend_Acl_Role_Registry_Exception
0228      * @return Zend_Acl_Role_Registry Provides a fluent interface
0229      */
0230     public function remove($role)
0231     {
0232         /**
0233          * @see Zend_Acl_Role_Registry_Exception
0234          */
0235         // require_once 'Zend/Acl/Role/Registry/Exception.php';
0236         try {
0237             $roleId = $this->get($role)->getRoleId();
0238         } catch (Zend_Acl_Role_Registry_Exception $e) {
0239             throw new Zend_Acl_Role_Registry_Exception($e->getMessage(), $e->getCode(), $e);
0240         }
0241 
0242         foreach ($this->_roles[$roleId]['children'] as $childId => $child) {
0243             unset($this->_roles[$childId]['parents'][$roleId]);
0244         }
0245         foreach ($this->_roles[$roleId]['parents'] as $parentId => $parent) {
0246             unset($this->_roles[$parentId]['children'][$roleId]);
0247         }
0248 
0249         unset($this->_roles[$roleId]);
0250 
0251         return $this;
0252     }
0253 
0254     /**
0255      * Removes all Roles from the registry
0256      *
0257      * @return Zend_Acl_Role_Registry Provides a fluent interface
0258      */
0259     public function removeAll()
0260     {
0261         $this->_roles = array();
0262 
0263         return $this;
0264     }
0265 
0266     public function getRoles()
0267     {
0268         return $this->_roles;
0269     }
0270 
0271 }