1

当覆盖 FOSUserBundle 重置密码控制器时,会调用“authenticateUser”方法(第 104 行):

https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Controller/ResettingController.php#L104

....
$this->authenticateUser($user);
....

我的问题是我已经覆盖了 Symfony 身份验证处理程序,并且在用户登录时有我自己的逻辑。

编辑 这是我的身份验证处理程序:

<?php

/* ... all includes ... */

class AuthenticationHandler implements AuthenticationSuccessHandlerInterface, LogoutSuccessHandlerInterface
{
    private $router;
    private $container;

    public function __construct(Router $router, ContainerInterface $container)
    {
        $this->router = $router;
        $this->container = $container;
    }

    public function onAuthenticationSuccess(Request $request, TokenInterface $token)
    {
        // retrieve user and session id
        $user = $token->getUser();

        /* ... here I do things in database when logging in, and dont want to write it again and again ... */

        // prepare redirection URL
        if($targetPath = $request->getSession()->get('_security.target_path')) {
            $url = $targetPath;
        }
        else {
            $url = $this->router->generate('my_route');
        }

        return new RedirectResponse($url);
    }

}

那么,如何从 ResettingController 中的身份验证处理程序调用“onAuthenticationSuccess”方法?为了避免重写相同的代码...

谢谢你的帮助 !

奥雷尔

4

1 回答 1

1

您应该调用 onAuthenticationSuccess 方法将其作为服务加载。在您的 config.yml 中:

authentication_handler:
    class: Acme\Bundle\Service\AuthenticationHandler
    arguments:
        container: "@service_container"

然后,在 authenticateUser 函数中调用它:

protected function authenticateUser(UserInterface $user) {
      try {
        $this->container->get('fos_user.user_checker')->checkPostAuth($user);
      } catch (AccountStatusException $e) {
          // Don't authenticate locked, disabled or expired users
          return;
      }

      $providerKey = $this->container->getParameter('fos_user.firewall_name');
      $token = new UsernamePasswordToken($user, null, $providerKey, $user->getRoles());
      $this->container->get('security.context')->setToken($token);
      $request = $this->container->get('request');
      $this->container->get('authentication_handler')->onAuthenticationSuccess($request, $token);
}

这可以解决问题并通过您的自定义身份验证处理程序。更多信息

于 2012-05-28T11:06:20.570 回答