Add PhpSession with tests

PhpSession handles file based sessions as supported by PHP

refs #4265
refs #4250
This commit is contained in:
Jannis Moßhammer 2013-06-10 14:21:17 +02:00 committed by Marius Hein
parent db61cfafe1
commit 70e8562814
2 changed files with 93 additions and 2 deletions

View File

@ -4,6 +4,16 @@ namespace Icinga\Authentication;
use Icinga\Application\Logger as Logger;
/**
* Standard PHP Session handling
* You have to call read() first in order to start the session. If
* no parameter is given to read, the session is closed immediately
* after reading the persisted variables, in order to avoid concurrent
* requests to be blocked. Otherwise, you can call write() (again with
* no parameter in order to auto-close it) to persist all values previously
* set with the set() method
*
**/
class PhpSession extends Session
{
const SESSION_NAME = "Icinga2Web";
@ -24,7 +34,9 @@ class PhpSession extends Session
public function __construct(array $options = null)
{
if ($options !== null) {
$options = array_merge(PhpSession::DEFAULT_COOKIEOPTIONS, $options);
$options = array_merge(PhpSession::$DEFAULT_COOKIEOPTIONS, $options);
} else {
$options = PhpSession::$DEFAULT_COOKIEOPTIONS;
}
foreach ($options as $sessionVar => $value) {
if (ini_set("session.".$sessionVar, $value) === false) {
@ -115,8 +127,27 @@ class PhpSession extends Session
public function purge()
{
if (!$this->ensureOpen() && !$this->isFlushed) {
if ($this->ensureOpen() && !$this->isFlushed) {
$_SESSION = array();
session_destroy();
$this->clearCookies();
}
}
private function clearCookies()
{
if (ini_get("session.use_cookies")) {
$params = session_get_cookie_params();
setcookie(
session_name(),
'',
time() - 42000,
$params["path"],
$params["domain"],
$params["secure"],
$params["httponly"]
);
}
}
}

View File

@ -0,0 +1,60 @@
<?php
namespace Tests\Icinga\Authentication;
require_once("../../library/Icinga/Authentication/Session.php");
require_once("../../library/Icinga/Authentication/PhpSession.php");
require_once("../../library/Icinga/Application/Logger.php");
require_once("Zend/Log.php");
use Icinga\Authentication\PhpSession as PhpSession;
class PHPSessionTest extends \PHPUnit_Framework_TestCase
{
/**
* @runInSeparateProcess
**/
public function testSessionCreation()
{
$session = new PhpSession();
$session = new PhpSession(array("ssesion.use_cookies"=>false));
}
/**
* @runInSeparateProcess
**/
public function testOpenSession()
{
$this->assertEquals(session_id(), '');
$session = new PhpSession();
$session->open();
$this->assertNotEquals(session_id(), '');
}
/**
* @runInSeparateProcess
**/
public function testCloseSession()
{
$this->assertEquals(session_id(), '');
$session = new PhpSession();
$session->open();
$this->assertNotEquals(session_id(), '');
$session->close();
}
/**
* @runInSeparateProcess
**/
public function testPurgeSession()
{
$this->assertEquals(session_id(), '');
$session = new PhpSession();
$session->open();
$this->assertNotEquals(session_id(), '');
$session->purge();
$this->assertEquals(session_id(), '');
}
}