如何在Symfony2中动态更改用户角色

时间:2014-01-30 15:43:38

标签: symfony

我需要根据会话中存储的特定数据动态更改用户角色。因此,在每个请求上,授权系统可以检查用户是否可以访问某些资源。我不希望在数据库中存储角色。该角色将取决于数据。

例如,真实州公司admin的用户可以管理其代理和属性,但agent只能管理分配给他的属性。另外,buyer可以查看他已购买的房产数据。

1 个答案:

答案 0 :(得分:0)

查看This blog post,它显示了如何添加用户相关角色。您可以修改实际设置角色名称的部分。

虽然我会对这个方法进行一些修改

class User implements UserInterface
{
    public function getRoles()
    {
        return new UserDependentRole($this);
    }
}

UserDependentRole课程中,请改为:

public function getRole()
{
    $roles[] = 'ROLE_' . strtoupper($this->user->getUsername());
    //... make an array of all the roles you want the user to have

    return $roles;
}

我确定你读过这篇文章,你会弄明白该怎么做。

相关问题