mirror of
https://github.com/nextcloud/server.git
synced 2026-06-04 22:35:24 -04:00
Merge pull request #3511 from owncloud/sessionclass
Abstract session access away in a class
This commit is contained in:
commit
fee43ec506
20 changed files with 336 additions and 238 deletions
|
|
@ -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 );
|
||||
|
||||
|
|
|
|||
|
|
@ -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 );
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
|||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 );
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
|
|
|
|||
45
lib/base.php
45
lib/base.php
|
|
@ -74,6 +74,11 @@ class OC {
|
|||
*/
|
||||
protected static $router = null;
|
||||
|
||||
/**
|
||||
* @var \OC\Session\Session
|
||||
*/
|
||||
public static $session = null;
|
||||
|
||||
/**
|
||||
* @var \OC\Autoloader $loader
|
||||
*/
|
||||
|
|
@ -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']);
|
||||
|
|
|
|||
39
lib/session/internal.php
Normal file
39
lib/session/internal.php
Normal file
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
63
lib/session/memory.php
Normal file
63
lib/session/memory.php
Normal file
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
79
lib/session/session.php
Normal file
79
lib/session/session.php
Normal file
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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') {
|
||||
|
|
|
|||
16
lib/user.php
16
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;
|
||||
|
|
|
|||
14
lib/util.php
14
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 {
|
||||
|
|
|
|||
16
tests/lib/session/memory.php
Normal file
16
tests/lib/session/memory.php
Normal file
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
64
tests/lib/session/session.php
Normal file
64
tests/lib/session/session.php
Normal file
|
|
@ -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']));
|
||||
}
|
||||
}
|
||||
|
|
@ -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'));
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue