简体   繁体   English

Symfony2 在注册期间分配用户角色

[英]Symfony2 Assigning User Roles During Registration

I've followed the Symfony2 Security documentation and have the entities, database and forms doing their thing.我遵循了 Symfony2 安全文档并让实体、数据库和表单做他们的事情。 Trouble was I didn't have any users in my database so I completed the steps in the Registration Documentation , but in my opinion these should be better connected.问题是我的数据库中没有任何用户,所以我完成了注册文档中的步骤,但我认为这些应该更好地连接。 The Registration docs say nothing about the roles.注册文档没有说明角色。 It seems to me the best time to assign a default role would be during the user registration.在我看来,分配默认角色的最佳时间是在用户注册期间。 This is what I'm after but I'm a bit lost on how to do this considering the User Entity does not have any methods for adding roles.这就是我所追求的,但考虑到用户实体没有任何添加角色的方法,我对如何做到这一点有点迷茫。 My question is how do I assign a default role ("ROLE_USER"), which is stored in the database, to users during registration?我的问题是如何在注册期间为用户分配存储在数据库中的默认角色(“ROLE_USER”)?

Relevant Code:相关代码:

User Entity: http://pastebin.com/zi8nWGb8用户实体: http : //pastebin.com/zi8nWGb8

Role Entity: http://pastebin.com/Q8D5kB0A角色实体: http : //pastebin.com/Q8D5kB0A

UserRepository: http://pastebin.com/BLfAjgkt用户库: http ://pastebin.com/BLfAjgkt

Registration and Login Actions: http://pastebin.com/rdbAcBXu注册和登录操作: http : //pastebin.com/rdbAcBXu

The signupCreateAction is where I suspect the magic should happen or perhaps in the __construct() method of the user entity. signupCreateAction 是我怀疑魔法应该发生的地方,或者可能发生在用户实体的__construct()方法中。 I'm simply lost as to the correct way to do this.我只是迷失了正确的方法来做到这一点。

Two things first.先说两件事。 You need to create accessor methods for both classes.您需要为两个类创建访问器方法。 This means creating:这意味着创建:

/**
 * ...
 */
class TblUser
{
    // ...
    public function getRoles()
    {
        return $this->roles->toArray();
    }

    public function setRoles(Collection $roles)
    {
        foreach ($roles as $role) {
            $this->addRole($role);
        }
    }

    public function addRole(TblPrivilege $role)
    {
        if (!$this->roles->contains($role)) {
            $this->roles->add($role);
            $role->addUser($this);
        }
    }

    public function removeRole(TblPrivilege $role)
    {
        if ($this->roles->contains($role)) {
            $this->roles->removeElement($role);
            $role->removeUser($this);
        }
    }
}

/**
 * ...
 */
class TblPrivileges
{
    // ...
    public function getUsers()
    {
        return $this->users;
    }

    public function setUsers(Collection $users)
    {
        foreach ($users as $user) {
            $this->addUser($user);
        }
    }

    public function addUser(TblUser $user)
    {
        if (!$this->users->contains($user)) {
            $this->users->add($user);
        }
    }

    public function removeUser(TblUser $user)
    {
        if ($this->users->contains($user)) {
            $this->users->removeElement($user);
        }
    }
}

For actually calling the $user->addRole() method, I would actually go for a Doctrine event subscriber, which would actually fetch the default role on persist (and thus, during registration).为了实际调用 $user->addRole() 方法,我实际上会选择一个 Doctrine 事件订阅者,它实际上会在持久化(因此,在注册期间)获取默认角色。 This is well documented on the Doctrine documentation .这在Doctrine 文档中有详细记录

To register an event subscriber, you should look at the Symfony documentation :要注册事件订阅者,您应该查看Symfony 文档

I use the FOSUserBundle to manage users and roles.我使用 FOSUserBundle 来管理用户和角色。 They have a facility called Groups that lets you manage the roles in the database.他们有一个名为 Groups 的工具,可让您管理数据库中的角色。 That said this shouldn't be difficult to roll yourself.也就是说,这应该不难自己滚动。

The FOSUserBundle provides most of the things you need to manage users. FOSUserBundle 提供了管理用户所需的大部分内容。

You can also try this in your user entity您也可以在您的用户实体中尝试此操作

private $roles = array();

/**
 * Returns the roles or permissions granted to the user for security.
 */
public function getRoles()
{
    $roles = $this->roles;

    // guarantees that a user always has at least one role for security
    if (empty($roles)) {
        $roles[] = 'ROLE_USER';
    }

    return array_unique($roles);
}

public function setRoles($roles)
{
    $this->roles = $roles;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM