【问题标题】:Api Platform access_control : adding role is not workingApi Platform access_control:添加角色不起作用
【发布时间】:2019-12-16 23:21:56
【问题描述】:

我正在构建一个 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 部分中的角色。

知道为什么吗?

【问题讨论】:

    标签: api symfony access-control api-platform.com symfony-4.3


    【解决方案1】:

    不知道这是否仍然重要,但请尝试:

    "put"={"access_control"="is_granted('ROLE_ADMIN')"},
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-27
      • 2019-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-25
      • 2017-07-22
      相关资源
      最近更新 更多