File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change @@ -72,3 +72,42 @@ class Offer
72
72
// ...
73
73
}
74
74
```
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
+ ```
You can’t perform that action at this time.
0 commit comments