首页 > Linux操作系统 > Linux操作系统 > 学习笔记三 创建保存动态的ACL

This is the basic factory we’ll be using now:
class AclFactory {
/**
* Creates an ACL for a specific page
* @param Page $page
* @return Zend_Acl
*/
public function createAcl(Page $page) {
//Lets assume we have a model for the page_privileges with a method like this
//which would return PagePrivilege objects with the page_id passed as the param.
$privileges = PagePrivilege::findByPageId($page->getId());
$acl = new Zend_Acl();
$acl->add(new Zend_Acl_Resource($page->getId()));
foreach($privileges as $privilege) {
$acl->addRole(new Zend_Acl_Role($privilege->getUserId()));
$acl->allow($privilege->getUserId(), $page->getId());
}
return $acl;
}
}
//Lets say $page is a page we want to access
$factory = new AclFactory();
$acl = $factory->createAcl($page);
//and $user is the currently logged in user
if($acl->hasRole($user->getId()) && $acl->isAllowed($user->getId(), $page->getId()) {
//Access OK
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/8225983/viewspace-662901/ ,如需转载,请注明出处,否则将追究法律责任。