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