How to use the AccessDecisionManager in Symfony2 for authorization of arbitrary users?

后端 未结 7 1864
庸人自扰
庸人自扰 2020-12-14 21:14

I\'d like to be able to verify whether or not attributes (roles) are granted to any arbitrary object implementing UserInterface in Symfony2. Is this possible?

7条回答
  •  囚心锁ツ
    2020-12-14 22:00

    This looks like an issue with the:

    abstract class AbstractToken implements TokenInterface

    Look at the constructor. Looks like roles are created on instantiation and not queried at run time.

    public function __construct(array $roles = array())
    {
        $this->authenticated = false;
        $this->attributes = array();
    
        $this->roles = array();
        foreach ($roles as $role) {
            if (is_string($role)) {
                $role = new Role($role);
            } elseif (!$role instanceof RoleInterface) {
                throw new \InvalidArgumentException(sprintf('$roles must be an array of strings, or RoleInterface instances, but got %s.', gettype($role)));
            }
    
            $this->roles[] = $role;
        }
    }
    

    Hence, the roles cannot change after the token has been created. I think the option is to write your own voter. I'm still looking around.

提交回复
热议问题