Store PHP sessions in the database

This commit is contained in:
Frédéric Guillot
2017-12-06 16:19:11 -08:00
committed by Frédéric Guillot
parent 421531bd4f
commit ccd177ada6
58 changed files with 405 additions and 382 deletions

View File

@@ -116,7 +116,7 @@ class ActionManagerTest extends Base
public function testAttachEventsWithLoggedUser()
{
$this->container['sessionStorage']->user = array('id' => 1);
$_SESSION['user'] = array('id' => 1);
$projectModel = new ProjectModel($this->container);
$projectUserRoleModel = new ProjectUserRoleModel($this->container);

View File

@@ -56,7 +56,7 @@ class AuthenticationManagerTest extends Base
$authManager = new AuthenticationManager($this->container);
$authManager->register(new DatabaseAuth($this->container));
$this->container['sessionStorage']->user = array('id' => 1);
$_SESSION['user'] = array('id' => 1, 'username' => 'test');
$this->assertTrue($this->container['userSession']->isLogged());
$this->assertTrue($authManager->checkCurrentSession());
@@ -67,7 +67,7 @@ class AuthenticationManagerTest extends Base
$authManager = new AuthenticationManager($this->container);
$authManager->register(new DatabaseAuth($this->container));
$this->container['sessionStorage']->user = array('id' => 2);
$_SESSION['user'] = array('id' => 42, 'username' => 'test');
$this->assertTrue($this->container['userSession']->isLogged());
$this->assertFalse($authManager->checkCurrentSession());

View File

@@ -1,60 +0,0 @@
<?php
require_once __DIR__.'/../../Base.php';
use Kanboard\Core\Session\SessionStorage;
class SessionStorageTest extends Base
{
public function testNotPersistentStorage()
{
$storage = new SessionStorage();
$storage->something = array('a' => 'b');
$this->assertEquals(array('a' => 'b'), $storage->something);
$this->assertTrue(isset($storage->something));
$this->assertFalse(isset($storage->something->x));
$this->assertFalse(isset($storage->notFound));
$this->assertFalse(isset($storage->notFound->x));
$this->assertFalse(isset($storage->notFound['x']));
}
public function testPersistentStorage()
{
$session = array('d' => 'e');
$storage = new SessionStorage();
$storage->setStorage($session);
$storage->something = array('a' => 'b');
$this->assertEquals(array('a' => 'b'), $storage->something);
$this->assertEquals('e', $storage->d);
$storage->something['a'] = 'c';
$this->assertEquals('c', $storage->something['a']);
$storage = null;
$this->assertEquals(array('something' => array('a' => 'c'), 'd' => 'e'), $session);
}
public function testFlush()
{
$session = array('d' => 'e');
$storage = new SessionStorage();
$storage->setStorage($session);
$storage->something = array('a' => 'b');
$this->assertEquals(array('a' => 'b'), $storage->something);
$this->assertEquals('e', $storage->d);
$storage->flush();
$this->assertFalse(isset($storage->d));
$this->assertFalse(isset($storage->something));
$storage->foo = 'bar';
$storage = null;
$this->assertEquals(array('foo' => 'bar'), $session);
}
}

View File

@@ -15,8 +15,8 @@ class UserProfileTest extends Base
$user = new DatabaseUserProvider(array('id' => 1));
$this->assertTrue($userProfile->initialize($user));
$this->assertNotEmpty($this->container['sessionStorage']->user);
$this->assertEquals('admin', $this->container['sessionStorage']->user['username']);
$this->assertNotEmpty($_SESSION['user']);
$this->assertEquals('admin', $_SESSION['user']['username']);
}
public function testInitializeLocalUserNotFound()
@@ -25,7 +25,7 @@ class UserProfileTest extends Base
$user = new DatabaseUserProvider(array('id' => 2));
$this->assertFalse($userProfile->initialize($user));
$this->assertFalse(isset($this->container['sessionStorage']->user));
$this->assertFalse(isset($_SESSION['user']));
}
public function testInitializeRemoteUser()
@@ -34,17 +34,17 @@ class UserProfileTest extends Base
$user = new LdapUserProvider('ldapId', 'bob', 'Bob', '', Role::APP_MANAGER, array());
$this->assertTrue($userProfile->initialize($user));
$this->assertNotEmpty($this->container['sessionStorage']->user);
$this->assertEquals(2, $this->container['sessionStorage']->user['id']);
$this->assertEquals('bob', $this->container['sessionStorage']->user['username']);
$this->assertEquals(Role::APP_MANAGER, $this->container['sessionStorage']->user['role']);
$this->assertNotEmpty($_SESSION['user']);
$this->assertEquals(2, $_SESSION['user']['id']);
$this->assertEquals('bob', $_SESSION['user']['username']);
$this->assertEquals(Role::APP_MANAGER, $_SESSION['user']['role']);
$user = new LdapUserProvider('ldapId', 'bob', 'Bob', '', Role::APP_MANAGER, array());
$this->assertTrue($userProfile->initialize($user));
$this->assertNotEmpty($this->container['sessionStorage']->user);
$this->assertEquals(2, $this->container['sessionStorage']->user['id']);
$this->assertEquals('bob', $this->container['sessionStorage']->user['username']);
$this->assertNotEmpty($_SESSION['user']);
$this->assertEquals(2, $_SESSION['user']['id']);
$this->assertEquals('bob', $_SESSION['user']['username']);
}
public function testAssignRemoteUser()
@@ -53,11 +53,11 @@ class UserProfileTest extends Base
$user = new LdapUserProvider('ldapId', 'bob', 'Bob', '', Role::APP_MANAGER, array());
$this->assertTrue($userProfile->assign(1, $user));
$this->assertNotEmpty($this->container['sessionStorage']->user);
$this->assertEquals(1, $this->container['sessionStorage']->user['id']);
$this->assertEquals('admin', $this->container['sessionStorage']->user['username']);
$this->assertEquals('Bob', $this->container['sessionStorage']->user['name']);
$this->assertEquals('', $this->container['sessionStorage']->user['email']);
$this->assertEquals(Role::APP_MANAGER, $this->container['sessionStorage']->user['role']);
$this->assertNotEmpty($_SESSION['user']);
$this->assertEquals(1, $_SESSION['user']['id']);
$this->assertEquals('admin', $_SESSION['user']['username']);
$this->assertEquals('Bob', $_SESSION['user']['name']);
$this->assertEquals('', $_SESSION['user']['email']);
$this->assertEquals(Role::APP_MANAGER, $_SESSION['user']['role']);
}
}

