php - 如何根据连接用户的角色获取属性?
问题描述
我有一个产品实体,如果连接的用户是 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 来自定义它吗?
解决方案
推荐阅读
- python - 更改对数轴上的刻度线大小
- postgresql - Postgresql 计算记录之间的最小时间差
- azure - 如何禁用 Azure BLOB 静态网站上的目录列表?
- vim - 在 VIM 中使用插入时出现延迟
- node.js - GSI 上的 DynamoDB 查询使用 nodeJs 返回 null
- python - 用于测试 setUp 中所有实例的 Python unittest 在属性中是否具有唯一编号?
- python - 键盘 python 库不适用于 MacOS Mojave
- docker - 如何使用自定义文件名运行 docker-compose
- python - RDFLib + JSON-LD + 命名图:使用@graph 时如何覆盖图名?
- unicode - 如何使用 Inno Setup 保存带有 BOM 文件的 UTF-16