Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Add BackedEnum support to Gate methods #52677

Merged
merged 3 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions src/Illuminate/Auth/Access/Gate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Auth\Access;

use BackedEnum;
use Closure;
use Exception;
use Illuminate\Auth\Access\Events\GateEvaluated;
Expand Down Expand Up @@ -191,14 +192,16 @@ protected function authorizeOnDemand($condition, $message, $code, $allowWhenResp
/**
* Define a new ability.
*
* @param string $ability
* @param \BackedEnum|string $ability
* @param callable|array|string $callback
* @return $this
*
* @throws \InvalidArgumentException
*/
public function define($ability, $callback)
{
$ability = $ability instanceof BackedEnum ? $ability->value : $ability;

if (is_array($callback) && isset($callback[0]) && is_string($callback[0])) {
$callback = $callback[0].'@'.$callback[1];
}
Expand Down Expand Up @@ -320,7 +323,7 @@ public function after(callable $callback)
/**
* Determine if all of the given abilities should be granted for the current user.
*
* @param iterable|string $ability
* @param iterable|\BackedEnum|string $ability
* @param array|mixed $arguments
* @return bool
*/
Expand All @@ -332,7 +335,7 @@ public function allows($ability, $arguments = [])
/**
* Determine if any of the given abilities should be denied for the current user.
*
* @param iterable|string $ability
* @param iterable|\BackedEnum|string $ability
* @param array|mixed $arguments
* @return bool
*/
Expand All @@ -344,7 +347,7 @@ public function denies($ability, $arguments = [])
/**
* Determine if all of the given abilities should be granted for the current user.
*
* @param iterable|string $abilities
* @param iterable|\BackedEnum|string $abilities
* @param array|mixed $arguments
* @return bool
*/
Expand All @@ -358,7 +361,7 @@ 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 iterable|\BackedEnum|string $abilities
* @param array|mixed $arguments
* @return bool
*/
Expand All @@ -370,7 +373,7 @@ public function any($abilities, $arguments = [])
/**
* Determine if all of the given abilities should be denied for the current user.
*
* @param iterable|string $abilities
* @param iterable|\BackedEnum|string $abilities
* @param array|mixed $arguments
* @return bool
*/
Expand All @@ -382,7 +385,7 @@ public function none($abilities, $arguments = [])
/**
* Determine if the given ability should be granted for the current user.
*
* @param string $ability
* @param \BackedEnum|string $ability
* @param array|mixed $arguments
* @return \Illuminate\Auth\Access\Response
*
Expand All @@ -396,12 +399,14 @@ public function authorize($ability, $arguments = [])
/**
* Inspect the user for the given ability.
*
* @param string $ability
* @param \BackedEnum|string $ability
* @param array|mixed $arguments
* @return \Illuminate\Auth\Access\Response
*/
public function inspect($ability, $arguments = [])
{
$ability = $ability instanceof BackedEnum ? $ability->value : $ability;

try {
$result = $this->raw($ability, $arguments);

Expand Down
77 changes: 75 additions & 2 deletions tests/Auth/AuthAccessGateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,40 @@ public function testAfterCallbacksDoNotOverrideEachOther()
$this->assertTrue($gate->denies('deny'));
}

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

$gate->define(AbilitiesEnum::VIEW_DASHBOARD, function ($user) {
return true;
});

$this->assertTrue($gate->allows('view-dashboard'));
}

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

$gate->define(AbilitiesEnum::VIEW_DASHBOARD, function ($user) {
return true;
});

$this->assertTrue($gate->allows(AbilitiesEnum::VIEW_DASHBOARD));
}


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

$gate->define(AbilitiesEnum::VIEW_DASHBOARD, function ($user) {
return false;
});

$this->assertTrue($gate->denies(AbilitiesEnum::VIEW_DASHBOARD));
}

public function testArrayAbilitiesInAllows()
{
$gate = $this->getBasicGate();
Expand All @@ -336,12 +370,15 @@ public function testArrayAbilitiesInAllows()
$gate->define('allow_2', function ($user) {
return true;
});
$gate->define(AbilitiesEnum::VIEW_DASHBOARD, 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->assertTrue($gate->allows(['allow_1', 'allow_2', AbilitiesEnum::VIEW_DASHBOARD]));
$this->assertFalse($gate->allows(['allow_1', 'allow_2', 'deny']));
$this->assertFalse($gate->allows(['deny', 'allow_1', 'allow_2']));
}
Expand All @@ -356,12 +393,15 @@ public function testArrayAbilitiesInDenies()
$gate->define('deny_2', function ($user) {
return false;
});
$gate->define(AbilitiesEnum::VIEW_DASHBOARD, 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', 'deny_2', AbilitiesEnum::VIEW_DASHBOARD]));
$this->assertTrue($gate->denies(['deny_1', 'allow']));
$this->assertTrue($gate->denies(['allow', 'deny_1']));
$this->assertFalse($gate->denies(['allow']));
Expand Down Expand Up @@ -1075,6 +1115,34 @@ public function testEveryAbilityCheckFailsIfNonePass()
$this->assertFalse($gate->check(['edit', 'update'], new AccessGateTestDummy));
}

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

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

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

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

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

$this->assertTrue($gate->none(['edit', AbilitiesEnum::UPDATE], new AccessGateTestDummy));
}

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

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

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


/**
* @param array $abilitiesToSet
* @param array|string $abilitiesToCheck
Expand Down Expand Up @@ -1475,3 +1543,8 @@ public function create()
throw new AuthorizationException('Not allowed.', 'some_code');
}
}

enum AbilitiesEnum: string {
case VIEW_DASHBOARD = 'view-dashboard';
case UPDATE = 'update';
}