Skip to content

Commit 27073e5

Browse files
authored
Merge pull request #823 from api-platform/antograssiot-patch-1
Document the `previous_object`added in Expression Language
2 parents 218b482 + 887e714 commit 27073e5

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

core/security.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ use Symfony\Component\Validator\Constraints as Assert;
3030
* "post"={"access_control"="is_granted('ROLE_ADMIN')"}
3131
* },
3232
* itemOperations={
33-
* "get"={"access_control"="is_granted('ROLE_USER') and object.owner == user"}
33+
* "get"={"access_control"="is_granted('ROLE_USER') and object.owner == user"},
34+
* "put"={"access_control"="is_granted('ROLE_USER') and previous_object.owner == user"},
3435
* }
3536
* )
3637
* @ORM\Entity
@@ -60,7 +61,7 @@ class Book
6061
* @ORM\ManyToOne(targetEntity=User::class)
6162
*/
6263
public $owner;
63-
64+
6465
// ...
6566
}
6667
```
@@ -69,6 +70,8 @@ This example is only going to allow fetching the book related to the current use
6970
linked to his account, it will not return the resource. In addition, only admins are able to create books which means
7071
that a user could not create a book.
7172

73+
Additionally, in some cases you need to perform security checks on the original data. For example here, only the actual owner should be allowed to edit their book. In these cases, you can use the `previous_object` variable which contains the object that was read from the data provider.
74+
7275
It is also possible to use the [event system](events.md) for more advanced logic or even [custom actions](operations.md#creating-custom-operations-and-controllers)
7376
if you really need to.
7477

0 commit comments

Comments
 (0)