2013-10-29 31 views
0

我正在爲ACL編寫一個模塊ZF2,而且我幾乎完成了它。從module.php轉發到另一個控制器/動作

我在哪裏stucked的一點是,當用戶未授權訪問請求的頁面,我想向前用戶顯示403消息的頁面。

我已經試過重定向用戶403但它更新URL,所以現在我特林向前用戶。

我想要做的是從Module.php。我曾嘗試下面的代碼 -

Module.php

if (!$isAllowed) { 
    $e->getApplication()->getServiceManager()->get('ControllerPluginManager')->get('forward')->dispatch('acl'); 
} 

使用此我得到了下面的錯誤 -

未捕獲的異常 '的Zend \的mvc \異常\ DomainException' 有消息「前進插件需要實現InjectApplicationEventInterface的控制器'

我也有t執行Acl控制器與InjectApplicationEventInterface,但問題依然如此。

你能解釋一下如何Forward到另一個ActionModule.php
讓我知道你是否需要更多細節。

+0

我不不要以爲你可以使用'forward'插件,或者你應該使用'forward'插件。爲什麼不使用正確的重定向(改變位置)呢? – guessimtoolate

+0

我無法重定向。這是要求。 –

回答

2

你可以做的是聽取派遣活動。您可以在此事件期間更新路線匹配,以匹配由您自己定義的控制器/操作對來渲染403頁面。

在代碼:

use MvcEvent; 

class Module 
{ 
    public function onBootstrap($e) 
    { 
     $app = $e->getApplication(); 
     $acl = $app->getServiceManager()->get('ACL'); // get your ACL here 

     if (!$acl->isAllowed()) { 
      $em = $app->getEventManager(); 
      $em->attach(MvcEvent::EVENT_DISPATCH, function($e) { 
       $routeMatch = $e->getRouteMatch(); 

       $routeMatch->setParam('controller', 'my-403-controller'); 
       $routeMatch->setParam('action', 'my-403-action'); 
      }, 1000); 
     } 
    } 
} 

轉發是派遣一個控制器時,另一個控制器已經派出的模式。這不是你的情況,因爲我從你的問題中讀到它。所以不要使用forward插件,而是在分離前修改路由匹配。

0

不能前頁但重定向到403頁面如下:

if (!$acl->isAllowed()) { 
    $response = $e->getResponse(); 
    $response->getHeaders()->addHeaderLine('Location', $e->getRequest()->getBaseUrl() . '/403page'); 
    $response->setStatusCode(403); 
} 
0

我認爲,如果你想將信息發送到用戶,他是在限制區內沒有你要的網址的更新:

  1. 更改顯示限制區域消息的佈局。
  2. 保留原始佈局,併爲您的操作切換模板,以顯示限制區域消息。

在執行其中一個操作後,您只需將響應更改爲403即可。 如果這就是您想要的簡單操作。對於在您的控制器要發送403種狀態的任何行動只是使用:

$viewModel->setTemplate('partial/noRights'); 
$this->getResponse()->setStatusCode('403'); 
return $viewModel; 

,或者如果你想改變佈局爲您定製一個:

$this->layout('layout/custom'); 
$this->getResponse()->setStatusCode('403'); 
return $viewModel; 

當然你也可以這樣做是爲了在引導你的模塊部分在event_dispatch上添加監聽器,然後檢查$ acl-> isAllowed()之後是否執行上面我寫的更改。例如:

public function onBootstrap(MvcEvent $e) 
{ 
    $app = $e->getApplication(); 
    $acl = $app->getServiceManager()->get('ACL'); // get your ACL here 

    if (!$acl->isAllowed()) { 
     $eventManager = $app->getEventManager(); 
     $sharedEventManager = $eventManager->getSharedManager(); 
     $sharedEventManager->attach(__NAMESPACE__, MvcEvent::EVENT_DISPATCH, function($e) { 
      $controller = $e->getTarget(); //controller`s action which triggered event_dispatch 
      $controller->getResponse()->setStatusCode('403'); 
      $controller->layout('layout/custom'); 
     }, 1000); 
    } 
} 
0

當我實現ACL時,我創建了自己的AUTH模塊進行授權和驗證。
在該模塊I創建的ACL插件..類似下面

//Module.php

/** 
    * This method is called once the MVC bootstrapping is complete 
    * 
    * @param \Zend\EventManager\EventInterface $e 
    */ 
    public function onBootstrap(Event $e) 
    { 
     $services = $e->getApplication()->getServiceManager(); 

     $eventManager = $e->getApplication()->getEventManager(); 
     $eventManager->attach('dispatch', array($this, 'loadConfiguration'), 101); 
    } 

    /** 
    * 
    * @param \Zend\Mvc\MvcEvent $e 
    */ 
    public function loadConfiguration(MvcEvent $e) 
    {   
     $e->getApplication()->getServiceManager() 
       ->get('ControllerPluginManager')->get('AclPlugin') 
       ->checkAcl($e); //Auth/src/Auth/Controller/AclPlugin  
    } 

//認證/ SRC /驗證/控制器/ AclPlugin

namespace Auth\Controller\Plugin; 

/** 
* load libraries here 
*/ 
class AclPlugin extends AbstractPlugin implements ServiceManagerAwareInterface 
{ 
    /* 
    * @var Doctrine\ORM\EntityManager 
    */ 

    protected $em; 
    protected $sm; 

    /** 
    * @param Doctrine\ORM\EntityManager $em 
    * @return string 
    */ 
public function checkAcl($e) 
    { 

     $matches = $e->getRouteMatch(); 
     $controller = $matches->getParam('controller'); 
     $action = $matches->getParam('action', 'index'); 

     if ($acl->isAllowed($role, $resource, $permission)) { 


      return; 

     } else { 
      $matches->setParam('controller', 'Auth\Controller\User'); // redirect 
      $matches->setParam('action', 'accessdenied'); 

      return; 
     } 

} 

/// rest of the code here 

} 
相關問題