2013-04-05 97 views
1

我想在用戶通過進行身份驗證後執行一些操作。認證後執行操作

我已經找到了如何執行前操作:

$events->attach('ZfcUser\Authentication\Adapter\AdapterChain', 'authenticate.pre', function($e) use ($startup) { 
    //actions 
}); 

,但沒有認證後,我錯過觸發,怎麼找呢?

感謝您的任何建議

回答

2

好吧,我不知道這是否是個好辦法,但它的工作..

我剛換了login_redirect_route到控制器動作來執行一些有用的東西,然後重定向到用戶個人資料。

順便說一下,觸發器工作完美@lluisaznar!

3

沒有定義驗證後觸發(至少在我使用ZfcUser版)。您可以在ZfcUser找到現有的觸發器\認證\適配器\ AdapterChain \ prepareForAuthentication()

public function prepareForAuthentication(Request $request) 
{ 
    $e = $this->getEvent() 
       ->setRequest($request); 

    //THE PRE-AUTHENTICATION YOU USE 
    $this->getEventManager()->trigger('authenticate.pre', $e); 

    //THIS ONE LAUNCHES THE AUTHENTICATION. LOOK AT: 
    //ZfcUser\Authentication\Adapter\Db\authenticate() 
    $result = $this->getEventManager()->trigger('authenticate', $e, function($test) { 
     return ($test instanceof Response); 
    }); 

    if ($result->stopped()) { 
     if($result->last() instanceof Response) { 
      return $result->last(); 
     } else { 
      // throw new Exception('Auth event was stopped without a response.'); 
     } 
    } 

    if ($e->getIdentity()) { 
     //THIS IS WHERE THE TRIGGER SHOULD BE PLACED 
     return true; 
    } 

    return false; 
} 

你可以重寫此類/法和放置觸發上述評論的地方。

+0

謝謝您的回答。但我不明白:如何獲得ZfcUser \ Entity \ User,因爲有了這個新的觸發器,我只能從數據庫獲得ID,另外如果用戶登錄(hasIdentity()),我沒有任何狀態。也許一個觸發器必須在驗證功能中設置,但它仍然是一樣的... – 2013-04-05 12:02:06

+0

這是真的。此功能只是驗證並將用戶密鑰放入存儲(會話)中。用戶實體生成的地方是'ZfcUser \ Authentication \ Storage \ read()',它在那裏執行'$ identity = $ this-> getMapper() - > findById($ identity)'。每次執行身份驗證時都會調用它,即在每個頁面請求中。在prepareForAuthentication()中,你還不能獲得實體。一種可能性是在你創建的觸發器之前自己做一個findById,並將實體傳遞給它。 – lluisaznar 2013-04-05 12:57:53

+0

好吧,我不知道這是否是好方法,但它的工作..我剛剛已更改_login_redirect_route_控制器操作來執行一些有用的東西,然後重定向到用戶配置文件。順便說一下,觸發器是完美的工作! – 2013-04-06 11:22:20