From 7913edd34b5cc823de7f57c704fd39f58a811327 Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Tue, 28 Jan 2020 12:34:10 +0200 Subject: [PATCH] Improve Acl\Actions --- system/src/Grav/Framework/Acl/Action.php | 22 +++++++ system/src/Grav/Framework/Acl/Permissions.php | 5 +- .../Framework/Acl/RecursiveActionIterator.php | 59 +++++++++++++++++++ 3 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 system/src/Grav/Framework/Acl/RecursiveActionIterator.php diff --git a/system/src/Grav/Framework/Acl/Action.php b/system/src/Grav/Framework/Acl/Action.php index 7a90456ed..9edfe6ad7 100644 --- a/system/src/Grav/Framework/Acl/Action.php +++ b/system/src/Grav/Framework/Acl/Action.php @@ -89,6 +89,28 @@ class Action implements \IteratorAggregate, \Countable $this->parent = $parent; } + public function getScope(): string + { + if (($pos = strpos($this->name, '.')) > 0) { + return substr($this->name, 0, $pos); + } + + return $this->name; + } + + public function getLevels(): int + { + return substr_count($this->name, '.'); + } + + /** + * @return bool + */ + public function hasChildren(): bool + { + return !empty($this->children); + } + /** * @return Action[] */ diff --git a/system/src/Grav/Framework/Acl/Permissions.php b/system/src/Grav/Framework/Acl/Permissions.php index 43c51f63d..b927b4eef 100644 --- a/system/src/Grav/Framework/Acl/Permissions.php +++ b/system/src/Grav/Framework/Acl/Permissions.php @@ -23,7 +23,10 @@ class Permissions implements \ArrayAccess, \Countable, \IteratorAggregate */ public function getInstances(): array { - return $this->instances; + $iterator = new RecursiveActionIterator($this->actions); + $recursive = new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::SELF_FIRST); + + return iterator_to_array($recursive); } /** diff --git a/system/src/Grav/Framework/Acl/RecursiveActionIterator.php b/system/src/Grav/Framework/Acl/RecursiveActionIterator.php new file mode 100644 index 000000000..1866a0478 --- /dev/null +++ b/system/src/Grav/Framework/Acl/RecursiveActionIterator.php @@ -0,0 +1,59 @@ +current(); + + return $current->name; + } + + /** + * @see \RecursiveIterator::hasChildren() + * @return bool + */ + public function hasChildren(): bool + { + /** @var Action $current */ + $current = $this->current(); + + return $current->hasChildren(); + } + + /** + * @see \RecursiveIterator::getChildren() + * @return RecursiveActionIterator + */ + public function getChildren(): self + { + /** @var Action $current */ + $current = $this->current(); + + return new static($current->getChildren()); + } +}