diff --git a/src/Database/Concerns/HasAbilities.php b/src/Database/Concerns/HasAbilities.php index 5fe2f33..14f2e1c 100644 --- a/src/Database/Concerns/HasAbilities.php +++ b/src/Database/Concerns/HasAbilities.php @@ -53,10 +53,14 @@ public function getForbiddenAbilities() * * @param mixed $ability * @param mixed|null $model - * @return $this + * @return \Silber\Bouncer\Conductors\GivesAbility|$this */ - public function allow($ability, $model = null) + public function allow($ability = null, $model = null) { + if (is_null($ability)) { + return new GivesAbility($this); + } + (new GivesAbility($this))->to($ability, $model); return $this; @@ -67,10 +71,14 @@ public function allow($ability, $model = null) * * @param mixed $ability * @param mixed|null $model - * @return $this + * @return \Silber\Bouncer\Conductors\RemovesAbility|$this */ - public function disallow($ability, $model = null) + public function disallow($ability = null, $model = null) { + if (is_null($ability)) { + return new RemovesAbility($this); + } + (new RemovesAbility($this))->to($ability, $model); return $this; @@ -81,10 +89,14 @@ public function disallow($ability, $model = null) * * @param mixed $ability * @param mixed|null $model - * @return $this + * @return \Silber\Bouncer\Conductors\ForbidsAbility|$this */ - public function forbid($ability, $model = null) + public function forbid($ability = null, $model = null) { + if (is_null($ability)) { + return new ForbidsAbility($this); + } + (new ForbidsAbility($this))->to($ability, $model); return $this; @@ -95,10 +107,14 @@ public function forbid($ability, $model = null) * * @param mixed $ability * @param mixed|null $model - * @return $this + * @return \Silber\Bouncer\Conductors\UnforbidsAbility|$this */ - public function unforbid($ability, $model = null) + public function unforbid($ability = null, $model = null) { + if (is_null($ability)) { + return new UnforbidsAbility($this); + } + (new UnforbidsAbility($this))->to($ability, $model); return $this; diff --git a/tests/HasRolesAndAbilitiesTraitTest.php b/tests/HasRolesAndAbilitiesTraitTest.php index e1d14dd..6299c1d 100644 --- a/tests/HasRolesAndAbilitiesTraitTest.php +++ b/tests/HasRolesAndAbilitiesTraitTest.php @@ -64,6 +64,23 @@ public function test_can_give_and_remove_model_abilities() $this->assertTrue($bouncer->denies('delete', $user)); } + public function test_can_give_and_remove_ability_for_everything() + { + $bouncer = $this->bouncer($user = User::create())->dontCache(); + + $user->allow()->everything(); + + $this->assertTrue($bouncer->allows('delete')); + $this->assertTrue($bouncer->allows('delete', '*')); + $this->assertTrue($bouncer->allows('*', '*')); + + $user->disallow()->everything(); + + $this->assertTrue($bouncer->denies('delete')); + $this->assertTrue($bouncer->denies('delete', '*')); + $this->assertTrue($bouncer->denies('*', '*')); + } + public function test_can_forbid_and_unforbid_abilities() { $bouncer = $this->bouncer($user = User::create())->dontCache(); @@ -92,6 +109,20 @@ public function test_can_forbid_and_unforbid_model_abilities() $this->assertTrue($bouncer->allows('delete', $user)); } + public function test_can_forbid_and_unforbid_everything() + { + $bouncer = $this->bouncer($user = User::create())->dontCache(); + + $user->allow('delete', $user); + $user->forbid()->everything(); + + $this->assertTrue($bouncer->denies('delete', $user)); + + $user->unforbid()->everything(); + + $this->assertTrue($bouncer->allows('delete', $user)); + } + public function test_can_assign_and_retract_roles() { $bouncer = $this->bouncer($user = User::create())->dontCache();