首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Api Platform 2.5:当前实体嵌套时才执行collectionOperations

Api Platform 2.5:当前实体嵌套时才执行collectionOperations
EN

Stack Overflow用户
提问于 2019-12-08 20:59:52
回答 1查看 191关注 0票数 0

我开始使用API平台之前,有一个很好的文档和看了一个专业的教程,现在我遇到了一个问题。

我有两个实体User & Role,角色嵌套在User中。我想禁用对角色的直接操作,我只希望当我使用角色发布用户时,post操作可以工作,但当我直接在角色/api/角色上发布时,post操作必须被禁用。

用户:

代码语言:javascript
复制
/**
 * @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;

角色:

代码语言:javascript
复制
/**
 * @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;
EN

回答 1

Stack Overflow用户

发布于 2020-08-28 23:43:11

您可以像这样执行disable operations (但不包括get操作):

代码语言:javascript
复制
/**
 * @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;
...
}

现在,您只能读取角色,而不能创建或编辑角色。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59235634

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档