Skip to content

Commit 26f46bd

Browse files
author
Gregory Haddow
committed
feat: support Illuminate\Auth\Access\Response from authorizer
1 parent 2f0c672 commit 26f46bd

File tree

7 files changed

+66
-8
lines changed

7 files changed

+66
-8
lines changed

src/Http/Requests/FormRequest.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace LaravelJsonApi\Laravel\Http\Requests;
1313

14+
use Illuminate\Auth\Access\Response;
1415
use Illuminate\Auth\AuthenticationException;
1516
use Illuminate\Contracts\Auth\Guard;
1617
use Illuminate\Foundation\Http\FormRequest as BaseFormRequest;
@@ -233,8 +234,9 @@ protected function passesAuthorization()
233234
* the default authorization to run.
234235
*/
235236
if (method_exists($this, 'authorize')) {
236-
if (is_bool($passes = $this->container->call([$this, 'authorize']))) {
237-
return $passes;
237+
$result = $this->container->call([$this, 'authorize']);
238+
if ($result !== null) {
239+
return $result instanceof Response ? $result->authorize() : $result;
238240
}
239241
}
240242

@@ -245,7 +247,8 @@ protected function passesAuthorization()
245247
* `mustAuthorize()` method).
246248
*/
247249
if (method_exists($this, 'authorizeResource')) {
248-
return $this->container->call([$this, 'authorizeResource']);
250+
$result = $this->container->call([$this, 'authorizeResource']);
251+
return $result instanceof Response ? $result->authorize() : $result;
249252
}
250253

251254
return true;

src/Http/Requests/ResourceQuery.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace LaravelJsonApi\Laravel\Http\Requests;
1313

14+
use Illuminate\Auth\Access\Response;
1415
use Illuminate\Contracts\Validation\Validator;
1516
use Illuminate\Database\Eloquent\Model;
1617
use LaravelJsonApi\Contracts\Auth\Authorizer;
@@ -104,9 +105,9 @@ public static function queryOne(string $resourceType): QueryParameters
104105
* Perform resource authorization.
105106
*
106107
* @param Authorizer $authorizer
107-
* @return bool
108+
* @return bool|Response
108109
*/
109-
public function authorizeResource(Authorizer $authorizer): bool
110+
public function authorizeResource(Authorizer $authorizer): bool|Response
110111
{
111112
if ($this->isViewingAny()) {
112113
return $authorizer->index(

src/Http/Requests/ResourceRequest.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace LaravelJsonApi\Laravel\Http\Requests;
1313

14+
use Illuminate\Auth\Access\Response;
1415
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
1516
use Illuminate\Contracts\Validation\Validator;
1617
use Illuminate\Database\Eloquent\Model;
@@ -150,9 +151,9 @@ public function toMany(): Collection
150151
* Perform resource authorization.
151152
*
152153
* @param Authorizer $authorizer
153-
* @return bool
154+
* @return bool|Response
154155
*/
155-
public function authorizeResource(Authorizer $authorizer): bool
156+
public function authorizeResource(Authorizer $authorizer): bool|Response
156157
{
157158
if ($this->isCreating()) {
158159
return $authorizer->store(

tests/dummy/app/Http/Controllers/Api/V1/UserController.php

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
class UserController extends Controller
2020
{
2121
use Actions\FetchOne;
22+
use Actions\Destroy;
2223
use Actions\FetchRelated;
2324
use Actions\FetchRelationship;
2425
use Actions\UpdateRelationship;

tests/dummy/app/Policies/UserPolicy.php

+14
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace App\Policies;
1313

1414
use App\Models\User;
15+
use Illuminate\Auth\Access\Response;
1516

1617
class UserPolicy
1718
{
@@ -50,4 +51,17 @@ public function updatePhone(User $user, User $other): bool
5051
{
5152
return $user->is($other);
5253
}
54+
55+
/**
56+
* Determine if the user can delete the other user.
57+
*
58+
* @param User $user
59+
* @param User $other
60+
* @return bool|Response
61+
*/
62+
public function delete(User $user, User $other)
63+
{
64+
return $user->is($other) ? true : Response::denyAsNotFound('not found message');
65+
}
66+
5367
}

tests/dummy/routes/api.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
});
2626

2727
/** Users */
28-
$server->resource('users')->only('show')->relationships(function ($relationships) {
28+
$server->resource('users')->only('show','destroy')->relationships(function ($relationships) {
2929
$relationships->hasOne('phone');
3030
})->actions(function ($actions) {
3131
$actions->get('me');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/*
3+
* Copyright 2024 Cloud Creativity Limited
4+
*
5+
* Use of this source code is governed by an MIT-style
6+
* license that can be found in the LICENSE file or at
7+
* https://opensource.org/licenses/MIT.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace App\Tests\Api\V1\Users;
13+
14+
use App\Models\User;
15+
use App\Tests\Api\V1\TestCase;
16+
17+
class DeleteTest extends TestCase
18+
{
19+
20+
public function test(): void
21+
{
22+
$user = User::factory()->createOne();
23+
24+
$expected = $this->serializer
25+
->user($user);
26+
$response = $this
27+
->actingAs(User::factory()->createOne())
28+
->jsonApi('users')
29+
->delete(url('/api/v1/users', $expected['id']));
30+
31+
$response->assertNotFound()
32+
->assertHasError(404, [
33+
'detail' => 'not found message',
34+
'status' => '404',
35+
'title' => 'Not Found',
36+
]);
37+
}
38+
}

0 commit comments

Comments
 (0)