Making SessionComponent mostly a wrapper for CakeSession.

Adding CakeSession::begin() to replace SessionComponent::__start().
Tests updated.  Tests related to autoStart were removed/skipped as that feature isn't really around right now.
This commit is contained in:
mark_story 2010-07-20 23:35:59 -04:00
parent e660416545
commit 4b65ebd64f
3 changed files with 71 additions and 118 deletions

View file

@ -211,6 +211,24 @@ class CakeSession {
return self::started(); return self::started();
} }
/**
* Begins the session if it hasn't already been started, and runs _checkValid which sets up some
* session tampering settings.
*
* @return void
*/
public static function begin() {
if (self::started() === false) {
if (!self::id() && self::start()) {
self::_checkValid();
} else {
self::start();
}
}
self::$error = array();
return self::started();
}
/** /**
* Determine if Session has been started. * Determine if Session has been started.
* *
@ -339,6 +357,19 @@ class CakeSession {
return self::$valid; return self::$valid;
} }
/**
* Get / Set the userAgent
*
* @param string $userAgent Set the userAgent
* @return void
*/
public static function userAgent($userAgent = null) {
if ($userAgent) {
self::$_userAgent = $userAgent;
}
return self::$_userAgent;
}
/** /**
* Returns given session variable, or all of them, if no parameters given. * Returns given session variable, or all of them, if no parameters given.
* *

View file

@ -31,7 +31,7 @@ if (!class_exists('cakesession')) {
* @link http://book.cakephp.org/view/1310/Sessions * @link http://book.cakephp.org/view/1310/Sessions
* *
*/ */
class SessionComponent extends CakeSession { class SessionComponent extends Object {
/** /**
* Used to determine if methods implementation is used, or bypassed * Used to determine if methods implementation is used, or bypassed
@ -54,12 +54,9 @@ class SessionComponent extends CakeSession {
* *
* @param string $base The base path for the Session * @param string $base The base path for the Session
*/ */
public function __construct($base = null) {
if (Configure::read('Session.start') === true) { public function __construct() {
parent::__construct($base); CakeSession::begin();
} else {
$this->__active = false;
}
} }
/** /**
@ -69,9 +66,9 @@ class SessionComponent extends CakeSession {
* @return void * @return void
*/ */
public function startup(&$controller) { public function startup(&$controller) {
if ($this->started() === false && $this->__active === true) { /*if ($this->started() === false && $this->__active === true) {
$this->_start(); $this->_start();
} }*/
} }
/** /**
@ -81,11 +78,11 @@ class SessionComponent extends CakeSession {
* @return void * @return void
*/ */
public function activate($base = null) { public function activate($base = null) {
if ($this->__active === true) { /*if ($this->__active === true) {
return; return;
} }
parent::__construct($base); parent::__construct($base);
$this->__active = true; $this->__active = true;*/
} }
/** /**
@ -104,10 +101,7 @@ class SessionComponent extends CakeSession {
* @return void * @return void
*/ */
public function userAgent($userAgent = null) { public function userAgent($userAgent = null) {
if ($userAgent) { return CakeSession::userAgent($userAgent);
$this->_userAgent = $userAgent;
}
return $this->_userAgent;
} }
/** /**
@ -122,22 +116,7 @@ class SessionComponent extends CakeSession {
* @link http://book.cakephp.org/view/1312/write * @link http://book.cakephp.org/view/1312/write
*/ */
public function write($name, $value = null) { public function write($name, $value = null) {
if ($this->__active === true) { return CakeSession::write($name, $value);
$this->_start();
if (is_array($name)) {
foreach ($name as $key => $value) {
if (parent::write($key, $value) === false) {
return false;
}
}
return true;
}
if (parent::write($name, $value) === false) {
return false;
}
return true;
}
return false;
} }
/** /**
@ -151,11 +130,7 @@ class SessionComponent extends CakeSession {
* @link http://book.cakephp.org/view/1314/read * @link http://book.cakephp.org/view/1314/read
*/ */
public function read($name = null) { public function read($name = null) {
if ($this->__active === true) { return CakeSession::read($name);
$this->_start();
return parent::read($name);
}
return false;
} }
/** /**
@ -168,11 +143,7 @@ class SessionComponent extends CakeSession {
* @link http://book.cakephp.org/view/1316/delete * @link http://book.cakephp.org/view/1316/delete
*/ */
public function delete($name) { public function delete($name) {
if ($this->__active === true) { return CakeSession::delete($name);
$this->_start();
return parent::delete($name);
}
return false;
} }
/** /**
@ -185,11 +156,7 @@ class SessionComponent extends CakeSession {
* @link http://book.cakephp.org/view/1315/check * @link http://book.cakephp.org/view/1315/check
*/ */
public function check($name) { public function check($name) {
if ($this->__active === true) { return CakeSession::check($name);
$this->_start();
return parent::check($name);
}
return false;
} }
/** /**
@ -201,11 +168,7 @@ class SessionComponent extends CakeSession {
* @link http://book.cakephp.org/view/1318/error * @link http://book.cakephp.org/view/1318/error
*/ */
public function error() { public function error() {
if ($this->__active === true) { return CakeSession::error();
$this->_start();
return parent::error();
}
return false;
} }
/** /**
@ -222,10 +185,7 @@ class SessionComponent extends CakeSession {
* @link http://book.cakephp.org/view/1313/setFlash * @link http://book.cakephp.org/view/1313/setFlash
*/ */
public function setFlash($message, $element = 'default', $params = array(), $key = 'flash') { public function setFlash($message, $element = 'default', $params = array(), $key = 'flash') {
if ($this->__active === true) { CakeSession::write('Message.' . $key, compact('message', 'element', 'params'));
$this->_start();
$this->write('Message.' . $key, compact('message', 'element', 'params'));
}
} }
/** /**
@ -236,10 +196,7 @@ class SessionComponent extends CakeSession {
* @return void * @return void
*/ */
public function renew() { public function renew() {
if ($this->__active === true) { return CakeSession::renew();
$this->_start();
parent::renew();
}
} }
/** /**
@ -250,11 +207,7 @@ class SessionComponent extends CakeSession {
* @return boolean true is session is valid, false is session is invalid * @return boolean true is session is valid, false is session is invalid
*/ */
public function valid() { public function valid() {
if ($this->__active === true) { return CakeSession::valid();
$this->_start();
return parent::valid();
}
return false;
} }
/** /**
@ -266,10 +219,7 @@ class SessionComponent extends CakeSession {
* @link http://book.cakephp.org/view/1317/destroy * @link http://book.cakephp.org/view/1317/destroy
*/ */
public function destroy() { public function destroy() {
if ($this->__active === true) { return CakeSession::destroy();
$this->_start();
parent::destroy();
}
} }
/** /**
@ -282,7 +232,7 @@ class SessionComponent extends CakeSession {
* @return string * @return string
*/ */
public function id($id = null) { public function id($id = null) {
return parent::id($id); return CakeSession::id($id);
} }
/** /**
@ -293,6 +243,7 @@ class SessionComponent extends CakeSession {
* @access protected * @access protected
*/ */
protected function _start() { protected function _start() {
/*
if ($this->started() === false) { if ($this->started() === false) {
if (!$this->id() && parent::start()) { if (!$this->id() && parent::start()) {
parent::_checkValid(); parent::_checkValid();
@ -301,6 +252,7 @@ class SessionComponent extends CakeSession {
} }
} }
return $this->started(); return $this->started();
*/
} }
/** /**

View file

@ -88,6 +88,7 @@ class SessionComponentTest extends CakeTestCase {
*/ */
function setUp() { function setUp() {
$this->_session = Configure::read('Session'); $this->_session = Configure::read('Session');
$_SESSION = array();
} }
/** /**
@ -97,6 +98,7 @@ class SessionComponentTest extends CakeTestCase {
* @return void * @return void
*/ */
function tearDown() { function tearDown() {
CakeSession::destroy();
Configure::write('Session', $this->_session); Configure::write('Session', $this->_session);
} }
@ -107,6 +109,7 @@ class SessionComponentTest extends CakeTestCase {
* @return void * @return void
*/ */
function testSessionAutoStart() { function testSessionAutoStart() {
$this->markTestSkipped('Autostarting is broken/disabled for now.');
Configure::write('Session.start', false); Configure::write('Session.start', false);
$Session = new SessionComponent(); $Session = new SessionComponent();
$this->assertFalse($Session->active()); $this->assertFalse($Session->active());
@ -139,6 +142,7 @@ class SessionComponentTest extends CakeTestCase {
* @return void * @return void
*/ */
function testSessionActivate() { function testSessionActivate() {
$this->markTestSkipped('Activation is not implemented right now, sessions are always on');
$Session = new SessionComponent(); $Session = new SessionComponent();
$this->assertTrue($Session->active()); $this->assertTrue($Session->active());
@ -165,24 +169,13 @@ class SessionComponentTest extends CakeTestCase {
$this->assertTrue($Session->valid()); $this->assertTrue($Session->valid());
Configure::write('Session.checkAgent', true);
$Session->userAgent('rweerw'); $Session->userAgent('rweerw');
$this->assertFalse($Session->valid()); $this->assertFalse($Session->valid());
Configure::write('Session.start', false);
$Session = new SessionComponent();
$this->assertFalse($Session->active());
$this->assertFalse($Session->valid());
Configure::write('Session.start', true);
$Session = new SessionComponent(); $Session = new SessionComponent();
$Session->time = $Session->read('Config.time') + 1; $Session->time = $Session->read('Config.time') + 1;
$this->assertFalse($Session->valid()); $this->assertFalse($Session->valid());
Configure::write('Session.checkAgent', false);
$Session = new SessionComponent();
$Session->time = $Session->read('Config.time') + 1;
$this->assertFalse($Session->valid());
Configure::write('Session.checkAgent', true);
} }
/** /**
@ -195,12 +188,6 @@ class SessionComponentTest extends CakeTestCase {
$Session = new SessionComponent(); $Session = new SessionComponent();
$this->assertFalse($Session->error()); $this->assertFalse($Session->error());
Configure::write('Session.start', false);
$Session = new SessionComponent();
$this->assertFalse($Session->active());
$this->assertFalse($Session->error());
Configure::write('Session.start', true);
} }
/** /**
@ -235,13 +222,6 @@ class SessionComponentTest extends CakeTestCase {
$this->assertTrue($Session->write(array('Test' => 'some value'))); $this->assertTrue($Session->write(array('Test' => 'some value')));
$this->assertEqual($Session->read('Test'), 'some value'); $this->assertEqual($Session->read('Test'), 'some value');
$Session->delete('Test'); $Session->delete('Test');
Configure::write('Session.start', false);
$Session = new SessionComponent();
$this->assertFalse($Session->write('Test', 'some value'));
$Session->write('Test', 'some value');
$this->assertFalse($Session->read('Test'));
Configure::write('Session.start', true);
} }
/** /**
@ -257,12 +237,6 @@ class SessionComponentTest extends CakeTestCase {
$Session->write('Test', 'some value'); $Session->write('Test', 'some value');
$this->assertTrue($Session->delete('Test')); $this->assertTrue($Session->delete('Test'));
Configure::write('Session.start', false);
$Session = new SessionComponent();
$Session->write('Test', 'some value');
$this->assertFalse($Session->delete('Test'));
Configure::write('Session.start', true);
} }
/** /**
@ -279,12 +253,6 @@ class SessionComponentTest extends CakeTestCase {
$Session->write('Test', 'some value'); $Session->write('Test', 'some value');
$this->assertTrue($Session->check('Test')); $this->assertTrue($Session->check('Test'));
$Session->delete('Test'); $Session->delete('Test');
Configure::write('Session.start', false);
$Session = new SessionComponent();
$Session->write('Test', 'some value');
$this->assertFalse($Session->check('Test'));
Configure::write('Session.start', true);
} }
/** /**
@ -322,7 +290,7 @@ class SessionComponentTest extends CakeTestCase {
function testSessionId() { function testSessionId() {
unset($_SESSION); unset($_SESSION);
$Session = new SessionComponent(); $Session = new SessionComponent();
$this->assertNull($Session->id()); $this->assertEquals(session_id(), $Session->id());
} }
/** /**
@ -354,30 +322,32 @@ class SessionComponentTest extends CakeTestCase {
$Session =& new SessionComponent(); $Session =& new SessionComponent();
$Session->destroy(); $Session->destroy();
$Session->write('Test', 'some value'); $Session->write('Test', 'some value');
$this->assertEqual($Session->sessionTime, time() + (300 * Configure::read('Session.timeout')));
$this->assertEqual(CakeSession::$sessionTime, mktime() + (300 * Configure::read('Session.timeout')));
$this->assertEqual($_SESSION['Config']['timeout'], 10); $this->assertEqual($_SESSION['Config']['timeout'], 10);
$this->assertEqual($_SESSION['Config']['time'], $Session->sessionTime); $this->assertEqual($_SESSION['Config']['time'], CakeSession::$sessionTime);
$this->assertEqual($Session->time, time()); $this->assertEqual(CakeSession::$time, mktime());
$this->assertEqual($_SESSION['Config']['time'], $Session->time + (300 * Configure::read('Session.timeout'))); $this->assertEqual($_SESSION['Config']['time'], CakeSession::$time + (Security::inactiveMins() * Configure::read('Session.timeout')));
Configure::write('Security.level', 'medium'); Configure::write('Security.level', 'medium');
$Session =& new SessionComponent(); $Session =& new SessionComponent();
$Session->destroy(); $Session->destroy();
$Session->write('Test', 'some value'); $Session->write('Test', 'some value');
$this->assertEqual($Session->sessionTime, mktime() + (100 * Configure::read('Session.timeout'))); $this->assertEqual(CakeSession::$sessionTime, mktime() + (100 * Configure::read('Session.timeout')));
$this->assertEqual($_SESSION['Config']['timeout'], 10); $this->assertEqual($_SESSION['Config']['timeout'], 10);
$this->assertEqual($_SESSION['Config']['time'], $Session->sessionTime); $this->assertEqual($_SESSION['Config']['time'], CakeSession::$sessionTime);
$this->assertEqual($Session->time, time()); $this->assertEqual(CakeSession::$time, mktime());
$this->assertEqual($_SESSION['Config']['time'], $Session->time + (Security::inactiveMins() * Configure::read('Session.timeout'))); $this->assertEqual($_SESSION['Config']['time'], CakeSession::$time + (Security::inactiveMins() * Configure::read('Session.timeout')));
Configure::write('Security.level', 'high'); Configure::write('Security.level', 'high');
$Session =& new SessionComponent(); $Session =& new SessionComponent();
$Session->destroy(); $Session->destroy();
$Session->write('Test', 'some value'); $Session->write('Test', 'some value');
$this->assertEqual($Session->sessionTime, time() + (10 * Configure::read('Session.timeout'))); $this->assertEqual(CakeSession::$sessionTime, mktime() + (10 * Configure::read('Session.timeout')));
$this->assertEqual($_SESSION['Config']['timeout'], 10); $this->assertEqual($_SESSION['Config']['timeout'], 10);
$this->assertEqual($_SESSION['Config']['time'], $Session->sessionTime); $this->assertEqual($_SESSION['Config']['time'], $Session->sessionTime);
$this->assertEqual($Session->time, time()); $this->assertEqual(CakeSession::$time, mktime());
$this->assertEqual($_SESSION['Config']['time'], $Session->time + (Security::inactiveMins() * Configure::read('Session.timeout'))); $this->assertEqual($_SESSION['Config']['time'], CakeSession::$time + (Security::inactiveMins() * Configure::read('Session.timeout')));
} }
} }