|
18 | 18 | use Symfony\Component\DependencyInjection\ContainerInterface;
|
19 | 19 | use Symfony\Component\HttpFoundation\Request;
|
20 | 20 | use Symfony\Component\HttpFoundation\Response;
|
| 21 | +use Symfony\Component\HttpFoundation\Session\SessionInterface; |
21 | 22 | use Symfony\Component\HttpKernel\HttpKernelBrowser;
|
22 | 23 | use Symfony\Component\HttpKernel\KernelInterface;
|
23 | 24 | use Symfony\Component\HttpKernel\Profiler\Profile as HttpProfile;
|
@@ -63,6 +64,38 @@ public function getProfile(): HttpProfile|false|null
|
63 | 64 | return $this->getContainer()->get('profiler')->loadProfileFromResponse($this->response);
|
64 | 65 | }
|
65 | 66 |
|
| 67 | + public function getSession(): ?SessionInterface |
| 68 | + { |
| 69 | + $container = $this->getContainer(); |
| 70 | + |
| 71 | + if (!$container->has('session.factory')) { |
| 72 | + return null; |
| 73 | + } |
| 74 | + |
| 75 | + $session = $container->get('session.factory')->createSession(); |
| 76 | + |
| 77 | + $cookieJar = $this->getCookieJar(); |
| 78 | + $cookie = $cookieJar->get($session->getName()); |
| 79 | + |
| 80 | + // Load the current session if the cookie already exists |
| 81 | + if ($cookie instanceof Cookie) { |
| 82 | + $session->setId($cookie->getValue()); |
| 83 | + } |
| 84 | + |
| 85 | + $session->start(); |
| 86 | + |
| 87 | + // Create the cookie if it does not already exist |
| 88 | + if (!$cookie instanceof Cookie) { |
| 89 | + $domains = array_unique(array_map(fn (Cookie $cookie) => $cookie->getName() === $session->getName() ? $cookie->getDomain() : '', $cookieJar->all())) ?: ['']; |
| 90 | + foreach ($domains as $domain) { |
| 91 | + $cookie = new Cookie($session->getName(), $session->getId(), null, null, $domain); |
| 92 | + $cookieJar->set($cookie); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return $session; |
| 97 | + } |
| 98 | + |
66 | 99 | /**
|
67 | 100 | * Enables the profiler for the very next request.
|
68 | 101 | *
|
@@ -116,20 +149,14 @@ public function loginUser(object $user, string $firewallContext = 'main', array
|
116 | 149 | $container = $this->getContainer();
|
117 | 150 | $container->get('security.untracked_token_storage')->setToken($token);
|
118 | 151 |
|
119 |
| - if (!$container->has('session.factory')) { |
| 152 | + $session = $this->getSession(); |
| 153 | + if (!$session instanceof SessionInterface) { |
120 | 154 | return $this;
|
121 | 155 | }
|
122 | 156 |
|
123 |
| - $session = $container->get('session.factory')->createSession(); |
124 | 157 | $session->set('_security_'.$firewallContext, serialize($token));
|
125 | 158 | $session->save();
|
126 | 159 |
|
127 |
| - $domains = array_unique(array_map(fn (Cookie $cookie) => $cookie->getName() === $session->getName() ? $cookie->getDomain() : '', $this->getCookieJar()->all())) ?: ['']; |
128 |
| - foreach ($domains as $domain) { |
129 |
| - $cookie = new Cookie($session->getName(), $session->getId(), null, null, $domain); |
130 |
| - $this->getCookieJar()->set($cookie); |
131 |
| - } |
132 |
| - |
133 | 160 | return $this;
|
134 | 161 | }
|
135 | 162 |
|
|
0 commit comments