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

Commit bff23d0

Browse files
feat: add support to custom guard in controller and resource (#1)
1 parent 49683b3 commit bff23d0

File tree

5 files changed

+34
-7
lines changed

5 files changed

+34
-7
lines changed

src/Http/Api/Contracts/HasParser.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ protected function filterByParent(): array
8686
$parentPolicy = Gate::getPolicyFor($routeRelation);
8787

8888
if (! is_null($parentPolicy)) {
89-
$this->authorize('view', $routeRelation);
89+
$user = auth($this->guard)->user();
90+
$this->authorizeForUser($user, 'view', $routeRelation);
9091
}
9192

9293
$filter = match (class_basename(get_class($relation))) {

src/Http/Api/Contracts/HasPolicies.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ trait HasPolicies
1313
*/
1414
protected function qualifyCollectionQuery(): void
1515
{
16-
$user = auth()->user();
16+
$user = auth($this->guard)->user();
1717
$modelPolicy = Gate::getPolicyFor($this->model());
1818

1919
if ($modelPolicy && method_exists($modelPolicy, 'qualifyCollectionQueryWithUser')) {
@@ -28,7 +28,7 @@ protected function qualifyCollectionQuery(): void
2828
*/
2929
protected function qualifyItemQuery(): void
3030
{
31-
$user = auth()->user();
31+
$user = auth($this->guard)->user();
3232
$modelPolicy = Gate::getPolicyFor($this->model());
3333

3434
if ($modelPolicy && method_exists($modelPolicy, 'qualifyItemQueryWithUser')) {
@@ -45,7 +45,7 @@ protected function qualifyItemQuery(): void
4545
*/
4646
protected function qualifyStoreQuery(array $data): array
4747
{
48-
$user = auth()->user();
48+
$user = auth($this->guard)->user();
4949
$modelPolicy = Gate::getPolicyFor($this->model());
5050

5151
if ($modelPolicy && method_exists($modelPolicy, 'qualifyStoreDataWithUser')) {
@@ -64,7 +64,7 @@ protected function qualifyStoreQuery(array $data): array
6464
*/
6565
protected function qualifyUpdateQuery(array $data): array
6666
{
67-
$user = auth()->user();
67+
$user = auth($this->guard)->user();
6868
$modelPolicy = Gate::getPolicyFor($this->model());
6969

7070
if ($modelPolicy && method_exists($modelPolicy, 'qualifyUpdateDataWithUser')) {
@@ -124,8 +124,10 @@ protected function testUserPolicyAction(string $ability, $arguments = null, bool
124124
return true;
125125
}
126126

127+
$user = auth($this->guard)->user();
128+
127129
/* @scrutinizer ignore-call */
128-
$this->authorize($ability, $model);
130+
$this->authorizeForUser($user, $ability, $model);
129131

130132
return true;
131133
}

src/Http/Api/Contracts/HasResponse.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ protected function respondWithMany($items, $code = null, $headers = [])
5858
protected function respondWithResource($resource, $data, $code = null, $headers = [])
5959
{
6060
return $resource::make($data)
61+
->setGuard($this->guard)
6162
->response()
6263
->setStatusCode($code ?? $this->getStatusCode())
6364
->withHeaders($headers);

src/Http/Api/Controller.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ abstract class Controller extends BaseController
6262
*/
6363
protected $maximumLimit = 0;
6464

65+
/**
66+
* Guard to use for authentication and authorization.
67+
* null defaults to default guard config (auth.defaults.guard)
68+
*
69+
* @var ?string
70+
*/
71+
protected $guard = null;
72+
6573
/**
6674
* Display a listing of the resource.
6775
* GET /api/{resource}.

src/Http/Resources/Contracts/AllowableFields.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ trait AllowableFields
5353
*/
5454
protected static ?array $fieldGates = null;
5555

56+
/**
57+
* Guard used to retrieve the user from the request.
58+
* null defaults to default guard config (auth.defaults.guard)
59+
*
60+
* @var ?string
61+
*/
62+
protected ?string $guard = null;
63+
5664
/**
5765
* Makes sure we only return allowable fields.
5866
*
@@ -141,6 +149,13 @@ protected function mapFields($request): array
141149
return $this->filterAllowedFields($fields);
142150
}
143151

152+
public function setGuard(string $guard): static
153+
{
154+
$this->guard = $guard;
155+
156+
return $this;
157+
}
158+
144159
public function filterAllowedFields($fields)
145160
{
146161
if (empty(static::$allowedFields) || static::$allowedFields === ['*']) {
@@ -217,7 +232,7 @@ protected function filterUserViewableFields($request): array
217232
return collect($this->mapFields($request))
218233
->when(
219234
! empty(static::$fieldGates),
220-
fn($collection) => $collection->filter(fn($field) => $this->filterUserField($field, $request->user()))
235+
fn($collection) => $collection->filter(fn($field) => $this->filterUserField($field, $request->user($this->guard)))
221236
)
222237
->toArray();
223238
}

0 commit comments

Comments
 (0)