Whats the way to use Zend_Acl in View to show/hide parts of view

拜拜、爱过 提交于 2019-12-09 13:09:30

问题


I am wondering whats the way to use Zend_Acl to show/hide parts of view? I am thinking I will

  1. Create a Controller Plugin that passes the logged in user + acl to view

     $this->view->loggedInUser = Zend_Auth::getIdentity();
     $this->view->acl = Zend_Registry::get('acl');
    
  2. Then in view scripts do something like

    $this->acl->isAllowed($this->view->loggedInUser, 'resource', 'privilege');
    

Or is there a better way? Or should I use a View Helper? That returns a boolean whether the logged in user is allowed?


回答1:


You are using it in the view, so for me ViewHelper is correct place for that - I've done it once that way:

class Zend_View_Helper_HasAccess extends Zend_View_Helper_Abstract
{
    private $_acl;
    public function hasAccess($role, $controller, $action)
    {
        if (!$this->_acl) {
            $this->_acl = Zend_Controller_Front::getInstance()->getPlugin('Acl'); 
            //In yout case registry, but front controller plugin is better way to implement ACL
        }
        return $this->_acl->isAllowed($role, $controller, $action);
    }
}


来源:https://stackoverflow.com/questions/4578391/whats-the-way-to-use-zend-acl-in-view-to-show-hide-parts-of-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!