Browse Source

Write to session in batch at the end of the request

tags/v8.2beta1
Lukas Reschke 8 years ago
parent
commit
0b91087489

+ 18
- 4
lib/private/session/cryptosessiondata.php View File

protected $passphrase; protected $passphrase;
/** @var array */ /** @var array */
protected $sessionValues; protected $sessionValues;
/** @var bool */
protected $isModified = false;
CONST encryptedSessionName = 'encrypted_session_data'; CONST encryptedSessionName = 'encrypted_session_data';


/** /**
$this->initializeSession(); $this->initializeSession();
} }


/**
* Close session if class gets destructed
*/
public function __destruct() {
$this->close();
}

protected function initializeSession() { protected function initializeSession() {
$encryptedSessionData = $this->session->get(self::encryptedSessionName); $encryptedSessionData = $this->session->get(self::encryptedSessionName);
try { try {
*/ */
public function set($key, $value) { public function set($key, $value) {
$this->sessionValues[$key] = $value; $this->sessionValues[$key] = $value;
$encryptedValue = $this->crypto->encrypt(json_encode($this->sessionValues), $this->passphrase);
$this->session->set(self::encryptedSessionName, $encryptedValue);
$this->isModified = true;
} }


/** /**
* @return string|null Either the value or null * @return string|null Either the value or null
*/ */
public function get($key) { public function get($key) {

if(isset($this->sessionValues[$key])) { if(isset($this->sessionValues[$key])) {
return $this->sessionValues[$key]; return $this->sessionValues[$key];
} }
* @param string $key * @param string $key
*/ */
public function remove($key) { public function remove($key) {
$this->isModified = true;
unset($this->sessionValues[$key]); unset($this->sessionValues[$key]);
$this->session->remove(self::encryptedSessionName); $this->session->remove(self::encryptedSessionName);
} }
*/ */
public function clear() { public function clear() {
$this->sessionValues = []; $this->sessionValues = [];
$this->isModified = true;
$this->session->clear(); $this->session->clear();
} }


/** /**
* Close the session and release the lock
* Close the session and release the lock, also writes all changed data in batch
*/ */
public function close() { public function close() {
if($this->isModified) {
$encryptedValue = $this->crypto->encrypt(json_encode($this->sessionValues), $this->passphrase);
$this->session->set(self::encryptedSessionName, $encryptedValue);
$this->isModified = false;
}
$this->session->close(); $this->session->close();
} }



+ 4
- 4
lib/private/session/internal.php View File

* @package OC\Session * @package OC\Session
*/ */
class Internal extends Session { class Internal extends Session {
/**
* @param string $name
* @throws \Exception
*/
public function __construct($name) { public function __construct($name) {
session_name($name); session_name($name);
set_error_handler(array($this, 'trapError')); set_error_handler(array($this, 'trapError'));
} }
} }


public function __destruct() {
$this->close();
}

/** /**
* @param string $key * @param string $key
* @param integer $value * @param integer $value

+ 0
- 9
tests/lib/session/cryptowrappingtest.php View File

$this->instance = new CryptoSessionData($this->wrappedSession, $this->crypto, 'PASS'); $this->instance = new CryptoSessionData($this->wrappedSession, $this->crypto, 'PASS');
} }


public function testWrappingSet() {
$unencryptedValue = 'foobar';

$this->wrappedSession->expects($this->once())
->method('set')
->with('encrypted_session_data', $this->crypto->encrypt(json_encode(['encrypted_session_data' => $unencryptedValue])));
$this->instance->set('encrypted_session_data', $unencryptedValue);
}

public function testUnwrappingGet() { public function testUnwrappingGet() {
$unencryptedValue = 'foobar'; $unencryptedValue = 'foobar';
$encryptedValue = $this->crypto->encrypt($unencryptedValue); $encryptedValue = $this->crypto->encrypt($unencryptedValue);

Loading…
Cancel
Save