- Added a new taxus_has() method to check if a privilege exists

This commit is contained in:
Dave M. 2023-12-04 12:35:32 -05:00
parent 972d54bd21
commit f219cf25de
2 changed files with 33 additions and 5 deletions

View File

@ -19,6 +19,7 @@ class Extension implements \Picea\Extension\Extension
public function register(Context $context) : void public function register(Context $context) : void
{ {
$context->pushFunction("taxus", [ $this, 'taxus' ]); $context->pushFunction("taxus", [ $this, 'taxus' ]);
$context->pushFunction("taxus_has", [ $this, 'taxusHas' ]);
} }
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
@ -31,4 +32,8 @@ class Extension implements \Picea\Extension\Extension
return $this->taxus->granted($name, ...$arguments); return $this->taxus->granted($name, ...$arguments);
} }
public function taxusHas(string $name) : bool
{
return $this->taxus->hasPrivilege($name);
}
} }

View File

@ -4,14 +4,26 @@ namespace Taxus;
class Taxus class Taxus
{ {
protected PermissionGrantInterface $gate; # @ArrayOf Privilege
protected array $list = [];
public function __construct(PermissionGrantInterface $gate) # @ArrayOf PermissionGrantInterface
protected array $gates = [];
public function __construct(PermissionGrantInterface ... $gate,)
{ {
$this->gate = $gate; $this->pushGate(... $gate);
} }
protected array $list; public function pushGate(PermissionGrantInterface ... $gates) : void
{
$this->gates = array_merge($this->gates, $gates);
}
public function hasPrivilege(string $name) : bool
{
return isset($this->list[$name]);
}
public function add(...$privileges) : self public function add(...$privileges) : self
{ {
@ -44,6 +56,17 @@ class Taxus
$callback = $this->list[$name][1] ?? 'default'; $callback = $this->list[$name][1] ?? 'default';
return $this->gate->$callback(...$arguments); foreach($this->gates as $gate) {
if ( method_exists($gate, $callback) ) {
return $gate->$callback(...$arguments);
}
}
throw new \Exception(
sprintf(
"Method '$callback' could not be found from given gates list: %s",
implode(', ', array_map(fn($e) => $e::class, $this->gates))
)
);
} }
} }