Skip to content

Commit 6463963

Browse files
valentindrdtsoyukadunglas
authored
Docs/automatic policy (#13)
* doc: add automatic policy detection documentation * doc: add automatic policy detection documentation * doc: doc review * doc: typo * Apply suggestions from code review Co-authored-by: Kévin Dunglas <kevin@dunglas.fr> --------- Co-authored-by: Antoine Bluchet <soyuka@users.noreply.github.com> Co-authored-by: Kévin Dunglas <kevin@dunglas.fr>
1 parent 0830ac0 commit 6463963

File tree

2 files changed

+58
-28
lines changed

2 files changed

+58
-28
lines changed

laravel/index.md

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,8 @@ Let's replace our author column by a relation to a new `author` table:
287287
}
288288
```
289289

290-
By doing so, API Platform will automatically handle links to that relation using your prefered format (JSON:API, JSON-LD etc)
291-
and when we request a Book we obtain:
290+
By doing so, API Platform will automatically handle links to that relation using your prefered format (JSON:API, JSON-LD etc)
291+
and when we request a Book we obtain:
292292

293293
```json
294294
{
@@ -649,29 +649,7 @@ To protect an operation and ensure that only authorized users can access it, sta
649649
php artisan make:policy BookPolicy --model=Book
650650
```
651651

652-
If the standard Laravel conventions are followed, the Policy class is autodetected and used automatically.
653-
Otherwise, you can use the `policy` property on an operation attribute to explicitly enforce a policy:
654-
655-
```patch
656-
// app/Models/Book.php
657-
namespace App\Models;
658-
659-
use ApiPlatform\Metadata\ApiResource;
660-
+use ApiPlatform\Metadata\Patch;
661-
use Illuminate\Database\Eloquent\Model;
662-
663-
-#[ApiResource]
664-
#[ApiResource(
665-
+ operations: [
666-
+ new Patch(
667-
+ policy: 'update',
668-
+ ),
669-
+ ],
670-
)]
671-
class Book extends Model
672-
{
673-
}
674-
```
652+
Laravel will automatically detect your new policy and use it when manipulating a Book.
675653

676654
Read the detailed documentation about using [Laravel gates and policies with API Platform](security.md).
677655

laravel/security.md

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,78 @@
11
# Security
22

3-
API platform is compatible with Laravel [authorization](https://laravel.com/docs/authorization) mechanism. Once a gate is defined, you can specify the policy to use within an operation:
3+
## Policies
4+
5+
API platform is compatible with Laravel [authorization](https://laravel.com/docs/authorization) mechanism. Once a gate is defined, API Platform will automatically detect your policy.
46

57
```php
68
// app/Models/Book.php
79

810
use ApiPlatform\Metadata\Patch;
911

10-
#[Patch(policy: 'update')]
12+
#[Patch]
1113
class Book extends Model
1214
{
1315
}
1416
```
1517

18+
API Platform will detect the operation and map it to a specific method in your policy according to the rules defined in this table:
19+
20+
| Operation | Policy |
21+
|----------------|------------------------------------------------------------|
22+
| GET collection | `viewAny` |
23+
| GET | `view` |
24+
| POST | `create` |
25+
| PATCH | `update` |
26+
| DELETE | `delete` |
27+
| PUT | `update` or `create` if the resource doesn't already exist |
28+
29+
If your policy methods do not match Laravel's conventions, you can always use the `policy` property on an operation attribute to enforce this policy:
30+
```php
31+
// app/Models/Book.php
32+
namespace App\Models;
33+
34+
use ApiPlatform\Metadata\ApiResource;
35+
+use ApiPlatform\Metadata\Patch;
36+
use Illuminate\Database\Eloquent\Model;
37+
38+
-#[ApiResource]
39+
#[ApiResource(
40+
paginationItemsPerPage: 10,
41+
+ operations: [
42+
+ new Patch(
43+
+ policy: 'myCustomPolicy',
44+
+ ),
45+
+ ],
46+
)]
47+
class Book extends Model
48+
{
49+
}
50+
```
51+
52+
You also can link a model to a policy:
53+
54+
```php
55+
use App\Models\Book;
56+
use App\Tests\Book\BookPolicy;
57+
use Illuminate\Support\Facades\Gate;
58+
59+
Gate::guessPolicyNamesUsing(function (string $modelClass): ?string {
60+
return Book::class === $modelClass ?
61+
BookPolicy::class :
62+
null;
63+
});
64+
```
65+
66+
## Authentication
67+
1668
Usually, you will use [Sanctum](https://laravel.com/docs/sanctum) and add a middleware on secured routes:
1769

1870
```php
1971
// app/Models/Book.php
2072

2173
use ApiPlatform\Metadata\Patch;
2274

23-
#[Patch(middleware: 'auth:sanctum', policy: 'update')]
75+
#[Patch(middleware: 'auth:sanctum')]
2476
class Book extends Model
2577
{
2678
}

0 commit comments

Comments
 (0)