2
votes

Hello guys am trying to understand logic in symfony about user and user roles and how its works. Am new in symfony and i follow this tutorial : http://symfony.com/doc/current/security/entity_provider.html

Everything work perfect in my project but when i look in database i have user table and all user what i registred like data in table.

But where is table for user roles ? How symfony know who is ROLE_ADMIN and who is ROLE_MODERATOR. How can implement to all user has specific roles and save that roles in database where i can change it later from some admin panel? ACL meybe?

2
By default, roles are stored as a column in the user table. It is actually a serialized array so a use can have have multiple roles. You can of course make your own implementation and have a user roles table if you feel one is needed. - Cerad
This link might help you too - mapmalith

2 Answers

6
votes

Symfony is not resposible of storing your roles. You have to store them if you need them to be managed throw an admin console for example.

The Symfony\Component\Security\Core\User\UserInterface Interface your User entity implements contains a method name getRoles that should return either an array of String representing the user role or and Array of Symfony\Component\Security\Core\Role\RoleInterface. So you can create an entity in your project named Role that implements Symfony\Component\Security\Core\Role\RoleInterface. Then link that entity to your User entity throw a ManyToMany association. Finally implements the getRoles method that should return the Roles of the given user.

You can then manage your roles as any other entity in your project.

3
votes

RoleInterface has now been deprecated and is due for removal in 4.0 so you can do the same as Genoud Magloire said but be sure to extend Symfony\Component\Security\Core\Role\Role

Here is an example of my role entity.

<?php

namespace ExampleCoreBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\Role\Role;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * ExampleRole
 *
 * @ORM\Table(name="EXAMPLE_ROLE")
 * @ORM\Entity
 */
class ExampleRole extends Role
{
    /**
     * @var integer
     *
     * @ORM\Column(name="ID", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="SEQUENCE")
     */
    private $id;

    /**
     * @var string
     *
     * @Assert\NotBlank()
     * @Assert\Length(
     *      min = 9,
     *      max = 100,
     *      minMessage = "Role name must be at least {{ limit }} characters long",
     *      maxMessage = "Role name cannot be longer than {{ limit }} characters"
     * )
     * @ORM\Column(name="ROLE_NAME", type="string", length=255, nullable=false, unique=true)
     *
     */
    private $roleName;

    /**
     * @var string
     *
     * @Assert\NotBlank()
     * @Assert\Choice({"Y","N"})
     *
     * @ORM\Column(name="GRANTABLE", type="string", length=1, nullable=false)
     */
    private $grantable;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="CREATED_ON", type="datetime", nullable=false)
     */
    private $createdOn;

    //...and so on with whatever else you want to save. 

    public function __construct( $roleName = null )
    {
        parent::__construct( $roleName );
    }