首页 > 解决方案 > 如何根据连接用户的角色获取属性?

问题描述

我有一个产品实体,如果连接的用户是 SUBADMIN,我想获取产品的所有信息,但是如果连接的用户是简单的 ADMIN,我不希望收到描述,我正在使用这个组注释,我了解读写属性,但我不知道如何获得我想要的结果。

/**
 * @ApiResource(
        itemOperations={
 *          "get"={
 *              "normalization_context"={"groups"="read"}
 *          },
 *          "put"={"access_control"="isgranted('ROLE_ADMIN') or isgranted('ROLE_SUBADMIN')"},
 *          "delete"={"access_control"="isgranted('ROLE_ADMIN') or isgranted('ROLE_SUBADMIN')"}
 *      },
 *      collectionOperations={
 *          "get"={
 *              "normalization_context"={"groups"="read"}
 *          },,
 *          "post"={"access_control"="isgranted('ROLE_ADMIN') or isgranted('ROLE_SUBADMIN')"}
 *      },
 * )
 * @ORM\Entity(repositoryClass="App\Repository\ProduitRepository")
 */
class Produit
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups("read")
     */
    private $name;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups("read")
     */
    private $title;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups("read")
     */
    private $description;

    // getters and setters here
}

我必须使用 ContextBuilder 来自定义它吗?

标签: phpsymfonyapi-platform.com

解决方案


推荐阅读