Source for file Permission.php

Documentation is available at Permission.php

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