From f84bd21051a81f1ad06194abc7c0a9e0dec2cd84 Mon Sep 17 00:00:00 2001 From: Gregory Haddow Date: Tue, 3 Dec 2024 20:29:51 +0000 Subject: [PATCH] fix: authorizer response should be honoured on destroy action when no request class for resource --- src/Http/Controllers/Actions/Destroy.php | 18 +++++-- tests/dummy/app/Policies/TagPolicy.php | 25 ++++++++++ tests/dummy/routes/api.php | 3 ++ tests/dummy/tests/Api/V1/Tags/DeleteTest.php | 50 ++++++++++++++++++++ 4 files changed, 93 insertions(+), 3 deletions(-) create mode 100644 tests/dummy/app/Policies/TagPolicy.php create mode 100644 tests/dummy/tests/Api/V1/Tags/DeleteTest.php diff --git a/src/Http/Controllers/Actions/Destroy.php b/src/Http/Controllers/Actions/Destroy.php index 8ab981b..6e85a1b 100644 --- a/src/Http/Controllers/Actions/Destroy.php +++ b/src/Http/Controllers/Actions/Destroy.php @@ -12,6 +12,7 @@ namespace LaravelJsonApi\Laravel\Http\Controllers\Actions; use Illuminate\Auth\Access\AuthorizationException; +use Illuminate\Auth\Access\Response as AuthResponse; use Illuminate\Auth\AuthenticationException; use Illuminate\Contracts\Support\Responsable; use Illuminate\Http\Response; @@ -63,13 +64,24 @@ public function destroy(Route $route, StoreContract $store) * So we need to trigger authorization in this case. */ if (!$request) { - $check = $route->authorizer()->destroy( + $result = $route->authorizer()->destroy( $request = \request(), $model, ); - throw_if(false === $check && Auth::guest(), new AuthenticationException()); - throw_if(false === $check, new AuthorizationException()); + if ($result instanceof AuthResponse) { + try { + $result->authorize(); + } catch (AuthorizationException $ex) { + if (!$ex->hasStatus()) { + throw_if(Auth::guest(), new AuthenticationException()); + } + throw $ex; + } + } + + throw_if(false === $result && Auth::guest(), new AuthenticationException()); + throw_if(false === $result, new AuthorizationException()); } $response = null; diff --git a/tests/dummy/app/Policies/TagPolicy.php b/tests/dummy/app/Policies/TagPolicy.php new file mode 100644 index 0000000..ff13681 --- /dev/null +++ b/tests/dummy/app/Policies/TagPolicy.php @@ -0,0 +1,25 @@ +prefix('v1') @@ -35,4 +36,6 @@ $server->resource('videos')->relationships(function ($relationships) { $relationships->hasMany('tags'); }); + + $server->resource('tags', '\\' . JsonApiController::class)->only('destroy'); }); diff --git a/tests/dummy/tests/Api/V1/Tags/DeleteTest.php b/tests/dummy/tests/Api/V1/Tags/DeleteTest.php new file mode 100644 index 0000000..ebb5460 --- /dev/null +++ b/tests/dummy/tests/Api/V1/Tags/DeleteTest.php @@ -0,0 +1,50 @@ +createOne(); + + $response = $this + ->actingAs(User::factory()->createOne()) + ->jsonApi('users') + ->delete(url('/api/v1/tags', $tag)); + + $response->assertNotFound()->assertErrorStatus([ + 'detail' => 'not found message', + 'status' => '404', + 'title' => 'Not Found', + ]); + } + + public function testUnauthenticated(): void + { + $tag = Tag::factory()->createOne(); + + $response = $this + ->jsonApi('users') + ->delete(url('/api/v1/tags', $tag)); + + $response->assertNotFound()->assertErrorStatus([ + 'detail' => 'not found message', + 'status' => '404', + 'title' => 'Not Found', + ]); + } +}