View File

@@ -9,8 +9,7 @@ class UserSessionTest extends Base
{
public function testInitialize()
{
$us = new UserSession($this->container);
$userSession = new UserSession($this->container);
$user = array(
'id' => '123',
'username' => 'john',
@@ -23,101 +22,97 @@ class UserSessionTest extends Base
'role' => Role::APP_MANAGER,
);
$us->initialize($user);
$userSession->initialize($user);
$session = $this->container['sessionStorage']->getAll();
$this->assertNotEmpty($_SESSION);
$this->assertEquals(123, $_SESSION['user']['id']);
$this->assertEquals('john', $_SESSION['user']['username']);
$this->assertEquals(Role::APP_MANAGER, $_SESSION['user']['role']);
$this->assertFalse($_SESSION['user']['is_ldap_user']);
$this->assertFalse($_SESSION['user']['twofactor_activated']);
$this->assertArrayNotHasKey('password', $_SESSION['user']);
$this->assertArrayNotHasKey('twofactor_secret', $_SESSION['user']);
$this->assertArrayNotHasKey('is_admin', $_SESSION['user']);
$this->assertArrayNotHasKey('is_project_admin', $_SESSION['user']);
$this->assertNotEmpty($session);
$this->assertEquals(123, $session['user']['id']);
$this->assertEquals('john', $session['user']['username']);
$this->assertEquals(Role::APP_MANAGER, $session['user']['role']);
$this->assertFalse($session['user']['is_ldap_user']);
$this->assertFalse($session['user']['twofactor_activated']);
$this->assertArrayNotHasKey('password', $session['user']);
$this->assertArrayNotHasKey('twofactor_secret', $session['user']);
$this->assertArrayNotHasKey('is_admin', $session['user']);
$this->assertArrayNotHasKey('is_project_admin', $session['user']);
$this->assertEquals('john', $us->getUsername());
$this->assertEquals('john', $userSession->getUsername());
}
public function testGetId()
{
$us = new UserSession($this->container);
$userSession = new UserSession($this->container);
$this->assertEquals(0, $us->getId());
$this->assertEquals(0, $userSession->getId());
$this->container['sessionStorage']->user = array('id' => 2);
$this->assertEquals(2, $us->getId());
$_SESSION['user'] = array('id' => 2);
$this->assertEquals(2, $userSession->getId());
$this->container['sessionStorage']->user = array('id' => '2');
$this->assertEquals(2, $us->getId());
$_SESSION['user'] = array('id' => '2');
$this->assertEquals(2, $userSession->getId());
}
public function testIsLogged()
{
$us = new UserSession($this->container);
$userSession = new UserSession($this->container);
$this->assertFalse($userSession->isLogged());
$this->assertFalse($us->isLogged());
$_SESSION['user'] = array();
$this->assertFalse($userSession->isLogged());
$this->container['sessionStorage']->user = array();
$this->assertFalse($us->isLogged());
$this->container['sessionStorage']->user = array('id' => 1);
$this->assertTrue($us->isLogged());
$_SESSION['user'] = array('id' => 1);
$this->assertTrue($userSession->isLogged());
}
public function testIsAdmin()
{
$us = new UserSession($this->container);
$userSession = new UserSession($this->container);
$this->assertFalse($userSession->isAdmin());
$this->assertFalse($us->isAdmin());
$_SESSION['user'] = array('role' => Role::APP_ADMIN);
$this->assertTrue($userSession->isAdmin());
$this->container['sessionStorage']->user = array('role' => Role::APP_ADMIN);
$this->assertTrue($us->isAdmin());
$_SESSION['user'] = array('role' => Role::APP_USER);
$this->assertFalse($userSession->isAdmin());
$this->container['sessionStorage']->user = array('role' => Role::APP_USER);
$this->assertFalse($us->isAdmin());
$this->container['sessionStorage']->user = array('role' => '');
$this->assertFalse($us->isAdmin());
$_SESSION['user'] = array('role' => '');
$this->assertFalse($userSession->isAdmin());
}
public function testFilters()
{
$us = new UserSession($this->container);
$this->assertEquals('status:open', $us->getFilters(1));
$userSession = new UserSession($this->container);
$this->assertEquals('status:open', $userSession->getFilters(1));
$us->setFilters(1, 'assignee:me');
$this->assertEquals('assignee:me', $us->getFilters(1));
$userSession->setFilters(1, 'assignee:me');
$this->assertEquals('assignee:me', $userSession->getFilters(1));
$this->assertEquals('status:open', $us->getFilters(2));
$this->assertEquals('status:open', $userSession->getFilters(2));
$us->setFilters(2, 'assignee:bob');
$this->assertEquals('assignee:bob', $us->getFilters(2));
$userSession->setFilters(2, 'assignee:bob');
$this->assertEquals('assignee:bob', $userSession->getFilters(2));
}
public function testPostAuthentication()
{
$us = new UserSession($this->container);
$this->assertFalse($us->isPostAuthenticationValidated());
$userSession = new UserSession($this->container);
$this->assertFalse($userSession->isPostAuthenticationValidated());
$this->container['sessionStorage']->postAuthenticationValidated = false;
$this->assertFalse($us->isPostAuthenticationValidated());
$_SESSION['postAuthenticationValidated'] = false;
$this->assertFalse($userSession->isPostAuthenticationValidated());
$us->validatePostAuthentication();
$this->assertTrue($us->isPostAuthenticationValidated());
$userSession->validatePostAuthentication();
$this->assertTrue($userSession->isPostAuthenticationValidated());
$this->container['sessionStorage']->user = array();
$this->assertFalse($us->hasPostAuthentication());
$_SESSION['user'] = array();
$this->assertFalse($userSession->hasPostAuthentication());
$this->container['sessionStorage']->user = array('twofactor_activated' => false);
$this->assertFalse($us->hasPostAuthentication());
$_SESSION['user'] = array('twofactor_activated' => false);
$this->assertFalse($userSession->hasPostAuthentication());
$this->container['sessionStorage']->user = array('twofactor_activated' => true);
$this->assertTrue($us->hasPostAuthentication());
$_SESSION['user'] = array('twofactor_activated' => true);
$this->assertTrue($userSession->hasPostAuthentication());
$us->disablePostAuthentication();
$this->assertFalse($us->hasPostAuthentication());
$userSession->disablePostAuthentication();
$this->assertFalse($userSession->hasPostAuthentication());
}
}