Registry.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  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.
  14. *
  15. * @category Zend
  16. * @package Zend_Acl
  17. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @see Zend_Acl_Role_Interface
  23. */
  24. #require_once 'Zend/Acl/Role/Interface.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Acl
  28. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Acl_Role_Registry
  32. {
  33. /**
  34. * Internal Role registry data storage
  35. *
  36. * @var array
  37. */
  38. protected $_roles = array();
  39. /**
  40. * Adds a Role having an identifier unique to the registry
  41. *
  42. * The $parents parameter may be a reference to, or the string identifier for,
  43. * a Role existing in the registry, or $parents may be passed as an array of
  44. * these - mixing string identifiers and objects is ok - to indicate the Roles
  45. * from which the newly added Role will directly inherit.
  46. *
  47. * In order to resolve potential ambiguities with conflicting rules inherited
  48. * from different parents, the most recently added parent takes precedence over
  49. * parents that were previously added. In other words, the first parent added
  50. * will have the least priority, and the last parent added will have the
  51. * highest priority.
  52. *
  53. * @param Zend_Acl_Role_Interface $role
  54. * @param Zend_Acl_Role_Interface|string|array $parents
  55. * @throws Zend_Acl_Role_Registry_Exception
  56. * @return Zend_Acl_Role_Registry Provides a fluent interface
  57. */
  58. public function add(Zend_Acl_Role_Interface $role, $parents = null)
  59. {
  60. $roleId = $role->getRoleId();
  61. if ($this->has($roleId)) {
  62. /**
  63. * @see Zend_Acl_Role_Registry_Exception
  64. */
  65. #require_once 'Zend/Acl/Role/Registry/Exception.php';
  66. throw new Zend_Acl_Role_Registry_Exception("Role id '$roleId' already exists in the registry");
  67. }
  68. $roleParents = array();
  69. if (null !== $parents) {
  70. if (!is_array($parents)) {
  71. $parents = array($parents);
  72. }
  73. /**
  74. * @see Zend_Acl_Role_Registry_Exception
  75. */
  76. #require_once 'Zend/Acl/Role/Registry/Exception.php';
  77. foreach ($parents as $parent) {
  78. try {
  79. if ($parent instanceof Zend_Acl_Role_Interface) {
  80. $roleParentId = $parent->getRoleId();
  81. } else {
  82. $roleParentId = $parent;
  83. }
  84. $roleParent = $this->get($roleParentId);
  85. } catch (Zend_Acl_Role_Registry_Exception $e) {
  86. throw new Zend_Acl_Role_Registry_Exception("Parent Role id '$roleParentId' does not exist", 0, $e);
  87. }
  88. $roleParents[$roleParentId] = $roleParent;
  89. $this->_roles[$roleParentId]['children'][$roleId] = $role;
  90. }
  91. }
  92. $this->_roles[$roleId] = array(
  93. 'instance' => $role,
  94. 'parents' => $roleParents,
  95. 'children' => array()
  96. );
  97. return $this;
  98. }
  99. /**
  100. * Returns the identified Role
  101. *
  102. * The $role parameter can either be a Role or a Role identifier.
  103. *
  104. * @param Zend_Acl_Role_Interface|string $role
  105. * @throws Zend_Acl_Role_Registry_Exception
  106. * @return Zend_Acl_Role_Interface
  107. */
  108. public function get($role)
  109. {
  110. if ($role instanceof Zend_Acl_Role_Interface) {
  111. $roleId = $role->getRoleId();
  112. } else {
  113. $roleId = (string) $role;
  114. }
  115. if (!$this->has($role)) {
  116. /**
  117. * @see Zend_Acl_Role_Registry_Exception
  118. */
  119. #require_once 'Zend/Acl/Role/Registry/Exception.php';
  120. throw new Zend_Acl_Role_Registry_Exception("Role '$roleId' not found");
  121. }
  122. return $this->_roles[$roleId]['instance'];
  123. }
  124. /**
  125. * Returns true if and only if the Role exists in the registry
  126. *
  127. * The $role parameter can either be a Role or a Role identifier.
  128. *
  129. * @param Zend_Acl_Role_Interface|string $role
  130. * @return boolean
  131. */
  132. public function has($role)
  133. {
  134. if ($role instanceof Zend_Acl_Role_Interface) {
  135. $roleId = $role->getRoleId();
  136. } else {
  137. $roleId = (string) $role;
  138. }
  139. return isset($this->_roles[$roleId]);
  140. }
  141. /**
  142. * Returns an array of an existing Role's parents
  143. *
  144. * The array keys are the identifiers of the parent Roles, and the values are
  145. * the parent Role instances. The parent Roles are ordered in this array by
  146. * ascending priority. The highest priority parent Role, last in the array,
  147. * corresponds with the parent Role most recently added.
  148. *
  149. * If the Role does not have any parents, then an empty array is returned.
  150. *
  151. * @param Zend_Acl_Role_Interface|string $role
  152. * @uses Zend_Acl_Role_Registry::get()
  153. * @return array
  154. */
  155. public function getParents($role)
  156. {
  157. $roleId = $this->get($role)->getRoleId();
  158. return $this->_roles[$roleId]['parents'];
  159. }
  160. /**
  161. * Returns true if and only if $role inherits from $inherit
  162. *
  163. * Both parameters may be either a Role or a Role identifier. If
  164. * $onlyParents is true, then $role must inherit directly from
  165. * $inherit in order to return true. By default, this method looks
  166. * through the entire inheritance DAG to determine whether $role
  167. * inherits from $inherit through its ancestor Roles.
  168. *
  169. * @param Zend_Acl_Role_Interface|string $role
  170. * @param Zend_Acl_Role_Interface|string $inherit
  171. * @param boolean $onlyParents
  172. * @throws Zend_Acl_Role_Registry_Exception
  173. * @return boolean
  174. */
  175. public function inherits($role, $inherit, $onlyParents = false)
  176. {
  177. /**
  178. * @see Zend_Acl_Role_Registry_Exception
  179. */
  180. #require_once 'Zend/Acl/Role/Registry/Exception.php';
  181. try {
  182. $roleId = $this->get($role)->getRoleId();
  183. $inheritId = $this->get($inherit)->getRoleId();
  184. } catch (Zend_Acl_Role_Registry_Exception $e) {
  185. throw new Zend_Acl_Role_Registry_Exception($e->getMessage(), $e->getCode(), $e);
  186. }
  187. $inherits = isset($this->_roles[$roleId]['parents'][$inheritId]);
  188. if ($inherits || $onlyParents) {
  189. return $inherits;
  190. }
  191. foreach ($this->_roles[$roleId]['parents'] as $parentId => $parent) {
  192. if ($this->inherits($parentId, $inheritId)) {
  193. return true;
  194. }
  195. }
  196. return false;
  197. }
  198. /**
  199. * Removes the Role from the registry
  200. *
  201. * The $role parameter can either be a Role or a Role identifier.
  202. *
  203. * @param Zend_Acl_Role_Interface|string $role
  204. * @throws Zend_Acl_Role_Registry_Exception
  205. * @return Zend_Acl_Role_Registry Provides a fluent interface
  206. */
  207. public function remove($role)
  208. {
  209. /**
  210. * @see Zend_Acl_Role_Registry_Exception
  211. */
  212. #require_once 'Zend/Acl/Role/Registry/Exception.php';
  213. try {
  214. $roleId = $this->get($role)->getRoleId();
  215. } catch (Zend_Acl_Role_Registry_Exception $e) {
  216. throw new Zend_Acl_Role_Registry_Exception($e->getMessage(), $e->getCode(), $e);
  217. }
  218. foreach ($this->_roles[$roleId]['children'] as $childId => $child) {
  219. unset($this->_roles[$childId]['parents'][$roleId]);
  220. }
  221. foreach ($this->_roles[$roleId]['parents'] as $parentId => $parent) {
  222. unset($this->_roles[$parentId]['children'][$roleId]);
  223. }
  224. unset($this->_roles[$roleId]);
  225. return $this;
  226. }
  227. /**
  228. * Removes all Roles from the registry
  229. *
  230. * @return Zend_Acl_Role_Registry Provides a fluent interface
  231. */
  232. public function removeAll()
  233. {
  234. $this->_roles = array();
  235. return $this;
  236. }
  237. public function getRoles()
  238. {
  239. return $this->_roles;
  240. }
  241. }