Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3e2ff71

Browse files
committed
merged branch fabpot/avoid-session-creation (PR #6964)
This PR was merged into the 2.1 branch. Commits ------- 8ca00c5 [Security] fixed session creation when none is needed (closes #6917) Discussion ---------- [Security] fixed session creation when none is needed (closes #6917) | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #6917 | License | MIT | Doc PR | n/a --------------------------------------------------------------------------- by drak at 2013-02-04T16:24:49Z That looks good. Maybe we need a test for this logic to prevent any regression in the future? --------------------------------------------------------------------------- by bendavies at 2013-02-04T16:30:38Z Yep, this was exactly what i tried locally, but really wasn't familiar enough with it to be confident enough to submit it as a fix. Works for me! --------------------------------------------------------------------------- by bendavies at 2013-02-04T17:19:32Z A few test failures which were added by the breaking PR #2414 in the first place. --------------------------------------------------------------------------- by fabpot at 2013-02-04T18:00:31Z I've fixed the tests which now really test that the session is not started.
2 parents 09bbb68 + 8ca00c5 commit 3e2ff71

File tree

2 files changed

+34
-30
lines changed

2 files changed

+34
-30
lines changed

src/Symfony/Component/Security/Http/Firewall/ContextListener.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ public function handle(GetResponseEvent $event)
7070
}
7171

7272
$request = $event->getRequest();
73-
7473
$session = $request->hasPreviousSession() ? $request->getSession() : null;
7574

7675
if (null === $session || null === $token = $session->get('_security_'.$this->contextKey)) {
@@ -117,7 +116,10 @@ public function onKernelResponse(FilterResponseEvent $event)
117116
$this->logger->debug('Write SecurityContext in the session');
118117
}
119118

120-
if (null === $session = $event->getRequest()->getSession()) {
119+
$request = $event->getRequest();
120+
$session = $request->hasPreviousSession() ? $request->getSession() : null;
121+
122+
if (null === $session) {
121123
return;
122124
}
123125

src/Symfony/Component/Security/Tests/Http/Firewall/ContextListenerTest.php

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -82,36 +82,12 @@ public function testOnKernelResponseWillRemoveSession()
8282
$this->assertFalse($session->has('_security_session'));
8383
}
8484

85-
protected function runSessionOnKernelResponse($newToken, $original = null)
86-
{
87-
$session = new Session(new MockArraySessionStorage());
88-
89-
if ($original !== null) {
90-
$session->set('_security_session', $original);
91-
}
92-
93-
$this->securityContext->setToken($newToken);
94-
95-
$request = new Request();
96-
$request->setSession($session);
97-
98-
$event = new FilterResponseEvent(
99-
$this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface'),
100-
$request,
101-
HttpKernelInterface::MASTER_REQUEST,
102-
new Response()
103-
);
104-
105-
$listener = new ContextListener($this->securityContext, array(), 'session');
106-
$listener->onKernelResponse($event);
107-
108-
return $session;
109-
}
110-
11185
public function testOnKernelResponseWithoutSession()
11286
{
11387
$this->securityContext->setToken(new UsernamePasswordToken('test1', 'pass1', 'phpunit'));
11488
$request = new Request();
89+
$session = new Session(new MockArraySessionStorage());
90+
$request->setSession($session);
11591

11692
$event = new FilterResponseEvent(
11793
$this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface'),
@@ -123,7 +99,7 @@ public function testOnKernelResponseWithoutSession()
12399
$listener = new ContextListener($this->securityContext, array(), 'session');
124100
$listener->onKernelResponse($event);
125101

126-
$this->assertFalse($request->hasSession());
102+
$this->assertFalse($session->isStarted());
127103
}
128104

129105
/**
@@ -168,4 +144,30 @@ public function provideInvalidToken()
168144
array(null),
169145
);
170146
}
171-
}
147+
148+
protected function runSessionOnKernelResponse($newToken, $original = null)
149+
{
150+
$session = new Session(new MockArraySessionStorage());
151+
152+
if ($original !== null) {
153+
$session->set('_security_session', $original);
154+
}
155+
156+
$this->securityContext->setToken($newToken);
157+
158+
$request = new Request();
159+
$request->setSession($session);
160+
$request->cookies->set('MOCKSESSID', true);
161+
162+
$event = new FilterResponseEvent(
163+
$this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface'),
164+
$request,
165+
HttpKernelInterface::MASTER_REQUEST,
166+
new Response()
167+
);
168+
169+
$listener = new ContextListener($this->securityContext, array(), 'session');
170+
$listener->onKernelResponse($event);
171+
172+
return $session;
173+
}}

0 commit comments

Comments
 (0)