注册 FOSUserBundle 后保持相同的会话
Keep same session after registration FOSUserBundle
我正在开发一个使用 FOSUserBundle 的 Symfony 3 应用程序。
我希望当前用户 (UserA) 添加新用户 (userB) 并保留与 UserA 的会话值。 FOSUser 更改与最后注册用户 (UserB) 的会话。
如何在注册用户 B 后让用户 A 保持会话状态?
解决方案:
<?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'));
}
}
欢迎任何其他解决方案。
我正在开发一个使用 FOSUserBundle 的 Symfony 3 应用程序。
我希望当前用户 (UserA) 添加新用户 (userB) 并保留与 UserA 的会话值。 FOSUser 更改与最后注册用户 (UserB) 的会话。
如何在注册用户 B 后让用户 A 保持会话状态?
解决方案:
<?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'));
}
}
欢迎任何其他解决方案。