我一直在阅读 Zend 框架的 Zend_Acl 组件。我想实现一个数据库驱动的解决方案,但注意到我已经从数据库中加载所有角色、权限和资源来构建 ACL。根据需要将这些规则延迟加载到 ACL 中看起来并不容易或不可能。看起来如果实现了任何延迟加载,整个 Zend_Acl 类都必须被删除。有没有人有一个很好的例子来说明如何做到这一点?
问问题
374 次
1 回答
1
The approach I would recommend is to build the ACL from the DB and cache it with Zend_Cache so it doesn't need to be loaded with every request.
Firstly, build the ACL object with $acl = new Zend_Acl();
Then get your roles, resources and permissions from the DB. Loop through your resources and roles and add them with $acl->addResource()
and $acl->addRole()
, then add permissions with $acl->allow()
Assuming you have bootstrapped your $cache
object and it is available here (via Zend_Registry or whatever), do something like:
if (($acl = $cache->load('acl')) === false) {
$acl = $this->_buildACL(); // Build you ACL in the _buildAcl() method
$cache->save($acl, 'acl', array('ACL'));
}
Zend_Registry::set('acl', $acl);
于 2014-02-04T11:35:51.973 回答