Skip to content

Commit 7d183ae

Browse files
Fix #1646
1 parent e497997 commit 7d183ae

File tree

3 files changed

+66
-21
lines changed

3 files changed

+66
-21
lines changed

core/graphql.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ For the previous page, you would add the `startCursor` from the current page as
783783
How do you know when you have reached the last page? It is the aim of the property `hasNextPage` or `hasPreviousPage` in `pageInfo`.
784784
When it is false, you know it is the last page and moving forward or backward will give you an empty result.
785785

786-
## Security (`access_control`)
786+
## Security
787787

788788
To add a security layer to your queries and mutations, follow the [security](security.md) documentation.
789789

@@ -802,17 +802,17 @@ use ApiPlatform\Core\Annotation\ApiResource;
802802
803803
/**
804804
* @ApiResource(
805-
* attributes={"access_control"="is_granted('ROLE_USER')"},
805+
* attributes={"security"="is_granted('ROLE_USER')"},
806806
* collectionOperations={
807-
* "post"={"access_control"="is_granted('ROLE_ADMIN')", "access_control_message"="Only admins can add books."}
807+
* "post"={"security"="is_granted('ROLE_ADMIN')", "security_message"="Only admins can add books."}
808808
* },
809809
* itemOperations={
810-
* "get"={"access_control"="is_granted('ROLE_USER') and object.owner == user", "access_control_message"="Sorry, but you are not the book owner."}
810+
* "get"={"security"="is_granted('ROLE_USER') and object.owner == user", "security_message"="Sorry, but you are not the book owner."}
811811
* },
812812
* graphql={
813-
* "query"={"access_control"="is_granted('ROLE_USER') and object.owner == user"},
814-
* "delete"={"access_control"="is_granted('ROLE_ADMIN')"},
815-
* "create"={"access_control"="is_granted('ROLE_ADMIN')"}
813+
* "query"={"security"="is_granted('ROLE_USER') and object.owner == user"},
814+
* "delete"={"security"="is_granted('ROLE_ADMIN')"},
815+
* "create"={"security"="is_granted('ROLE_ADMIN')"}
816816
* }
817817
* )
818818
*/

core/operations.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ class Question
468468

469469
### Access Control of Subresources
470470

471-
The `subresourceOperations` attribute also allows you to add an access control on each path with the attribute `access_control`.
471+
The `subresourceOperations` attribute also allows you to add an access control on each path with the attribute `security`.
472472

473473
```php
474474
<?php
@@ -479,7 +479,7 @@ The `subresourceOperations` attribute also allows you to add an access control o
479479
* @ApiResource(
480480
* subresourceOperations={
481481
* "api_questions_answer_get_subresource"= {
482-
* "access_control"="has_role('ROLE_AUTHENTICATED')"
482+
* "security"="has_role('ROLE_AUTHENTICATED')"
483483
* }
484484
* }
485485
* )

core/security.md

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ use Symfony\Component\Validator\Constraints as Assert;
2424
* Secured resource.
2525
*
2626
* @ApiResource(
27-
* attributes={"access_control"="is_granted('ROLE_USER')"},
27+
* attributes={"security"="is_granted('ROLE_USER')"},
2828
* collectionOperations={
2929
* "get",
30-
* "post"={"access_control"="is_granted('ROLE_ADMIN')"}
30+
* "post"={"security"="is_granted('ROLE_ADMIN')"}
3131
* },
3232
* itemOperations={
33-
* "get"={"access_control"="is_granted('ROLE_USER') and object.owner == user"},
33+
* "get"={"security"="is_granted('ROLE_USER') and object.owner == user"},
3434
* "put"={"access_control"="is_granted('ROLE_USER') and previous_object.owner == user"},
3535
* }
3636
* )
@@ -78,7 +78,7 @@ if you really need to.
7878
## Configuring the Access Control Message
7979

8080
By default when API requests are denied, you will get the "Access Denied" message.
81-
You can change it by configuring the "access\_control\_message" attribute.
81+
You can change it by configuring the "security\_message" attribute.
8282

8383
For example:
8484

@@ -93,12 +93,12 @@ use ApiPlatform\Core\Annotation\ApiResource;
9393
/**
9494
* ...
9595
* @ApiResource(
96-
* attributes={"access_control"="is_granted('ROLE_USER')"},
96+
* attributes={"security"="is_granted('ROLE_USER')"},
9797
* collectionOperations={
98-
* "post"={"access_control"="is_granted('ROLE_ADMIN')", "access_control_message"="Only admins can add books."}
98+
* "post"={"security"="is_granted('ROLE_ADMIN')", "security_message"="Only admins can add books."}
9999
* },
100100
* itemOperations={
101-
* "get"={"access_control"="is_granted('ROLE_USER') and object.owner == user", "access_control_message"="Sorry, but you are not the book owner."}
101+
* "get"={"security"="is_granted('ROLE_USER') and object.owner == user", "security_message"="Sorry, but you are not the book owner."}
102102
* }
103103
* )
104104
*/
@@ -114,17 +114,62 @@ Alternatively, using YAML:
114114
# api/config/api_platform/resources.yaml
115115
App\Entity\Book:
116116
attributes:
117-
access_control: 'is_granted("ROLE_USER")'
117+
security: 'is_granted("ROLE_USER")'
118118
collectionOperations:
119119
post:
120120
method: 'POST'
121-
access_control: 'is_granted("ROLE_ADMIN")'
122-
access_control_message: 'Only admins can add books.'
121+
security: 'is_granted("ROLE_ADMIN")'
122+
security_message: 'Only admins can add books.'
123123
itemOperations:
124124
get:
125125
method: 'GET'
126-
access_control: 'is_granted("ROLE_USER") and object.owner == user'
127-
access_control_message: 'Sorry, but you are not the book owner.'
126+
security: 'is_granted("ROLE_USER") and object.owner == user'
127+
security_message: 'Sorry, but you are not the book owner.'
128+
# ...
129+
```
130+
131+
## Execute security after denormalization
132+
133+
The "security" attribute is executed before the object denormalization. For some cases, it might be useful to execute
134+
a security after the denormalization.
135+
To do so, prefer using "late\_security", which allows you to use the "previous\_object" as the denormalized object:
136+
137+
```php
138+
<?php
139+
// src/Entity/Book.php
140+
141+
namespace App\Entity;
142+
143+
use ApiPlatform\Core\Annotation\ApiResource;
144+
145+
/**
146+
* ...
147+
* @ApiResource(
148+
* attributes={"security"="is_granted('ROLE_USER')"},
149+
* itemOperations={
150+
* "get",
151+
* "put"={"late_security"="is_granted("ROLE_USER") and previous_object.owner == user"}
152+
* }
153+
* )
154+
*/
155+
class Book
156+
{
157+
// ...
158+
}
159+
```
160+
161+
Alternatively, using YAML:
162+
163+
```yaml
164+
# api/config/api_platform/resources.yaml
165+
App\Entity\Book:
166+
attributes:
167+
security: 'is_granted("ROLE_USER")'
168+
itemOperations:
169+
get: ~
170+
put:
171+
method: 'PUT'
172+
late_security: 'is_granted("ROLE_USER") and previous_object.owner == user'
128173
# ...
129174
```
130175

0 commit comments

Comments
 (0)