Search code examples
phpsymfonypermissionssymfony4roles

Symfony4 : create custom ROLES


I know there is a way to create and manage custom roles on a symfony app, but I don't find any documentation or tutorial, so I tried to do it alone and I didn't succeed. Here is what I did:

role_hierarchy:
    ROLE_TEST:
    ROLE_USER: ROLE_TEST
    ROLE_ADMIN: ROLE_USER

ROLE_TEST should be a lower role than ROLE_USER and my Users who are granted ROLE_TEST can access to method protected by @IsGranted("ROLE_USER") (and they souldn't)

So my question is what's missing?


Solution

  • By reading comments, I think if you followed Symfony documentation for your login system, you should have this :

    $roles[] = 'ROLE_USER';
    

    in your getRoles method, just replace 'ROLE_USER' by '' and try again to use your methods with @IsGranted("ROLE_USER")