我需要根据存储在会话中的特定数据动态地更改用户角色。因此,对于每个请求,授权系统可以检查用户是否可以访问某些资源。我不想要数据库中的商店角色。角色将依赖于数据。
例如,作为房地产公司admin的用户可以管理其代理和属性,但agent只能管理分配给他的属性。此外,buyer还可以查看他所购买的房产的数据。
发布于 2014-01-31 00:04:28
看看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;
}我相信,如果你读了这篇文章,你会知道怎么做的。
https://stackoverflow.com/questions/21460944
复制相似问题