Question

I've followed the Symfony2 Security documentation and have the entities, database and forms doing their thing. 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?

Relevant Code:

User Entity: http://pastebin.com/zi8nWGb8

Role Entity: http://pastebin.com/Q8D5kB0A

UserRepository: http://pastebin.com/BLfAjgkt

Registration and Login Actions: http://pastebin.com/rdbAcBXu

The signupCreateAction is where I suspect the magic should happen or perhaps in the __construct() method of the user entity. I'm simply lost as to the correct way to do this.

Was it helpful?

Solution

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). This is well documented on the Doctrine documentation.

To register an event subscriber, you should look at the Symfony documentation:

OTHER TIPS

I use the FOSUserBundle to manage users and roles. They have a facility called Groups that lets you manage the roles in the database. That said this shouldn't be difficult to roll yourself.

The FOSUserBundle provides most of the things you need to manage users.

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top