我想知道如何使用api平台保护自定义项的操作,我在文档中找到了以下代码:
/**
* Secured resource.
*
* @ApiResource(
* attributes={"access_control"="is_granted('ROLE_USER')"},
* collectionOperations={
* "get"={"method"="GET"},
* "post"={"method"="POST", "access_control"="is_granted('ROLE_ADMIN')"}
* },
* itemOperations={
* "get"{"method"="GET","access_control"="is_granted('ROLE_USER') and object.owner == user"}
* }
* )
* @ORM\Entity
*/
Run Code Online (Sandbox Code Playgroud)
但我想做类似的事情:
/**
* @ApiResource(itemOperations={
* "get"={"method"="GET"} //Public route,
* "special"={"route_name"="special", "access_control"="is_granted('ROLE_ADMIN') or object.owner == user"}},
* "special2"={"route_name"="special2", "access_control"="is_granted('ROLE_USER')"}
* })
*/
Run Code Online (Sandbox Code Playgroud)
它行得通吗?还是我必须检查特殊Action文件中的用户角色?
在这种情况下,最佳做法是什么?