使用FOSUserBundle在Symfony2中访问控制列表 - 向用户添加角色

时间:2022-10-16 13:55:53

I'm hoping this is more simple than the docs I've been reading.

我希望这比我读过的文档更简单。

I have a number of entities with ManyToOne relationships with a standard FOSUserbundle user entity that can login users, register them, logout etc.

我有许多与一个标准FOSUserbundle用户实体具有ManyToOne关系的实体,可以登录用户,注册用户,注销等。

class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    public function __construct()
    {
        parent::__construct();
    }

Each of my users can also have a number of pets. I.e. here's the cat entity (simplified):

我的每个用户也可以拥有许多宠物。即这是猫实体(简化):

class Cat
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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

    /**
     * @ORM\ManyToOne(targetEntity="User")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     **/
    private $user_id;

How should I add an access control list user role that defines if the user has any cats or not, and if they do, allows them onto the "cats only" part of my website.

我应该如何添加一个访问控制列表用户角色来定义用户是否有任何猫,如果他们这样做,则允许他们进入我网站的“仅猫”部分。

Note (slightly related): I feel mildly retarded when it comes to database design using doctrine, I'll upvote anyone that can point me to a good tutorial / explanation of designing things with different kind of joins, especially if it's a in Symfony/Doctrine environment.

注意(略有关联):在使用doctrine进行数据库设计方面,我觉得有点迟钝,我会向任何人指出一个很好的教程/解释来设计具有不同类型连接的东西,特别是如果它是在Symfony /学说环境。

1 个解决方案

#1


0  

Well, since you haven't gotten anything else yet. I'll throw something out there but my experience is only moderate so far.

好吧,既然你还没有得到任何其他东西。我会抛出一些东西,但到目前为止我的经验只是温和的。

In the controller for your cats owners page, you could query if the user has any associated cat entities.

在您的猫主人页面的控制器中,您可以查询用户是否有任何关联的猫实体。

You need to add OneToMany/ManyToOne annotations to your user and cat class, and a variable for the associations. This you could review straight out of the doctrine section of the symfony2 book (If you are working with Symfony2 and haven't taken the time to read the book, that is the minimal level of knowledge to work with the full stack and I suggest you read it all). (Do you really need a entity class just for cat, you could have a pet entity with a "type" option.)

您需要向用户和cat类添加OneToMany / ManyToOne注释,并为关联添加变量。你可以直接从symfony2书的学说部分中查看(如果你正在使用Symfony2并且没有花时间阅读这本书,那就是使用完整堆栈的最低知识水平,我建议你阅读全部)。 (你真的需要一个仅用于cat的实体类,你可以拥有一个带有“type”选项的pet实体。)

check the security context to ensure logged in, then query db.

检查安全上下文以确保登录,然后查询db。

    $em = $this->getDoctrine()->getManager();

    $ownersCats = $em->getRepository('PetsPetSiteBundle:Blog')->findBy(array('type'=>'cat'));

or for one result only

或仅为一个结果

    $ownersCats = $em->getRepository('PetsPetSiteBundle:Blog')->findOneBy(array('type'=>'cat'));

Once you have this result if it comes back false, you know he has no cats. Otherwise continue.

一旦你得到这个结果,如果它回来假,你知道他没有猫。否则继续。

Obviously you could make this perform better by only querying for id's or something if you don't plan on using the entities on that page or just performing a count query and seeing if it's greater than 0?

显然,如果您不打算在该页面上使用实体或只是执行计数查询并查看它是否大于0,那么只需查询id或其他内容就可以使这个表现更好?

#1


0  

Well, since you haven't gotten anything else yet. I'll throw something out there but my experience is only moderate so far.

好吧,既然你还没有得到任何其他东西。我会抛出一些东西,但到目前为止我的经验只是温和的。

In the controller for your cats owners page, you could query if the user has any associated cat entities.

在您的猫主人页面的控制器中,您可以查询用户是否有任何关联的猫实体。

You need to add OneToMany/ManyToOne annotations to your user and cat class, and a variable for the associations. This you could review straight out of the doctrine section of the symfony2 book (If you are working with Symfony2 and haven't taken the time to read the book, that is the minimal level of knowledge to work with the full stack and I suggest you read it all). (Do you really need a entity class just for cat, you could have a pet entity with a "type" option.)

您需要向用户和cat类添加OneToMany / ManyToOne注释,并为关联添加变量。你可以直接从symfony2书的学说部分中查看(如果你正在使用Symfony2并且没有花时间阅读这本书,那就是使用完整堆栈的最低知识水平,我建议你阅读全部)。 (你真的需要一个仅用于cat的实体类,你可以拥有一个带有“type”选项的pet实体。)

check the security context to ensure logged in, then query db.

检查安全上下文以确保登录,然后查询db。

    $em = $this->getDoctrine()->getManager();

    $ownersCats = $em->getRepository('PetsPetSiteBundle:Blog')->findBy(array('type'=>'cat'));

or for one result only

或仅为一个结果

    $ownersCats = $em->getRepository('PetsPetSiteBundle:Blog')->findOneBy(array('type'=>'cat'));

Once you have this result if it comes back false, you know he has no cats. Otherwise continue.

一旦你得到这个结果,如果它回来假,你知道他没有猫。否则继续。

Obviously you could make this perform better by only querying for id's or something if you don't plan on using the entities on that page or just performing a count query and seeing if it's greater than 0?

显然,如果您不打算在该页面上使用实体或只是执行计数查询并查看它是否大于0,那么只需查询id或其他内容就可以使这个表现更好?