2011-11-16 101 views
5

我正在使用Zend Acl和Zend Navigation。我在bootstrap中设置导航。我试图让链接不显示,如果用户不能访问资源。我看过几个教程,通过了Zend参考了手动几次,但都在导航的链接仍然出现在来宾用户即使有些只能显示一个管理员用户Zend导航和Zend ACL

protected function _initNavigationMenu() 
{ 
    $this->bootstrap("layout"); 
    $layout = $this->getResource('layout'); 
    $view = $layout->getView(); 
    $navigation_model = new Core_Model_Navigation(); 
    $result = $navigation_model->getTopLevelNavigationLinksForDisplay(); 
    $sanitized = $navigation_model->sanatizeNavigationForDisplay($result); 

    $config = new Zend_Config($sanitized); 
    $nav = new Zend_Navigation($config); 

    $view->navigation($nav) 
      ->setAcl($this->_acl->acl()) 
      ->setRole((string)BW::user() -> role); 
} 

所有ACL角色和资源和导航的情况下,来自DB的是,这里重要的

创建德阵列$消毒

Array 
(
    [0] => Array 
     (
      [parent_id] => 0 
      [label] => File Manager 
      [order] => 1 
      [resource] => 9 
      [active] => 1 
      [visible] => 1 
      [internal_page] => 1 
      [module] => file 
      [reset_params] => 1 
      [id] => fileManagerLink 
     ) 

    [1] => Array 
     (
      [parent_id] => 0 
      [label] => Upload 
      [title] => Upload a file 
      [order] => 2 
      [resource] => 9 
      [active] => 1 
      [visible] => 1 
      [internal_page] => 1 
      [controller] => upload 
      [module] => file 
      [reset_params] => 1 
      [id] => fileManagerUploadLink 
     ) 

    [2] => Array 
     (
      [parent_id] => 0 
      [label] => Files 
      [title] => Manage your files 
      [order] => 3 
      [resource] => 9 
      [active] => 1 
      [visible] => 1 
      [internal_page] => 1 
      [controller] => manage 
      [module] => file 
      [reset_params] => 1 
      [id] => FileManagerFilesLink 
     ) 

    [3] => Array 
     (
      [parent_id] => 0 
      [label] => Contacts 
      [order] => 4 
      [resource] => 9 
      [active] => 1 
      [visible] => 1 
      [internal_page] => 1 
      [controller] => contact 
      [module] => file 
      [reset_params] => 1 
      [id] => Contacts 
     ) 

    [4] => Array 
     (
      [parent_id] => 0 
      [label] => My Account 
      [title] => Your Account 
      [order] => 5 
      [resource] => 9 
      [active] => 1 
      [visible] => 1 
      [internal_page] => 1 
      [action] => index 
      [controller] => user 
      [reset_params] => 1 
      [id] => myAccountNavigationLink 
     ) 

    [5] => Array 
     (
      [parent_id] => 0 
      [label] => Admin 
      [title] => The administration panel 
      [order] => 6 
      [resource] => 9 
      [active] => 1 
      [visible] => 1 
      [internal_page] => 1 
      [module] => admin 
      [reset_params] => 1 
      [id] => Administration 
     ) 

    [6] => Array 
     (
      [parent_id] => 0 
      [label] => Test for ACL 
      [order] => 0 
      [resource] => 9 
      [active] => 1 
      [visible] => 1 
      [internal_page] => 1 
      [action] => add-navigation 
      [controller] => manage 
      [module] => admin 
      [reset_params] => 1 
     ) 

    [7] => Array 
     ( 
      [parent_id] => 0 
      [label] => Test for ACL 
      [order] => 0 
      [resource] => 9 
      [active] => 1 
      [visible] => 1 
      [internal_page] => 1 
      [action] => add-navigation 
      [controller] => manage 
      [module] => admin 
      [reset_params] => 1 
     ) 

    [8] => Array 
     (
      [parent_id] => 0 
      [label] => Test for ACL 
      [order] => 0 
      [resource] => 9 
      [active] => 1 
      [visible] => 1 
      [internal_page] => 1 
      [action] => add-navigation 
      [controller] => manage 
      [module] => admin 
      [reset_params] => 1 
     ) 

    [9] => Array 
     (
      [parent_id] => 0 
      [label] => Test for ACL 
      [order] => 0 
      [resource] => 9 
      [active] => 1 
      [visible] => 1 
      [internal_page] => 1 
      [action] => add-navigation 
      [controller] => manage 
      [module] => admin 
      [reset_params] => 1 
     ) 

    [10] => Array 
     (
      [parent_id] => 0 
      [label] => ACL Test 
      [order] => 0 
      [resource] => 8 
      [privilage] => index 
      [active] => 1 
      [visible] => 1 
      [internal_page] => 1 
      [action] => add-navigation 
      [controller] => manage 
      [module] => admin 
      [reset_params] => 1 
     ) 

    [11] => Array 
     (
      [parent_id] => 0 
      [label] => Joey 
      [order] => 0 
      [resource] => adminIndexIndex 
      [privilage] => index 
      [active] => 1 
      [visible] => 1 
      [internal_page] => 1 
      [action] => add-navigation 
      [controller] => manage 
      [module] => admin 
      [reset_params] => 1 
     ) 

    [12] => Array 
     (
      [parent_id] => 0 
      [label] => another test 
      [order] => 0 
      [resource] => 9 
      [active] => 1 
      [visible] => 1 
      [internal_page] => 1 
      [action] => add-navigation 
      [controller] => manage 
      [module] => admin 
      [reset_params] => 1 
     ) 

    [13] => Array 
     (
      [parent_id] => 0 
      [label] => another test 
      [order] => 0 
      [resource] => 9 
      [active] => 1 
      [visible] => 1 
      [internal_page] => 1 
      [action] => add-navigation 
      [controller] => manage 
      [module] => admin 
      [reset_params] => 1 
     ) 

    [14] => Array 
     (
      [parent_id] => 0 
      [label] => another test 
      [order] => 0 
      [resource] => 9 
      [active] => 1 
      [visible] => 1 
      [internal_page] => 1 
      [action] => add-navigation 
      [controller] => manage 
      [module] => admin 
      [reset_params] => 1 
     ) 

    [15] => Array 
     (
      [parent_id] => 0 
      [label] => another stupid test 
      [order] => 0 
      [resource] => Admin Homepage 
      [privilage] => index 
      [active] => 1 
      [visible] => 1 
      [internal_page] => 1 
      [action] => add-navigation 
      [controller] => manage 
      [module] => admin 
      [reset_params] => 1 
     ) 

    [16] => Array 
     (
      [parent_id] => 0 
      [label] => another stupid test 
      [order] => 0 
      [resource] => 9 
      [active] => 1 
      [visible] => 1 
      [internal_page] => 1 
      [action] => add-navigation 
      [controller] => manage 
      [module] => admin 
      [reset_params] => 1 
     ) 

) 

