Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Diagnostics
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
      • Diagnostics
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • PhpGenerator
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
  • NetteModule
  • none

Classes

  • Identity
  • Permission
  • SimpleAuthenticator
  • User

Interfaces

  • IAuthenticator
  • IAuthorizator
  • IIdentity
  • IResource
  • IRole
  • IUserStorage

Exceptions

  • AuthenticationException
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Other releases
  • Nette homepage
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://nette.org)
  5:  * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\Security;
  9: 
 10: use Nette;
 11: 
 12: 
 13: /**
 14:  * Access control list (ACL) functionality and privileges management.
 15:  *
 16:  * This solution is mostly based on Zend_Acl (c) Zend Technologies USA Inc. (http://www.zend.com), new BSD license
 17:  *
 18:  * @copyright  Copyright (c) 2005, 2007 Zend Technologies USA Inc.
 19:  * @author     David Grudl
 20:  *
 21:  * @property-read array $roles
 22:  * @property-read array $resources
 23:  * @property-read mixed $queriedRole
 24:  * @property-read mixed $queriedResource
 25:  */
 26: class Permission extends Nette\Object implements IAuthorizator
 27: {
 28:     /** @var array  Role storage */
 29:     private $roles = array();
 30: 
 31:     /** @var array  Resource storage */
 32:     private $resources = array();
 33: 
 34:     /** @var array  Access Control List rules; whitelist (deny everything to all) by default */
 35:     private $rules = array(
 36:         'allResources' => array(
 37:             'allRoles' => array(
 38:                 'allPrivileges' => array(
 39:                     'type' => self::DENY,
 40:                     'assert' => NULL,
 41:                 ),
 42:                 'byPrivilege' => array(),
 43:             ),
 44:             'byRole' => array(),
 45:         ),
 46:         'byResource' => array(),
 47:     );
 48: 
 49:     /** @var mixed */
 50:     private $queriedRole, $queriedResource;
 51: 
 52: 
 53:     /********************* roles ****************d*g**/
 54: 
 55: 
 56:     /**
 57:      * Adds a Role to the list. The most recently added parent
 58:      * takes precedence over parents that were previously added.
 59:      * @param  string
 60:      * @param  string|array
 61:      * @throws Nette\InvalidArgumentException
 62:      * @throws Nette\InvalidStateException
 63:      * @return self
 64:      */
 65:     public function addRole($role, $parents = NULL)
 66:     {
 67:         $this->checkRole($role, FALSE);
 68:         if (isset($this->roles[$role])) {
 69:             throw new Nette\InvalidStateException("Role '$role' already exists in the list.");
 70:         }
 71: 
 72:         $roleParents = array();
 73: 
 74:         if ($parents !== NULL) {
 75:             if (!is_array($parents)) {
 76:                 $parents = array($parents);
 77:             }
 78: 
 79:             foreach ($parents as $parent) {
 80:                 $this->checkRole($parent);
 81:                 $roleParents[$parent] = TRUE;
 82:                 $this->roles[$parent]['children'][$role] = TRUE;
 83:             }
 84:         }
 85: 
 86:         $this->roles[$role] = array(
 87:             'parents'  => $roleParents,
 88:             'children' => array(),
 89:         );
 90: 
 91:         return $this;
 92:     }
 93: 
 94: 
 95:     /**
 96:      * Returns TRUE if the Role exists in the list.
 97:      * @param  string
 98:      * @return bool
 99:      */
100:     public function hasRole($role)
101:     {
102:         $this->checkRole($role, FALSE);
103:         return isset($this->roles[$role]);
104:     }
105: 
106: 
107:     /**
108:      * Checks whether Role is valid and exists in the list.
109:      * @param  string
110:      * @param  bool
111:      * @throws Nette\InvalidStateException
112:      * @return void
113:      */
114:     private function checkRole($role, $need = TRUE)
115:     {
116:         if (!is_string($role) || $role === '') {
117:             throw new Nette\InvalidArgumentException('Role must be a non-empty string.');
118: 
119:         } elseif ($need && !isset($this->roles[$role])) {
120:             throw new Nette\InvalidStateException("Role '$role' does not exist.");
121:         }
122:     }
123: 
124: 
125:     /**
126:      * Returns all Roles.
127:      * @return array
128:      */
129:     public function getRoles()
130:     {
131:         return array_keys($this->roles);
132:     }
133: 
134: 
135:     /**
136:      * Returns existing Role's parents ordered by ascending priority.
137:      * @param  string
138:      * @return array
139:      */
140:     public function getRoleParents($role)
141:     {
142:         $this->checkRole($role);
143:         return array_keys($this->roles[$role]['parents']);
144:     }
145: 
146: 
147:     /**
148:      * Returns TRUE if $role inherits from $inherit. If $onlyParents is TRUE,
149:      * then $role must inherit directly from $inherit.
150:      * @param  string
151:      * @param  string
152:      * @param  bool
153:      * @throws Nette\InvalidStateException
154:      * @return bool
155:      */
156:     public function roleInheritsFrom($role, $inherit, $onlyParents = FALSE)
157:     {
158:         $this->checkRole($role);
159:         $this->checkRole($inherit);
160: 
161:         $inherits = isset($this->roles[$role]['parents'][$inherit]);
162: 
163:         if ($inherits || $onlyParents) {
164:             return $inherits;
165:         }
166: 
167:         foreach ($this->roles[$role]['parents'] as $parent => $foo) {
168:             if ($this->roleInheritsFrom($parent, $inherit)) {
169:                 return TRUE;
170:             }
171:         }
172: 
173:         return FALSE;
174:     }
175: 
176: 
177:     /**
178:      * Removes the Role from the list.
179:      *
180:      * @param  string
181:      * @throws Nette\InvalidStateException
182:      * @return self
183:      */
184:     public function removeRole($role)
185:     {
186:         $this->checkRole($role);
187: 
188:         foreach ($this->roles[$role]['children'] as $child => $foo) {
189:             unset($this->roles[$child]['parents'][$role]);
190:         }
191: 
192:         foreach ($this->roles[$role]['parents'] as $parent => $foo) {
193:             unset($this->roles[$parent]['children'][$role]);
194:         }
195: 
196:         unset($this->roles[$role]);
197: 
198:         foreach ($this->rules['allResources']['byRole'] as $roleCurrent => $rules) {
199:             if ($role === $roleCurrent) {
200:                 unset($this->rules['allResources']['byRole'][$roleCurrent]);
201:             }
202:         }
203: 
204:         foreach ($this->rules['byResource'] as $resourceCurrent => $visitor) {
205:             if (isset($visitor['byRole'])) {
206:                 foreach ($visitor['byRole'] as $roleCurrent => $rules) {
207:                     if ($role === $roleCurrent) {
208:                         unset($this->rules['byResource'][$resourceCurrent]['byRole'][$roleCurrent]);
209:                     }
210:                 }
211:             }
212:         }
213: 
214:         return $this;
215:     }
216: 
217: 
218:     /**
219:      * Removes all Roles from the list.
220:      *
221:      * @return self
222:      */
223:     public function removeAllRoles()
224:     {
225:         $this->roles = array();
226: 
227:         foreach ($this->rules['allResources']['byRole'] as $roleCurrent => $rules) {
228:             unset($this->rules['allResources']['byRole'][$roleCurrent]);
229:         }
230: 
231:         foreach ($this->rules['byResource'] as $resourceCurrent => $visitor) {
232:             foreach ($visitor['byRole'] as $roleCurrent => $rules) {
233:                 unset($this->rules['byResource'][$resourceCurrent]['byRole'][$roleCurrent]);
234:             }
235:         }
236: 
237:         return $this;
238:     }
239: 
240: 
241:     /********************* resources ****************d*g**/
242: 
243: 
244:     /**
245:      * Adds a Resource having an identifier unique to the list.
246:      *
247:      * @param  string
248:      * @param  string
249:      * @throws Nette\InvalidArgumentException
250:      * @throws Nette\InvalidStateException
251:      * @return self
252:      */
253:     public function addResource($resource, $parent = NULL)
254:     {
255:         $this->checkResource($resource, FALSE);
256: 
257:         if (isset($this->resources[$resource])) {
258:             throw new Nette\InvalidStateException("Resource '$resource' already exists in the list.");
259:         }
260: 
261:         if ($parent !== NULL) {
262:             $this->checkResource($parent);
263:             $this->resources[$parent]['children'][$resource] = TRUE;
264:         }
265: 
266:         $this->resources[$resource] = array(
267:             'parent'   => $parent,
268:             'children' => array()
269:         );
270: 
271:         return $this;
272:     }
273: 
274: 
275:     /**
276:      * Returns TRUE if the Resource exists in the list.
277:      * @param  string
278:      * @return bool
279:      */
280:     public function hasResource($resource)
281:     {
282:         $this->checkResource($resource, FALSE);
283:         return isset($this->resources[$resource]);
284:     }
285: 
286: 
287:     /**
288:      * Checks whether Resource is valid and exists in the list.
289:      * @param  string
290:      * @param  bool
291:      * @throws Nette\InvalidStateException
292:      * @return void
293:      */
294:     private function checkResource($resource, $need = TRUE)
295:     {
296:         if (!is_string($resource) || $resource === '') {
297:             throw new Nette\InvalidArgumentException('Resource must be a non-empty string.');
298: 
299:         } elseif ($need && !isset($this->resources[$resource])) {
300:             throw new Nette\InvalidStateException("Resource '$resource' does not exist.");
301:         }
302:     }
303: 
304: 
305:     /**
306:      * Returns all Resources.
307:      * @return array
308:      */
309:     public function getResources()
310:     {
311:         return array_keys($this->resources);
312:     }
313: 
314: 
315:     /**
316:      * Returns TRUE if $resource inherits from $inherit. If $onlyParents is TRUE,
317:      * then $resource must inherit directly from $inherit.
318:      *
319:      * @param  string
320:      * @param  string
321:      * @param  bool
322:      * @throws Nette\InvalidStateException
323:      * @return bool
324:      */
325:     public function resourceInheritsFrom($resource, $inherit, $onlyParent = FALSE)
326:     {
327:         $this->checkResource($resource);
328:         $this->checkResource($inherit);
329: 
330:         if ($this->resources[$resource]['parent'] === NULL) {
331:             return FALSE;
332:         }
333: 
334:         $parent = $this->resources[$resource]['parent'];
335:         if ($inherit === $parent) {
336:             return TRUE;
337: 
338:         } elseif ($onlyParent) {
339:             return FALSE;
340:         }
341: 
342:         while ($this->resources[$parent]['parent'] !== NULL) {
343:             $parent = $this->resources[$parent]['parent'];
344:             if ($inherit === $parent) {
345:                 return TRUE;
346:             }
347:         }
348: 
349:         return FALSE;
350:     }
351: 
352: 
353:     /**
354:      * Removes a Resource and all of its children.
355:      *
356:      * @param  string
357:      * @throws Nette\InvalidStateException
358:      * @return self
359:      */
360:     public function removeResource($resource)
361:     {
362:         $this->checkResource($resource);
363: 
364:         $parent = $this->resources[$resource]['parent'];
365:         if ($parent !== NULL) {
366:             unset($this->resources[$parent]['children'][$resource]);
367:         }
368: 
369:         $removed = array($resource);
370:         foreach ($this->resources[$resource]['children'] as $child => $foo) {
371:             $this->removeResource($child);
372:             $removed[] = $child;
373:         }
374: 
375:         foreach ($removed as $resourceRemoved) {
376:             foreach ($this->rules['byResource'] as $resourceCurrent => $rules) {
377:                 if ($resourceRemoved === $resourceCurrent) {
378:                     unset($this->rules['byResource'][$resourceCurrent]);
379:                 }
380:             }
381:         }
382: 
383:         unset($this->resources[$resource]);
384:         return $this;
385:     }
386: 
387: 
388:     /**
389:      * Removes all Resources.
390:      * @return self
391:      */
392:     public function removeAllResources()
393:     {
394:         foreach ($this->resources as $resource => $foo) {
395:             foreach ($this->rules['byResource'] as $resourceCurrent => $rules) {
396:                 if ($resource === $resourceCurrent) {
397:                     unset($this->rules['byResource'][$resourceCurrent]);
398:                 }
399:             }
400:         }
401: 
402:         $this->resources = array();
403:         return $this;
404:     }
405: 
406: 
407:     /********************* defining rules ****************d*g**/
408: 
409: 
410:     /**
411:      * Allows one or more Roles access to [certain $privileges upon] the specified Resource(s).
412:      * If $assertion is provided, then it must return TRUE in order for rule to apply.
413:      *
414:      * @param  string|array|Permission::ALL  roles
415:      * @param  string|array|Permission::ALL  resources
416:      * @param  string|array|Permission::ALL  privileges
417:      * @param  callable    assertion
418:      * @return self
419:      */
420:     public function allow($roles = self::ALL, $resources = self::ALL, $privileges = self::ALL, $assertion = NULL)
421:     {
422:         $this->setRule(TRUE, self::ALLOW, $roles, $resources, $privileges, $assertion);
423:         return $this;
424:     }
425: 
426: 
427:     /**
428:      * Denies one or more Roles access to [certain $privileges upon] the specified Resource(s).
429:      * If $assertion is provided, then it must return TRUE in order for rule to apply.
430:      *
431:      * @param  string|array|Permission::ALL  roles
432:      * @param  string|array|Permission::ALL  resources
433:      * @param  string|array|Permission::ALL  privileges
434:      * @param  callable    assertion
435:      * @return self
436:      */
437:     public function deny($roles = self::ALL, $resources = self::ALL, $privileges = self::ALL, $assertion = NULL)
438:     {
439:         $this->setRule(TRUE, self::DENY, $roles, $resources, $privileges, $assertion);
440:         return $this;
441:     }
442: 
443: 
444:     /**
445:      * Removes "allow" permissions from the list in the context of the given Roles, Resources, and privileges.
446:      *
447:      * @param  string|array|Permission::ALL  roles
448:      * @param  string|array|Permission::ALL  resources
449:      * @param  string|array|Permission::ALL  privileges
450:      * @return self
451:      */
452:     public function removeAllow($roles = self::ALL, $resources = self::ALL, $privileges = self::ALL)
453:     {
454:         $this->setRule(FALSE, self::ALLOW, $roles, $resources, $privileges);
455:         return $this;
456:     }
457: 
458: 
459:     /**
460:      * Removes "deny" restrictions from the list in the context of the given Roles, Resources, and privileges.
461:      *
462:      * @param  string|array|Permission::ALL  roles
463:      * @param  string|array|Permission::ALL  resources
464:      * @param  string|array|Permission::ALL  privileges
465:      * @return self
466:      */
467:     public function removeDeny($roles = self::ALL, $resources = self::ALL, $privileges = self::ALL)
468:     {
469:         $this->setRule(FALSE, self::DENY, $roles, $resources, $privileges);
470:         return $this;
471:     }
472: 
473: 
474:     /**
475:      * Performs operations on Access Control List rules.
476:      * @param  bool  operation add?
477:      * @param  bool  type
478:      * @param  string|array|Permission::ALL  roles
479:      * @param  string|array|Permission::ALL  resources
480:      * @param  string|array|Permission::ALL  privileges
481:      * @param  callable    assertion
482:      * @throws Nette\InvalidStateException
483:      * @return self
484:      */
485:     protected function setRule($toAdd, $type, $roles, $resources, $privileges, $assertion = NULL)
486:     {
487:         // ensure that all specified Roles exist; normalize input to array of Roles or NULL
488:         if ($roles === self::ALL) {
489:             $roles = array(self::ALL);
490: 
491:         } else {
492:             if (!is_array($roles)) {
493:                 $roles = array($roles);
494:             }
495: 
496:             foreach ($roles as $role) {
497:                 $this->checkRole($role);
498:             }
499:         }
500: 
501:         // ensure that all specified Resources exist; normalize input to array of Resources or NULL
502:         if ($resources === self::ALL) {
503:             $resources = array(self::ALL);
504: 
505:         } else {
506:             if (!is_array($resources)) {
507:                 $resources = array($resources);
508:             }
509: 
510:             foreach ($resources as $resource) {
511:                 $this->checkResource($resource);
512:             }
513:         }
514: 
515:         // normalize privileges to array
516:         if ($privileges === self::ALL) {
517:             $privileges = array();
518: 
519:         } elseif (!is_array($privileges)) {
520:             $privileges = array($privileges);
521:         }
522: 
523:         if ($toAdd) { // add to the rules
524:             foreach ($resources as $resource) {
525:                 foreach ($roles as $role) {
526:                     $rules = & $this->getRules($resource, $role, TRUE);
527:                     if (count($privileges) === 0) {
528:                         $rules['allPrivileges']['type'] = $type;
529:                         $rules['allPrivileges']['assert'] = $assertion;
530:                         if (!isset($rules['byPrivilege'])) {
531:                             $rules['byPrivilege'] = array();
532:                         }
533:                     } else {
534:                         foreach ($privileges as $privilege) {
535:                             $rules['byPrivilege'][$privilege]['type'] = $type;
536:                             $rules['byPrivilege'][$privilege]['assert'] = $assertion;
537:                         }
538:                     }
539:                 }
540:             }
541: 
542:         } else { // remove from the rules
543:             foreach ($resources as $resource) {
544:                 foreach ($roles as $role) {
545:                     $rules = & $this->getRules($resource, $role);
546:                     if ($rules === NULL) {
547:                         continue;
548:                     }
549:                     if (count($privileges) === 0) {
550:                         if ($resource === self::ALL && $role === self::ALL) {
551:                             if ($type === $rules['allPrivileges']['type']) {
552:                                 $rules = array(
553:                                     'allPrivileges' => array(
554:                                         'type' => self::DENY,
555:                                         'assert' => NULL
556:                                         ),
557:                                     'byPrivilege' => array()
558:                                     );
559:                             }
560:                             continue;
561:                         }
562:                         if ($type === $rules['allPrivileges']['type']) {
563:                             unset($rules['allPrivileges']);
564:                         }
565:                     } else {
566:                         foreach ($privileges as $privilege) {
567:                             if (isset($rules['byPrivilege'][$privilege]) &&
568:                                 $type === $rules['byPrivilege'][$privilege]['type']
569:                             ) {
570:                                 unset($rules['byPrivilege'][$privilege]);
571:                             }
572:                         }
573:                     }
574:                 }
575:             }
576:         }
577:         return $this;
578:     }
579: 
580: 
581:     /********************* querying the ACL ****************d*g**/
582: 
583: 
584:     /**
585:      * Returns TRUE if and only if the Role has access to [certain $privileges upon] the Resource.
586:      *
587:      * This method checks Role inheritance using a depth-first traversal of the Role list.
588:      * The highest priority parent (i.e., the parent most recently added) is checked first,
589:      * and its respective parents are checked similarly before the lower-priority parents of
590:      * the Role are checked.
591:      *
592:      * @param  string|Permission::ALL|IRole  role
593:      * @param  string|Permission::ALL|IResource  resource
594:      * @param  string|Permission::ALL  privilege
595:      * @throws Nette\InvalidStateException
596:      * @return bool
597:      */
598:     public function isAllowed($role = self::ALL, $resource = self::ALL, $privilege = self::ALL)
599:     {
600:         $this->queriedRole = $role;
601:         if ($role !== self::ALL) {
602:             if ($role instanceof IRole) {
603:                 $role = $role->getRoleId();
604:             }
605:             $this->checkRole($role);
606:         }
607: 
608:         $this->queriedResource = $resource;
609:         if ($resource !== self::ALL) {
610:             if ($resource instanceof IResource) {
611:                 $resource = $resource->getResourceId();
612:             }
613:             $this->checkResource($resource);
614:         }
615: 
616:         do {
617:             // depth-first search on $role if it is not 'allRoles' pseudo-parent
618:             if ($role !== NULL && NULL !== ($result = $this->searchRolePrivileges($privilege === self::ALL, $role, $resource, $privilege))) {
619:                 break;
620:             }
621: 
622:             if ($privilege === self::ALL) {
623:                 if ($rules = $this->getRules($resource, self::ALL)) { // look for rule on 'allRoles' psuedo-parent
624:                     foreach ($rules['byPrivilege'] as $privilege => $rule) {
625:                         if (self::DENY === ($result = $this->getRuleType($resource, NULL, $privilege))) {
626:                             break 2;
627:                         }
628:                     }
629:                     if (NULL !== ($result = $this->getRuleType($resource, NULL, NULL))) {
630:                         break;
631:                     }
632:                 }
633:             } else {
634:                 if (NULL !== ($result = $this->getRuleType($resource, NULL, $privilege))) { // look for rule on 'allRoles' pseudo-parent
635:                     break;
636: 
637:                 } elseif (NULL !== ($result = $this->getRuleType($resource, NULL, NULL))) {
638:                     break;
639:                 }
640:             }
641: 
642:             $resource = $this->resources[$resource]['parent']; // try next Resource
643:         } while (TRUE);
644: 
645:         $this->queriedRole = $this->queriedResource = NULL;
646:         return $result;
647:     }
648: 
649: 
650:     /**
651:      * Returns real currently queried Role. Use by assertion.
652:      * @return mixed
653:      */
654:     public function getQueriedRole()
655:     {
656:         return $this->queriedRole;
657:     }
658: 
659: 
660:     /**
661:      * Returns real currently queried Resource. Use by assertion.
662:      * @return mixed
663:      */
664:     public function getQueriedResource()
665:     {
666:         return $this->queriedResource;
667:     }
668: 
669: 
670:     /********************* internals ****************d*g**/
671: 
672: 
673:     /**
674:      * Performs a depth-first search of the Role DAG, starting at $role, in order to find a rule
675:      * allowing/denying $role access to a/all $privilege upon $resource.
676:      * @param  bool  all (true) or one?
677:      * @param  string
678:      * @param  string
679:      * @param  string  only for one
680:      * @return mixed  NULL if no applicable rule is found, otherwise returns ALLOW or DENY
681:      */
682:     private function searchRolePrivileges($all, $role, $resource, $privilege)
683:     {
684:         $dfs = array(
685:             'visited' => array(),
686:             'stack' => array($role),
687:         );
688: 
689:         while (NULL !== ($role = array_pop($dfs['stack']))) {
690:             if (isset($dfs['visited'][$role])) {
691:                 continue;
692:             }
693:             if ($all) {
694:                 if ($rules = $this->getRules($resource, $role)) {
695:                     foreach ($rules['byPrivilege'] as $privilege2 => $rule) {
696:                         if (self::DENY === $this->getRuleType($resource, $role, $privilege2)) {
697:                             return self::DENY;
698:                         }
699:                     }
700:                     if (NULL !== ($type = $this->getRuleType($resource, $role, NULL))) {
701:                         return $type;
702:                     }
703:                 }
704:             } else {
705:                 if (NULL !== ($type = $this->getRuleType($resource, $role, $privilege))) {
706:                     return $type;
707: 
708:                 } elseif (NULL !== ($type = $this->getRuleType($resource, $role, NULL))) {
709:                     return $type;
710:                 }
711:             }
712: 
713:             $dfs['visited'][$role] = TRUE;
714:             foreach ($this->roles[$role]['parents'] as $roleParent => $foo) {
715:                 $dfs['stack'][] = $roleParent;
716:             }
717:         }
718:         return NULL;
719:     }
720: 
721: 
722:     /**
723:      * Returns the rule type associated with the specified Resource, Role, and privilege.
724:      * @param  string|Permission::ALL
725:      * @param  string|Permission::ALL
726:      * @param  string|Permission::ALL
727:      * @return mixed  NULL if a rule does not exist or assertion fails, otherwise returns ALLOW or DENY
728:      */
729:     private function getRuleType($resource, $role, $privilege)
730:     {
731:         if (!$rules = $this->getRules($resource, $role)) {
732:             return NULL;
733:         }
734: 
735:         if ($privilege === self::ALL) {
736:             if (isset($rules['allPrivileges'])) {
737:                 $rule = $rules['allPrivileges'];
738:             } else {
739:                 return NULL;
740:             }
741:         } elseif (!isset($rules['byPrivilege'][$privilege])) {
742:             return NULL;
743: 
744:         } else {
745:             $rule = $rules['byPrivilege'][$privilege];
746:         }
747: 
748:         if ($rule['assert'] === NULL || Nette\Utils\Callback::invoke($rule['assert'], $this, $role, $resource, $privilege)) {
749:             return $rule['type'];
750: 
751:         } elseif ($resource !== self::ALL || $role !== self::ALL || $privilege !== self::ALL) {
752:             return NULL;
753: 
754:         } elseif (self::ALLOW === $rule['type']) {
755:             return self::DENY;
756: 
757:         } else {
758:             return self::ALLOW;
759:         }
760:     }
761: 
762: 
763:     /**
764:      * Returns the rules associated with a Resource and a Role, or NULL if no such rules exist.
765:      * If the $create parameter is TRUE, then a rule set is first created and then returned to the caller.
766:      * @param  string|Permission::ALL
767:      * @param  string|Permission::ALL
768:      * @param  bool
769:      * @return array|NULL
770:      */
771:     private function & getRules($resource, $role, $create = FALSE)
772:     {
773:         $null = NULL;
774:         if ($resource === self::ALL) {
775:             $visitor = & $this->rules['allResources'];
776:         } else {
777:             if (!isset($this->rules['byResource'][$resource])) {
778:                 if (!$create) {
779:                     return $null;
780:                 }
781:                 $this->rules['byResource'][$resource] = array();
782:             }
783:             $visitor = & $this->rules['byResource'][$resource];
784:         }
785: 
786:         if ($role === self::ALL) {
787:             if (!isset($visitor['allRoles'])) {
788:                 if (!$create) {
789:                     return $null;
790:                 }
791:                 $visitor['allRoles']['byPrivilege'] = array();
792:             }
793:             return $visitor['allRoles'];
794:         }
795: 
796:         if (!isset($visitor['byRole'][$role])) {
797:             if (!$create) {
798:                 return $null;
799:             }
800:             $visitor['byRole'][$role]['byPrivilege'] = array();
801:         }
802: 
803:         return $visitor['byRole'][$role];
804:     }
805: 
806: }
807: 
Nette 2.1 API documentation generated by ApiGen 2.8.0