Skip to content

Commit

Permalink
[11.x] Add BackedEnum support to Gate methods (#52677)
Browse files Browse the repository at this point in the history
* Add backed enum support to define

* Support backed enum in gate methods

* Add tests
  • Loading branch information
diaafares authored Sep 6, 2024
1 parent 139aba9 commit 260cbc9
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions 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

0 comments on commit 260cbc9

Please sign in to comment.