File tree 5 files changed +100
-3
lines changed
src/Http/Controllers/Actions
5 files changed +100
-3
lines changed Original file line number Diff line number Diff line change @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file. This projec
5
5
6
6
## Unreleased
7
7
8
+ ## [ 5.0.2] - 2025-12-03
9
+
10
+ ### Fixed
11
+
12
+ - [ #302 ] ( https://github.com/laravel-json-api/laravel/pull/302 ) Ensure auth response is used when deleting a resource
13
+ that does not have a resource response class.
14
+
8
15
## [ 5.0.1] - 2025-12-02
9
16
10
17
### Fixed
Original file line number Diff line number Diff line change 12
12
namespace LaravelJsonApi \Laravel \Http \Controllers \Actions ;
13
13
14
14
use Illuminate \Auth \Access \AuthorizationException ;
15
+ use Illuminate \Auth \Access \Response as AuthResponse ;
15
16
use Illuminate \Auth \AuthenticationException ;
16
17
use Illuminate \Contracts \Support \Responsable ;
17
18
use Illuminate \Http \Response ;
@@ -63,13 +64,24 @@ public function destroy(Route $route, StoreContract $store)
63
64
* So we need to trigger authorization in this case.
64
65
*/
65
66
if (!$ request ) {
66
- $ check = $ route ->authorizer ()->destroy (
67
+ $ result = $ route ->authorizer ()->destroy (
67
68
$ request = \request (),
68
69
$ model ,
69
70
);
70
71
71
- throw_if (false === $ check && Auth::guest (), new AuthenticationException ());
72
- throw_if (false === $ check , new AuthorizationException ());
72
+ if ($ result instanceof AuthResponse) {
73
+ try {
74
+ $ result ->authorize ();
75
+ } catch (AuthorizationException $ ex ) {
76
+ if (!$ ex ->hasStatus ()) {
77
+ throw_if (Auth::guest (), new AuthenticationException ());
78
+ }
79
+ throw $ ex ;
80
+ }
81
+ }
82
+
83
+ throw_if (false === $ result && Auth::guest (), new AuthenticationException ());
84
+ throw_if (false === $ result , new AuthorizationException ());
73
85
}
74
86
75
87
$ response = null ;
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace App \Policies ;
6
+
7
+ use App \Models \Tag ;
8
+ use App \Models \User ;
9
+ use Illuminate \Auth \Access \Response ;
10
+
11
+ class TagPolicy
12
+ {
13
+
14
+ /**
15
+ * Determine if the user can delete the tag
16
+ *
17
+ * @param ?User $user
18
+ * @param Tag $tag
19
+ * @return bool|Response
20
+ */
21
+ public function delete (?User $ user , Tag $ tag )
22
+ {
23
+ return Response::denyAsNotFound ('not found message ' );
24
+ }
25
+ }
Original file line number Diff line number Diff line change 8
8
*/
9
9
10
10
use LaravelJsonApi \Laravel \Facades \JsonApiRoute ;
11
+ use LaravelJsonApi \Laravel \Http \Controllers \JsonApiController ;
11
12
12
13
JsonApiRoute::server ('v1 ' )
13
14
->prefix ('v1 ' )
35
36
$ server ->resource ('videos ' )->relationships (function ($ relationships ) {
36
37
$ relationships ->hasMany ('tags ' );
37
38
});
39
+
40
+ $ server ->resource ('tags ' , '\\' . JsonApiController::class)->only ('destroy ' );
38
41
});
Original file line number Diff line number Diff line change
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 \Tags ;
13
+
14
+ use App \Models \Tag ;
15
+ use App \Models \User ;
16
+ use App \Tests \Api \V1 \TestCase ;
17
+
18
+ class DeleteTest extends TestCase
19
+ {
20
+ public function test (): void
21
+ {
22
+ $ tag = Tag::factory ()->createOne ();
23
+
24
+ $ response = $ this
25
+ ->actingAs (User::factory ()->createOne ())
26
+ ->jsonApi ('users ' )
27
+ ->delete (url ('/api/v1/tags ' , $ tag ));
28
+
29
+ $ response ->assertNotFound ()->assertErrorStatus ([
30
+ 'detail ' => 'not found message ' ,
31
+ 'status ' => '404 ' ,
32
+ 'title ' => 'Not Found ' ,
33
+ ]);
34
+ }
35
+
36
+ public function testUnauthenticated (): void
37
+ {
38
+ $ tag = Tag::factory ()->createOne ();
39
+
40
+ $ response = $ this
41
+ ->jsonApi ('users ' )
42
+ ->delete (url ('/api/v1/tags ' , $ tag ));
43
+
44
+ $ response ->assertNotFound ()->assertErrorStatus ([
45
+ 'detail ' => 'not found message ' ,
46
+ 'status ' => '404 ' ,
47
+ 'title ' => 'Not Found ' ,
48
+ ]);
49
+ }
50
+ }
You can’t perform that action at this time.
0 commit comments