Check if a role is granted for a specific user in Symfony2 ACL

后端 未结 3 1424
忘了有多久
忘了有多久 2020-12-10 19:03

I want to check if a role is granted for a specific user in Symfony2 (not the logged user). I know that I can check it for the logged user by:

$securityCont         


        
3条回答
  •  庸人自扰
    2020-12-10 19:45

    You just need to create a custom security context that will take a user object and generate a UserSecurityIdentity out of it. Here are the steps:

    Create a new service in YourApp/AppBundle/Resources/config.yml

    yourapp.security_context:
        class: YourApp\AppBundle\Security\Core\SecurityContext
        arguments: [ @security.acl.provider ]
    

    Create a custom Security Context Class like this:

    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;
            }
        }
    }
    

    Now you can inject that service where needed, or use it from a controller like this:

    $someUser = $this->findSomeUserFromYourDatabase();
    
    if ($this->get('yourapp.security_context')->isGranted('VIEW', $article, $someUser) {
       // ...
    }
    

提交回复
热议问题