Skip to content

Commit

Permalink
Set default to run after policies
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephSilber committed Feb 21, 2022
1 parent fcbb176 commit 74403e6
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/Bouncer.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,14 +453,14 @@ public function ability(array $attributes = [])
}

/**
* Set Bouncer to run its checks after the policies.
* Set Bouncer to run its checks before the policies.
*
* @param bool $boolean
* @return $this
*/
public function runAfterPolicies($boolean = true)
public function runBeforePolicies($boolean = true)
{
$this->guard->slot($boolean ? 'after' : 'before');
$this->guard->slot($boolean ? 'before' : 'after');

return $this;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Guard.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Guard
*
* @var string
*/
protected $slot = 'before';
protected $slot = 'after';

/**
* Create a new guard instance.
Expand Down
24 changes: 19 additions & 5 deletions tests/AfterPoliciesTest.php → tests/BeforePoliciesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
use Silber\Bouncer\Database\Role;
use Silber\Bouncer\Database\Ability;

class AfterPoliciesTest extends BaseTestCase
class BeforePoliciesTest extends BaseTestCase
{
use Concerns\TestsClipboards;

/**
* @test
* @dataProvider bouncerProvider
*/
function fails_auth_check_when_policy_fails_even_if_bouncer_allows($provider)
function policy_forbids_and_bouncer_allows($provider)
{
list($bouncer, $user) = $provider();

Expand All @@ -26,13 +26,17 @@ function fails_auth_check_when_policy_fails_even_if_bouncer_allows($provider)
$bouncer->allow($user)->to('view', $account);

$this->assertTrue($bouncer->cannot('view', $account));

$bouncer->runBeforePolicies();

$this->assertTrue($bouncer->can('view', $account));
}

/**
* @test
* @dataProvider bouncerProvider
*/
function passes_auth_check_when_policy_passes_even_if_bouncer_fails($provider)
function policy_allows_and_bouncer_forbids($provider)
{
list($bouncer, $user) = $provider();

Expand All @@ -43,6 +47,10 @@ function passes_auth_check_when_policy_passes_even_if_bouncer_fails($provider)
$bouncer->forbid($user)->to('view', $account);

$this->assertTrue($bouncer->can('view', $account));

$bouncer->runBeforePolicies();

$this->assertTrue($bouncer->cannot('view', $account));
}

/**
Expand All @@ -60,6 +68,10 @@ function passes_auth_check_when_bouncer_allows($provider)
$bouncer->allow($user)->to('view', $account);

$this->assertTrue($bouncer->can('view', $account));

$bouncer->runBeforePolicies();

$this->assertTrue($bouncer->can('view', $account));
}

/**
Expand All @@ -75,6 +87,10 @@ function fails_auth_check_when_bouncer_does_not_allow($provider)
$account = Account::create(['name' => 'ignored by policy']);

$this->assertTrue($bouncer->cannot('view', $account));

$bouncer->runBeforePolicies();

$this->assertTrue($bouncer->cannot('view', $account));
}

/**
Expand All @@ -84,8 +100,6 @@ function fails_auth_check_when_bouncer_does_not_allow($provider)
*/
protected function setUpWithPolicy(Bouncer $bouncer)
{
$bouncer->runAfterPolicies();

$bouncer->gate()->policy(Account::class, AccountPolicyForAfter::class);
}
}
Expand Down
2 changes: 2 additions & 0 deletions tests/ForbidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ function forbidding_an_ability_stops_all_further_checks($provider)

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

$bouncer->runBeforePolicies();

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

0 comments on commit 74403e6

Please sign in to comment.