0

I am using ACL to grant resources to roles in the system, the allowed actions is excuted and denied actions are routed to custom page, I want to show and hide menu elements at run time using resources at ACL, and also I want to show and hide anchors, buttons in views.

I make a helper class

  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);
  }
} 

I define the view helper in config.ini file like this

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

how can I use this helper to make views created at run time?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
palAlaa
  • 9,500
  • 33
  • 107
  • 166

1 Answers1

1

Your method name should match class name hence it should be permission instead of hasAccess.

I myself use a global method show() instead of using view helper

    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);
    }

To keep it simple it takes controller , module name from request object . To hide edit action link in list action view simply doo

list.phtml code as follow

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

Update

The global function show was defined inside library/Util.php which was loaded inside public/index.php

require_once 'Zend/Application.php';
require_once 'Util.php';
Mr Coder
  • 8,169
  • 5
  • 45
  • 74
  • Where did you define this global variable? did you define it inside a plugin ? – palAlaa May 27 '12 at 10:03
  • I defined it coz it easier to use but you can use view helper also . I have update my answer with info where I defined it. – Mr Coder May 27 '12 at 10:12
  • how can I make it for navigator, since I create it using xml file?? – palAlaa May 27 '12 at 11:03
  • 1
    If you are using Zend_Navigation and its one of view helper then you don't need to create anything if not then simply iterate over xml nodes and call show method on each of them. – Mr Coder May 27 '12 at 11:42