aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoricewind1991 <icewind1991@gmail.com>2013-05-28 13:37:39 -0700
committericewind1991 <icewind1991@gmail.com>2013-05-28 13:37:39 -0700
commitfee43ec506ee423f67ddae0a9ef29a3135b99ab6 (patch)
treee33dd76c10f0002f2e8d41c5c35827e9804d6f7c
parent1d720099c328fe3084e05fe3d2bdd9e49acb8dfe (diff)
parentfa6bfe8837c5f22e724df97c7049d60c1bb904ff (diff)
downloadnextcloud-server-fee43ec506ee423f67ddae0a9ef29a3135b99ab6.tar.gz
nextcloud-server-fee43ec506ee423f67ddae0a9ef29a3135b99ab6.zip
Merge pull request #3511 from owncloud/sessionclass
Abstract session access away in a class
-rw-r--r--apps/files_encryption/hooks/hooks.php8
-rw-r--r--apps/files_encryption/lib/proxy.php8
-rw-r--r--apps/files_encryption/lib/session.php24
-rw-r--r--apps/files_encryption/lib/stream.php2
-rw-r--r--apps/files_encryption/lib/util.php2
-rwxr-xr-xapps/files_encryption/tests/crypt.php4
-rwxr-xr-xapps/files_encryption/tests/util.php6
-rw-r--r--apps/files_sharing/public.php6
-rw-r--r--lib/base.php45
-rw-r--r--lib/session/internal.php39
-rw-r--r--lib/session/memory.php63
-rw-r--r--lib/session/session.php79
-rw-r--r--lib/template.php8
-rw-r--r--lib/user.php16
-rwxr-xr-xlib/util.php14
-rw-r--r--tests/lib/session/memory.php16
-rw-r--r--tests/lib/session/session.php64
-rw-r--r--tests/lib/user/backend.php99
-rw-r--r--tests/lib/user/database.php44
-rw-r--r--tests/lib/user/dummy.php27
20 files changed, 336 insertions, 238 deletions
diff --git a/apps/files_encryption/hooks/hooks.php b/apps/files_encryption/hooks/hooks.php
index 9af1f2c6459..0ef796dbaef 100644
--- a/apps/files_encryption/hooks/hooks.php
+++ b/apps/files_encryption/hooks/hooks.php
@@ -57,7 +57,7 @@ class Hooks {
$privateKey = Crypt::symmetricDecryptFileContent( $encryptedKey, $params['password'] );
- $session = new Session( $view );
+ $session = new \OCA\Encryption\Session( $view );
$session->setPrivateKey( $privateKey, $params['uid'] );
@@ -151,7 +151,7 @@ class Hooks {
$view = new \OC_FilesystemView('/');
- $session = new Session($view);
+ $session = new \OCA\Encryption\Session($view);
// Get existing decrypted private key
$privateKey = $session->getPrivateKey();
@@ -266,7 +266,7 @@ class Hooks {
if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') {
$view = new \OC_FilesystemView('/');
- $session = new Session($view);
+ $session = new \OCA\Encryption\Session($view);
$userId = \OCP\User::getUser();
$util = new Util($view, $userId);
$path = $util->fileIdToPath($params['itemSource']);
@@ -438,7 +438,7 @@ class Hooks {
\OC_FileProxy::$enabled = false;
$view = new \OC_FilesystemView('/');
- $session = new Session($view);
+ $session = new \OCA\Encryption\Session($view);
$userId = \OCP\User::getUser();
$util = new Util( $view, $userId );
diff --git a/apps/files_encryption/lib/proxy.php b/apps/files_encryption/lib/proxy.php
index d9520810bf4..e381ecab5c3 100644
--- a/apps/files_encryption/lib/proxy.php
+++ b/apps/files_encryption/lib/proxy.php
@@ -111,7 +111,7 @@ class Proxy extends \OC_FileProxy
$userId = \OCP\USER::getUser();
$view = new \OC_FilesystemView( '/' );
$util = new Util( $view, $userId );
- $session = new Session( $view );
+ $session = new \OCA\Encryption\Session( $view );
$privateKey = $session->getPrivateKey();
$filePath = $util->stripUserFilesPath( $path );
// Set the filesize for userland, before encrypting
@@ -197,7 +197,7 @@ class Proxy extends \OC_FileProxy
\OC_FileProxy::$enabled = false;
// init session
- $session = new Session( $view );
+ $session = new \OCA\Encryption\Session( $view );
// If data is a catfile
if (
@@ -220,7 +220,7 @@ class Proxy extends \OC_FileProxy
} elseif (
Crypt::mode() == 'server'
- && isset( $_SESSION['legacyenckey'] )
+ &&\OC::$session->exists('legacyenckey')
&& Crypt::isEncryptedMeta( $path )
) {
$plainData = Crypt::legacyBlockDecrypt( $data, $session->getLegacyKey() );
@@ -439,7 +439,7 @@ class Proxy extends \OC_FileProxy
\OC_FileProxy::$enabled = false;
$view = new \OC_FilesystemView( '/' );
- $session = new Session( $view );
+ $session = new \OCA\Encryption\Session( $view );
$userId = \OCP\User::getUser();
$util = new Util( $view, $userId );
diff --git a/apps/files_encryption/lib/session.php b/apps/files_encryption/lib/session.php
index 1551275c63f..d60c386fb1c 100644
--- a/apps/files_encryption/lib/session.php
+++ b/apps/files_encryption/lib/session.php
@@ -105,7 +105,7 @@ class Session
*/
public function setPrivateKey( $privateKey ) {
- $_SESSION['privateKey'] = $privateKey;
+ \OC::$session->set('privateKey', $privateKey);
return true;
@@ -122,8 +122,8 @@ class Session
if (\OCA\Encryption\Helper::isPublicAccess()) {
return $this->getPublicSharePrivateKey();
} else {
- if (isset($_SESSION['privateKey']) && !empty($_SESSION['privateKey'])) {
- return $_SESSION['privateKey'];
+ if (!is_null( \OC::$session->get('privateKey') )) {
+ return \OC::$session->get('privateKey');
} else {
return false;
}
@@ -137,7 +137,7 @@ class Session
*/
public function setPublicSharePrivateKey($privateKey) {
- $_SESSION['publicSharePrivateKey'] = $privateKey;
+ \OC::$session->set('publicSharePrivateKey', $privateKey);
return true;
@@ -150,12 +150,11 @@ class Session
*/
public function getPublicSharePrivateKey() {
- if (isset($_SESSION['publicSharePrivateKey']) && !empty($_SESSION['publicSharePrivateKey'])) {
- return $_SESSION['publicSharePrivateKey'];
+ if (!is_null( \OC::$session->get('publicSharePrivateKey') )) {
+ return \OC::$session->get('publicSharePrivateKey');
} else {
return false;
}
-
}
@@ -166,7 +165,7 @@ class Session
*/
public function setLegacyKey( $legacyKey ) {
- $_SESSION['legacyKey'] = $legacyKey;
+ \OC::$session->set('legacyKey', $legacyKey);
return true;
}
@@ -178,12 +177,9 @@ class Session
*/
public function getLegacyKey() {
- if (
- isset( $_SESSION['legacyKey'] )
- && !empty( $_SESSION['legacyKey'] )
- ) {
+ if ( !is_null( \OC::$session->get('legacyKey') ) ) {
- return $_SESSION['legacyKey'];
+ return \OC::$session->get('legacyKey');
} else {
@@ -193,4 +189,4 @@ class Session
}
-} \ No newline at end of file
+}
diff --git a/apps/files_encryption/lib/stream.php b/apps/files_encryption/lib/stream.php
index fa9df02f085..da8c2494139 100644
--- a/apps/files_encryption/lib/stream.php
+++ b/apps/files_encryption/lib/stream.php
@@ -228,7 +228,7 @@ class Stream
// If a keyfile already exists
if ( $this->encKeyfile ) {
- $session = new Session( $this->rootView );
+ $session = new \OCA\Encryption\Session( $this->rootView );
$privateKey = $session->getPrivateKey( $this->userId );
diff --git a/apps/files_encryption/lib/util.php b/apps/files_encryption/lib/util.php
index 48485cf2e86..6ffe31c9bb4 100644
--- a/apps/files_encryption/lib/util.php
+++ b/apps/files_encryption/lib/util.php
@@ -1420,7 +1420,7 @@ class Util {
if ($item['type'] == 'dir') {
$this->addRecoveryKeys($filePath . '/');
} else {
- $session = new Session(new \OC_FilesystemView('/'));
+ $session = new \OCA\Encryption\Session(new \OC_FilesystemView('/'));
$sharingEnabled = \OCP\Share::isEnabled();
$file = substr($filePath, 0, -4);
$usersSharing = $this->getSharingUsersArray($sharingEnabled, $file);
diff --git a/apps/files_encryption/tests/crypt.php b/apps/files_encryption/tests/crypt.php
index e9f155e2649..5b0486aad8c 100755
--- a/apps/files_encryption/tests/crypt.php
+++ b/apps/files_encryption/tests/crypt.php
@@ -270,7 +270,7 @@ class Test_Encryption_Crypt extends \PHPUnit_Framework_TestCase {
$shareKey = Encryption\Keymanager::getShareKey($this->view, $this->userId, $filename);
// get session
- $session = new Encryption\Session($this->view);
+ $session = new \OCA\Encryption\Session($this->view);
// get private key
$privateKey = $session->getPrivateKey($this->userId);
@@ -345,7 +345,7 @@ class Test_Encryption_Crypt extends \PHPUnit_Framework_TestCase {
$shareKey = Encryption\Keymanager::getShareKey($this->view, $this->userId, $filename);
// get session
- $session = new Encryption\Session($this->view);
+ $session = new \OCA\Encryption\Session($this->view);
// get private key
$privateKey = $session->getPrivateKey($this->userId);
diff --git a/apps/files_encryption/tests/util.php b/apps/files_encryption/tests/util.php
index 2069cae27e5..0dc452a41c8 100755
--- a/apps/files_encryption/tests/util.php
+++ b/apps/files_encryption/tests/util.php
@@ -183,7 +183,7 @@ class Test_Encryption_Util extends \PHPUnit_Framework_TestCase {
$this->assertTrue(OCA\Encryption\Hooks::login($params));
- $this->assertEquals($this->legacyKey, $_SESSION['legacyKey']);
+ $this->assertEquals($this->legacyKey, \OC::$session->get('legacyKey'));
}
function testRecoveryEnabledForUser() {
@@ -273,7 +273,7 @@ class Test_Encryption_Util extends \PHPUnit_Framework_TestCase {
$this->assertTrue(OCA\Encryption\Hooks::login($params));
- $this->assertEquals($this->legacyKey, $_SESSION['legacyKey']);
+ $this->assertEquals($this->legacyKey, \OC::$session->get('legacyKey'));
$files = $util->findEncFiles('/' . \Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER . '/files/');
@@ -314,4 +314,4 @@ class Test_Encryption_Util extends \PHPUnit_Framework_TestCase {
$params['password'] = $password;
OCA\Encryption\Hooks::login($params);
}
-} \ No newline at end of file
+}
diff --git a/apps/files_sharing/public.php b/apps/files_sharing/public.php
index 59598e35fa2..98d2a84fb66 100644
--- a/apps/files_sharing/public.php
+++ b/apps/files_sharing/public.php
@@ -84,7 +84,7 @@ if (isset($path)) {
exit();
} else {
// Save item id in session for future requests
- $_SESSION['public_link_authenticated'] = $linkItem['id'];
+ \OC::$session->set('public_link_authenticated', $linkItem['id']);
}
} else {
OCP\Util::writeLog('share', 'Unknown share type '.$linkItem['share_type']
@@ -97,8 +97,8 @@ if (isset($path)) {
} else {
// Check if item id is set in session
- if (!isset($_SESSION['public_link_authenticated'])
- || $_SESSION['public_link_authenticated'] !== $linkItem['id']
+ if ( ! \OC::$session->exists('public_link_authenticated')
+ || \OC::$session->get('public_link_authenticated') !== $linkItem['id']
) {
// Prompt for password
$tmpl = new OCP\Template('files_sharing', 'authenticate', 'guest');
diff --git a/lib/base.php b/lib/base.php
index 724bd250a5c..f1145b651ae 100644
--- a/lib/base.php
+++ b/lib/base.php
@@ -75,6 +75,11 @@ class OC {
protected static $router = null;
/**
+ * @var \OC\Session\Session
+ */
+ public static $session = null;
+
+ /**
* @var \OC\Autoloader $loader
*/
public static $loader = null;
@@ -283,14 +288,17 @@ class OC {
$cookie_path = OC::$WEBROOT ?: '/';
ini_set('session.cookie_path', $cookie_path);
- // set the session name to the instance id - which is unique
- session_name(OC_Util::getInstanceId());
+ try{
+ // set the session name to the instance id - which is unique
+ self::$session = new \OC\Session\Internal(OC_Util::getInstanceId());
+ // if session cant be started break with http 500 error
+ }catch (Exception $e){
+ //set the session object to a dummy session so code relying on the session existing still works
+ self::$session = new \OC\Session\Memory('');
- // if session cant be started break with http 500 error
- if (session_start() === false){
- OC_Log::write('core', 'Session could not be initialized',
+ OC_Log::write('core', 'Session could not be initialized',
OC_Log::ERROR);
-
+
header('HTTP/1.1 500 Internal Server Error');
OC_Util::addStyle("styles");
$error = 'Session could not be initialized. Please contact your ';
@@ -304,15 +312,15 @@ class OC {
}
// regenerate session id periodically to avoid session fixation
- if (!isset($_SESSION['SID_CREATED'])) {
- $_SESSION['SID_CREATED'] = time();
- } else if (time() - $_SESSION['SID_CREATED'] > 60*60*12) {
+ if (!self::$session->exists('SID_CREATED')) {
+ self::$session->set('SID_CREATED', time());
+ } else if (time() - self::$session->get('SID_CREATED') > 60*60*12) {
session_regenerate_id(true);
- $_SESSION['SID_CREATED'] = time();
+ self::$session->set('SID_CREATED', time());
}
// session timeout
- if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 60*60*24)) {
+ if (self::$session->exists('LAST_ACTIVITY') && (time() - self::$session->get('LAST_ACTIVITY') > 60*60*24)) {
if (isset($_COOKIE[session_name()])) {
setcookie(session_name(), '', time() - 42000, $cookie_path);
}
@@ -320,7 +328,8 @@ class OC {
session_destroy();
session_start();
}
- $_SESSION['LAST_ACTIVITY'] = time();
+
+ self::$session->set('LAST_ACTIVITY', time());
}
public static function getRouter() {
@@ -436,6 +445,8 @@ class OC {
self::checkSSL();
if ( !self::$CLI ) {
self::initSession();
+ } else {
+ self::$session = new \OC\Session\Memory('');
}
$errors = OC_Util::checkServer();
@@ -446,14 +457,14 @@ class OC {
// User and Groups
if (!OC_Config::getValue("installed", false)) {
- $_SESSION['user_id'] = '';
+ self::$session->set('user_id','');
}
OC_User::useBackend(new OC_User_Database());
OC_Group::useBackend(new OC_Group_Database());
- if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SESSION['user_id'])
- && $_SERVER['PHP_AUTH_USER'] != $_SESSION['user_id']) {
+ if (isset($_SERVER['PHP_AUTH_USER']) && self::$session->exists('user_id')
+ && $_SERVER['PHP_AUTH_USER'] != self::$session->get('user_id')) {
OC_User::logout();
}
@@ -598,7 +609,7 @@ class OC {
// Handle redirect URL for logged in users
if (isset($_REQUEST['redirect_url']) && OC_User::isLoggedIn()) {
$location = OC_Helper::makeURLAbsolute(urldecode($_REQUEST['redirect_url']));
-
+
// Deny the redirect if the URL contains a @
// This prevents unvalidated redirects like ?redirect_url=:user@domain.com
if (strpos($location, '@') === false) {
@@ -748,7 +759,7 @@ class OC {
if (OC_User::login($_POST["user"], $_POST["password"])) {
// setting up the time zone
if (isset($_POST['timezone-offset'])) {
- $_SESSION['timezone'] = $_POST['timezone-offset'];
+ self::$session->set('timezone', $_POST['timezone-offset']);
}
self::cleanupLoginTokens($_POST['user']);
diff --git a/lib/session/internal.php b/lib/session/internal.php
new file mode 100644
index 00000000000..60aecccc8aa
--- /dev/null
+++ b/lib/session/internal.php
@@ -0,0 +1,39 @@
+<?php
+/**
+ * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace OC\Session;
+
+/**
+ * Class Internal
+ *
+ * wrap php's internal session handling into the Session interface
+ *
+ * @package OC\Session
+ */
+class Internal extends Memory {
+ public function __construct($name) {
+ session_name($name);
+ session_start();
+ if (!isset($_SESSION)) {
+ throw new \Exception('Failed to start session');
+ }
+ $this->data = $_SESSION;
+ }
+
+ public function __destruct() {
+ $_SESSION = $this->data;
+ session_write_close();
+ }
+
+ public function clear() {
+ session_unset();
+ @session_regenerate_id(true);
+ @session_start();
+ $this->data = $_SESSION = array();
+ }
+}
diff --git a/lib/session/memory.php b/lib/session/memory.php
new file mode 100644
index 00000000000..c148ff4b9b9
--- /dev/null
+++ b/lib/session/memory.php
@@ -0,0 +1,63 @@
+<?php
+/**
+ * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace OC\Session;
+
+/**
+ * Class Internal
+ *
+ * store session data in an in-memory array, not persistance
+ *
+ * @package OC\Session
+ */
+class Memory extends Session {
+ protected $data;
+
+ public function __construct($name) {
+ //no need to use $name since all data is already scoped to this instance
+ $this->data = array();
+ }
+
+ /**
+ * @param string $key
+ * @param mixed $value
+ */
+ public function set($key, $value) {
+ $this->data[$key] = $value;
+ }
+
+ /**
+ * @param string $key
+ * @return mixed
+ */
+ public function get($key) {
+ if (!$this->exists($key)) {
+ return null;
+ }
+ return $this->data[$key];
+ }
+
+ /**
+ * @param string $key
+ * @return bool
+ */
+ public function exists($key) {
+ return isset($this->data[$key]);
+ }
+
+ /**
+ * @param string $key
+ */
+ public function remove($key) {
+ unset($this->data[$key]);
+ }
+
+ public function clear() {
+ $this->data = array();
+ }
+}
diff --git a/lib/session/session.php b/lib/session/session.php
new file mode 100644
index 00000000000..55515f57a87
--- /dev/null
+++ b/lib/session/session.php
@@ -0,0 +1,79 @@
+<?php
+/**
+ * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace OC\Session;
+
+abstract class Session implements \ArrayAccess {
+ /**
+ * $name serves as a namespace for the session keys
+ *
+ * @param string $name
+ */
+ abstract public function __construct($name);
+
+ /**
+ * @param string $key
+ * @param mixed $value
+ */
+ abstract public function set($key, $value);
+
+ /**
+ * @param string $key
+ * @return mixed should return null if $key does not exist
+ */
+ abstract public function get($key);
+
+ /**
+ * @param string $key
+ * @return bool
+ */
+ abstract public function exists($key);
+
+ /**
+ * should not throw any errors if $key does not exist
+ *
+ * @param string $key
+ */
+ abstract public function remove($key);
+
+ /**
+ * removes all entries within the cache namespace
+ */
+ abstract public function clear();
+
+ /**
+ * @param mixed $offset
+ * @return bool
+ */
+ public function offsetExists($offset) {
+ return $this->exists($offset);
+ }
+
+ /**
+ * @param mixed $offset
+ * @return mixed
+ */
+ public function offsetGet($offset) {
+ return $this->get($offset);
+ }
+
+ /**
+ * @param mixed $offset
+ * @param mixed $value
+ */
+ public function offsetSet($offset, $value) {
+ $this->set($offset, $value);
+ }
+
+ /**
+ * @param mixed $offset
+ */
+ public function offsetUnset($offset) {
+ $this->remove($offset);
+ }
+}
diff --git a/lib/template.php b/lib/template.php
index 2f535335648..9467dedb62a 100644
--- a/lib/template.php
+++ b/lib/template.php
@@ -246,14 +246,14 @@ class OC_Template{
// if the formfactor is not yet autodetected do the
// autodetection now. For possible formfactors check the
// detectFormfactor documentation
- if(!isset($_SESSION['formfactor'])) {
- $_SESSION['formfactor'] = self::detectFormfactor();
+ if (!\OC::$session->exists('formfactor')) {
+ \OC::$session->set('formfactor', self::detectFormfactor());
}
// allow manual override via GET parameter
if(isset($_GET['formfactor'])) {
- $_SESSION['formfactor']=$_GET['formfactor'];
+ \OC::$session->set('formfactor', $_GET['formfactor']);
}
- $formfactor=$_SESSION['formfactor'];
+ $formfactor = \OC::$session->get('formfactor');
if($formfactor=='default') {
$fext='';
}elseif($formfactor=='mobile') {
diff --git a/lib/user.php b/lib/user.php
index 26fe73f8bfe..1dde87a1339 100644
--- a/lib/user.php
+++ b/lib/user.php
@@ -264,7 +264,7 @@ class OC_User {
* @brief Sets user id for session and triggers emit
*/
public static function setUserId($uid) {
- $_SESSION['user_id'] = $uid;
+ \OC::$session->set('user_id', $uid);
}
/**
@@ -285,7 +285,7 @@ class OC_User {
$result = true;
}
if (OC_User::getUser() === $uid) {
- $_SESSION['display_name'] = $displayName;
+ \OC::$session->set('display_name', $displayName);
}
return $result;
}
@@ -328,10 +328,10 @@ class OC_User {
* Checks if the user is logged in
*/
public static function isLoggedIn() {
- if( isset($_SESSION['user_id']) AND $_SESSION['user_id']) {
+ if( \OC::$session->get('user_id')) {
OC_App::loadApps(array('authentication'));
self::setupBackends();
- if (self::userExists($_SESSION['user_id']) ) {
+ if (self::userExists(\OC::$session->get('user_id')) ) {
return true;
}
}
@@ -356,8 +356,8 @@ class OC_User {
* @return string uid or false
*/
public static function getUser() {
- if( isset($_SESSION['user_id']) AND $_SESSION['user_id'] ) {
- return $_SESSION['user_id'];
+ if( \OC::$session->get('user_id') ) {
+ return \OC::$session->get('user_id');
}
else{
return false;
@@ -371,8 +371,8 @@ class OC_User {
public static function getDisplayName($user=null) {
if ( $user ) {
return self::determineDisplayName($user);
- } else if( isset($_SESSION['display_name']) AND $_SESSION['display_name'] ) {
- return $_SESSION['display_name'];
+ } else if( \OC::$session->get('display_name') ) {
+ return \OC::$session->get('display_name');
}
else{
return false;
diff --git a/lib/util.php b/lib/util.php
index ce68568183b..581f35bc0ac 100755
--- a/lib/util.php
+++ b/lib/util.php
@@ -151,10 +151,10 @@ class OC_Util {
* @param bool dateOnly option to omit time from the result
*/
public static function formatDate( $timestamp, $dateOnly=false) {
- if(isset($_SESSION['timezone'])) {//adjust to clients timezone if we know it
+ if(\OC::$session->exists('timezone')) {//adjust to clients timezone if we know it
$systemTimeZone = intval(date('O'));
$systemTimeZone=(round($systemTimeZone/100, 0)*60)+($systemTimeZone%100);
- $clientTimeZone=$_SESSION['timezone']*60;
+ $clientTimeZone=\OC::$session->get('timezone')*60;
$offset=$clientTimeZone-$systemTimeZone;
$timestamp=$timestamp+$offset*60;
}
@@ -458,13 +458,13 @@ class OC_Util {
*/
public static function callRegister() {
// Check if a token exists
- if(!isset($_SESSION['requesttoken'])) {
+ if(!\OC::$session->exists('requesttoken')) {
// No valid token found, generate a new one.
$requestToken = self::generate_random_bytes(20);
- $_SESSION['requesttoken']=$requestToken;
+ \OC::$session->set('requesttoken', $requestToken);
} else {
// Valid token already exists, send it
- $requestToken = $_SESSION['requesttoken'];
+ $requestToken = \OC::$session->get('requesttoken');
}
return($requestToken);
}
@@ -476,7 +476,7 @@ class OC_Util {
* @see OC_Util::callRegister()
*/
public static function isCallRegistered() {
- if(!isset($_SESSION['requesttoken'])) {
+ if(!\OC::$session->exists('requesttoken')) {
return false;
}
@@ -492,7 +492,7 @@ class OC_Util {
}
// Check if the token is valid
- if($token !== $_SESSION['requesttoken']) {
+ if($token !== \OC::$session->get('requesttoken')) {
// Not valid
return false;
} else {
diff --git a/tests/lib/session/memory.php b/tests/lib/session/memory.php
new file mode 100644
index 00000000000..2dc236b73bf
--- /dev/null
+++ b/tests/lib/session/memory.php
@@ -0,0 +1,16 @@
+<?php
+
+/**
+ * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace Test\Session;
+
+class Memory extends Session {
+ public function setUp() {
+ $this->instance = new \OC\Session\Memory(uniqid());
+ }
+}
diff --git a/tests/lib/session/session.php b/tests/lib/session/session.php
new file mode 100644
index 00000000000..72dee44e7cb
--- /dev/null
+++ b/tests/lib/session/session.php
@@ -0,0 +1,64 @@
+<?php
+
+/**
+ * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace Test\Session;
+
+abstract class Session extends \PHPUnit_Framework_TestCase {
+ /**
+ * @var \OC\Session\Session
+ */
+ protected $instance;
+
+ public function tearDown() {
+ $this->instance->clear();
+ }
+
+ public function testNotExistsEmpty() {
+ $this->assertFalse($this->instance->exists('foo'));
+ }
+
+ public function testExistsAfterSet() {
+ $this->instance->set('foo', 1);
+ $this->assertTrue($this->instance->exists('foo'));
+ }
+
+ public function testNotExistsAfterRemove() {
+ $this->instance->set('foo', 1);
+ $this->instance->remove('foo');
+ $this->assertFalse($this->instance->exists('foo'));
+ }
+
+ public function testGetNonExisting() {
+ $this->assertNull($this->instance->get('foo'));
+ }
+
+ public function testGetAfterSet() {
+ $this->instance->set('foo', 'bar');
+ $this->assertEquals('bar', $this->instance->get(('foo')));
+ }
+
+ public function testRemoveNonExisting() {
+ $this->instance->remove('foo');
+ }
+
+ public function testNotExistsAfterClear() {
+ $this->instance->set('foo', 1);
+ $this->instance->clear();
+ $this->assertFalse($this->instance->exists('foo'));
+ }
+
+ public function testArrayInterface() {
+ $this->assertFalse(isset($this->instance['foo']));
+ $this->instance['foo'] = 'bar';
+ $this->assertTrue(isset($this->instance['foo']));
+ $this->assertEquals('bar', $this->instance['foo']);
+ unset($this->instance['foo']);
+ $this->assertFalse(isset($this->instance['foo']));
+ }
+}
diff --git a/tests/lib/user/backend.php b/tests/lib/user/backend.php
deleted file mode 100644
index 40674424c96..00000000000
--- a/tests/lib/user/backend.php
+++ /dev/null
@@ -1,99 +0,0 @@
-<?php
-/**
-* ownCloud
-*
-* @author Robin Appelman
-* @copyright 2012 Robin Appelman icewind@owncloud.com
-*
-* This library is free software; you can redistribute it and/or
-* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
-* License as published by the Free Software Foundation; either
-* version 3 of the License, or any later version.
-*
-* This library is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
-*
-* You should have received a copy of the GNU Affero General Public
-* License along with this library. If not, see <http://www.gnu.org/licenses/>.
-*
-*/
-
-/**
- * Abstract class to provide the basis of backend-specific unit test classes.
- *
- * All subclasses MUST assign a backend property in setUp() which implements
- * user operations (add, remove, etc.). Test methods in this class will then be
- * run on each separate subclass and backend therein.
- *
- * For an example see /tests/lib/user/dummy.php
- */
-
-abstract class Test_User_Backend extends PHPUnit_Framework_TestCase {
- /**
- * @var OC_User_Backend $backend
- */
- protected $backend;
-
- /**
- * get a new unique user name
- * test cases can override this in order to clean up created user
- * @return array
- */
- public function getUser() {
- return uniqid('test_');
- }
-
- public function testAddRemove() {
- //get the number of groups we start with, in case there are exising groups
- $startCount=count($this->backend->getUsers());
-
- $name1=$this->getUser();
- $name2=$this->getUser();
- $this->backend->createUser($name1, '');
- $count=count($this->backend->getUsers())-$startCount;
- $this->assertEquals(1, $count);
- $this->assertTrue((array_search($name1, $this->backend->getUsers())!==false));
- $this->assertFalse((array_search($name2, $this->backend->getUsers())!==false));
- $this->backend->createUser($name2, '');
- $count=count($this->backend->getUsers())-$startCount;
- $this->assertEquals(2, $count);
- $this->assertTrue((array_search($name1, $this->backend->getUsers())!==false));
- $this->assertTrue((array_search($name2, $this->backend->getUsers())!==false));
-
- $this->backend->deleteUser($name2);
- $count=count($this->backend->getUsers())-$startCount;
- $this->assertEquals(1, $count);
- $this->assertTrue((array_search($name1, $this->backend->getUsers())!==false));
- $this->assertFalse((array_search($name2, $this->backend->getUsers())!==false));
- }
-
- public function testLogin() {
- $name1=$this->getUser();
- $name2=$this->getUser();
-
- $this->assertFalse($this->backend->userExists($name1));
- $this->assertFalse($this->backend->userExists($name2));
-
- $this->backend->createUser($name1, 'pass1');
- $this->backend->createUser($name2, 'pass2');
-
- $this->assertTrue($this->backend->userExists($name1));
- $this->assertTrue($this->backend->userExists($name2));
-
- $this->assertTrue($this->backend->checkPassword($name1, 'pass1'));
- $this->assertTrue($this->backend->checkPassword($name2, 'pass2'));
-
- $this->assertFalse($this->backend->checkPassword($name1, 'pass2'));
- $this->assertFalse($this->backend->checkPassword($name2, 'pass1'));
-
- $this->assertFalse($this->backend->checkPassword($name1, 'dummy'));
- $this->assertFalse($this->backend->checkPassword($name2, 'foobar'));
-
- $this->backend->setPassword($name1, 'newpass1');
- $this->assertFalse($this->backend->checkPassword($name1, 'pass1'));
- $this->assertTrue($this->backend->checkPassword($name1, 'newpass1'));
- $this->assertFalse($this->backend->checkPassword($name2, 'newpass1'));
- }
-}
diff --git a/tests/lib/user/database.php b/tests/lib/user/database.php
deleted file mode 100644
index fe7d87c44de..00000000000
--- a/tests/lib/user/database.php
+++ /dev/null
@@ -1,44 +0,0 @@
-<?php
-/**
-* ownCloud
-*
-* @author Robin Appelman
-* @copyright 2012 Robin Appelman icewind@owncloud.com
-*
-* This library is free software; you can redistribute it and/or
-* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
-* License as published by the Free Software Foundation; either
-* version 3 of the License, or any later version.
-*
-* This library is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
-*
-* You should have received a copy of the GNU Affero General Public
-* License along with this library. If not, see <http://www.gnu.org/licenses/>.
-*
-*/
-
-class Test_User_Database extends Test_User_Backend {
- /**
- * get a new unique user name
- * test cases can override this in order to clean up created user
- * @return array
- */
- public function getUser() {
- $user=uniqid('test_');
- $this->users[]=$user;
- return $user;
- }
-
- public function setUp() {
- $this->backend=new OC_User_Dummy();
- }
-
- public function tearDown() {
- foreach($this->users as $user) {
- $this->backend->deleteUser($user);
- }
- }
-}
diff --git a/tests/lib/user/dummy.php b/tests/lib/user/dummy.php
deleted file mode 100644
index e417fd97603..00000000000
--- a/tests/lib/user/dummy.php
+++ /dev/null
@@ -1,27 +0,0 @@
-<?php
-/**
-* ownCloud
-*
-* @author Robin Appelman
-* @copyright 2012 Robin Appelman icewind@owncloud.com
-*
-* This library is free software; you can redistribute it and/or
-* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
-* License as published by the Free Software Foundation; either
-* version 3 of the License, or any later version.
-*
-* This library is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
-*
-* You should have received a copy of the GNU Affero General Public
-* License along with this library. If not, see <http://www.gnu.org/licenses/>.
-*
-*/
-
-class Test_User_Dummy extends Test_User_Backend {
- public function setUp() {
- $this->backend=new OC_User_Dummy();
- }
-}