2012-05-27 31 views
0

按钮和锚我使用ACL授予资源角色在系统中,允许的动作是excuted并否认行动被路由到自定义页面,我要显示和隐藏菜单元素在运行时使用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

你的方法名称应与类名,因此它应该是允许的,而不是hasAccess。

我自己用的一个全球性的方法秀()而不是使用视图助手

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

更新

全局函数显示了其内部装载库/ Util.php中定义 public/index.php

require_once 'Zend/Application.php'; 
require_once 'Util.php'; 
+0

你在哪里定义了这个全局变量?你是否在插件中定义了它? – palAlaa

+0

我定义了它更容易使用,但你也可以使用视图助手。我已经更新了我的答案,并在信息中定义了它。 –

+0

我怎样才能使它的航海家,因为我创建使用XML文件呢?? – palAlaa