使用ACL隐藏和显示导航器菜单项,按钮和锚点

时间:2012-05-27 09:34:30

标签: zend-framework resources acl role

我使用ACL为系统中的角色授予资源,允许的操作被执行,被拒绝的操作被路由到自定义页面,我想在运行时使用ACL的资源显示和隐藏菜单元素,我也想要在视图中显示和隐藏锚点,按钮。

我做了一个帮助类

  class Zend_View_Helper_Permission extends Zend_View_Helper_Abstract
  {
   private $_acl;
    public function hasAccess($role, $action, $controller)
    {
      if (!$this->_acl) {

           $this->_acl = Zend_Registry::get("Acl");
    }

     return $this->_acl->isAllowed($role, $controller, $action);
  }
} 

我在config.ini文件中定义视图助手,如此

resources.view.helperPath.Zend_View_Helper = APPLICATION_PATH "/modules/privileges/views/helpers"

如何使用此帮助程序在运行时创建视图?

1 个答案:

答案 0 :(得分:1)

您的方法名称应与类名匹配,因此它应该是权限而不是hasAccess。

我自己使用全局方法show()而不是使用视图助手

    function show($action = null)
    {

        $request = Zend_Controller_Front::getInstance()->getRequest();
        $action = $action === null ? $request->getActionName() : $action;
        $module = $request->getModuleName();
        $controller = $request->getControllerName();

        if(!Zend_Registry::isRegistered('acl')) throw new Exception('Show function can only be called inside view after preDispatch');

        $acl = Zend_Registry::get('acl');
$resource = $module . '#' . $controller;
        return $acl->isAllowed(Zend_Auth::getInstance()->getIdentity(),$resource,$action);
    }

为了简单起见,它需要来自请求对象的控制器,模块名称。 要在列表操作视图中隐藏编辑操作链接,只需执行

list.phtml代码如下

<h2>Listing page Only superadmin can see edit link</h2>
<?php if(show('edit')): ?>
<a href="<?echo $this->url(array('action'=>'edit')) ?>">Edit</a>
<?php endif;?>

更新

全局函数show在library / Util.php中定义,里面已加载 公共/ index.php的

require_once 'Zend/Application.php';
require_once 'Util.php';
相关问题