Skip to content

Commit

Permalink
fix: authorizer response with status should be honoured when unauthen…
Browse files Browse the repository at this point in the history
…ticated
  • Loading branch information
Gregory Haddow committed Dec 2, 2024
1 parent 8a2db50 commit 62038dd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/Http/Requests/FormRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,9 @@ protected function passesAuthorization()
}

} catch (AuthorizationException $ex) {
$this->failIfUnauthenticated();
if (!$ex->hasStatus() || $ex->hasStatus() && $ex->status() === 403) {
$this->failIfUnauthenticated();
}
throw $ex;
}
return true;
Expand Down
6 changes: 3 additions & 3 deletions tests/dummy/app/Policies/UserPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ public function updatePhone(User $user, User $other): bool
/**
* Determine if the user can delete the other user.
*
* @param User $user
* @param ?User $user
* @param User $other
* @return bool|Response
*/
public function delete(User $user, User $other)
public function delete(?User $user, User $other)
{
return $user->is($other) ? true : Response::denyAsNotFound('not found message');
return $user?->is($other) ? true : Response::denyAsNotFound('not found message');
}

}
18 changes: 18 additions & 0 deletions tests/dummy/tests/Api/V1/Users/DeleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,22 @@ public function test(): void
'title' => 'Not Found',
]);
}

public function testUnauthenticated(): void
{
$user = User::factory()->createOne();

$expected = $this->serializer
->user($user);
$response = $this
->jsonApi('users')
->delete(url('/api/v1/users', $expected['id']));

$response->assertNotFound()
->assertHasError(404, [
'detail' => 'not found message',
'status' => '404',
'title' => 'Not Found',
]);
}
}

0 comments on commit 62038dd

Please sign in to comment.