diff --git a/src/Symfony/Bundle/FrameworkBundle/KernelBrowser.php b/src/Symfony/Bundle/FrameworkBundle/KernelBrowser.php index 4fb751d4bfb32..80df16c180e04 100644 --- a/src/Symfony/Bundle/FrameworkBundle/KernelBrowser.php +++ b/src/Symfony/Bundle/FrameworkBundle/KernelBrowser.php @@ -18,6 +18,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpFoundation\Session\SessionInterface; use Symfony\Component\HttpKernel\HttpKernelBrowser; use Symfony\Component\HttpKernel\KernelInterface; use Symfony\Component\HttpKernel\Profiler\Profile as HttpProfile; @@ -33,6 +34,7 @@ class KernelBrowser extends HttpKernelBrowser private bool $hasPerformedRequest = false; private bool $profiler = false; private bool $reboot = true; + private SessionInterface $session; /** * {@inheritdoc} @@ -131,21 +133,37 @@ public function loginUser(object $user, string $firewallContext = 'main'): stati return $this; } - $session = $container->get('session.factory')->createSession(); - $session->set('_security_'.$firewallContext, serialize($token)); - $session->save(); + $this->session = $container->get('session.factory')->createSession(); + $this->setLoginSessionValue('_security_'.$firewallContext, serialize($token)); - $domains = array_unique(array_map(function (Cookie $cookie) use ($session) { - return $cookie->getName() === $session->getName() ? $cookie->getDomain() : ''; + $domains = array_unique(array_map(function (Cookie $cookie) { + return $cookie->getName() === $this->session->getName() ? $cookie->getDomain() : ''; }, $this->getCookieJar()->all())) ?: ['']; foreach ($domains as $domain) { - $cookie = new Cookie($session->getName(), $session->getId(), null, null, $domain); + $cookie = new Cookie($this->session->getName(), $this->session->getId(), null, null, $domain); $this->getCookieJar()->set($cookie); } return $this; } + /** + * Set value on session initialized by loginUser(). + * + * @param mixed $value + * + * @return $this + */ + protected function setLoginSessionValue(string $name, $value): self + { + if (isset($this->session)) { + $this->session->set($name, $value); + $this->session->save(); + return $this; + } + throw new \LogicException("loginUser() must be called to initialize session"); + } + /** * {@inheritdoc} *
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: