2011-04-14 25 views
0

我正在使用控制器插件來處理Authentication Plugin。我在application.ini文件中定義了我的導航配置,然後使用它和數據庫用戶記錄動態加載ACL並將其應用於Zend_Navigation。這個位起作用,因爲它成功地加載菜單並且僅顯示用戶被允許看到的頁面。如何從Controller插件中的Zend_Navigation獲取當前頁面

但是,這並不能阻止用戶直接進入頁面。我想要做的是確定用戶何時進入他們無法訪問Controller頁面內的頁面,以便我可以將他們的請求重定向到Authentication頁面。

我在想,必須有一個函數來從Zend_Navigation檢索當前頁面,但是我找不到它......所以也許它不存在。

無論如何,這是我的完整Controller插件。任何人都看到解決方案

<?php 
class Pog_Model_AuthPlugin extends Zend_Controller_Plugin_Abstract 
{ 
    public function preDispatch(Zend_Controller_Request_Abstract $oRequest) 
    { 
     /** 
     * Load user 
     */ 
     $oAuth = Zend_Auth::getInstance(); 
     $oDbUsers = new Pog_Model_DbTable_Users(); 

     if (!$oAuth->hasIdentity()) 
     { 
      $oUser  = $oDbUsers->createRow(); 
      $oUser->name = "guest"; 
      $oUser->setReadOnly(true); 
     } 
     else 
     { 
      $oUser = $oAuth->getIdentity(); 
      $oUser->setTable($oDbUsers); 
     } 

     /** 
     * Load ACL 
     */  
     $oAcl = new Zend_Acl(); 
     $oAcl->addRole($oUser->name); 


     /** 
     * Add current user privileges 
     */ 
     $oPrivileges = $oUser->getPrivileges(); 
     foreach ($oPrivileges as $oPrivilege) 
     { 
      if (!$oAcl->has($oPrivilege->resource)) 
       $oAcl->addResource($oPrivilege->resource); 

      $oAcl->allow($oUser->name, $oPrivilege->resource, $oPrivilege->privilege); 
     } 


     /** 
     * Load Navigation view helper 
     */ 
     $oViewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer'); 
     $oNavigation = $oViewRenderer->view->navigation(); 


     /** 
     * Add remaining Navigation resources 
     */ 
     foreach ($oNavigation->getPages() as $oPage) 
     { 
      if (!is_null($oPage->getResource()) && !$oAcl->has($oPage->getResource())) 
       $oAcl->addResource($oPage->getResource()); 
     } 


     /** 
     * Set ACL and Role 
     */ 
     $oNavigation->setAcl($oAcl)->setRole($oUser->name); 


     /** 
     * Check if use is allowed to be here 
     */ 
     ...MAGIC GOES HERE... 
    } 
} 

回答

4

我認爲你應該能夠得到當前導航頁面如下:

 /** 
    * Load Navigation view helper 
    */ 
    $oViewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer'); 
    $oNavigation = $oViewRenderer->view->navigation(); 

    /*@var $active array */   
    $active = $oNavigation->findActive($oNavigation->getContainer()); 

    /*@var $activePage Zend_Navigation_Page_Mvc */ 
    $activePage = $active['page']; 

    // example of getting page info   
    var_dump($activePage->getLabel(), $activePage->getController(), $activePage->getAction()); 

希望這有助於。

+0

這看起來像什麼我之後,但我得到一個空數組作爲響應。它可能與我在Controller Plugin中運行它有關嗎? – 2011-04-14 13:37:00

+0

我只是把一些測試代碼放在視圖腳本中,上面的工作正常。看起來像導航助手沒有完全設置,當我在控制器插件中找到它,但在那之後它很好...現在找出它何時被設置,看看我是否可以觸發它。 – 2011-04-14 13:55:44

+0

@Vororin。我測試了我的acl控制器插件的preDispatch中的代碼,這沒關係。你如何設置導航?我在bootstrap.php中設置導航?也許這對我來說是這樣初始化的呢? – Marcin 2011-04-15 01:33:58

0

這是我使用的解決方案,因爲由於某種原因我無法在我的設置中使用Marcin的解決方案。

我做了一些更多的思考和思考問題的一個很好的簡單的解決方案。我沒有使用導航模塊來查找活動頁面,而是自己找到它。由於我已經遍歷頁面,所以比較控制器和操作是一塊蛋糕 - 如果這兩者匹配,我有我的活動頁面!

新GETPAGES()foreach循環是這樣的:

$oCurrentPage = null; 
foreach ($oNavigation->getPages() as $oPage) 
{ 
    /** 
    * Check for Current Page 
    */ 
    if ($oPage->getController() == $oRequest->getControllerName() 
     && $oPage->getAction() == $oRequest->getActionName()) 
     $oCurrentPage = $oPage; 


    /** 
    * Add Resource, if missing 
    */ 
    if (!is_null($oPage->getResource()) && !$oAcl->has($oPage->getResource())) 
     $oAcl->addResource($oPage->getResource()); 
} 
相關問題