mirror of
https://github.com/getgrav/grav.git
synced 2026-03-02 02:21:29 +01:00
Improve Acl\Actions
This commit is contained in:
@@ -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[]
|
||||
*/
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
59
system/src/Grav/Framework/Acl/RecursiveActionIterator.php
Normal file
59
system/src/Grav/Framework/Acl/RecursiveActionIterator.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Grav\Framework\Acl
|
||||
*
|
||||
* @copyright Copyright (C) 2015 - 2020 Trilby Media, LLC. All rights reserved.
|
||||
* @license MIT License; see LICENSE file for details.
|
||||
*/
|
||||
|
||||
namespace Grav\Framework\Acl;
|
||||
|
||||
use RocketTheme\Toolbox\ArrayTraits\Constructor;
|
||||
use RocketTheme\Toolbox\ArrayTraits\Countable;
|
||||
use RocketTheme\Toolbox\ArrayTraits\Iterator;
|
||||
|
||||
/**
|
||||
* Class Action
|
||||
* @package Grav\Framework\Acl
|
||||
*/
|
||||
class RecursiveActionIterator implements \RecursiveIterator, \Countable
|
||||
{
|
||||
use Constructor, Iterator, Countable;
|
||||
|
||||
/**
|
||||
* @see \Iterator::key()
|
||||
* @return string
|
||||
*/
|
||||
public function key()
|
||||
{
|
||||
/** @var Action $current */
|
||||
$current = $this->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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user