Сохранять ту же сессию после регистрации FOSUserBundle
Я разрабатываю приложение Symfony 3, в котором я использую FOSUserBundle.
Я хочу, чтобы текущий пользователь (UserA) добавил нового пользователя (userB) и сохранил значение сеанса с UserA. Сеанс смены FOSUser с последним зарегистрированным пользователем (UserB).
Как мне сохранить USerA в сессии после регистрации userB.
1 ответ
Решение
Решение:
<?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'));
}
}
Любое другое решение приветствуется.