Search code examples
phpsymfonysymfony-security

Where and how symfony store user roles


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?


Solution

  • 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.