分配角色:
$roleGuest = new Zend_Acl_Role('guest');
$acl->addRole($roleGuest);
$acl->addRole(new Zend_Acl_Role('staff'), $roleGuest);
$acl->addRole(new Zend_Acl_Role('editor'), 'staff');
$acl->addRole(new Zend_Acl_Role('administrator'));
guest->staff->editor派生关系;administrator另外新建的角色
添加权限:
// Guest 只可以浏览内容
$acl->allow($roleGuest, null, 'view');
// Staff 从 guest 继承浏览权限,但也要另外的权限
$acl->allow('staff', null, array('edit', 'submit', 'revise'));
// Editor 从 Staff 继承 view, edit, submit 和 revise 权限 但也要另外的权限
$acl->allow('editor', null, array('publish', 'archive', 'delete'));
// Administrator 不需要继承任何
$acl->allow('administrator');
查询权限:
$acl->isAllowed('guest', null, 'view') ? "allowed" : "denied"; // 允许
$acl->isAllowed('editor', null, 'view') ? "allowed" : "denied"; // 允许,因为从 guest 继承而来
$acl->isAllowed('administrator') ? "allowed" : "denied"; // 允许,因为 administrator 有所有权限
$acl->isAllowed('administrator', null, 'update') ? "allowed" : "denied"; // 允许,因为 administrator 有所有权限
下面这句话的意思是:给marketing角色赋予publish和archive的权利去操作资源newsletter和latest
$acl->allow('marketing', array('newsletter', 'latest'), array('publish', 'archive'));
增加和删除权限:
//除去 “禁止 staff 修订最近的新闻”
$acl->removeDeny('staff', 'latest', 'revise');
echo $acl->isAllowed('marketing', 'latest', 'revise') ? "allowed" : "denied"; // allowed可以访问了
// 除去 marketing 发布和归档 newsletters 的许可
$acl->removeAllow('marketing', 'newsletter', array('publish', 'archive'));
echo $acl->isAllowed('marketing', 'newsletter', 'publish') ? "allowed" : "denied"; // denied权限移除拒绝访问