Symfony2重定向响应在EventSubscriberInterface中不起作用

时间:2015-07-31 02:06:58

标签: php api symfony login

我正在尝试在用户登录时进行api调用.api调用有效,我可以成功将用户登录到XenForo,但问题是用户没有登录到Symfony2?

有人能发现为什么我的代码会发生这种情况吗?

namespace Company\UserBundle\Event;

use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\UserEvent;
use FOS\UserBundle\Model\UserManagerInterface;
use FOS\UserBundle\Model\UserInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Http\SecurityEvents;
use Symfony\Component\HttpFoundation\RedirectResponse;

use Company\UserBundle\Service\XenApiService;

/**
 * Listener responsible to change the redirection at the end of the password resetting
 */
class LoginListener implements EventSubscriberInterface
{
    protected $userManager;

    public function __construct(UserManagerInterface $userManager)
    {
        $this->userManager = $userManager;
    }

    /**
     * {@inheritDoc}
     */
    public static function getSubscribedEvents()
    {
        return array(
            SecurityEvents::INTERACTIVE_LOGIN => 'onSecurityInteractiveLogin'
        );
    }

    public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
    {

        // Build a readable session
        $user = $event->getAuthenticationToken()->getUser();
        $session = $event->getRequest()->getSession();
        $session->set('username', $user->getHandle());

        if ($user instanceof UserInterface) {
            $user->setLastLogin(new \DateTime());
            $this->userManager->updateUser($user);
        }

        // Authenticate with XenForo
        $xenAPI = new XenAPIService('http://forum.Company-local.com:8080/api.php', '123456789', 's5OoXYDQ');
        $url = $xenAPI->login('james@beamish-white.com', 'password', 'http://Company-local.com:8080/app_dev.php');
        $response = new RedirectResponse($url);

        $event->setResponse($response);

        // try {

        //     $url = $xenAPI->login('james@beamish-white.com', 'password', 'http://Company-local.com:8080/app_dev.php');
        //     $response = new RedirectResponse($url);
        //     //$response->send();

        // } catch (Exception $e) {
        //     if ($e->getCode() == 400) {
        //         $error = json_decode($e->getMessage(), TRUE);
        //         die('API call failed: API ERROR CODE=' . $error['error'] . ' & API ERROR MESSAGE=' . $error['message']);
        //     } else {
        //         die('API call failed: HTTP RESPONSE=' . $e->getMessage() . ' & HTTP STATUS CODE=' . $e->getCode());
        //     }
        // }

    }
}

1 个答案:

答案 0 :(得分:1)

我认为app_dev.php(如果在Symfony防火墙下)将执行另一次重定向。为什么不以编程方式对用户进行身份验证?

$token = new UsernamePasswordToken('james@beamish-white.com', 'password', 'public');
$container->get("security.context")->setToken($token);

// Fire the login event
$login_event = new InteractiveLoginEvent($event->getRequest(), $token);
$container->get("event_dispatcher")->dispatch("security.interactive_login", $login_event);

请记住同时注入Container(或更好的是,只注入security.contextevent_dispatchersetter injection(例如)