Skip to content

Commit a27b1b5

Browse files
committed
Add a Security section
1 parent 8d81c22 commit a27b1b5

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

core/graphql.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,42 @@ class Offer
7272
// ...
7373
}
7474
```
75+
76+
## Security (access_control)
77+
78+
To add a security layer to your queries and mutations, follow the [security](security.md) documentation.
79+
80+
If your security needs differ between REST and GraphQL, add the particular parts in the `graphql` key.
81+
82+
In the example below, we want the same security rules as in REST, but we also want to allow an admin to delete a book in GraphQL only.
83+
Please note it's not possible to update a book in GraphQL because the operation is not defined.
84+
85+
```php
86+
<?php
87+
// api/src/Entity/Book.php
88+
89+
namespace App\Entity;
90+
91+
use ApiPlatform\Core\Annotation\ApiResource;
92+
93+
/**
94+
* @ApiResource(
95+
* attributes={"access_control"="is_granted('ROLE_USER')"},
96+
* collectionOperations={
97+
* "post"={"access_control"="is_granted('ROLE_ADMIN')", "access_control_message"="Only admins can add books."}
98+
* },
99+
* itemOperations={
100+
* "get"={"access_control"="is_granted('ROLE_USER') and object.owner == user", "access_control_message"="Sorry, but you are not the book owner."}
101+
* },
102+
* graphql={
103+
* "query"={"access_control"="is_granted('ROLE_USER') and object.owner == user"},
104+
* "delete"={"access_control"="is_granted('ROLE_ADMIN')"},
105+
* "create"={"access_control"="is_granted('ROLE_ADMIN')"}
106+
* }
107+
* )
108+
*/
109+
class Book
110+
{
111+
// ...
112+
}
113+
```

0 commit comments

Comments
 (0)