Skip to content

Commit f3b3069

Browse files
committed
fix(laravel): call authorize on delete but not validation
1 parent a49bde1 commit f3b3069

File tree

5 files changed

+64
-2
lines changed

5 files changed

+64
-2
lines changed

src/Laravel/State/ValidateProvider.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use ApiPlatform\Metadata\Error;
1717
use ApiPlatform\Metadata\Operation;
1818
use ApiPlatform\State\ProviderInterface;
19+
use Illuminate\Auth\Access\AuthorizationException;
1920
use Illuminate\Contracts\Foundation\Application;
2021
use Illuminate\Foundation\Http\FormRequest;
2122
use Illuminate\Support\Facades\Validator;
@@ -42,7 +43,7 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
4243
$request = $context['request'];
4344
$body = $this->inner->provide($operation, $uriVariables, $context);
4445

45-
if (!$operation->canValidate() || $operation instanceof Error) {
46+
if ($operation instanceof Error) {
4647
return $body;
4748
}
4849

@@ -53,15 +54,23 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
5354

5455
if (\is_string($rules) && is_a($rules, FormRequest::class, true)) {
5556
try {
57+
// this also throws an AuthorizationException
5658
$this->app->make($rules);
57-
// } catch (AuthorizationException $e) { // TODO: we may want to catch this to transform to an error
5859
} catch (ValidationException $e) { // @phpstan-ignore-line make->($rules) may throw this
60+
if (!$operation->canValidate()) {
61+
return $body;
62+
}
63+
5964
throw $this->getValidationError($e->validator, $e);
6065
}
6166

6267
return $body;
6368
}
6469

70+
if (!$operation->canValidate()) {
71+
return $body;
72+
}
73+
6574
if (!\is_array($rules)) {
6675
return $body;
6776
}

src/Laravel/Tests/AuthTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,12 @@ public function testAuthenticatedPolicy(): void
4747
$response = $this->post('/api/vaults', [], ['accept' => ['application/ld+json'], 'content-type' => ['application/ld+json'], 'authorization' => 'Bearer '.$token]);
4848
$response->assertStatus(403);
4949
}
50+
51+
public function testAuthenticatedDeleteWithPolicy(): void
52+
{
53+
$response = $this->post('/tokens/create');
54+
$token = $response->json()['token'];
55+
$response = $this->delete('/api/vaults/1', [], ['accept' => ['application/ld+json'], 'authorization' => 'Bearer '.$token]);
56+
$response->assertStatus(403);
57+
}
5058
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Workbench\App\Http\Requests;
15+
16+
use Illuminate\Foundation\Http\FormRequest;
17+
use Workbench\App\Models\Vault;
18+
19+
class VaultFormRequest extends FormRequest
20+
{
21+
public function authorize(): bool
22+
{
23+
return $this->user()->can('delete', new Vault());
24+
}
25+
26+
/**
27+
* Get the validation rules that apply to the request.
28+
*
29+
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
30+
*/
31+
public function rules(): array
32+
{
33+
return [
34+
'secret' => 'required',
35+
];
36+
}
37+
}

src/Laravel/workbench/app/Models/Vault.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
namespace Workbench\App\Models;
1515

1616
use ApiPlatform\Metadata\ApiResource;
17+
use ApiPlatform\Metadata\Delete;
1718
use ApiPlatform\Metadata\GetCollection;
1819
use ApiPlatform\Metadata\Post;
1920
use Illuminate\Database\Eloquent\Factories\HasFactory;
2021
use Illuminate\Database\Eloquent\Model;
22+
use Workbench\App\Http\Requests\VaultFormRequest;
2123

2224
#[ApiResource(
2325
operations: [
@@ -30,6 +32,7 @@
3032
read: true,
3133
write: false
3234
),
35+
new Delete(middleware: 'auth:sanctum', rules: VaultFormRequest::class, provider: [self::class, 'provide']),
3336
]
3437
)]
3538
class Vault extends Model

src/Laravel/workbench/app/Policies/VaultPolicy.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,9 @@ public function update(User $user, Vault $vault): bool
2222
{
2323
return false;
2424
}
25+
26+
public function delete(User $user): bool
27+
{
28+
return false;
29+
}
2530
}

0 commit comments

Comments
 (0)