Skip to content

Commit 2b22cce

Browse files
Add notes on usage of ApiProperty security_post_denormalize. (#1349)
1 parent 9073325 commit 2b22cce

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

core/security.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class Book
8989
/**
9090
* @var string Property viewable and writtable only by users with ROLE_ADMIN
9191
*
92-
* @ApiProperty(security="is_granted('ROLE_ADMIN')")
92+
* @ApiProperty(security="is_granted('ROLE_ADMIN')", security_post_denormalize="is_granted('UPDATE', object)")
9393
*/
9494
private $adminOnlyProperty;
9595
}
@@ -101,11 +101,13 @@ In this example:
101101
* Only users having [the role](https://symfony.com/doc/current/security.html#roles) `ROLE_ADMIN` can create a new resource (configured on the `post` operation)
102102
* Only users having the `ROLE_ADMIN` or owning the current object can replace an existing book (configured on the `put` operation)
103103
* Only users having the `ROLE_ADMIN` can view or modify the `adminOnlyProperty` property. Only users having the `ROLE_ADMIN` can create a new resource specifying `adminOnlyProperty` value.
104+
* Only users that are granted the `UPDATE` attribute on the book (via a voter) can write to the field
104105

105106
Available variables are:
106107

107108
* `user`: the current logged in object, if any
108-
* `object`: the current resource, or collection of resources for collection operations
109+
* `object`: the current resource, or collection of resources for collection operations (note: this is `null` for update/create operations)
110+
* `previous_object`: (`security_post_denormalize` only) a clone of `object`, before modifications were made - this is `null` for create operations
109111
* `request`: the current request
110112

111113
Access control checks in the `security` attribute are always executed before the [denormalization step](serialization.md).

0 commit comments

Comments
 (0)