Skip to content
This repository was archived by the owner on Nov 7, 2025. It is now read-only.

Commit 797c900

Browse files
committed
ci: fix unit tests
1 parent c98cc54 commit 797c900

2 files changed

Lines changed: 29 additions & 8 deletions

File tree

src/Authorization/OpenFgaGate.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use OpenFGA\Exceptions\ClientThrowable;
1414
use OpenFGA\Laravel\Contracts\{AuthorizableUser, AuthorizationObject, AuthorizationType, ManagerInterface, OpenFgaGateInterface};
1515
use Override;
16-
use UnitEnum;
1716

1817
use function is_array;
1918
use function is_object;
@@ -30,6 +29,7 @@
3029
* arguments provided, making migration from Laravel's default authorization smooth.
3130
*
3231
* @template TUser of Authenticatable&Model
32+
*
3333
* @extends Gate<object>
3434
*/
3535
final class OpenFgaGate extends Gate implements OpenFgaGateInterface
@@ -67,11 +67,15 @@ public function check($abilities, $arguments = [])
6767
$argumentsArray = is_array($arguments) ? $arguments : [$arguments];
6868

6969
// 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);
7579
}
7680

7781
// Fall back to Laravel's default behavior
@@ -112,6 +116,22 @@ public function checkOpenFgaPermission(string $ability, mixed $arguments, ?Authe
112116
return $this->manager->check($userId, $ability, $object);
113117
}
114118

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+
115135
/**
116136
* Determine if arguments represent an OpenFGA permission check.
117137
*

tests/Unit/Authorization/OpenFgaGateTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ enum OpenFgaGateTest
165165
);
166166
});
167167

168-
it('handles custom user parameter', function (): void {
168+
it('handles custom user via forUser method', function (): void {
169169
$customUser = TestFactories::createTestUser(
170170
authId: TestConstants::ALTERNATIVE_USER_ID,
171171
identifier: 999,
@@ -177,7 +177,8 @@ enum OpenFgaGateTest
177177
->with('user:999', 'admin', 'system:1')
178178
->andReturn(true);
179179

180-
$result = $this->gate->check('admin', 'system:1', $customUser);
180+
// Use Laravel's standard way to check permissions for a different user
181+
$result = $this->gate->forUser($customUser)->check('admin', 'system:1');
181182
expect($result)->toBeTrue();
182183
});
183184
});

0 commit comments

Comments
 (0)