File tree Expand file tree Collapse file tree 4 files changed +93
-3
lines changed
src/Http/Controllers/Actions Expand file tree Collapse file tree 4 files changed +93
-3
lines changed Original file line number Diff line number Diff line change 1212namespace LaravelJsonApi \Laravel \Http \Controllers \Actions ;
1313
1414use Illuminate \Auth \Access \AuthorizationException ;
15+ use Illuminate \Auth \Access \Response as AuthResponse ;
1516use Illuminate \Auth \AuthenticationException ;
1617use Illuminate \Contracts \Support \Responsable ;
1718use Illuminate \Http \Response ;
@@ -63,13 +64,24 @@ public function destroy(Route $route, StoreContract $store)
6364 * So we need to trigger authorization in this case.
6465 */
6566 if (!$ request ) {
66- $ check = $ route ->authorizer ()->destroy (
67+ $ result = $ route ->authorizer ()->destroy (
6768 $ request = \request (),
6869 $ model ,
6970 );
7071
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 ());
7385 }
7486
7587 $ 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 88 */
99
1010use LaravelJsonApi \Laravel \Facades \JsonApiRoute ;
11+ use LaravelJsonApi \Laravel \Http \Controllers \JsonApiController ;
1112
1213JsonApiRoute::server ('v1 ' )
1314 ->prefix ('v1 ' )
3536 $ server ->resource ('videos ' )->relationships (function ($ relationships ) {
3637 $ relationships ->hasMany ('tags ' );
3738 });
39+
40+ $ server ->resource ('tags ' , '\\' . JsonApiController::class)->only ('destroy ' );
3841 });
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