5

我正忙于需要一些 ACL 权限的 Symfony2 应用程序。

我是 Symfony2 的新手,所以不确定我是否以正确的方式看待这个问题。

我有多个客户,每个客户都有多个帐户。

我有一个超级管理员 (ROLE_SUPER_ADMIN) 可以访问所有客户和所有帐户。然后我有一个管理员角色 (ROLE_ADMIN),它只被允许访问特定客户端和该客户端的所有帐户。然后是代理(ROLE_AGENT),它应该只对客户的某些帐户具有权限。

我在 symfony 文档中看到要让用户访问特定对象,我可以使用以下代码:

// creating the ACL
$aclProvider = $this->get('security.acl.provider');
$objectIdentity = ObjectIdentity::fromDomainObject($account);
$acl = $aclProvider->createAcl($objectIdentity);

// retrieving the security identity of the currently logged-in user
$securityContext = $this->get('security.context');
$user = $securityContext->getToken()->getUser();
$securityIdentity = UserSecurityIdentity::fromAccount($user);

// grant owner access    
$acl->insertObjectAce($securityIdentity, MaskBuilder::MASK_OWNER);
$aclProvider->updateAcl($acl);

所以在创建新帐户时,我可以让当前登录的用户访问新创建的帐户。但是,我如何授予客户端访问帐户的所有其他用户的访问权限?

我不想遍历所有用户并为每个用户运行上述代码。

因此,例如在查看所有客户端时,我需要知道用户可以访问哪些客户端,或者在查看帐户时,我需要知道用户可以访问哪些帐户。

此外,当向客户端添加新用户时,用户自动需要访问该客户端的所有帐户。

作为旁注,我只需要知道用户是否有权访问帐户/客户端。如果用户有权访问,那么他们将被自动允许查看/编辑/删除等。

4

1 回答 1

0

对于这种情况,我使用了一个自定义安全服务来验证实体之间的多对多关系。这不是理想的决定,但请记住。

首先,我们需要制作将在每个控制器操作时触发的侦听器。

class SecurityListener
{
    protected $appSecurity;

    function __construct(AppSecurity $appSecurity)
    {
        $this->appSecurity = $appSecurity;
    }

    public function onKernelController(FilterControllerEvent $event)
    {
        $c = $event->getController();

        /*
         * $controller passed can be either a class or a Closure. This is not usual in Symfony2 but it may happen.
         * If it is a class, it comes in array format
         */
        if (!is_array($c)) {
            return;
        }

        $hasAccess = $this->appSecurity->hasAccessToContoller($c[0], $c[1], $event->getRequest());

        if(!$hasAccess) {
            throw new AccessDeniedHttpException('Access denied.');
        }

    }
}

在服务中,我们可以访问请求、控制器实例和被调用的操作。所以我们可以决定是否有用户访问权限。

class AppSecurity
{
    protected $em;
    protected $security;
    /** @var $user User */
    protected $user;

    public function __construct(EntityManager $em, SecurityContext $security)
    {
        $this->em = $em;
        $this->security = $security;

        if($security->getToken() !== null && !$security->getToken() instanceof AnonymousToken) {
            $this->user = $security->getToken()->getUser();
        }
    }

    /**
     * @param $controller
     * @param string $action
     */
    public function hasAccessToContoller($controller, $action, Request $request)
    {
        $attrs = $request->attributes->all();
        $client = $attrs['client'];

        /* db query to check link between logged user and request client */
    }
}

如果您使用非常讨厌的注释,ParamConverter您可以轻松地从请求中提取准备使用的实体。

于 2013-08-09T10:31:58.733 回答