|
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,35 @@ 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 | + if ($cookie instanceof Cookie) { |
| 81 | + $session->setId($cookie->getValue()); |
| 82 | + } |
| 83 | + |
| 84 | + $session->start(); |
| 85 | + |
| 86 | + if (!$cookie instanceof Cookie) { |
| 87 | + $domains = array_unique(array_map(fn (Cookie $cookie) => $cookie->getName() === $session->getName() ? $cookie->getDomain() : '', $cookieJar->all())) ?: ['']; |
| 88 | + foreach ($domains as $domain) { |
| 89 | + $cookieJar->set(new Cookie($session->getName(), $session->getId(), domain: $domain)); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return $session; |
| 94 | + } |
| 95 | + |
66 | 96 | /** |
67 | 97 | * Enables the profiler for the very next request. |
68 | 98 | * |
@@ -116,20 +146,13 @@ public function loginUser(object $user, string $firewallContext = 'main', array |
116 | 146 | $container = $this->getContainer(); |
117 | 147 | $container->get('security.untracked_token_storage')->setToken($token); |
118 | 148 |
|
119 | | - if (!$container->has('session.factory')) { |
| 149 | + if (!$session = $this->getSession()) { |
120 | 150 | return $this; |
121 | 151 | } |
122 | 152 |
|
123 | | - $session = $container->get('session.factory')->createSession(); |
124 | 153 | $session->set('_security_'.$firewallContext, serialize($token)); |
125 | 154 | $session->save(); |
126 | 155 |
|
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 | 156 | return $this; |
134 | 157 | } |
135 | 158 |
|
|
0 commit comments