【问题标题】:How to avoid multiple DB queries with ACL hasPermission in a loop如何避免在循环中使用 ACL hasPermission 进行多个数据库查询
【发布时间】:2012-10-07 07:46:54
【问题描述】:

我正在使用 Symfony2、Doctrine 和 ACL,但遇到了性能问题。

在 Twig 模板中,我需要检查集合中每个公司的当前用户角色以显示链接:

{%- if is_expr_granted("hasRole('ROLE_SUPPORT') or hasPermission(object, 'OPERATOR')", company) -%}
    <a href="configure">{{- company.name -}}</a>
{%- else -%}

我可以在 webProfiler 中看到为每个公司生成了以下 2 个查询:

SELECT a.ancestor_id
FROM acl_object_identities o 
INNER JOIN acl_classes c ON c.id = o.class_id INNER JOIN acl_object_identity_ancestors a ON a.object_identity_id = o.id
WHERE ((o.object_identifier = '154' AND c.class_type = 'St\\CoreBundle\\Entity\\Company'))`

SELECT o.id as acl_id, o.object_identifier, o.parent_object_identity_id, o.entries_inheriting, c.class_type, e.id as ace_id, e.object_identity_id, e.field_name, e.ace_order, e.mask, e.granting, e.granting_strategy, e.audit_success, e.audit_failure, s.username, s.identifier as security_identifier 
FROM acl_object_identities o
INNER JOIN acl_classes c ON c.id = o.class_id
LEFT JOIN acl_entries e
    ON ( e.class_id = o.class_id AND (e.object_identity_id = o.id OR e.object_identity_id IS NULL) ) 
LEFT JOIN acl_security_identities s ON ( s.id = e.security_identity_id )
WHERE (o.id =2189)

如何避免循环查询?

【问题讨论】:

    标签: optimization symfony doctrine-orm acl


    【解决方案1】:

    Acl 身份可以在一个查询中预加载。看到这个问题:How do one use ACL to filter a list of domain-objects according to a certain user's permissions (e.g. EDIT)?

    你必须在你的控制器中添加这样的东西

    // you entities
    $companies = ...
    
    // the acl provider
    $aclProvider = $this->get('security.acl.provider');
    
    // build up array of object identities
    $oids = array();
    foreach ($companies as $company) {
      $oids[] = ObjectIdentity::fromDomainObject($company);
    }
    
    // preload acls
    $aclProvider->findAcls($oids); // preload Acls from database
    

    【讨论】:

    • 感谢房间,我们将对其进行测试:)
    猜你喜欢
    • 2017-09-08
    • 1970-01-01
    • 2011-05-01
    • 2017-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-10
    • 1970-01-01
    相关资源
    最近更新 更多