我开始使用API平台之前,有一个很好的文档和看了一个专业的教程,现在我遇到了一个问题。
我有两个实体User & Role,角色嵌套在User中。我想禁用对角色的直接操作,我只希望当我使用角色发布用户时,post操作可以工作,但当我直接在角色/api/角色上发布时,post操作必须被禁用。
用户:
/**
* @ApiResource(
* normalizationContext={
* "groups"={"get"}
* },
* itemOperations={
* "get"={
* "security"="is_granted('IS_AUTHENTICATED_ANONYMOUSLY')",
* "normalization_context"={
* "groups"={"get"}
* }
* },
* "put"= {
* "security"="is_granted('IS_AUTHENTICATED_ANONYMOUSLY') or is_granted('IS_AUTHENTICATED_FULLY') and object.getAuthor() == user",
* "denormalization_context"={
* "groups"={"put"}
* }
* }
* },
* collectionOperations={
* "get"={
* "security"="is_granted('IS_AUTHENTICATED_ANONYMOUSLY')"
* },
* "post"={
* "security"="is_granted('IS_AUTHENTICATED_ANONYMOUSLY')",
* "denormalization_context"={
* "groups"={"user:post"}
* }
* }
* }
* )
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
*
* @UniqueEntity(fields={"username","email"})
*/
class User implements UserInterface
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*
* @Groups({"get","get_comment_with_author","get_post_with_comments"})
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*
* @Groups({"get","user:post","get_comment_with_author","get_post_with_comments"})
*/
private $username;
/**
* @ORM\Column(type="string", length=255)
*
* @Groups({"put","user:post","get_comment_with_author","get-to-admin"})
*/
private $email;
/**
* @ORM\ManyToMany(targetEntity="Role", fetch="EAGER")
* @Groups({"put","user:post"})
*/
private $rolesCollection;角色:
/**
* @ApiResource()
* @ORM\Entity(repositoryClass="App\Repository\RoleRepository")
*/
class Role
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
* @Groups({"user:post"})
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $label;发布于 2020-08-28 23:43:11
您可以像这样执行disable operations (但不包括get操作):
/**
* @ApiResource(
* collectionOperations={"get"},
* itemOperations={"get"}
* )
* @ORM\Entity(repositoryClass="App\Repository\RoleRepository")
*/
class Role
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
* @Groups({"user:post"})
*/
private $id;
...
}现在,您只能读取角色,而不能创建或编辑角色。
https://stackoverflow.com/questions/59235634
复制相似问题