注册FOSUserBundle后保持相同的会话

时间:2017-01-30 10:07:56

标签: session symfony registration fosuserbundle

我正在使用FOSUserBundle开发一个symfony 3应用程序。
我希望当前用户(UserA)添加新用户(userB)并保持UserA的会话值。 FOSUser与最后注册用户(UserB)的会话更改。

  

如何在注册userB后将USerA保持在会话中。

1 个答案:

答案 0 :(得分:0)

解决方案:

<?php
namespace UserBundle\EventListener;

use FOS\UserBundle\Event\FilterUserResponseEvent;
use FOS\UserBundle\Event\FormEvent;
use FOS\UserBundle\Event\UserEvent;
use FOS\UserBundle\FOSUserEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;


class RegistrationConfirmListener implements EventSubscriberInterface
{
    private $router;
    private $olduser;

    private $token;



    public function __construct(UrlGeneratorInterface $router, TokenStorage $token)
    {
        $this->olduser = $token->getToken()->getUser();
        $this->router = $router;
        $this->token =$token;
    }

    /**
     * {@inheritDoc}
     */
    public static function getSubscribedEvents()
    {
        return array(
            FOSUserEvents::REGISTRATION_COMPLETED => 'onRegistrationCompleted'
        );
    }

    public function onRegistrationCompleted(FilterUserResponseEvent $event)
    {
        $response = $event->getResponse();
        $this->token->getToken()->setUser($this->olduser);
        $response->setTargetUrl($this->router->generate('immobilier_dashboard'));
    }


}

赞赏任何其他解决方案。