回答

15

乔伊,

很好的文档可以很难找到,但它存在。你需要做的,在你的应用程序的自举什么是两两件事:

  1. 初始化你的ACL的
  2. 将它们链接到您的导航对象

在我白手起家,我用类似以下的功能去做这个。这里的关键方面的例子:

生成ACL的:

protected function _buildAclList() 
{ 
    $acl = new Zend_Acl(); 

    // setup the roles for the application 
    $acl->addRole(new Zend_Acl_Role('guest')); 

    $moduleResource = new Zend_Acl_Resource('administration'); 

    $acl->add($moduleResource) 
     ->add(new Zend_Acl_Resource('admin:copyright'), $moduleResource); 

    $acl->allow(
     array('guest'), 
     array('admin:copyright'), 
     array('view') 
    ); 

    Zend_Registry::set('acl', $acl); 
    return $acl; 
} 

这里,ACL的需要为您的应用程序都安装。资源方法返回它们供其他地方使用,并且它们也存储在注册表中。

链接导航到生成ACL的(还指定一个默认的角色):

protected function _buildNavigationList() 
{ 
    $this->bootstrap('layout'); 
    $layout = $this->getResource('layout'); 
    $view = $layout->getView(); 
    $config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml', 'nav'); 
    $acl = Zend_Registry::get('acl'); 
    $navigation = new Zend_Navigation($config); 
    $view->navigation($navigation); 
    Zend_View_Helper_Navigation_HelperAbstract::setDefaultAcl($acl); 
    Zend_View_Helper_Navigation_HelperAbstract::setDefaultRole(
     Common_Controller_Plugin_Acl::DEFAULT_ROLE 
    ); 
    return $navigation; 
} 

资源方法拿起从注册表之前创建的ACL的,并使用setDefaultAcl方法把它们分配给应用程序导航对象以及默认角色。

尊重的ACL

<?xml version="1.0" encoding="UTF-8"?> 
<config> 
    <nav> 
     <administration> 
      <label>Administration</label> 
      <uri></uri> 
      <resource>reports:report</resource> 
      <privilege>view</privilege> 
      <pages> 
       <page_admin_copyright> 
        <label>Copyright maintenance</label> 
        <uri>/admin/copyright</uri> 
        <resource>admin:copyright</resource> 
        <privilege>view</privilege> 
       </page_admin_copyright> 
      </pages> 
     </administration> 
    </nav> 
</config> 

这里建的导航,我们创建了一个叫做部分管理,要求用户有查看权限管理员:版权资源,其中客这要感谢预制的acl列表。

现在,当您调用$ this-> navigation() - > menu() - > render()等时,菜单选项将基于用户的访问权限。

嗯,我想我应该在我的网站上添加一篇文章。一切顺利吧。

马特

0

我建议你添加了一个标签在您的导航XML文件的东西,涉及到ACL资源,在你保存你的ACL映射你的ini文件映射。逻辑可以在引导程序中最好地实现,以便在事情发生之前就可以读取它。不要忘了将它实现到你的ajax调用,以防有一个。

0

我认为一个控制器插件是更好地与Zend_Nav和Zend_Acl把管理,如:

