Jerald
Jerald

Reputation: 4058

How to change role hierarchy storage in Symfony3?

I tried to change hieharchy storage like here. I overrided security.role_hierarchy service. service:

services:
    security.role_hierarchy:
        class: AppBundle\Role\DynamicRoleHierarchy
        public: false
        arguments: [%security.role_hierarchy.roles%, "@=service('doctrine.orm.default_entity_manager').getRepository('AppBundle:Role')"]

DynamicRoleHierarchy:

class DynamicRoleHierarchy extends RoleHierarchy
{
    /**
     * Constructor.
     *
     * @param array $hierarchy An array defining the hierarchy
     */
    public function __construct(array $hierarchy, RoleRepository $roleRepository)
    {
        // Actually, here I get $hierarchy from DB
        $hierarchy = [
            'ROLE_ADMIN' => [
                'ROLE_USER'
            ]
        ];

        parent::__construct($hierarchy);
    }
}

This code works fine, but only if there is something in role_hierarchy section in security.yml. If there is any text then hierarchy will work. For example:

// file security.yml
security:
    role_hierarchy:
        aaa:       bbb

But if there is no role_hierarchy section or it is empty then hierarchy doesn't work. What is the reason?

Upvotes: 2

Views: 422

Answers (1)

xabbuh
xabbuh

Reputation: 5881

If the config contains no role_hierarchy configuration, the role hierarchy voter is removed. Instead of trying to replace the built-in role hierarchy service, I would rather register a custom role hierarchy voter (and make sure to remove the built-in one if it conflicts with your implementation).

Upvotes: 3

Related Questions