我推荐 Manning Publications 的“Zend Framework in Action”一书作为对此的最新介绍。它可作为 PDF 下载,所以你现在可以拥有它:)
但是要回答这个特定的问题:
让我们从定义两个关键术语开始。
Zend_Auth 中的“Auth”指的是身份验证,它证明某人就是他们所说的人(即登录)。
Zend_Acl 中的“A”指的是授权,它证明某人有权做他们想做的事情(即访问控制)。
假设用户只有一个角色...
将用户的角色存储在您作为 Zend_Auth 的一部分获得的“身份”中。
登录时:
$auth = Zend_Auth::getInstance();
$identity = new stdClass();
$identity->user_pk = $user->getPrimaryKey();
$identity->user_name = $user->getName();
$identity->role = $user->getRole(); // select * from user_role where user_pk=xxx
$auth->getStorage()->write($identity);
在控制器中:
$acl->add(new Zend_Acl_Resource('news'))
->allow('defaultRole', 'news');
默认情况下所有内容都被拒绝,因此您实际上不需要指定:
->deny('defaultRole', 'news', 'add');
在控制器的代码中进一步说明:
$identity = Zend_Auth::getInstance()->getIdentity();
if(!$acl->isAllowed($identity->role, 'news', 'add'))
{
header('Location: http://www.yoursite.com/error/unauthorized');
}
如果用户的身份不允许做“news->add”,它会将他们重定向到未经授权的页面(假设你已经做了这样的页面)。
如果用户拥有 >1 个角色,您将在其身份中存储一组角色。
然后你的支票会是这样的:
$identity = Zend_Auth::getInstance()->getIdentity();
$isAllowed = false;
foreach($identity->role as $role)
{
if($acl->isAllowed($role, 'news', 'add'))
{
$isAllowed = true;
}
}
if(!$isAllowed)
{ // if NO ROLES have access, redirect to unauthorized page
header('Location: http://www.yoursite.com/error/unauthorized');
}
希望对您有所帮助。