我正在构建一个 symfony 4 cmf。我正在使用 API 平台。在 page.php 实体中,我添加了以下内容
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ApiResource(
* normalizationContext={"groups"={"read"}},
* denormalizationContext={"groups"={"write"}},
* collectionOperations={"get"},
* itemOperations={
* "get",
* "put"={"security"="is_granted('ROLE_ADMIN')"},
* }
* )
* @ORM\Entity(repositoryClass="App\Repository\PageRepository")
*/
class Page
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Groups("write")
*/
private $route;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"read", "write"})
*/
private $name;
.....
注意 在我正常的后台控制器中,我已经在使用选民和 IS_GRANTED。我想说的是,除了我的 API PLATFORM 实体之外,我的访问控制在其他地方运行良好。
现在,即使我已将 ROLE_ADMIN 添加到 PUT 方法中,我仍使用 ROLE_USER 唯一用户进行身份验证,并且我能够 PUT 页面。这意味着它没有考虑我在 ApiPlatform 注释的 is_granted 部分中的角色。
知道为什么吗?