api平台资源上的OneToMany access_control

时间:2018-08-28 06:37:15

标签: symfony4 api-platform.com

我想从子资源中检查access_control,但是它不起作用。

我的第一个实体与第二个实体具有OneToMany关系。

  

src / Entity / Course.php

/**
 * @ApiResource(
 *     collectionOperations={
 *         "get"={"access_control"="object.userCompanyRoles.user == user"}
 *     }
 * )
 *
 * @ORM\Table(name="course")
 * @ORM\Entity(repositoryClass="App\Repository\CourseRepository")
 */
class Course
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\OneToMany(targetEntity="UserCompanyRole", mappedBy="course")
     * @ApiSubresource()
     */
    private $userCompanyRoles;
}
  

src / Entity / UserCompanyRole.php

/**
 * @ApiResource()
 * @ORM\Table(name="user_company_role")
 * @ORM\Entity(repositoryClass="App\Repository\UserCompanyRoleRepository")
 */
class UserCompanyRole
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Course", inversedBy="userCompanyRoles", cascade={"all"})
     */
    private $course;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="userCompanyRoles", cascade={"all"})
     * @ORM\JoinColumn(nullable=false)
     */
    private $user;
}

但是当我发送请求时,我有这个:

  

“ hydra:description”:“注意:未定义的属性:ApiPlatform \ Core \ Bridge \ Doctrine \ Orm \ Paginator :: $ userCompanyRoles”,

我无法简化我的关系,因为许多用户可以在课程中扮演许多角色。

有人对我的问题有建议吗? 谢谢。

2 个答案:

答案 0 :(得分:0)

您在collectionOperations中,这意味着您将要检索一个数组。好吧,那不是一个精确的数组,而是一个Paginator,因此object.userCompanyRoles将成为Paginator,因为这就是您要检索的内容。

如果您在object.userCompanyRoles中,则access_control上的itemOperations应该可以工作。

那么如何在您的/courses上添加访问控制?首先,您不需要ApiSubresource。此注释意味着您可以在请求userCompanyRoles

上列出属于course项目ID的/courses/{id}/user_company_roles

我的理解是,只有在courses中列出了user时,您才希望列出userCompanyRoles.user。 这听起来不像是我的访问控制,而更像是覆盖operation或事件filter,因为当您请求/courses时,它应该相对于您所连接的用户进行过滤,对吗?

https://api-platform.com/docs/core/operations https://api-platform.com/docs/core/filters

让我知道您是否仍然遇到麻烦。

答案 1 :(得分:0)

集合的安全性表达式中没有可用的object变量。您可能要编写一个扩展。这是一个示例,该示例按当前用户的意愿来过滤当前用户的集合:

https://api-platform.com/docs/core/extensions/