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 @@ -135,3 +135,42 @@ Or order your results like:
135
135
}
136
136
}
137
137
```
138
+
139
+ ## Security (` access_control ` )
140
+
141
+ To add a security layer to your queries and mutations, follow the [ security] ( security.md ) documentation.
142
+
143
+ If your security needs differ between REST and GraphQL, add the particular parts in the ` graphql ` key.
144
+
145
+ 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.
146
+ Please note it's not possible to update a book in GraphQL because the ` update ` operation is not defined.
147
+
148
+ ``` php
149
+ <?php
150
+ // api/src/Entity/Book.php
151
+
152
+ namespace App\Entity;
153
+
154
+ use ApiPlatform\Core\Annotation\ApiResource;
155
+
156
+ /**
157
+ * @ApiResource(
158
+ * attributes={"access_control"="is_granted('ROLE_USER')"},
159
+ * collectionOperations={
160
+ * "post"={"access_control"="is_granted('ROLE_ADMIN')", "access_control_message"="Only admins can add books."}
161
+ * },
162
+ * itemOperations={
163
+ * "get"={"access_control"="is_granted('ROLE_USER') and object.owner == user", "access_control_message"="Sorry, but you are not the book owner."}
164
+ * },
165
+ * graphql={
166
+ * "query"={"access_control"="is_granted('ROLE_USER') and object.owner == user"},
167
+ * "delete"={"access_control"="is_granted('ROLE_ADMIN')"},
168
+ * "create"={"access_control"="is_granted('ROLE_ADMIN')"}
169
+ * }
170
+ * )
171
+ */
172
+ class Book
173
+ {
174
+ // ...
175
+ }
176
+ ```
You can’t perform that action at this time.
0 commit comments