例如,我在从Zend_Acl_Assert_Interface派生的类中实现了assert方法。
function assert(
Zend_Acl $acl,
Zend_Acl_Role_Interface $user = null,
Zend_Acl_Resource_Interface $item = null,
$privilege = null
) {
if (!$user instanceof User) throw new Exception("…");
if (!$item instanceof Item) throw new Exception("…");
return
$user->money >= $item->price &&
$user->rating >= $item->requiredRating;
}
它检查两个条件:用户有足够的钱和用户有足够的评级。如何显示错误信息,让用户知道哪个条件是失败的isAllowed方法只返回bool?
只需逐个检查
$error = array();
if(!($user->money >= $item->price))
$error[] = "user money is less then price";
if(!($user->rating >= $item->requiredRating))
$error[] = "user rating less then required rating ";
Zend_Registery::set('acl_error',$error);
if(count($error) == 2) return false;
return true;
你可以通过Zend_Registry::get('acl_error');并显示给用户你喜欢的