Skip to content

Commit f3beee3

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

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

core/security.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,32 @@ 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+
[/codeSelector]
109+
98110
In this example:
99111
100112
* The user must be logged in to interact with `Book` resources (configured at the resource level)
@@ -169,6 +181,8 @@ In order to give the current `object` to your voter, use the expression `is_gran
169181

170182
For example:
171183

184+
[codeSelector]
185+
172186
```php
173187
<?php
174188
// api/src/Entity/Book.php
@@ -198,6 +212,26 @@ class Book
198212
}
199213
```
200214

215+
```yaml
216+
# config/api/resources/Book.yaml
217+
App\Entity\Book:
218+
attributes:
219+
security: 'is_granted("ROLE_USER")'
220+
collectionOperations:
221+
get: ~
222+
post:
223+
security_post_denormalize: 'is_granted("BOOK_CREATE", object)'
224+
itemOperations:
225+
get:
226+
security: 'is_granted("BOOK_READ", object)'
227+
put:
228+
security: 'is_granted("BOOK_EDIT", object)'
229+
delete:
230+
security: 'is_granted("BOOK_DELETE", object)'
231+
```
232+
233+
[/codeSelector]
234+
201235
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.
202236
If you need to use `security_post_denormalize`, consider adding `security` for the other operations instead of the global one.
203237

0 commit comments

Comments
 (0)