1

In my web application, I want my user's to be able to create roles and add users to them dynamically. The only thing I imagine, is to edit the security.yml every time, but this can't be the best solution, can it? It would be very nice, if there is something like a User Provider for roles, so I can define one which loads the roles from a database (doctrine).

Thanks for your help, hice3000.

hice3000
  • 186
  • 3
  • 12

1 Answers1

1

Then, you should want to add a role Entity to your model Hice.

You have to know that Symfony2 provides support for dynamic roles too. You have a getRoles() method in the Symfony2 User spec in the API Doc, that your User entity should implement, that forces him to return Roles. These roles must either implement the role interface that specifies a getRole() method that returns, most usually, the role name itself.

You can then add the newly created role directly to your user role list that the getRoles() user method will then return.

Here is an example using annotations : First role class

/**
 * Role class
 *
 * @ORM\Entity()
 */
class Role implements RoleInterface, \Serializable
{
    /**
     * @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\ManyToMany(targetEntity="User", mappedBy="userRoles")
     */
    private $users;

    public function __construct()
    {
        $this->users = new \Doctrine\Common\Collections\ArrayCollection();
    }

    public function getRole()
    {
        return $this->name;
    }

}

And the User class

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

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

    /**
     * @ORM\ManyToMany(targetEntity="Role", inversedBy="users")
     * @ORM\JoinTable(name="user_roles")
     */
    private $userRoles;

    public function __construct()
    {
        $this->userRoles = new \Doctrine\Common\Collections\ArrayCollection();
    }

    public function getRoles()
    {
        return $this->userRoles->toArray();
    }

I've skipped imports and methods to simplify the approach.

EDIT : There is something to know about serialization too. As Sharom commented on github, you musn't serialize users in roles neither roles in users. Just read his post and I think you'll understand :)

felipsmartins
  • 13,269
  • 4
  • 48
  • 56
Yoann Chambonnet
  • 1,313
  • 16
  • 22
  • Oh Huston, we have a problem! The script throws a FatalErrorException: Error: Call to a member function toArray() on a non-object in E:\[...]\MainBundle\Entity\User.php line 138 – hice3000 Jul 17 '13 at 18:45
  • Sorry Hice, I forgot one thing about roles / users and serialization. I added them in my post. – Yoann Chambonnet Jul 19 '13 at 07:32