From d1b11870117deb0579c92868752444d7ded48d30 Mon Sep 17 00:00:00 2001 From: Joseph Silber Date: Tue, 12 Jul 2016 22:19:15 -0400 Subject: [PATCH] Add can, cant and cannot methods to the Role model --- src/Database/Authorizable.php | 57 +++++++++++++++++++++++++++++++++++ src/Database/Role.php | 4 ++- tests/AuthorizableTest.php | 17 +++++++++++ 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 src/Database/Authorizable.php create mode 100644 tests/AuthorizableTest.php diff --git a/src/Database/Authorizable.php b/src/Database/Authorizable.php new file mode 100644 index 0000000..6ce8cdb --- /dev/null +++ b/src/Database/Authorizable.php @@ -0,0 +1,57 @@ +getClipboardInstance()->check($this, $ability, $model); + } + + /** + * Determine if the authority does not have a given ability. + * + * @param string $ability + * @param \Illuminate\Database\Eloquent\Model|null $model + * @return bool + */ + public function cant($ability, $model = null) + { + return ! $this->can($ability, $model); + } + + /** + * Determine if the authority does not have a given ability. + * + * @param string $ability + * @param \Illuminate\Database\Eloquent\Model|null $model + * @return bool + */ + public function cannot($ability, $model = null) + { + return $this->cant($ability, $model); + } + + /** + * Get an instance of the bouncer's clipboard. + * + * @return \Silber\Bouncer\Clipboard + */ + protected function getClipboardInstance() + { + $container = Container::getInstance() ?: new Container; + + return $container->make(Clipboard::class); + } +} diff --git a/src/Database/Role.php b/src/Database/Role.php index 2a33d1a..6a66aaf 100644 --- a/src/Database/Role.php +++ b/src/Database/Role.php @@ -10,7 +10,9 @@ class Role extends Model { - use HasAbilities; + use HasAbilities, Authorizable { + HasAbilities::getClipboardInstance insteadof Authorizable; + } /** * The attributes that are mass assignable. diff --git a/tests/AuthorizableTest.php b/tests/AuthorizableTest.php new file mode 100644 index 0000000..fd84047 --- /dev/null +++ b/tests/AuthorizableTest.php @@ -0,0 +1,17 @@ + 'admin']); + + $role->allow('scream'); + + $this->assertTrue($role->can('scream')); + $this->assertTrue($role->cant('shout')); + $this->assertTrue($role->cannot('cry')); + } +}