我有一个与认证用户工作的多个应用程序。
所有这些应用程序都可以为不同的客户端一起部署,但使用相同的用户数据库。
例如,一家连锁酒店。
用户的角色信息在每个请求的标头中可用。
例如,经理在自己的酒店中具有完全访问权限,但在另一个酒店中仅具有读取访问权限。
例如:
{
["organization":"paris","roles":[ADMIN,ROOT]],
["organization":"london","roles":[READ]]
}
如何按组织处理多个级别的角色?
我在symfony上读到了一些关于投票者和角色的文档,但没有任何关于某种群体中角色的文档。
发布于 2019-03-15 04:25:45
投票者才是前进的方向
// src/Security/PostVoter.php
namespace App\Security;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class OrganisationVoter extends Voter
{
// these strings are just invented: you can use anything
const READ= 'READ';
const EDIT = 'EDIT ';
protected function supports($attribute, $subject); bool //todo
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
// [...] check class like documentation
$organisation= $subject;
switch ($attribute) {
case self::READ:
return $this->canView($organisation, $user);
case self::EDIT:
return $this->canEdit($organisation, $user);
}
}
private function canView(Organisation $organisation, User $user)
{
//here your logic if your user has the same organisation
}
private function canEdit(Organisation $organisation, User $user)
{
//here your logic if your user has the same organisation than the one in parameter and the good level of right (admin, root)
}
}
然后在你的控制器(或细枝或其他任何地方)
if ($this->security->isGranted(OrganisationVoter::EDIT, $organisation)) {
return true;
}
https://stackoverflow.com/questions/55170728
复制相似问题