Skip to content

[FrameworkBundle] make KernelBrowser::loginUser() session available for updating after login #47001

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 7.4
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions src/Symfony/Bundle/FrameworkBundle/KernelBrowser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,6 +34,7 @@ class KernelBrowser extends HttpKernelBrowser
private bool $hasPerformedRequest = false;
private bool $profiler = false;
private bool $reboot = true;
private SessionInterface $session;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keeping the session in a property might be a bad idea, it's a stateful object.


/**
* {@inheritdoc}
Expand Down Expand Up @@ -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}
*
Expand Down