@@ -24,13 +24,13 @@ use Symfony\Component\Validator\Constraints as Assert;
24
24
* Secured resource.
25
25
*
26
26
* @ApiResource(
27
- * attributes={"access_control "="is_granted('ROLE_USER')"},
27
+ * attributes={"security "="is_granted('ROLE_USER')"},
28
28
* collectionOperations={
29
29
* "get",
30
- * "post"={"access_control "="is_granted('ROLE_ADMIN')"}
30
+ * "post"={"security "="is_granted('ROLE_ADMIN')"}
31
31
* },
32
32
* itemOperations={
33
- * "get"={"access_control "="is_granted('ROLE_USER') and object.owner == user"},
33
+ * "get"={"security "="is_granted('ROLE_USER') and object.owner == user"},
34
34
* "put"={"access_control"="is_granted('ROLE_USER') and previous_object.owner == user"},
35
35
* }
36
36
* )
@@ -78,7 +78,7 @@ if you really need to.
78
78
## Configuring the Access Control Message
79
79
80
80
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.
82
82
83
83
For example:
84
84
@@ -93,12 +93,12 @@ use ApiPlatform\Core\Annotation\ApiResource;
93
93
/**
94
94
* ...
95
95
* @ApiResource(
96
- * attributes={"access_control "="is_granted('ROLE_USER')"},
96
+ * attributes={"security "="is_granted('ROLE_USER')"},
97
97
* 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."}
99
99
* },
100
100
* 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."}
102
102
* }
103
103
* )
104
104
*/
@@ -114,17 +114,62 @@ Alternatively, using YAML:
114
114
# api/config/api_platform/resources.yaml
115
115
App\Entity\Book :
116
116
attributes :
117
- access_control : ' is_granted("ROLE_USER")'
117
+ security : ' is_granted("ROLE_USER")'
118
118
collectionOperations :
119
119
post :
120
120
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.'
123
123
itemOperations :
124
124
get :
125
125
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'
128
173
# ...
129
174
```
130
175
0 commit comments