Acl.php 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  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-2010 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_Resource_Interface
  23. */
  24. require_once 'Zend/Acl/Resource/Interface.php';
  25. /**
  26. * @see Zend_Acl_Role_Registry
  27. */
  28. require_once 'Zend/Acl/Role/Registry.php';
  29. /**
  30. * @see Zend_Acl_Assert_Interface
  31. */
  32. require_once 'Zend/Acl/Assert/Interface.php';
  33. /**
  34. * @see Zend_Acl_Role
  35. */
  36. require_once 'Zend/Acl/Role.php';
  37. /**
  38. * @see Zend_Acl_Resource
  39. */
  40. require_once 'Zend/Acl/Resource.php';
  41. /**
  42. * @category Zend
  43. * @package Zend_Acl
  44. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  45. * @license http://framework.zend.com/license/new-bsd New BSD License
  46. */
  47. class Zend_Acl
  48. {
  49. /**
  50. * Rule type: allow
  51. */
  52. const TYPE_ALLOW = 'TYPE_ALLOW';
  53. /**
  54. * Rule type: deny
  55. */
  56. const TYPE_DENY = 'TYPE_DENY';
  57. /**
  58. * Rule operation: add
  59. */
  60. const OP_ADD = 'OP_ADD';
  61. /**
  62. * Rule operation: remove
  63. */
  64. const OP_REMOVE = 'OP_REMOVE';
  65. /**
  66. * Role registry
  67. *
  68. * @var Zend_Acl_Role_Registry
  69. */
  70. protected $_roleRegistry = null;
  71. /**
  72. * Resource tree
  73. *
  74. * @var array
  75. */
  76. protected $_resources = array();
  77. /**
  78. * @var Zend_Acl_Role_Interface
  79. */
  80. protected $_isAllowedRole = null;
  81. /**
  82. * @var Zend_Acl_Resource_Interface
  83. */
  84. protected $_isAllowedResource = null;
  85. /**
  86. * @var String
  87. */
  88. protected $_isAllowedPrivilege = null;
  89. /**
  90. * ACL rules; whitelist (deny everything to all) by default
  91. *
  92. * @var array
  93. */
  94. protected $_rules = array(
  95. 'allResources' => array(
  96. 'allRoles' => array(
  97. 'allPrivileges' => array(
  98. 'type' => self::TYPE_DENY,
  99. 'assert' => null
  100. ),
  101. 'byPrivilegeId' => array()
  102. ),
  103. 'byRoleId' => array()
  104. ),
  105. 'byResourceId' => array()
  106. );
  107. /**
  108. * Adds a Role having an identifier unique to the registry
  109. *
  110. * The $parents parameter may be a reference to, or the string identifier for,
  111. * a Role existing in the registry, or $parents may be passed as an array of
  112. * these - mixing string identifiers and objects is ok - to indicate the Roles
  113. * from which the newly added Role will directly inherit.
  114. *
  115. * In order to resolve potential ambiguities with conflicting rules inherited
  116. * from different parents, the most recently added parent takes precedence over
  117. * parents that were previously added. In other words, the first parent added
  118. * will have the least priority, and the last parent added will have the
  119. * highest priority.
  120. *
  121. * @param Zend_Acl_Role_Interface $role
  122. * @param Zend_Acl_Role_Interface|string|array $parents
  123. * @uses Zend_Acl_Role_Registry::add()
  124. * @return Zend_Acl Provides a fluent interface
  125. */
  126. public function addRole($role, $parents = null)
  127. {
  128. if (is_string($role)) {
  129. $role = new Zend_Acl_Role($role);
  130. }
  131. if (!$role instanceof Zend_Acl_Role_Interface) {
  132. require_once 'Zend/Acl/Exception.php';
  133. throw new Zend_Acl_Exception('addRole() expects $role to be of type Zend_Acl_Role_Interface');
  134. }
  135. $this->_getRoleRegistry()->add($role, $parents);
  136. return $this;
  137. }
  138. /**
  139. * Returns the identified Role
  140. *
  141. * The $role parameter can either be a Role or Role identifier.
  142. *
  143. * @param Zend_Acl_Role_Interface|string $role
  144. * @uses Zend_Acl_Role_Registry::get()
  145. * @return Zend_Acl_Role_Interface
  146. */
  147. public function getRole($role)
  148. {
  149. return $this->_getRoleRegistry()->get($role);
  150. }
  151. /**
  152. * Returns true if and only if the Role exists in the registry
  153. *
  154. * The $role parameter can either be a Role or a Role identifier.
  155. *
  156. * @param Zend_Acl_Role_Interface|string $role
  157. * @uses Zend_Acl_Role_Registry::has()
  158. * @return boolean
  159. */
  160. public function hasRole($role)
  161. {
  162. return $this->_getRoleRegistry()->has($role);
  163. }
  164. /**
  165. * Returns true if and only if $role inherits from $inherit
  166. *
  167. * Both parameters may be either a Role or a Role identifier. If
  168. * $onlyParents is true, then $role must inherit directly from
  169. * $inherit in order to return true. By default, this method looks
  170. * through the entire inheritance DAG to determine whether $role
  171. * inherits from $inherit through its ancestor Roles.
  172. *
  173. * @param Zend_Acl_Role_Interface|string $role
  174. * @param Zend_Acl_Role_Interface|string $inherit
  175. * @param boolean $onlyParents
  176. * @uses Zend_Acl_Role_Registry::inherits()
  177. * @return boolean
  178. */
  179. public function inheritsRole($role, $inherit, $onlyParents = false)
  180. {
  181. return $this->_getRoleRegistry()->inherits($role, $inherit, $onlyParents);
  182. }
  183. /**
  184. * Removes the Role from the registry
  185. *
  186. * The $role parameter can either be a Role or a Role identifier.
  187. *
  188. * @param Zend_Acl_Role_Interface|string $role
  189. * @uses Zend_Acl_Role_Registry::remove()
  190. * @return Zend_Acl Provides a fluent interface
  191. */
  192. public function removeRole($role)
  193. {
  194. $this->_getRoleRegistry()->remove($role);
  195. if ($role instanceof Zend_Acl_Role_Interface) {
  196. $roleId = $role->getRoleId();
  197. } else {
  198. $roleId = $role;
  199. }
  200. foreach ($this->_rules['allResources']['byRoleId'] as $roleIdCurrent => $rules) {
  201. if ($roleId === $roleIdCurrent) {
  202. unset($this->_rules['allResources']['byRoleId'][$roleIdCurrent]);
  203. }
  204. }
  205. foreach ($this->_rules['byResourceId'] as $resourceIdCurrent => $visitor) {
  206. if (array_key_exists('byRoleId', $visitor)) {
  207. foreach ($visitor['byRoleId'] as $roleIdCurrent => $rules) {
  208. if ($roleId === $roleIdCurrent) {
  209. unset($this->_rules['byResourceId'][$resourceIdCurrent]['byRoleId'][$roleIdCurrent]);
  210. }
  211. }
  212. }
  213. }
  214. return $this;
  215. }
  216. /**
  217. * Removes all Roles from the registry
  218. *
  219. * @uses Zend_Acl_Role_Registry::removeAll()
  220. * @return Zend_Acl Provides a fluent interface
  221. */
  222. public function removeRoleAll()
  223. {
  224. $this->_getRoleRegistry()->removeAll();
  225. foreach ($this->_rules['allResources']['byRoleId'] as $roleIdCurrent => $rules) {
  226. unset($this->_rules['allResources']['byRoleId'][$roleIdCurrent]);
  227. }
  228. foreach ($this->_rules['byResourceId'] as $resourceIdCurrent => $visitor) {
  229. foreach ($visitor['byRoleId'] as $roleIdCurrent => $rules) {
  230. unset($this->_rules['byResourceId'][$resourceIdCurrent]['byRoleId'][$roleIdCurrent]);
  231. }
  232. }
  233. return $this;
  234. }
  235. /**
  236. * Adds a Resource having an identifier unique to the ACL
  237. *
  238. * The $parent parameter may be a reference to, or the string identifier for,
  239. * the existing Resource from which the newly added Resource will inherit.
  240. *
  241. * @param Zend_Acl_Resource_Interface|string $resource
  242. * @param Zend_Acl_Resource_Interface|string $parent
  243. * @throws Zend_Acl_Exception
  244. * @return Zend_Acl Provides a fluent interface
  245. */
  246. public function addResource($resource, $parent = null)
  247. {
  248. if (is_string($resource)) {
  249. $resource = new Zend_Acl_Resource($resource);
  250. }
  251. if (!$resource instanceof Zend_Acl_Resource_Interface) {
  252. require_once 'Zend/Acl/Exception.php';
  253. throw new Zend_Acl_Exception('addResource() expects $resource to be of type Zend_Acl_Resource_Interface');
  254. }
  255. $resourceId = $resource->getResourceId();
  256. if ($this->has($resourceId)) {
  257. require_once 'Zend/Acl/Exception.php';
  258. throw new Zend_Acl_Exception("Resource id '$resourceId' already exists in the ACL");
  259. }
  260. $resourceParent = null;
  261. if (null !== $parent) {
  262. try {
  263. if ($parent instanceof Zend_Acl_Resource_Interface) {
  264. $resourceParentId = $parent->getResourceId();
  265. } else {
  266. $resourceParentId = $parent;
  267. }
  268. $resourceParent = $this->get($resourceParentId);
  269. } catch (Zend_Acl_Exception $e) {
  270. require_once 'Zend/Acl/Exception.php';
  271. throw new Zend_Acl_Exception("Parent Resource id '$resourceParentId' does not exist", 0, $e);
  272. }
  273. $this->_resources[$resourceParentId]['children'][$resourceId] = $resource;
  274. }
  275. $this->_resources[$resourceId] = array(
  276. 'instance' => $resource,
  277. 'parent' => $resourceParent,
  278. 'children' => array()
  279. );
  280. return $this;
  281. }
  282. /**
  283. * Adds a Resource having an identifier unique to the ACL
  284. *
  285. * The $parent parameter may be a reference to, or the string identifier for,
  286. * the existing Resource from which the newly added Resource will inherit.
  287. *
  288. * @deprecated in version 1.9.1 and will be available till 2.0. New code
  289. * should use addResource() instead.
  290. *
  291. * @param Zend_Acl_Resource_Interface $resource
  292. * @param Zend_Acl_Resource_Interface|string $parent
  293. * @throws Zend_Acl_Exception
  294. * @return Zend_Acl Provides a fluent interface
  295. */
  296. public function add(Zend_Acl_Resource_Interface $resource, $parent = null)
  297. {
  298. return $this->addResource($resource, $parent);
  299. }
  300. /**
  301. * Returns the identified Resource
  302. *
  303. * The $resource parameter can either be a Resource or a Resource identifier.
  304. *
  305. * @param Zend_Acl_Resource_Interface|string $resource
  306. * @throws Zend_Acl_Exception
  307. * @return Zend_Acl_Resource_Interface
  308. */
  309. public function get($resource)
  310. {
  311. if ($resource instanceof Zend_Acl_Resource_Interface) {
  312. $resourceId = $resource->getResourceId();
  313. } else {
  314. $resourceId = (string) $resource;
  315. }
  316. if (!$this->has($resource)) {
  317. require_once 'Zend/Acl/Exception.php';
  318. throw new Zend_Acl_Exception("Resource '$resourceId' not found");
  319. }
  320. return $this->_resources[$resourceId]['instance'];
  321. }
  322. /**
  323. * Returns true if and only if the Resource exists in the ACL
  324. *
  325. * The $resource parameter can either be a Resource or a Resource identifier.
  326. *
  327. * @param Zend_Acl_Resource_Interface|string $resource
  328. * @return boolean
  329. */
  330. public function has($resource)
  331. {
  332. if ($resource instanceof Zend_Acl_Resource_Interface) {
  333. $resourceId = $resource->getResourceId();
  334. } else {
  335. $resourceId = (string) $resource;
  336. }
  337. return isset($this->_resources[$resourceId]);
  338. }
  339. /**
  340. * Returns true if and only if $resource inherits from $inherit
  341. *
  342. * Both parameters may be either a Resource or a Resource identifier. If
  343. * $onlyParent is true, then $resource must inherit directly from
  344. * $inherit in order to return true. By default, this method looks
  345. * through the entire inheritance tree to determine whether $resource
  346. * inherits from $inherit through its ancestor Resources.
  347. *
  348. * @param Zend_Acl_Resource_Interface|string $resource
  349. * @param Zend_Acl_Resource_Interface|string $inherit
  350. * @param boolean $onlyParent
  351. * @throws Zend_Acl_Resource_Registry_Exception
  352. * @return boolean
  353. */
  354. public function inherits($resource, $inherit, $onlyParent = false)
  355. {
  356. try {
  357. $resourceId = $this->get($resource)->getResourceId();
  358. $inheritId = $this->get($inherit)->getResourceId();
  359. } catch (Zend_Acl_Exception $e) {
  360. require_once 'Zend/Acl/Exception.php';
  361. throw new Zend_Acl_Exception($e->getMessage(), $e->getCode(), $e);
  362. }
  363. if (null !== $this->_resources[$resourceId]['parent']) {
  364. $parentId = $this->_resources[$resourceId]['parent']->getResourceId();
  365. if ($inheritId === $parentId) {
  366. return true;
  367. } else if ($onlyParent) {
  368. return false;
  369. }
  370. } else {
  371. return false;
  372. }
  373. while (null !== $this->_resources[$parentId]['parent']) {
  374. $parentId = $this->_resources[$parentId]['parent']->getResourceId();
  375. if ($inheritId === $parentId) {
  376. return true;
  377. }
  378. }
  379. return false;
  380. }
  381. /**
  382. * Removes a Resource and all of its children
  383. *
  384. * The $resource parameter can either be a Resource or a Resource identifier.
  385. *
  386. * @param Zend_Acl_Resource_Interface|string $resource
  387. * @throws Zend_Acl_Exception
  388. * @return Zend_Acl Provides a fluent interface
  389. */
  390. public function remove($resource)
  391. {
  392. try {
  393. $resourceId = $this->get($resource)->getResourceId();
  394. } catch (Zend_Acl_Exception $e) {
  395. require_once 'Zend/Acl/Exception.php';
  396. throw new Zend_Acl_Exception($e->getMessage(), $e->getCode(), $e);
  397. }
  398. $resourcesRemoved = array($resourceId);
  399. if (null !== ($resourceParent = $this->_resources[$resourceId]['parent'])) {
  400. unset($this->_resources[$resourceParent->getResourceId()]['children'][$resourceId]);
  401. }
  402. foreach ($this->_resources[$resourceId]['children'] as $childId => $child) {
  403. $this->remove($childId);
  404. $resourcesRemoved[] = $childId;
  405. }
  406. foreach ($resourcesRemoved as $resourceIdRemoved) {
  407. foreach ($this->_rules['byResourceId'] as $resourceIdCurrent => $rules) {
  408. if ($resourceIdRemoved === $resourceIdCurrent) {
  409. unset($this->_rules['byResourceId'][$resourceIdCurrent]);
  410. }
  411. }
  412. }
  413. unset($this->_resources[$resourceId]);
  414. return $this;
  415. }
  416. /**
  417. * Removes all Resources
  418. *
  419. * @return Zend_Acl Provides a fluent interface
  420. */
  421. public function removeAll()
  422. {
  423. foreach ($this->_resources as $resourceId => $resource) {
  424. foreach ($this->_rules['byResourceId'] as $resourceIdCurrent => $rules) {
  425. if ($resourceId === $resourceIdCurrent) {
  426. unset($this->_rules['byResourceId'][$resourceIdCurrent]);
  427. }
  428. }
  429. }
  430. $this->_resources = array();
  431. return $this;
  432. }
  433. /**
  434. * Adds an "allow" rule to the ACL
  435. *
  436. * @param Zend_Acl_Role_Interface|string|array $roles
  437. * @param Zend_Acl_Resource_Interface|string|array $resources
  438. * @param string|array $privileges
  439. * @param Zend_Acl_Assert_Interface $assert
  440. * @uses Zend_Acl::setRule()
  441. * @return Zend_Acl Provides a fluent interface
  442. */
  443. public function allow($roles = null, $resources = null, $privileges = null, Zend_Acl_Assert_Interface $assert = null)
  444. {
  445. return $this->setRule(self::OP_ADD, self::TYPE_ALLOW, $roles, $resources, $privileges, $assert);
  446. }
  447. /**
  448. * Adds a "deny" rule to the ACL
  449. *
  450. * @param Zend_Acl_Role_Interface|string|array $roles
  451. * @param Zend_Acl_Resource_Interface|string|array $resources
  452. * @param string|array $privileges
  453. * @param Zend_Acl_Assert_Interface $assert
  454. * @uses Zend_Acl::setRule()
  455. * @return Zend_Acl Provides a fluent interface
  456. */
  457. public function deny($roles = null, $resources = null, $privileges = null, Zend_Acl_Assert_Interface $assert = null)
  458. {
  459. return $this->setRule(self::OP_ADD, self::TYPE_DENY, $roles, $resources, $privileges, $assert);
  460. }
  461. /**
  462. * Removes "allow" permissions from the ACL
  463. *
  464. * @param Zend_Acl_Role_Interface|string|array $roles
  465. * @param Zend_Acl_Resource_Interface|string|array $resources
  466. * @param string|array $privileges
  467. * @uses Zend_Acl::setRule()
  468. * @return Zend_Acl Provides a fluent interface
  469. */
  470. public function removeAllow($roles = null, $resources = null, $privileges = null)
  471. {
  472. if ($resources == null) {
  473. $resources = array_keys($this->_resources);
  474. }
  475. return $this->setRule(self::OP_REMOVE, self::TYPE_ALLOW, $roles, $resources, $privileges);
  476. }
  477. /**
  478. * Removes "deny" restrictions from the ACL
  479. *
  480. * @param Zend_Acl_Role_Interface|string|array $roles
  481. * @param Zend_Acl_Resource_Interface|string|array $resources
  482. * @param string|array $privileges
  483. * @uses Zend_Acl::setRule()
  484. * @return Zend_Acl Provides a fluent interface
  485. */
  486. public function removeDeny($roles = null, $resources = null, $privileges = null)
  487. {
  488. if ($resources == null) {
  489. $resources = array_keys($this->_resources);
  490. }
  491. return $this->setRule(self::OP_REMOVE, self::TYPE_DENY, $roles, $resources, $privileges);
  492. }
  493. /**
  494. * Performs operations on ACL rules
  495. *
  496. * The $operation parameter may be either OP_ADD or OP_REMOVE, depending on whether the
  497. * user wants to add or remove a rule, respectively:
  498. *
  499. * OP_ADD specifics:
  500. *
  501. * A rule is added that would allow one or more Roles access to [certain $privileges
  502. * upon] the specified Resource(s).
  503. *
  504. * OP_REMOVE specifics:
  505. *
  506. * The rule is removed only in the context of the given Roles, Resources, and privileges.
  507. * Existing rules to which the remove operation does not apply would remain in the
  508. * ACL.
  509. *
  510. * The $type parameter may be either TYPE_ALLOW or TYPE_DENY, depending on whether the
  511. * rule is intended to allow or deny permission, respectively.
  512. *
  513. * The $roles and $resources parameters may be references to, or the string identifiers for,
  514. * existing Resources/Roles, or they may be passed as arrays of these - mixing string identifiers
  515. * and objects is ok - to indicate the Resources and Roles to which the rule applies. If either
  516. * $roles or $resources is null, then the rule applies to all Roles or all Resources, respectively.
  517. * Both may be null in order to work with the default rule of the ACL.
  518. *
  519. * The $privileges parameter may be used to further specify that the rule applies only
  520. * to certain privileges upon the Resource(s) in question. This may be specified to be a single
  521. * privilege with a string, and multiple privileges may be specified as an array of strings.
  522. *
  523. * If $assert is provided, then its assert() method must return true in order for
  524. * the rule to apply. If $assert is provided with $roles, $resources, and $privileges all
  525. * equal to null, then a rule having a type of:
  526. *
  527. * TYPE_ALLOW will imply a type of TYPE_DENY, and
  528. *
  529. * TYPE_DENY will imply a type of TYPE_ALLOW
  530. *
  531. * when the rule's assertion fails. This is because the ACL needs to provide expected
  532. * behavior when an assertion upon the default ACL rule fails.
  533. *
  534. * @param string $operation
  535. * @param string $type
  536. * @param Zend_Acl_Role_Interface|string|array $roles
  537. * @param Zend_Acl_Resource_Interface|string|array $resources
  538. * @param string|array $privileges
  539. * @param Zend_Acl_Assert_Interface $assert
  540. * @throws Zend_Acl_Exception
  541. * @uses Zend_Acl_Role_Registry::get()
  542. * @uses Zend_Acl::get()
  543. * @return Zend_Acl Provides a fluent interface
  544. */
  545. public function setRule($operation, $type, $roles = null, $resources = null, $privileges = null,
  546. Zend_Acl_Assert_Interface $assert = null)
  547. {
  548. // ensure that the rule type is valid; normalize input to uppercase
  549. $type = strtoupper($type);
  550. if (self::TYPE_ALLOW !== $type && self::TYPE_DENY !== $type) {
  551. require_once 'Zend/Acl/Exception.php';
  552. throw new Zend_Acl_Exception("Unsupported rule type; must be either '" . self::TYPE_ALLOW . "' or '"
  553. . self::TYPE_DENY . "'");
  554. }
  555. // ensure that all specified Roles exist; normalize input to array of Role objects or null
  556. if (!is_array($roles)) {
  557. $roles = array($roles);
  558. } else if (0 === count($roles)) {
  559. $roles = array(null);
  560. }
  561. $rolesTemp = $roles;
  562. $roles = array();
  563. foreach ($rolesTemp as $role) {
  564. if (null !== $role) {
  565. $roles[] = $this->_getRoleRegistry()->get($role);
  566. } else {
  567. $roles[] = null;
  568. }
  569. }
  570. unset($rolesTemp);
  571. // ensure that all specified Resources exist; normalize input to array of Resource objects or null
  572. if (!is_array($resources)) {
  573. $resources = array($resources);
  574. } else if (0 === count($resources)) {
  575. $resources = array(null);
  576. }
  577. $resourcesTemp = $resources;
  578. $resources = array();
  579. foreach ($resourcesTemp as $resource) {
  580. if (null !== $resource) {
  581. $resources[] = $this->get($resource);
  582. } else {
  583. $resources[] = null;
  584. }
  585. }
  586. unset($resourcesTemp);
  587. // normalize privileges to array
  588. if (null === $privileges) {
  589. $privileges = array();
  590. } else if (!is_array($privileges)) {
  591. $privileges = array($privileges);
  592. }
  593. switch ($operation) {
  594. // add to the rules
  595. case self::OP_ADD:
  596. foreach ($resources as $resource) {
  597. foreach ($roles as $role) {
  598. $rules =& $this->_getRules($resource, $role, true);
  599. if (0 === count($privileges)) {
  600. $rules['allPrivileges']['type'] = $type;
  601. $rules['allPrivileges']['assert'] = $assert;
  602. if (!isset($rules['byPrivilegeId'])) {
  603. $rules['byPrivilegeId'] = array();
  604. }
  605. } else {
  606. foreach ($privileges as $privilege) {
  607. $rules['byPrivilegeId'][$privilege]['type'] = $type;
  608. $rules['byPrivilegeId'][$privilege]['assert'] = $assert;
  609. }
  610. }
  611. }
  612. }
  613. break;
  614. // remove from the rules
  615. case self::OP_REMOVE:
  616. foreach ($resources as $resource) {
  617. foreach ($roles as $role) {
  618. $rules =& $this->_getRules($resource, $role);
  619. if (null === $rules) {
  620. continue;
  621. }
  622. if (0 === count($privileges)) {
  623. if (null === $resource && null === $role) {
  624. if ($type === $rules['allPrivileges']['type']) {
  625. $rules = array(
  626. 'allPrivileges' => array(
  627. 'type' => self::TYPE_DENY,
  628. 'assert' => null
  629. ),
  630. 'byPrivilegeId' => array()
  631. );
  632. }
  633. continue;
  634. }
  635. if (isset($rules['allPrivileges']['type']) &&
  636. $type === $rules['allPrivileges']['type'])
  637. {
  638. unset($rules['allPrivileges']);
  639. }
  640. } else {
  641. foreach ($privileges as $privilege) {
  642. if (isset($rules['byPrivilegeId'][$privilege]) &&
  643. $type === $rules['byPrivilegeId'][$privilege]['type'])
  644. {
  645. unset($rules['byPrivilegeId'][$privilege]);
  646. }
  647. }
  648. }
  649. }
  650. }
  651. break;
  652. default:
  653. require_once 'Zend/Acl/Exception.php';
  654. throw new Zend_Acl_Exception("Unsupported operation; must be either '" . self::OP_ADD . "' or '"
  655. . self::OP_REMOVE . "'");
  656. }
  657. return $this;
  658. }
  659. /**
  660. * Returns true if and only if the Role has access to the Resource
  661. *
  662. * The $role and $resource parameters may be references to, or the string identifiers for,
  663. * an existing Resource and Role combination.
  664. *
  665. * If either $role or $resource is null, then the query applies to all Roles or all Resources,
  666. * respectively. Both may be null to query whether the ACL has a "blacklist" rule
  667. * (allow everything to all). By default, Zend_Acl creates a "whitelist" rule (deny
  668. * everything to all), and this method would return false unless this default has
  669. * been overridden (i.e., by executing $acl->allow()).
  670. *
  671. * If a $privilege is not provided, then this method returns false if and only if the
  672. * Role is denied access to at least one privilege upon the Resource. In other words, this
  673. * method returns true if and only if the Role is allowed all privileges on the Resource.
  674. *
  675. * This method checks Role inheritance using a depth-first traversal of the Role registry.
  676. * The highest priority parent (i.e., the parent most recently added) is checked first,
  677. * and its respective parents are checked similarly before the lower-priority parents of
  678. * the Role are checked.
  679. *
  680. * @param Zend_Acl_Role_Interface|string $role
  681. * @param Zend_Acl_Resource_Interface|string $resource
  682. * @param string $privilege
  683. * @uses Zend_Acl::get()
  684. * @uses Zend_Acl_Role_Registry::get()
  685. * @return boolean
  686. */
  687. public function isAllowed($role = null, $resource = null, $privilege = null)
  688. {
  689. // reset role & resource to null
  690. $this->_isAllowedRole = null;
  691. $this->_isAllowedResource = null;
  692. $this->_isAllowedPrivilege = null;
  693. if (null !== $role) {
  694. // keep track of originally called role
  695. $this->_isAllowedRole = $role;
  696. $role = $this->_getRoleRegistry()->get($role);
  697. if (!$this->_isAllowedRole instanceof Zend_Acl_Role_Interface) {
  698. $this->_isAllowedRole = $role;
  699. }
  700. }
  701. if (null !== $resource) {
  702. // keep track of originally called resource
  703. $this->_isAllowedResource = $resource;
  704. $resource = $this->get($resource);
  705. if (!$this->_isAllowedResource instanceof Zend_Acl_Resource_Interface) {
  706. $this->_isAllowedResource = $resource;
  707. }
  708. }
  709. if (null === $privilege) {
  710. // query on all privileges
  711. do {
  712. // depth-first search on $role if it is not 'allRoles' pseudo-parent
  713. if (null !== $role && null !== ($result = $this->_roleDFSAllPrivileges($role, $resource, $privilege))) {
  714. return $result;
  715. }
  716. // look for rule on 'allRoles' psuedo-parent
  717. if (null !== ($rules = $this->_getRules($resource, null))) {
  718. foreach ($rules['byPrivilegeId'] as $privilege => $rule) {
  719. if (self::TYPE_DENY === ($ruleTypeOnePrivilege = $this->_getRuleType($resource, null, $privilege))) {
  720. return false;
  721. }
  722. }
  723. if (null !== ($ruleTypeAllPrivileges = $this->_getRuleType($resource, null, null))) {
  724. return self::TYPE_ALLOW === $ruleTypeAllPrivileges;
  725. }
  726. }
  727. // try next Resource
  728. $resource = $this->_resources[$resource->getResourceId()]['parent'];
  729. } while (true); // loop terminates at 'allResources' pseudo-parent
  730. } else {
  731. $this->_isAllowedPrivilege = $privilege;
  732. // query on one privilege
  733. do {
  734. // depth-first search on $role if it is not 'allRoles' pseudo-parent
  735. if (null !== $role && null !== ($result = $this->_roleDFSOnePrivilege($role, $resource, $privilege))) {
  736. return $result;
  737. }
  738. // look for rule on 'allRoles' pseudo-parent
  739. if (null !== ($ruleType = $this->_getRuleType($resource, null, $privilege))) {
  740. return self::TYPE_ALLOW === $ruleType;
  741. } else if (null !== ($ruleTypeAllPrivileges = $this->_getRuleType($resource, null, null))) {
  742. return self::TYPE_ALLOW === $ruleTypeAllPrivileges;
  743. }
  744. // try next Resource
  745. $resource = $this->_resources[$resource->getResourceId()]['parent'];
  746. } while (true); // loop terminates at 'allResources' pseudo-parent
  747. }
  748. }
  749. /**
  750. * Returns the Role registry for this ACL
  751. *
  752. * If no Role registry has been created yet, a new default Role registry
  753. * is created and returned.
  754. *
  755. * @return Zend_Acl_Role_Registry
  756. */
  757. protected function _getRoleRegistry()
  758. {
  759. if (null === $this->_roleRegistry) {
  760. $this->_roleRegistry = new Zend_Acl_Role_Registry();
  761. }
  762. return $this->_roleRegistry;
  763. }
  764. /**
  765. * Performs a depth-first search of the Role DAG, starting at $role, in order to find a rule
  766. * allowing/denying $role access to all privileges upon $resource
  767. *
  768. * This method returns true if a rule is found and allows access. If a rule exists and denies access,
  769. * then this method returns false. If no applicable rule is found, then this method returns null.
  770. *
  771. * @param Zend_Acl_Role_Interface $role
  772. * @param Zend_Acl_Resource_Interface $resource
  773. * @return boolean|null
  774. */
  775. protected function _roleDFSAllPrivileges(Zend_Acl_Role_Interface $role, Zend_Acl_Resource_Interface $resource = null)
  776. {
  777. $dfs = array(
  778. 'visited' => array(),
  779. 'stack' => array()
  780. );
  781. if (null !== ($result = $this->_roleDFSVisitAllPrivileges($role, $resource, $dfs))) {
  782. return $result;
  783. }
  784. while (null !== ($role = array_pop($dfs['stack']))) {
  785. if (!isset($dfs['visited'][$role->getRoleId()])) {
  786. if (null !== ($result = $this->_roleDFSVisitAllPrivileges($role, $resource, $dfs))) {
  787. return $result;
  788. }
  789. }
  790. }
  791. return null;
  792. }
  793. /**
  794. * Visits an $role in order to look for a rule allowing/denying $role access to all privileges upon $resource
  795. *
  796. * This method returns true if a rule is found and allows access. If a rule exists and denies access,
  797. * then this method returns false. If no applicable rule is found, then this method returns null.
  798. *
  799. * This method is used by the internal depth-first search algorithm and may modify the DFS data structure.
  800. *
  801. * @param Zend_Acl_Role_Interface $role
  802. * @param Zend_Acl_Resource_Interface $resource
  803. * @param array $dfs
  804. * @return boolean|null
  805. * @throws Zend_Acl_Exception
  806. */
  807. protected function _roleDFSVisitAllPrivileges(Zend_Acl_Role_Interface $role, Zend_Acl_Resource_Interface $resource = null,
  808. &$dfs = null)
  809. {
  810. if (null === $dfs) {
  811. /**
  812. * @see Zend_Acl_Exception
  813. */
  814. require_once 'Zend/Acl/Exception.php';
  815. throw new Zend_Acl_Exception('$dfs parameter may not be null');
  816. }
  817. if (null !== ($rules = $this->_getRules($resource, $role))) {
  818. foreach ($rules['byPrivilegeId'] as $privilege => $rule) {
  819. if (self::TYPE_DENY === ($ruleTypeOnePrivilege = $this->_getRuleType($resource, $role, $privilege))) {
  820. return false;
  821. }
  822. }
  823. if (null !== ($ruleTypeAllPrivileges = $this->_getRuleType($resource, $role, null))) {
  824. return self::TYPE_ALLOW === $ruleTypeAllPrivileges;
  825. }
  826. }
  827. $dfs['visited'][$role->getRoleId()] = true;
  828. foreach ($this->_getRoleRegistry()->getParents($role) as $roleParentId => $roleParent) {
  829. $dfs['stack'][] = $roleParent;
  830. }
  831. return null;
  832. }
  833. /**
  834. * Performs a depth-first search of the Role DAG, starting at $role, in order to find a rule
  835. * allowing/denying $role access to a $privilege upon $resource
  836. *
  837. * This method returns true if a rule is found and allows access. If a rule exists and denies access,
  838. * then this method returns false. If no applicable rule is found, then this method returns null.
  839. *
  840. * @param Zend_Acl_Role_Interface $role
  841. * @param Zend_Acl_Resource_Interface $resource
  842. * @param string $privilege
  843. * @return boolean|null
  844. * @throws Zend_Acl_Exception
  845. */
  846. protected function _roleDFSOnePrivilege(Zend_Acl_Role_Interface $role, Zend_Acl_Resource_Interface $resource = null,
  847. $privilege = null)
  848. {
  849. if (null === $privilege) {
  850. /**
  851. * @see Zend_Acl_Exception
  852. */
  853. require_once 'Zend/Acl/Exception.php';
  854. throw new Zend_Acl_Exception('$privilege parameter may not be null');
  855. }
  856. $dfs = array(
  857. 'visited' => array(),
  858. 'stack' => array()
  859. );
  860. if (null !== ($result = $this->_roleDFSVisitOnePrivilege($role, $resource, $privilege, $dfs))) {
  861. return $result;
  862. }
  863. while (null !== ($role = array_pop($dfs['stack']))) {
  864. if (!isset($dfs['visited'][$role->getRoleId()])) {
  865. if (null !== ($result = $this->_roleDFSVisitOnePrivilege($role, $resource, $privilege, $dfs))) {
  866. return $result;
  867. }
  868. }
  869. }
  870. return null;
  871. }
  872. /**
  873. * Visits an $role in order to look for a rule allowing/denying $role access to a $privilege upon $resource
  874. *
  875. * This method returns true if a rule is found and allows access. If a rule exists and denies access,
  876. * then this method returns false. If no applicable rule is found, then this method returns null.
  877. *
  878. * This method is used by the internal depth-first search algorithm and may modify the DFS data structure.
  879. *
  880. * @param Zend_Acl_Role_Interface $role
  881. * @param Zend_Acl_Resource_Interface $resource
  882. * @param string $privilege
  883. * @param array $dfs
  884. * @return boolean|null
  885. * @throws Zend_Acl_Exception
  886. */
  887. protected function _roleDFSVisitOnePrivilege(Zend_Acl_Role_Interface $role, Zend_Acl_Resource_Interface $resource = null,
  888. $privilege = null, &$dfs = null)
  889. {
  890. if (null === $privilege) {
  891. /**
  892. * @see Zend_Acl_Exception
  893. */
  894. require_once 'Zend/Acl/Exception.php';
  895. throw new Zend_Acl_Exception('$privilege parameter may not be null');
  896. }
  897. if (null === $dfs) {
  898. /**
  899. * @see Zend_Acl_Exception
  900. */
  901. require_once 'Zend/Acl/Exception.php';
  902. throw new Zend_Acl_Exception('$dfs parameter may not be null');
  903. }
  904. if (null !== ($ruleTypeOnePrivilege = $this->_getRuleType($resource, $role, $privilege))) {
  905. return self::TYPE_ALLOW === $ruleTypeOnePrivilege;
  906. } else if (null !== ($ruleTypeAllPrivileges = $this->_getRuleType($resource, $role, null))) {
  907. return self::TYPE_ALLOW === $ruleTypeAllPrivileges;
  908. }
  909. $dfs['visited'][$role->getRoleId()] = true;
  910. foreach ($this->_getRoleRegistry()->getParents($role) as $roleParentId => $roleParent) {
  911. $dfs['stack'][] = $roleParent;
  912. }
  913. return null;
  914. }
  915. /**
  916. * Returns the rule type associated with the specified Resource, Role, and privilege
  917. * combination.
  918. *
  919. * If a rule does not exist or its attached assertion fails, which means that
  920. * the rule is not applicable, then this method returns null. Otherwise, the
  921. * rule type applies and is returned as either TYPE_ALLOW or TYPE_DENY.
  922. *
  923. * If $resource or $role is null, then this means that the rule must apply to
  924. * all Resources or Roles, respectively.
  925. *
  926. * If $privilege is null, then the rule must apply to all privileges.
  927. *
  928. * If all three parameters are null, then the default ACL rule type is returned,
  929. * based on whether its assertion method passes.
  930. *
  931. * @param Zend_Acl_Resource_Interface $resource
  932. * @param Zend_Acl_Role_Interface $role
  933. * @param string $privilege
  934. * @return string|null
  935. */
  936. protected function _getRuleType(Zend_Acl_Resource_Interface $resource = null, Zend_Acl_Role_Interface $role = null,
  937. $privilege = null)
  938. {
  939. // get the rules for the $resource and $role
  940. if (null === ($rules = $this->_getRules($resource, $role))) {
  941. return null;
  942. }
  943. // follow $privilege
  944. if (null === $privilege) {
  945. if (isset($rules['allPrivileges'])) {
  946. $rule = $rules['allPrivileges'];
  947. } else {
  948. return null;
  949. }
  950. } else if (!isset($rules['byPrivilegeId'][$privilege])) {
  951. return null;
  952. } else {
  953. $rule = $rules['byPrivilegeId'][$privilege];
  954. }
  955. // check assertion first
  956. if ($rule['assert']) {
  957. $assertion = $rule['assert'];
  958. $assertionValue = $assertion->assert(
  959. $this,
  960. ($this->_isAllowedRole instanceof Zend_Acl_Role_Interface) ? $this->_isAllowedRole : $role,
  961. ($this->_isAllowedResource instanceof Zend_Acl_Resource_Interface) ? $this->_isAllowedResource : $resource,
  962. $this->_isAllowedPrivilege
  963. );
  964. }
  965. if (null === $rule['assert'] || $assertionValue) {
  966. return $rule['type'];
  967. } else if (null !== $resource || null !== $role || null !== $privilege) {
  968. return null;
  969. } else if (self::TYPE_ALLOW === $rule['type']) {
  970. return self::TYPE_DENY;
  971. } else {
  972. return self::TYPE_ALLOW;
  973. }
  974. }
  975. /**
  976. * Returns the rules associated with a Resource and a Role, or null if no such rules exist
  977. *
  978. * If either $resource or $role is null, this means that the rules returned are for all Resources or all Roles,
  979. * respectively. Both can be null to return the default rule set for all Resources and all Roles.
  980. *
  981. * If the $create parameter is true, then a rule set is first created and then returned to the caller.
  982. *
  983. * @param Zend_Acl_Resource_Interface $resource
  984. * @param Zend_Acl_Role_Interface $role
  985. * @param boolean $create
  986. * @return array|null
  987. */
  988. protected function &_getRules(Zend_Acl_Resource_Interface $resource = null, Zend_Acl_Role_Interface $role = null,
  989. $create = false)
  990. {
  991. // create a reference to null
  992. $null = null;
  993. $nullRef =& $null;
  994. // follow $resource
  995. do {
  996. if (null === $resource) {
  997. $visitor =& $this->_rules['allResources'];
  998. break;
  999. }
  1000. $resourceId = $resource->getResourceId();
  1001. if (!isset($this->_rules['byResourceId'][$resourceId])) {
  1002. if (!$create) {
  1003. return $nullRef;
  1004. }
  1005. $this->_rules['byResourceId'][$resourceId] = array();
  1006. }
  1007. $visitor =& $this->_rules['byResourceId'][$resourceId];
  1008. } while (false);
  1009. // follow $role
  1010. if (null === $role) {
  1011. if (!isset($visitor['allRoles'])) {
  1012. if (!$create) {
  1013. return $nullRef;
  1014. }
  1015. $visitor['allRoles']['byPrivilegeId'] = array();
  1016. }
  1017. return $visitor['allRoles'];
  1018. }
  1019. $roleId = $role->getRoleId();
  1020. if (!isset($visitor['byRoleId'][$roleId])) {
  1021. if (!$create) {
  1022. return $nullRef;
  1023. }
  1024. $visitor['byRoleId'][$roleId]['byPrivilegeId'] = array();
  1025. }
  1026. return $visitor['byRoleId'][$roleId];
  1027. }
  1028. /**
  1029. * @return array of registered roles (Deprecated)
  1030. * @deprecated Deprecated since version 1.10 (December 2009)
  1031. */
  1032. public function getRegisteredRoles()
  1033. {
  1034. trigger_error('The method getRegisteredRoles() was deprecated as of '
  1035. . 'version 1.0, and may be removed. You\'re encouraged '
  1036. . 'to use getRoles() instead.');
  1037. return $this->_getRoleRegistry()->getRoles();
  1038. }
  1039. /**
  1040. * @return array of registered roles
  1041. */
  1042. public function getRoles()
  1043. {
  1044. return array_keys($this->_getRoleRegistry()->getRoles());
  1045. }
  1046. /**
  1047. * @return array of registered resources
  1048. */
  1049. public function getResources()
  1050. {
  1051. return array_keys($this->_resources);
  1052. }
  1053. }