Skip to content

Commit

Permalink
[10.x] Expand Gate::allows & Gate::denies signature (#49503)
Browse files Browse the repository at this point in the history
* [10.x] Expand Gate::allows & Gate::denies signature

* [10.x] Expand Gate::allows & Gate::denies signature

* [10.x] Expand Gate::allows & Gate::denies signature

---------

Co-authored-by: Anton Komarev <[email protected]>
  • Loading branch information
antonkomarev and Anton Komarev authored Dec 28, 2023
1 parent bd9b41e commit 6cfe010
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 14 deletions.
16 changes: 8 additions & 8 deletions src/Illuminate/Auth/Access/Gate.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,27 +318,27 @@ public function after(callable $callback)
}

/**
* 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 allows($ability, $arguments = [])
public function allows($abilities, $arguments = [])
{
return $this->check($ability, $arguments);
return $this->check($abilities, $arguments);
}

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

/**
Expand Down
12 changes: 6 additions & 6 deletions src/Illuminate/Contracts/Auth/Access/Gate.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,22 @@ public function before(callable $callback);
public function after(callable $callback);

/**
* 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 allows($ability, $arguments = []);
public function allows($abilities, $arguments = []);

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

/**
* Determine if all of the given abilities should be granted for the current user.
Expand Down
41 changes: 41 additions & 0 deletions tests/Auth/AuthAccessGateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,47 @@ public function testAfterCallbacksDoNotOverrideEachOther()
$this->assertTrue($gate->denies('deny'));
}

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

$gate->define('allow_1', function ($user) {
return true;
});
$gate->define('allow_2', function ($user) {
return true;
});
$gate->define('deny', function ($user) {
return false;
});

$this->assertTrue($gate->allows(['allow_1']));
$this->assertTrue($gate->allows(['allow_1', 'allow_2']));
$this->assertFalse($gate->allows(['allow_1', 'allow_2', 'deny']));
$this->assertFalse($gate->allows(['deny', 'allow_1', 'allow_2']));
}

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

$gate->define('deny_1', function ($user) {
return false;
});
$gate->define('deny_2', function ($user) {
return false;
});
$gate->define('allow', function ($user) {
return true;
});

$this->assertTrue($gate->denies(['deny_1']));
$this->assertTrue($gate->denies(['deny_1', 'deny_2']));
$this->assertTrue($gate->denies(['deny_1', 'allow']));
$this->assertTrue($gate->denies(['allow', 'deny_1']));
$this->assertFalse($gate->denies(['allow']));
}

public function testCurrentUserThatIsOnGateAlwaysInjectedIntoClosureCallbacks()
{
$gate = $this->getBasicGate();
Expand Down

0 comments on commit 6cfe010

Please sign in to comment.