vishal
vishal

Reputation: 4083

Symfony 2 override existing service definition in config?

In

vendor/symfony/symfony/src/Symfony/Bundle/SecurityBundle/Resources/config/security.xml

    <service id="security.access.role_hierarchy_voter" class="%security.access.role_hierarchy_voter.class%" public="false">
        <argument type="service" id="security.role_hierarchy" />
        <tag name="security.voter" priority="245" />
    </service>

is defined. The service class, Symfony\Component\Security\Core\Authorization\Voter\RoleHierarchyVoter has a second parameter $prefix = 'ROLE_' as default value.

In above service definition, second argument is not passed so it will take default value ROLE_

In my application, I have legacy roles, e.g login. which do not have ROLE_ prefix.

I want to override above service definition and pass second argument empty.

         <service id="security.access.role_hierarchy_voter" class="%security.access.role_hierarchy_voter.class%" public="false">
            <argument type="service" id="security.role_hierarchy" />
            <argument></argument>
            <tag name="security.voter" priority="245" />
        </service>

How can I do this?

Upvotes: 1

Views: 2717

Answers (1)

xdazz
xdazz

Reputation: 160833

You could create a new class which extends Symfony\Component\Security\Core\Authorization\Voter\RoleHierarchyVoter.

In the class you created, the prefix is under your control, what you have to do is just change the constructor method.

Code:

public function __construct(RoleHierarchyInterface $roleHierarchy)
{
    parent::__construct($roleHierarchy, 'THE_PREFIX_YOU_WANT_TO_SET');
}

Then set the parameter security.access.role_hierarchy_voter.class to the class you created.

Edit:

Set the prefix with empty string could cause warning, overwrite RoleVoter's supportAttribute is another solution.

Upvotes: 2

Related Questions