Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix: authorizer response should be honoured on destroy action when no request class for resource#302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletionssrc/Http/Controllers/Actions/Destroy.php
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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;
Expand DownExpand Up@@ -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;
Expand Down
25 changes: 25 additions & 0 deletionstests/dummy/app/Policies/TagPolicy.php
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace App\Policies;

use App\Models\Tag;
use App\Models\User;
use Illuminate\Auth\Access\Response;

class TagPolicy
{

/**
* Determine if the user can delete the tag
*
* @param ?User $user
* @param Tag $tag
* @return bool|Response
*/
public function delete(?User $user, Tag $tag)
{
return Response::denyAsNotFound('not found message');
}
}
3 changes: 3 additions & 0 deletionstests/dummy/routes/api.php
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,6 +8,7 @@
*/

use LaravelJsonApi\Laravel\Facades\JsonApiRoute;
use LaravelJsonApi\Laravel\Http\Controllers\JsonApiController;

JsonApiRoute::server('v1')
->prefix('v1')
Expand DownExpand Up@@ -35,4 +36,6 @@
$server->resource('videos')->relationships(function ($relationships) {
$relationships->hasMany('tags');
});

$server->resource('tags', '\\' . JsonApiController::class)->only('destroy');
});
50 changes: 50 additions & 0 deletionstests/dummy/tests/Api/V1/Tags/DeleteTest.php
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
<?php
/*
* Copyright 2024 Cloud Creativity Limited
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
*/

declare(strict_types=1);

namespace App\Tests\Api\V1\Tags;

use App\Models\Tag;
use App\Models\User;
use App\Tests\Api\V1\TestCase;

class DeleteTest extends TestCase
{
public function test(): void
{
$tag = Tag::factory()->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',
]);
}
}
Loading

[8]ページ先頭

©2009-2025 Movatter.jp