【问题标题】:Hide and show navigator menu items, buttons and anchors using ACL使用 ACL 隐藏和显示导航器菜单项、按钮和锚点
【发布时间】:2012-06-02 03:09:30
【问题描述】:

我正在使用 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"

我如何使用这个帮助器在运行时创建视图?

【问题讨论】:

    标签: zend-framework resources acl role


    【解决方案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);
        }
    

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

    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';
    

    【讨论】:

    • 你在哪里定义了这个全局变量?你是在插件里面定义的吗?
    • 我定义它是因为它更易于使用,但您也可以使用视图助手。我已经用我定义它的信息更新了我的答案。
    • 我如何为导航器制作它,因为我使用 xml 文件创建它??
    • 如果您使用 Zend_Navigation 及其视图助手之一,那么您不需要创建任何内容,如果不是,则只需遍历 xml 节点并在每个节点上调用 show 方法。
    猜你喜欢
    • 1970-01-01
    • 2020-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    • 2010-11-18
    相关资源
    最近更新 更多