ZF-5638: Apply patch to add getRoles() method to Zend_Acl
[zend/radio.git] / library / Zend / Acl / Role / Registry.php
bloba6efcc8057e557fb60378c99c04359d6c2dbea60
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
15 * @category Zend
16 * @package Zend_Acl
17 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
19 * @version $Id$
23 /**
24 * @see Zend_Acl_Role_Interface
26 require_once 'Zend/Acl/Role/Interface.php';
29 /**
30 * @category Zend
31 * @package Zend_Acl
32 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
33 * @license http://framework.zend.com/license/new-bsd New BSD License
35 class Zend_Acl_Role_Registry
37 /**
38 * Internal Role registry data storage
40 * @var array
42 protected $_roles = array();
44 /**
45 * Adds a Role having an identifier unique to the registry
47 * The $parents parameter may be a reference to, or the string identifier for,
48 * a Role existing in the registry, or $parents may be passed as an array of
49 * these - mixing string identifiers and objects is ok - to indicate the Roles
50 * from which the newly added Role will directly inherit.
52 * In order to resolve potential ambiguities with conflicting rules inherited
53 * from different parents, the most recently added parent takes precedence over
54 * parents that were previously added. In other words, the first parent added
55 * will have the least priority, and the last parent added will have the
56 * highest priority.
58 * @param Zend_Acl_Role_Interface $role
59 * @param Zend_Acl_Role_Interface|string|array $parents
60 * @throws Zend_Acl_Role_Registry_Exception
61 * @return Zend_Acl_Role_Registry Provides a fluent interface
63 public function add(Zend_Acl_Role_Interface $role, $parents = null)
65 $roleId = $role->getRoleId();
67 if ($this->has($roleId)) {
68 /**
69 * @see Zend_Acl_Role_Registry_Exception
71 require_once 'Zend/Acl/Role/Registry/Exception.php';
72 throw new Zend_Acl_Role_Registry_Exception("Role id '$roleId' already exists in the registry");
75 $roleParents = array();
77 if (null !== $parents) {
78 if (!is_array($parents)) {
79 $parents = array($parents);
81 /**
82 * @see Zend_Acl_Role_Registry_Exception
84 require_once 'Zend/Acl/Role/Registry/Exception.php';
85 foreach ($parents as $parent) {
86 try {
87 if ($parent instanceof Zend_Acl_Role_Interface) {
88 $roleParentId = $parent->getRoleId();
89 } else {
90 $roleParentId = $parent;
92 $roleParent = $this->get($roleParentId);
93 } catch (Zend_Acl_Role_Registry_Exception $e) {
94 throw new Zend_Acl_Role_Registry_Exception("Parent Role id '$roleParentId' does not exist");
96 $roleParents[$roleParentId] = $roleParent;
97 $this->_roles[$roleParentId]['children'][$roleId] = $role;
101 $this->_roles[$roleId] = array(
102 'instance' => $role,
103 'parents' => $roleParents,
104 'children' => array()
107 return $this;
111 * Returns the identified Role
113 * The $role parameter can either be a Role or a Role identifier.
115 * @param Zend_Acl_Role_Interface|string $role
116 * @throws Zend_Acl_Role_Registry_Exception
117 * @return Zend_Acl_Role_Interface
119 public function get($role)
121 if ($role instanceof Zend_Acl_Role_Interface) {
122 $roleId = $role->getRoleId();
123 } else {
124 $roleId = (string) $role;
127 if (!$this->has($role)) {
129 * @see Zend_Acl_Role_Registry_Exception
131 require_once 'Zend/Acl/Role/Registry/Exception.php';
132 throw new Zend_Acl_Role_Registry_Exception("Role '$roleId' not found");
135 return $this->_roles[$roleId]['instance'];
139 * Returns true if and only if the Role exists in the registry
141 * The $role parameter can either be a Role or a Role identifier.
143 * @param Zend_Acl_Role_Interface|string $role
144 * @return boolean
146 public function has($role)
148 if ($role instanceof Zend_Acl_Role_Interface) {
149 $roleId = $role->getRoleId();
150 } else {
151 $roleId = (string) $role;
154 return isset($this->_roles[$roleId]);
158 * Returns an array of an existing Role's parents
160 * The array keys are the identifiers of the parent Roles, and the values are
161 * the parent Role instances. The parent Roles are ordered in this array by
162 * ascending priority. The highest priority parent Role, last in the array,
163 * corresponds with the parent Role most recently added.
165 * If the Role does not have any parents, then an empty array is returned.
167 * @param Zend_Acl_Role_Interface|string $role
168 * @uses Zend_Acl_Role_Registry::get()
169 * @return array
171 public function getParents($role)
173 $roleId = $this->get($role)->getRoleId();
175 return $this->_roles[$roleId]['parents'];
179 * Returns true if and only if $role inherits from $inherit
181 * Both parameters may be either a Role or a Role identifier. If
182 * $onlyParents is true, then $role must inherit directly from
183 * $inherit in order to return true. By default, this method looks
184 * through the entire inheritance DAG to determine whether $role
185 * inherits from $inherit through its ancestor Roles.
187 * @param Zend_Acl_Role_Interface|string $role
188 * @param Zend_Acl_Role_Interface|string $inherit
189 * @param boolean $onlyParents
190 * @throws Zend_Acl_Role_Registry_Exception
191 * @return boolean
193 public function inherits($role, $inherit, $onlyParents = false)
196 * @see Zend_Acl_Role_Registry_Exception
198 require_once 'Zend/Acl/Role/Registry/Exception.php';
199 try {
200 $roleId = $this->get($role)->getRoleId();
201 $inheritId = $this->get($inherit)->getRoleId();
202 } catch (Zend_Acl_Role_Registry_Exception $e) {
203 throw $e;
206 $inherits = isset($this->_roles[$roleId]['parents'][$inheritId]);
208 if ($inherits || $onlyParents) {
209 return $inherits;
212 foreach ($this->_roles[$roleId]['parents'] as $parentId => $parent) {
213 if ($this->inherits($parentId, $inheritId)) {
214 return true;
218 return false;
222 * Removes the Role from the registry
224 * The $role parameter can either be a Role or a Role identifier.
226 * @param Zend_Acl_Role_Interface|string $role
227 * @throws Zend_Acl_Role_Registry_Exception
228 * @return Zend_Acl_Role_Registry Provides a fluent interface
230 public function remove($role)
233 * @see Zend_Acl_Role_Registry_Exception
235 require_once 'Zend/Acl/Role/Registry/Exception.php';
236 try {
237 $roleId = $this->get($role)->getRoleId();
238 } catch (Zend_Acl_Role_Registry_Exception $e) {
239 throw $e;
242 foreach ($this->_roles[$roleId]['children'] as $childId => $child) {
243 unset($this->_roles[$childId]['parents'][$roleId]);
245 foreach ($this->_roles[$roleId]['parents'] as $parentId => $parent) {
246 unset($this->_roles[$parentId]['children'][$roleId]);
249 unset($this->_roles[$roleId]);
251 return $this;
255 * Removes all Roles from the registry
257 * @return Zend_Acl_Role_Registry Provides a fluent interface
259 public function removeAll()
261 $this->_roles = array();
263 return $this;
266 public function getRoles()
268 return $this->_roles;