Merge pull request #24229 from owncloud/session-id-wrapper

add ISession::getId() wrapper for session_id
This commit is contained in:
Roeland Douma 2016-05-02 16:49:38 +02:00
commit b84825f030
6 changed files with 95 additions and 2 deletions

View File

@ -24,6 +24,7 @@ namespace OC\Session;
use OCP\ISession; use OCP\ISession;
use OCP\Security\ICrypto; use OCP\Security\ICrypto;
use OCP\Session\Exceptions\SessionNotAvailableException;
/** /**
* Class CryptoSessionData * Class CryptoSessionData
@ -141,6 +142,17 @@ class CryptoSessionData implements \ArrayAccess, ISession {
$this->session->regenerateId($deleteOldSession); $this->session->regenerateId($deleteOldSession);
} }
/**
* Wrapper around session_id
*
* @return string
* @throws SessionNotAvailableException
* @since 9.1.0
*/
public function getId() {
return $this->session->getId();
}
/** /**
* Close the session and release the lock, also writes all changed data in batch * Close the session and release the lock, also writes all changed data in batch
*/ */

View File

@ -26,6 +26,8 @@
namespace OC\Session; namespace OC\Session;
use OCP\Session\Exceptions\SessionNotAvailableException;
/** /**
* Class Internal * Class Internal
* *
@ -111,6 +113,21 @@ class Internal extends Session {
@session_regenerate_id($deleteOldSession); @session_regenerate_id($deleteOldSession);
} }
/**
* Wrapper around session_id
*
* @return string
* @throws SessionNotAvailableException
* @since 9.1.0
*/
public function getId() {
$id = @session_id();
if ($id === '') {
throw new SessionNotAvailableException();
}
return $id;
}
/** /**
* @throws \Exception * @throws \Exception
*/ */

View File

@ -26,6 +26,9 @@
namespace OC\Session; namespace OC\Session;
use Exception;
use OCP\Session\Exceptions\SessionNotAvailableException;
/** /**
* Class Internal * Class Internal
* *
@ -88,6 +91,17 @@ class Memory extends Session {
*/ */
public function regenerateId($deleteOldSession = true) {} public function regenerateId($deleteOldSession = true) {}
/**
* Wrapper around session_id
*
* @return string
* @throws SessionNotAvailableException
* @since 9.1.0
*/
public function getId() {
throw new SessionNotAvailableException('Memory session does not have an ID');
}
/** /**
* Helper function for PHPUnit execution - don't use in non-test code * Helper function for PHPUnit execution - don't use in non-test code
*/ */
@ -98,11 +112,11 @@ class Memory extends Session {
/** /**
* In case the session has already been locked an exception will be thrown * In case the session has already been locked an exception will be thrown
* *
* @throws \Exception * @throws Exception
*/ */
private function validateSession() { private function validateSession() {
if ($this->sessionClosed) { if ($this->sessionClosed) {
throw new \Exception('Session has been closed - no further changes to the session are allowed'); throw new Exception('Session has been closed - no further changes to the session are allowed');
} }
} }
} }

View File

@ -0,0 +1,32 @@
<?php
/**
* @author Christoph Wurst <christoph@owncloud.com>
*
* @copyright Copyright (c) 2016, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCP\Session\Exceptions;
use Exception;
/**
* @since 9.1.0
*/
class SessionNotAvailableException extends Exception {
}

View File

@ -95,4 +95,13 @@ interface ISession {
* @since 9.0.0 * @since 9.0.0
*/ */
public function regenerateId($deleteOldSession = true); public function regenerateId($deleteOldSession = true);
/**
* Wrapper around session_id
*
* @return string
* @throws SessionNotAvailableException
* @since 9.1.0
*/
public function getId();
} }

View File

@ -10,8 +10,17 @@
namespace Test\Session; namespace Test\Session;
class Memory extends Session { class Memory extends Session {
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
$this->instance = new \OC\Session\Memory($this->getUniqueID()); $this->instance = new \OC\Session\Memory($this->getUniqueID());
} }
/**
* @expectedException OCP\Session\Exceptions\SessionNotAvailableException
*/
public function testThrowsExceptionOnGetId() {
$this->instance->getId();
}
} }