Skip to content

Commit 2e8d925

Browse files
committed
[FrameworkBundle] Use correct cookie domain in loginUser()
1 parent 98d112e commit 2e8d925

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

KernelBrowser.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,13 @@ public function loginUser(object $user, string $firewallContext = 'main'): self
142142
$session->set('_security_'.$firewallContext, serialize($token));
143143
$session->save();
144144

145-
$cookie = new Cookie($session->getName(), $session->getId());
146-
$this->getCookieJar()->set($cookie);
145+
$domains = array_unique(array_map(function (Cookie $cookie) use ($session) {
146+
return $cookie->getName() === $session->getName() ? $cookie->getDomain() : '';
147+
}, $this->getCookieJar()->all())) ?: [''];
148+
foreach ($domains as $domain) {
149+
$cookie = new Cookie($session->getName(), $session->getId(), null, null, $domain);
150+
$this->getCookieJar()->set($cookie);
151+
}
147152

148153
return $this;
149154
}

Tests/Functional/SecurityTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,20 @@ public function testLoginInBetweenRequests()
7070
$client->request('GET', '/main/user_profile');
7171
$this->assertEquals('Welcome the-username!', $client->getResponse()->getContent());
7272
}
73+
74+
public function testLoginUserMultipleTimes()
75+
{
76+
$userFoo = new InMemoryUser('the-username', 'the-password', ['ROLE_FOO']);
77+
$userBar = new InMemoryUser('no-role-username', 'the-password');
78+
$client = $this->createClient(['test_case' => 'Security', 'root_config' => 'config.yml']);
79+
$client->loginUser($userFoo);
80+
81+
$client->request('GET', '/main/user_profile');
82+
$this->assertEquals('Welcome the-username!', $client->getResponse()->getContent());
83+
84+
$client->loginUser($userBar);
85+
86+
$client->request('GET', '/main/user_profile');
87+
$this->assertEquals('Welcome no-role-username!', $client->getResponse()->getContent());
88+
}
7389
}

0 commit comments

Comments
 (0)