Skip to content

Commit

Permalink
Add fluent API to HasAbilities trait
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephSilber committed Jan 25, 2017
1 parent d9c9ae6 commit 42e17f5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 8 deletions.
32 changes: 24 additions & 8 deletions src/Database/Concerns/HasAbilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
31 changes: 31 additions & 0 deletions tests/HasRolesAndAbilitiesTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 42e17f5

Please sign in to comment.