Skip to content

Commit 901ff96

Browse files
committed
docs: add missing security yaml documentation
1 parent 5e67f43 commit 901ff96

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

core/security.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,33 @@ App\Entity\Book:
8181
8282
Resource signature can be modified at the property level as well:
8383
84+
[codeSelector]
85+
8486
```php
8587
class Book
8688
{
8789
//...
8890

8991
/**
90-
* @var string Property viewable and writtable only by users with ROLE_ADMIN
92+
* @var string Property viewable and writable only by users with ROLE_ADMIN
9193
*
9294
* @ApiProperty(security="is_granted('ROLE_ADMIN')")
9395
*/
9496
private $adminOnlyProperty;
9597
}
9698
```
9799

100+
```yaml
101+
# config/api/resources/Book.yaml
102+
App\Entity\Book:
103+
properties:
104+
adminOnlyProperty:
105+
security: 'is_granted("ROLE_ADMIN")'
106+
107+
```
108+
109+
[/codeSelector]
110+
98111
In this example:
99112

100113
* The user must be logged in to interact with `Book` resources (configured at the resource level)
@@ -169,6 +182,8 @@ In order to give the current `object` to your voter, use the expression `is_gran
169182

170183
For example:
171184

185+
[codeSelector]
186+
172187
```php
173188
<?php
174189
// api/src/Entity/Book.php
@@ -198,6 +213,26 @@ class Book
198213
}
199214
```
200215

216+
```yaml
217+
# config/api/resources/Book.yaml
218+
App\Entity\Book:
219+
attributes:
220+
security: 'is_granted("ROLE_USER")'
221+
collectionOperations:
222+
get: ~
223+
post:
224+
security_post_denormalize: 'is_granted("BOOK_CREATE"), object'
225+
itemOperations:
226+
get:
227+
security: 'is_granted("BOOK_READ", object)'
228+
put:
229+
security: 'is_granted("BOOK_EDIT", object)'
230+
delete:
231+
security: 'is_granted("BOOK_DELETE", object)'
232+
```
233+
234+
[/codeSelector]
235+
201236
Please note that if you use both `attributes={"security"="..` and then `"post" = { "security_post_denormalize" = "...`, the `security` on top level is called first, and after `security_post_denormalize`. This could lead to unwanted behaviour, so avoid using both of them simultaneously.
202237
If you need to use `security_post_denormalize`, consider adding `security` for the other operations instead of the global one.
203238

0 commit comments

Comments
 (0)