验证后执行操作

时间:2013-04-05 09:44:27

标签: php authentication zend-framework2 zfcuser

我希望在用户使用进行身份验证后执行一些操作。

我之前发现了如何执行操作:

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

但验证后没有我错过了触发器,如何找到它?

感谢您的任何建议

3 个答案:

答案 0 :(得分:3)

没有定义认证后触发器(至少在我使用的ZfcUser版本中)。您可以在ZfcUser \ Authentication \ Adapter \ 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;
}

您可以覆盖此类/方法,并将触发器放在上面评论的位置。

答案 1 :(得分:2)

好的,我不知道这是不是很好,但它正在发挥作用..

我刚刚将 login_redirect_route 更改为控制器操作以执行一些有用的操作,然后然后将重定向到用户配置文件。

顺便说一句,触发器完美地工作了@lluisaznar!

答案 2 :(得分:1)

今天也与这个人搏斗,做了一个小小的写作,以防它在未来帮助任何人:

http://circlical.com/blog/2013/7/5/capturing-auth-events-with-zfcuser