Skip to content

Commit

Permalink
Break gate chain for forbidden abilities
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephSilber committed Dec 12, 2016
1 parent a1b7137 commit 4ccc7a2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/Clipboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public function registerAt(Gate $gate)
if ($id = $this->checkGetId($authority, $ability, $model)) {
return $this->allow('Bouncer granted permission via ability #'.$id);
}

// If the response from "checkGetId" is "false", then this ability
// has been explicity forbidden. We'll return false so the gate
// doesn't run any further checks. Otherwise we return null.
return $id;
});
}

Expand Down Expand Up @@ -81,7 +86,7 @@ public function check(Model $authority, $ability, $model = null)
* @param \Illuminate\Database\Eloquent\Model $authority
* @param string $ability
* @param \Illuminate\Database\Eloquent\Model|string|null $model
* @return int|bool
* @return int|bool|null
*/
protected function checkGetId(Model $authority, $ability, $model = null)
{
Expand All @@ -98,11 +103,9 @@ protected function checkGetId(Model $authority, $ability, $model = null)
return false;
}

$allowedId = $this->findMatchingAbility(
return $this->findMatchingAbility(
$this->getAbilities($authority), $applicable, $model, $authority
);

return $allowedId ?: false;
}

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/ForbidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,19 @@ public function test_forbidding_an_ability_when_everything_is_allowed()
$this->assertTrue($bouncer->allows('create', Account::class));
$this->assertTrue($bouncer->denies('create', User::class));
}

public function test_forbidding_an_ability_stops_all_further_checks()
{
$bouncer = $this->bouncer($user = User::create())->dontCache();

$bouncer->define('sleep', function () {
return true;
});

$this->assertTrue($bouncer->allows('sleep'));

$bouncer->forbid($user)->to('sleep');

$this->assertTrue($bouncer->denies('sleep'));
}
}

0 comments on commit 4ccc7a2

Please sign in to comment.