我想检查是否为Symfony2中的特定用户授予了角色(而不是已登录的用户).我知道我可以通过以下方式检查登录用户:
$securityContext = $this->get('security.context');
if (false === $securityContext->isGranted('VIEW', $objectIdentity)) {
//do anything
}
Run Code Online (Sandbox Code Playgroud)
但如果我是已登录的用户,我的魔杖检查其他用户是否isGranted?
"VIEW"是权限,而不是角色.
检查用户是否有权(无论是角色还是权限)的最佳方法是访问AccessDecisionManager.就像是:
$token = new UsernamePasswordToken($user, 'none', 'none', $user->getRoles());
$attributes = is_array($attributes) ? $attributes : array($attributes);
$this->get('security.access.decision_manager')->decide($token, $attributes, $object);
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请参阅原始答案:https://stackoverflow.com/a/22380765/971254.
您只需创建一个自定义安全上下文,该上下文将获取用户对象并从中生成UserSecurityIdentity.以下是步骤:
在YourApp/AppBundle/Resources/config.yml中创建一个新服务
yourapp.security_context:
class: YourApp\AppBundle\Security\Core\SecurityContext
arguments: [ @security.acl.provider ]
Run Code Online (Sandbox Code Playgroud)
像这样创建自定义安全上下文类:
namespace YourApp\AppBundle\Security\Core;
use Symfony\Component\Security\Acl\Model\MutableAclProviderInterface;
use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity;
use Symfony\Component\Security\Acl\Permission\MaskBuilder;
use Symfony\Component\Security\Acl\Exception\AclNotFoundException;
use Symfony\Component\Security\Acl\Exception\NoAceFoundException;
use YourApp\AppBundle\Document\User;
/**
* Allows ACL checking against a specific user object (regardless of whether that user is logged in or not)
*
*/
class SecurityContext
{
public function __construct(MutableAclProviderInterface $aclProvider)
{
$this->aclProvider = $aclProvider;
}
public function isGranted($mask, $object, User $user)
{
$objectIdentity = ObjectIdentity::fromDomainObject($object);
$securityIdentity = UserSecurityIdentity::fromAccount($user);
try {
$acl = $this->aclProvider->findAcl($objectIdentity, array($securityIdentity));
} catch (AclNotFoundException $e) {
return false;
}
if (!is_int($mask)) {
$builder = new MaskBuilder;
$builder->add($mask);
$mask = $builder->get();
}
try {
return $acl->isGranted(array($mask), array($securityIdentity), false);
} catch (NoAceFoundException $e) {
return false;
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在您可以在需要的地方注入该服务,或者从这样的控制器中使用它:
$someUser = $this->findSomeUserFromYourDatabase();
if ($this->get('yourapp.security_context')->isGranted('VIEW', $article, $someUser) {
// ...
}
Run Code Online (Sandbox Code Playgroud)
无法通过检查其他用户的角色来完成,SecurityContext
因为这将始终保存当前用户的会话令牌。例如getRoles
,如果您需要检查的用户实现了UserInterface
.
$otherUser = $this->get('doctrine')->... // fetch the user
if( $otherUser instanceof \Symfony\Component\Security\Core\User\UserInterface )
{
$roles = $otherUser->getRoles();
// your role could be VIEW or ROLE_VIEW, check the $roles array above.
if ( in_array( 'VIEW' , $roles ) )
{
// do something else
}
}
Run Code Online (Sandbox Code Playgroud)
如果您的用户实体实现了,那么就有一个专用的方法。在这种情况下,您可以使用单行:FosUserBundle
UserInterFace
hasRole
$otherUser = $this->get('doctrine')->... // fetch the user
if( $otherUser instanceof \FOS\UserBundle\Model\UserInterface )
{
// your role could be VIEW or ROLE_VIEW, check the proper role names
if ( $otherUser->hasRole( 'VIEW' ) )
{
// do something else
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
14630 次 |
最近记录: |