class App_Controller_Plugin_Layout extends Zend_Controller_Plugin_Abstract 
{ 


    protected $_layout; 
    protected $_view; 

    public function preDispatch(Zend_Controller_Request_Abstract $request) 
    { 
     $this->_layout = Zend_Controller_Action_HelperBroker::getStaticHelper('Layout'); 
     $this->_layout->disableLayout(); 
     $this->_view = $this->_layout->getView(); 
     $module = $request->getModuleName(); 
     if(null === $module){ 
      $module = 'default'; 
     } 
     $this->_buildMenu($module,$request); 
     $this->_layout->setLayout($module); 
    } 

    protected function _buildMenu($module,$request) 
    { 
     $configFilename = APPLICATION_PATH . '/modules/'.$module.'/configs/navigation.xml'; 
     if(file_exists($configFilename)){ 
      $role = null; 
      $view= $this->_layout->getView(); 
      $config = new Zend_Config_Xml($configFilename, 'nav'); 
      $container = new Zend_Navigation($config); 
      $view->navigation($container); 
      $uri = $request->getPathInfo(); 
      $pages = $container->getPages(); 
      foreach($pages as $page){ 
       $page->setParams(array('ref'=>$request->getParam('ref'))); 
      } 
      $activeNav = $view->navigation()->findByUri($uri); 
      if ($activeNav == null){ 
       $activeNav = $view->navigation()->findOneByController($request->getControllerName()); 
      } 
      if ($activeNav != null){ 
       $activeNav->active = true; 
       $customCls = $activeNav->getClass(); 
       $activeNav->setClass('active'.!empty($customCls)?' '.$customCls:''); 
      } 
      $front = Zend_Controller_Front::getInstance(); 
      if ($front->hasPlugin('App_Controller_Plugin_Acl')) { 
       $aclPlugin = $front->getPlugin('App_Controller_Plugin_Acl'); 
      } 
      else{ 
       $front->registerPlugin(new App_Controller_Plugin_Acl()); 
       $aclPlugin = $this->getAclPlugin(); 
      } 
      $auth = Zend_Auth::getInstance(); 
      if ($auth->hasIdentity()) { 
       $role = is_object($auth->getIdentity())?$auth->getIdentity()->role:null; 
      } 
      $view->navigation()->setAcl($aclPlugin->getAcl())->setRole($role); 
     } 
    } 

} 
0

试试这个建立导航动态地bootstarp

protected function _initNavigation() 
      { 



      $this->bootstrap('layout'); 
      $layout = $this->getResource('layout'); 
      $view = $layout->getView(); 

      $config = $this->getOptions(); 

      $db = Zend_Db::factory($config['resources']['db']['adapter'], $config['resources']['db']['params']); 

      if ($db) { 
       $sql = "your query1 here"; 

       $result= $db->query($sql)->fetchAll(); 
       $configuration = array(); 
       if (count($result)){ 
       foreach($result as $key=>$row) 
       { 


        $subsql = "your query 2 here"; 

        $subMenu = $result= $db->query($subsql)->fetchAll(); 
        if(count($subMenu)>0){ 
         $pages = array(); 
        foreach ($subMenu As $k=>$v){ 
         $subcatpages = array(); 
         $subcatgroup = array(); 
         $group = array(); 
         $page['label'] =$v['heading']; 
          if ($v['path']) == $row['path']){ 

         $page['uri'] ='/'.$row['path'].'.html'; 
         }elseif($row['id'] ==76){ 

         $page['uri'] ='/'.$v['path'].'.html'; 
          }else{ 

         $page['uri'] ='/'.$row['path'].'/'.$v['path'].'.html'; 
                  } 


         $supersubsql = "Query 3"; 

         $superSubMenu = $db->query($supersubsql)->fetchAll(); 
          if(count($superSubMenu)>0){ 
          if ($row['id'] != 76){ 
         foreach ($superSubMenu as $menu=>$item){ 

           $subpage['label'] =$item['heading']; 
           if ($v['path'] == $row['path']){ 


           $subpage['uri'] ='/'.$row['path'].'/'.$item['path'].'.html'; 

           }else{ 
          $subpage['uri'] = '/'.$row['path'].'/'.$v['path'].'/'.$item['path'].'.html'; 

           } 

           $subpage['params'] = array('action'=>'index', 
            'category'=> $item['path']); 

         $group[] =$subpage; 

         } 

         } 
         $page['pages'] =$group; 
         foreach ($group as $k=>$v){ 
          unset($group[$k]); 
         }  

         } 




         $pages[] =$page; 
         foreach ($page as $k=>$v){ 
          unset($page[$k]); 
         } 

        } 

        } 
        $configuration[$row['name']] = array(
          'label' => $row['name'], 
          'uri' => '/'.$row['path'].'.html', 


          ), 

          'pages' => $pages, 

        ); 
       } 

        } 
        } 

      $navigation = new Zend_Navigation($configuration); 
      $view->navigation($navigation); 

      } 

我希望它帮你