Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
915973f
commit d94420f
Showing
10 changed files
with
271 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/Symfony/Component/HttpKernel/Security/Logout/CookieClearingLogoutHandler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\HttpKernel\Security\Logout; | ||
|
||
use Symfony\Component\Security\Authentication\Token\TokenInterface; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
/** | ||
* This handler cleares the passed cookies when a user logs out. | ||
* | ||
* @author Johannes M. Schmitt <schmittjoh@gmail.com> | ||
*/ | ||
class CookieClearingLogoutHandler implements LogoutHandlerInterface | ||
{ | ||
protected $cookieNames; | ||
|
||
/** | ||
* Constructor | ||
* @param array $cookieNames An array of cookie names to unset | ||
*/ | ||
public function __construct(array $cookieNames) | ||
{ | ||
$this->cookieNames = $cookieNames; | ||
} | ||
|
||
/** | ||
* Returns the names of the cookies to unset | ||
* @return array | ||
*/ | ||
public function getCookieNames() | ||
{ | ||
return $this->cookieNames; | ||
} | ||
|
||
/** | ||
* Implementation for the LogoutHandlerInterface. Deletes all requested cookies. | ||
* | ||
* @param Request $request | ||
* @param Response $response | ||
* @param TokenInterface $token | ||
* @return void | ||
*/ | ||
public function logout(Request $request, Response $response, TokenInterface $token) | ||
{ | ||
$expires = time() - 86400; | ||
|
||
foreach ($this->cookieNames as $cookieName) { | ||
$response->headers->setCookie($cookieName, '', null, $expires); | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/Symfony/Component/HttpKernel/Security/Logout/LogoutHandlerInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\HttpKernel\Security\Logout; | ||
|
||
use Symfony\Component\Security\Authentication\Token\TokenInterface; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
/** | ||
* Interface that needs to be implemented by LogoutHandlers. | ||
* | ||
* @author Johannes M. Schmitt <schmittjoh@gmail.com> | ||
*/ | ||
interface LogoutHandlerInterface | ||
{ | ||
/** | ||
* This method is called by the LogoutListener when a user has requested | ||
* to be logged out. Usually, you would unset session variables, or remove | ||
* cookies, etc. | ||
* | ||
* @param Request $request | ||
* @param Response $response | ||
* @param TokenInterface $token | ||
* @return void | ||
*/ | ||
function logout(Request $request, Response $response, TokenInterface $token); | ||
} |
37 changes: 37 additions & 0 deletions
37
src/Symfony/Component/HttpKernel/Security/Logout/SessionLogoutHandler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\HttpKernel\Security\Logout; | ||
|
||
use Symfony\Component\Security\Authentication\Token\TokenInterface; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
/** | ||
* Handler for clearing invalidating the current session. | ||
* | ||
* @author Johannes M. Schmitt <schmittjoh@gmail.com> | ||
*/ | ||
class SessionLogoutHandler implements LogoutHandlerInterface | ||
{ | ||
/** | ||
* Invalidate the current session | ||
* | ||
* @param Request $request | ||
* @param Response $response | ||
* @param TokenInterface $token | ||
* @return void | ||
*/ | ||
public function logout(Request $request, Response $response, TokenInterface $token) | ||
{ | ||
$request->getSession()->invalidate(); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
tests/Symfony/Tests/Component/HttpKernel/Security/Logout/CookieClearingLogoutHandlerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace Symfony\Tests\Component\HttpKernel\Security\Logout; | ||
|
||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\Security\Logout\CookieClearingLogoutHandler; | ||
|
||
class CookieClearingLogoutHandlerTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testConstructor() | ||
{ | ||
$cookieNames = array('foo', 'foo2', 'foo3'); | ||
|
||
$handler = new CookieClearingLogoutHandler($cookieNames); | ||
|
||
$this->assertEquals($cookieNames, $handler->getCookieNames()); | ||
} | ||
|
||
public function testLogout() | ||
{ | ||
$request = new Request(); | ||
$response = new Response(); | ||
$token = $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface'); | ||
|
||
$handler = new CookieClearingLogoutHandler(array('foo', 'foo2')); | ||
|
||
$this->assertFalse($response->headers->has('Set-Cookie')); | ||
|
||
$handler->logout($request, $response, $token); | ||
|
||
$headers = $response->headers->all(); | ||
$cookies = $headers['set-cookie']; | ||
$this->assertEquals(2, count($cookies)); | ||
|
||
$cookie = $cookies[0]; | ||
$this->assertStringStartsWith('foo=;', $cookie); | ||
|
||
$cookie = $cookies[1]; | ||
$this->assertStringStartsWith('foo2=;', $cookie); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
tests/Symfony/Tests/Component/HttpKernel/Security/Logout/SessionLogoutHandlerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Symfony\Tests\Component\HttpKernel\Security\Logout; | ||
|
||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\Security\Logout\SessionLogoutHandler; | ||
|
||
class SessionLogoutHandlerTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testLogout() | ||
{ | ||
$handler = new SessionLogoutHandler(); | ||
|
||
$request = $this->getMock('Symfony\Component\HttpFoundation\Request'); | ||
$response = new Response(); | ||
$session = $this->getMock('Symfony\Component\HttpFoundation\Session', array(), array(), '', false); | ||
|
||
$request | ||
->expects($this->once()) | ||
->method('getSession') | ||
->will($this->returnValue($session)) | ||
; | ||
|
||
$session | ||
->expects($this->once()) | ||
->method('invalidate') | ||
; | ||
|
||
$handler->logout($request, $response, $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface')); | ||
} | ||
} |