phpsymfonyrolesfosuserbundle

Symfony 2 - Adding user roles under the ROLE_USER


I'm trying to create a new role in Symfony 2 below the default USER_ROLE (that role would have limited write access to some features). I am using FOSUserBundle.

I've written the following security settings so far but my ROLE_DEMO users still get the ROLE_USER.

role_hierarchy:
        ROLE_DEMO:        []
        ROLE_USER:        [ROLE_DEMO]
        ROLE_ADMIN:       [ROLE_USER, ROLE_SONATA_ADMIN]
        ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

Is it possible to create a role under the ROLE_USER in Symfony 2. If yes, how?


Solution

  • A even shorter solution i came up with was to override the const ROLE_DEFAULT at the beginning of my owner User class.

    class User extends BaseUser
    {
        /** 
         * Override FOSUserBundle User base class default role.
         */
        const ROLE_DEFAULT = 'ROLE_DEMO';
    
        [...]
    }
    

    That way i did not even have to override the FOS user bundle getRoles() method.