Skip to content

docs: add missing security yaml documentation #1367

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion core/security.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,32 @@ App\Entity\Book:

Resource signature can be modified at the property level as well:

[codeSelector]

```php
class Book
{
//...

/**
* @var string Property viewable and writtable only by users with ROLE_ADMIN
* @var string Property viewable and writable only by users with ROLE_ADMIN
*
* @ApiProperty(security="is_granted('ROLE_ADMIN')")
*/
private $adminOnlyProperty;
}
```

```yaml
# config/api/resources/Book.yaml
App\Entity\Book:
properties:
adminOnlyProperty:
security: 'is_granted("ROLE_ADMIN")'
```

[/codeSelector]

In this example:

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

For example:

[codeSelector]

```php
<?php
// api/src/Entity/Book.php
Expand Down Expand Up @@ -198,6 +212,26 @@ class Book
}
```

```yaml
# config/api/resources/Book.yaml
App\Entity\Book:
attributes:
security: 'is_granted("ROLE_USER")'
collectionOperations:
get: ~
post:
security_post_denormalize: 'is_granted("BOOK_CREATE", object)'
itemOperations:
get:
security: 'is_granted("BOOK_READ", object)'
put:
security: 'is_granted("BOOK_EDIT", object)'
delete:
security: 'is_granted("BOOK_DELETE", object)'
```

[/codeSelector]

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.
If you need to use `security_post_denormalize`, consider adding `security` for the other operations instead of the global one.

Expand Down