commit 2845badd40320763ff9b034ac779ba0d5dde2808 Author: Dave Mc Nicoll Date: Fri Aug 27 18:08:35 2021 +0000 - First commit diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..95d2671 --- /dev/null +++ b/composer.json @@ -0,0 +1,17 @@ +{ + "name": "mcnd/taxus", + "description": "A simple privilege library to handle an application's permissions.", + "keywords": ["mcnd","privilege","permission","taxus"], + "license": "MIT", + "authors": [ + { + "name": "Dave Mc Nicoll", + "email": "dave.mcnicoll@cslsj.qc.ca" + } + ], + "autoload": { + "psr-4": { + "Taxus\\": "src/" + } + } +} diff --git a/src/PermissionGrantInterface.php b/src/PermissionGrantInterface.php new file mode 100644 index 0000000..dca8398 --- /dev/null +++ b/src/PermissionGrantInterface.php @@ -0,0 +1,8 @@ +name = $name; + $this->description = $description; + $this->function = $function; + $this->success = $success; + $this->error = $error; + } +} diff --git a/src/Taxus.php b/src/Taxus.php new file mode 100644 index 0000000..6140691 --- /dev/null +++ b/src/Taxus.php @@ -0,0 +1,50 @@ +gate = $gate; + } + + /** + * @var array + */ + protected $list; + + public function add(...$privileges) : self + { + foreach($privileges as $item) { + $item = (array) $item; + + if ( ! $item[0] instanceof Privilege ) { + throw new \InvalidArgumentException("First element must be an instance of '" . Privilege::class . "'"); + } + + if ( ( $item[1] ?? false ) && ! is_string($item[1]) ) { + throw new \InvalidArgumentException("Second element must be a callable function from your Permission Handler"); + } + + $this->list[ $item[0]->name ] = $item; + } + + return $this; + } + + public function granted($name, ...$arguments) : bool + { + if ( ! isset($this->list[$name]) ) { + throw new \InvalidArgumentException("Privilege '{$name}' could not be found. Did you forgot to add it to your Taxus object ?"); + } + + $obj = $this->list[$name][0]; + + $callback = $this->list[$name][1] ?? 'default'; + + return $this->gate->$callback(...$arguments); + } +} diff --git a/src/UserEntityTrait.php b/src/UserEntityTrait.php new file mode 100644 index 0000000..f49bf84 --- /dev/null +++ b/src/UserEntityTrait.php @@ -0,0 +1,22 @@ +taxus) { + throw new \Exception("An instance of Taxus must be linked to this object."); + } + + return $this->taxus->granted($privilegeName, ...$arguments); + } + + public function cannot(string $privilegeName, ...$arguments) : bool + { + return ! $this->can($privilegeName, ...$arguments); + } +} \ No newline at end of file