|
13 | 13 | use OpenFGA\Exceptions\ClientThrowable; |
14 | 14 | use OpenFGA\Laravel\Contracts\{AuthorizableUser, AuthorizationObject, AuthorizationType, ManagerInterface, OpenFgaGateInterface}; |
15 | 15 | use Override; |
16 | | -use UnitEnum; |
17 | 16 |
|
18 | 17 | use function is_array; |
19 | 18 | use function is_object; |
|
30 | 29 | * arguments provided, making migration from Laravel's default authorization smooth. |
31 | 30 | * |
32 | 31 | * @template TUser of Authenticatable&Model |
| 32 | + * |
33 | 33 | * @extends Gate<object> |
34 | 34 | */ |
35 | 35 | final class OpenFgaGate extends Gate implements OpenFgaGateInterface |
@@ -67,11 +67,15 @@ public function check($abilities, $arguments = []) |
67 | 67 | $argumentsArray = is_array($arguments) ? $arguments : [$arguments]; |
68 | 68 |
|
69 | 69 | // Handle single string ability first to avoid iterable/string confusion |
70 | | - if (is_string($abilities)) { |
71 | | - // For string abilities, check if this is an OpenFGA permission check |
72 | | - if ($this->isOpenFgaPermission($argumentsArray)) { |
73 | | - return $this->checkOpenFgaPermission($abilities, $argumentsArray, null); |
74 | | - } |
| 70 | + if (! is_string($abilities)) { |
| 71 | + // Fall back to Laravel's default behavior |
| 72 | + // Note: Laravel's Gate::check() is designed to handle varied argument types internally |
| 73 | + return parent::check($abilities, $arguments); |
| 74 | + } |
| 75 | + |
| 76 | + // For string abilities, check if this is an OpenFGA permission check |
| 77 | + if ($this->isOpenFgaPermission($argumentsArray)) { |
| 78 | + return $this->checkOpenFgaPermission($abilities, $argumentsArray, null); |
75 | 79 | } |
76 | 80 |
|
77 | 81 | // Fall back to Laravel's default behavior |
@@ -112,6 +116,22 @@ public function checkOpenFgaPermission(string $ability, mixed $arguments, ?Authe |
112 | 116 | return $this->manager->check($userId, $ability, $object); |
113 | 117 | } |
114 | 118 |
|
| 119 | + /** |
| 120 | + * Get a gate instance for the given user. |
| 121 | + * |
| 122 | + * @param Authenticatable|mixed $user |
| 123 | + * @return static |
| 124 | + */ |
| 125 | + #[Override] |
| 126 | + public function forUser($user) |
| 127 | + { |
| 128 | + return new static( |
| 129 | + $this->manager, |
| 130 | + $this->container, |
| 131 | + static fn (): ?Authenticatable => $user instanceof Authenticatable ? $user : null, |
| 132 | + ); |
| 133 | + } |
| 134 | + |
115 | 135 | /** |
116 | 136 | * Determine if arguments represent an OpenFGA permission check. |
117 | 137 | * |
|
0 commit comments