Permissions.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. /**
  3. * @package Grav\Framework\Acl
  4. *
  5. * @copyright Copyright (c) 2015 - 2022 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Framework\Acl;
  9. use ArrayAccess;
  10. use ArrayIterator;
  11. use Countable;
  12. use IteratorAggregate;
  13. use RecursiveIteratorIterator;
  14. use RuntimeException;
  15. use Traversable;
  16. use function count;
  17. /**
  18. * Class Permissions
  19. * @package Grav\Framework\Acl
  20. * @implements ArrayAccess<string,Action>
  21. * @implements IteratorAggregate<string,Action>
  22. */
  23. class Permissions implements ArrayAccess, Countable, IteratorAggregate
  24. {
  25. /** @var array<string,Action> */
  26. protected $instances = [];
  27. /** @var array<string,Action> */
  28. protected $actions = [];
  29. /** @var array */
  30. protected $nested = [];
  31. /** @var array */
  32. protected $types = [];
  33. /**
  34. * @return array
  35. */
  36. public function getInstances(): array
  37. {
  38. $iterator = new RecursiveActionIterator($this->actions);
  39. $recursive = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST);
  40. return iterator_to_array($recursive);
  41. }
  42. /**
  43. * @param string $name
  44. * @return bool
  45. */
  46. public function hasAction(string $name): bool
  47. {
  48. return isset($this->instances[$name]);
  49. }
  50. /**
  51. * @param string $name
  52. * @return Action|null
  53. */
  54. public function getAction(string $name): ?Action
  55. {
  56. return $this->instances[$name] ?? null;
  57. }
  58. /**
  59. * @param Action $action
  60. * @return void
  61. */
  62. public function addAction(Action $action): void
  63. {
  64. $name = $action->name;
  65. $parent = $this->getParent($name);
  66. if ($parent) {
  67. $parent->addChild($action);
  68. } else {
  69. $this->actions[$name] = $action;
  70. }
  71. $this->instances[$name] = $action;
  72. // If Action has children, add those, too.
  73. foreach ($action->getChildren() as $child) {
  74. $this->instances[$child->name] = $child;
  75. }
  76. }
  77. /**
  78. * @return array
  79. */
  80. public function getActions(): array
  81. {
  82. return $this->actions;
  83. }
  84. /**
  85. * @param Action[] $actions
  86. * @return void
  87. */
  88. public function addActions(array $actions): void
  89. {
  90. foreach ($actions as $action) {
  91. $this->addAction($action);
  92. }
  93. }
  94. /**
  95. * @param string $name
  96. * @return bool
  97. */
  98. public function hasType(string $name): bool
  99. {
  100. return isset($this->types[$name]);
  101. }
  102. /**
  103. * @param string $name
  104. * @return Action|null
  105. */
  106. public function getType(string $name): ?Action
  107. {
  108. return $this->types[$name] ?? null;
  109. }
  110. /**
  111. * @param string $name
  112. * @param array $type
  113. * @return void
  114. */
  115. public function addType(string $name, array $type): void
  116. {
  117. $this->types[$name] = $type;
  118. }
  119. /**
  120. * @return array
  121. */
  122. public function getTypes(): array
  123. {
  124. return $this->types;
  125. }
  126. /**
  127. * @param array $types
  128. * @return void
  129. */
  130. public function addTypes(array $types): void
  131. {
  132. $types = array_replace($this->types, $types);
  133. $this->types = $types;
  134. }
  135. /**
  136. * @param array|null $access
  137. * @return Access
  138. */
  139. public function getAccess(array $access = null): Access
  140. {
  141. return new Access($access ?? []);
  142. }
  143. /**
  144. * @param int|string $offset
  145. * @return bool
  146. */
  147. public function offsetExists($offset): bool
  148. {
  149. return isset($this->nested[$offset]);
  150. }
  151. /**
  152. * @param int|string $offset
  153. * @return Action|null
  154. */
  155. public function offsetGet($offset): ?Action
  156. {
  157. return $this->nested[$offset] ?? null;
  158. }
  159. /**
  160. * @param int|string $offset
  161. * @param mixed $value
  162. * @return void
  163. */
  164. public function offsetSet($offset, $value): void
  165. {
  166. throw new RuntimeException(__METHOD__ . '(): Not Supported');
  167. }
  168. /**
  169. * @param int|string $offset
  170. * @return void
  171. */
  172. public function offsetUnset($offset): void
  173. {
  174. throw new RuntimeException(__METHOD__ . '(): Not Supported');
  175. }
  176. /**
  177. * @return int
  178. */
  179. public function count(): int
  180. {
  181. return count($this->actions);
  182. }
  183. /**
  184. * @return ArrayIterator|Traversable
  185. */
  186. #[\ReturnTypeWillChange]
  187. public function getIterator()
  188. {
  189. return new ArrayIterator($this->actions);
  190. }
  191. /**
  192. * @return array
  193. */
  194. #[\ReturnTypeWillChange]
  195. public function __debugInfo()
  196. {
  197. return [
  198. 'actions' => $this->actions
  199. ];
  200. }
  201. /**
  202. * @param string $name
  203. * @return Action|null
  204. */
  205. protected function getParent(string $name): ?Action
  206. {
  207. if ($pos = strrpos($name, '.')) {
  208. $parentName = substr($name, 0, $pos);
  209. $parent = $this->getAction($parentName);
  210. if (!$parent) {
  211. $parent = new Action($parentName);
  212. $this->addAction($parent);
  213. }
  214. return $parent;
  215. }
  216. return null;
  217. }
  218. }