Skip to content

Commit

Permalink
[5.5] Implement iterable Gate::check() and Gate::any() (#20084)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikebronner authored and taylorotwell committed Jul 15, 2017
1 parent b106ac9 commit 2bd933a
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 11 deletions.
32 changes: 24 additions & 8 deletions src/Illuminate/Auth/Access/Gate.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,19 +221,35 @@ public function denies($ability, $arguments = [])
}

/**
* Determine if the given ability should be granted for the current user.
* Determine if all of the given abilities should be granted for the current user.
*
* @param string $ability
* @param iterable|string $abilities
* @param array|mixed $arguments
* @return bool
*/
public function check($ability, $arguments = [])
public function check($abilities, $arguments = [])
{
try {
return (bool) $this->raw($ability, $arguments);
} catch (AuthorizationException $e) {
return false;
}
return collect($abilities)->every(function ($ability) use ($arguments) {
try {
return (bool) $this->raw($ability, $arguments);
} catch (AuthorizationException $e) {
return false;
}
});
}

/**
* Determine if any one of the given abilities should be granted for the current user.
*
* @param iterable|string $abilities
* @param array|mixed $arguments
* @return bool
*/
public function any($abilities, $arguments = [])
{
return collect($abilities)->contains(function ($ability) use ($arguments) {
return $this->check($ability, $arguments);
});
}

/**
Expand Down
15 changes: 12 additions & 3 deletions src/Illuminate/Contracts/Auth/Access/Gate.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,22 @@ public function allows($ability, $arguments = []);
public function denies($ability, $arguments = []);

/**
* Determine if the given ability should be granted.
* Determine if all of the given abilities should be granted for the current user.
*
* @param string $ability
* @param iterable|string $abilities
* @param array|mixed $arguments
* @return bool
*/
public function check($abilities, $arguments = []);

/**
* Determine if any one of the given abilities should be granted for the current user.
*
* @param iterable|string $abilities
* @param array|mixed $arguments
* @return bool
*/
public function check($ability, $arguments = []);
public function any($abilities, $arguments = []);

/**
* Determine if the given ability should be granted for the current user.
Expand Down
93 changes: 93 additions & 0 deletions tests/Auth/AuthAccessGateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,60 @@ protected function getBasicGate($isAdmin = false)
return (object) ['id' => 1, 'isAdmin' => $isAdmin];
});
}

public function test_any_ability_check_passes_if_all_pass()
{
$gate = $this->getBasicGate();

$gate->policy(AccessGateTestDummy::class, AccessGateTestPolicyWithAllPermissions::class);

$this->assertTrue($gate->any(['edit', 'update'], new AccessGateTestDummy));
}

public function test_any_ability_check_passes_if_at_least_one_passes()
{
$gate = $this->getBasicGate();

$gate->policy(AccessGateTestDummy::class, AccessGateTestPolicyWithMixedPermissions::class);

$this->assertTrue($gate->any(['edit', 'update'], new AccessGateTestDummy));
}

public function test_any_ability_check_fails_if_none_pass()
{
$gate = $this->getBasicGate();

$gate->policy(AccessGateTestDummy::class, AccessGateTestPolicyWithNoPermissions::class);

$this->assertFalse($gate->any(['edit', 'update'], new AccessGateTestDummy));
}

public function test_every_ability_check_passes_if_all_pass()
{
$gate = $this->getBasicGate();

$gate->policy(AccessGateTestDummy::class, AccessGateTestPolicyWithAllPermissions::class);

$this->assertTrue($gate->check(['edit', 'update'], new AccessGateTestDummy));
}

public function test_every_ability_check_fails_if_at_least_one_fails()
{
$gate = $this->getBasicGate();

$gate->policy(AccessGateTestDummy::class, AccessGateTestPolicyWithMixedPermissions::class);

$this->assertFalse($gate->check(['edit', 'update'], new AccessGateTestDummy));
}

public function test_every_ability_check_fails_if_none_pass()
{
$gate = $this->getBasicGate();

$gate->policy(AccessGateTestDummy::class, AccessGateTestPolicyWithNoPermissions::class);

$this->assertFalse($gate->check(['edit', 'update'], new AccessGateTestDummy));
}
}

class AccessGateTestClass
Expand Down Expand Up @@ -422,3 +476,42 @@ public function bar($user)
return true;
}
}

class AccessGateTestPolicyWithMixedPermissions
{
public function edit($user, AccessGateTestDummy $dummy)
{
return false;
}

public function update($user, AccessGateTestDummy $dummy)
{
return true;
}
}

class AccessGateTestPolicyWithNoPermissions
{
public function edit($user, AccessGateTestDummy $dummy)
{
return false;
}

public function update($user, AccessGateTestDummy $dummy)
{
return false;
}
}

class AccessGateTestPolicyWithAllPermissions
{
public function edit($user, AccessGateTestDummy $dummy)
{
return true;
}

public function update($user, AccessGateTestDummy $dummy)
{
return true;
}
}

0 comments on commit 2bd933a

Please sign in to comment.