From 0d53e86421faef0300d509b385934754b4dab88c Mon Sep 17 00:00:00 2001 From: Christoph Wurst Date: Mon, 25 Apr 2016 10:23:06 +0200 Subject: add ISession::getId() wrapper for session_id --- lib/private/Session/CryptoSessionData.php | 10 ++++++++++ lib/private/Session/Internal.php | 10 ++++++++++ lib/private/Session/Memory.php | 10 ++++++++++ 3 files changed, 30 insertions(+) (limited to 'lib/private') diff --git a/lib/private/Session/CryptoSessionData.php b/lib/private/Session/CryptoSessionData.php index f6c585c1611..23731ef4560 100644 --- a/lib/private/Session/CryptoSessionData.php +++ b/lib/private/Session/CryptoSessionData.php @@ -141,6 +141,16 @@ class CryptoSessionData implements \ArrayAccess, ISession { $this->session->regenerateId($deleteOldSession); } + /** + * Wrapper around session_id + * + * @return string + * @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 */ diff --git a/lib/private/Session/Internal.php b/lib/private/Session/Internal.php index 09175bf1f2f..4fadb1ac801 100644 --- a/lib/private/Session/Internal.php +++ b/lib/private/Session/Internal.php @@ -111,6 +111,16 @@ class Internal extends Session { @session_regenerate_id($deleteOldSession); } + /** + * Wrapper around session_id + * + * @return string + * @since 9.1.0 + */ + public function getId() { + return @session_id(); + } + /** * @throws \Exception */ diff --git a/lib/private/Session/Memory.php b/lib/private/Session/Memory.php index 777458a9aa5..3dba274f395 100644 --- a/lib/private/Session/Memory.php +++ b/lib/private/Session/Memory.php @@ -88,6 +88,16 @@ class Memory extends Session { */ public function regenerateId($deleteOldSession = true) {} + /** + * Wrapper around session_id + * + * @return string + * @since 9.1.0 + */ + public function getId() { + throw new \Exception('Memory session does not have an ID'); + } + /** * Helper function for PHPUnit execution - don't use in non-test code */ -- cgit v1.2.3 From e93bf80b29cde236c5d78023b49435283e4b2562 Mon Sep 17 00:00:00 2001 From: Christoph Wurst Date: Tue, 26 Apr 2016 09:29:15 +0200 Subject: throw SessionNotAvailableException if session_id returns empty string --- lib/private/Session/CryptoSessionData.php | 2 ++ lib/private/Session/Internal.php | 9 +++++- lib/private/Session/Memory.php | 10 +++++-- .../Exceptions/SessionNotAvailableException.php | 32 ++++++++++++++++++++++ lib/public/isession.php | 1 + tests/lib/session/memory.php | 2 +- 6 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 lib/public/Session/Exceptions/SessionNotAvailableException.php (limited to 'lib/private') diff --git a/lib/private/Session/CryptoSessionData.php b/lib/private/Session/CryptoSessionData.php index 23731ef4560..629e6af5412 100644 --- a/lib/private/Session/CryptoSessionData.php +++ b/lib/private/Session/CryptoSessionData.php @@ -24,6 +24,7 @@ namespace OC\Session; use OCP\ISession; use OCP\Security\ICrypto; +use OCP\Session\Exceptions\SessionNotAvailableException; /** * Class CryptoSessionData @@ -145,6 +146,7 @@ class CryptoSessionData implements \ArrayAccess, ISession { * Wrapper around session_id * * @return string + * @throws SessionNotAvailableException * @since 9.1.0 */ public function getId() { diff --git a/lib/private/Session/Internal.php b/lib/private/Session/Internal.php index 4fadb1ac801..a24aec55222 100644 --- a/lib/private/Session/Internal.php +++ b/lib/private/Session/Internal.php @@ -26,6 +26,8 @@ namespace OC\Session; +use OCP\Session\Exceptions\SessionNotAvailableException; + /** * Class Internal * @@ -115,10 +117,15 @@ class Internal extends Session { * Wrapper around session_id * * @return string + * @throws SessionNotAvailableException * @since 9.1.0 */ public function getId() { - return @session_id(); + $id = @session_id(); + if ($id === '') { + throw new SessionNotAvailableException(); + } + return $id; } /** diff --git a/lib/private/Session/Memory.php b/lib/private/Session/Memory.php index 3dba274f395..bcb1f1d950c 100644 --- a/lib/private/Session/Memory.php +++ b/lib/private/Session/Memory.php @@ -26,6 +26,9 @@ namespace OC\Session; +use Exception; +use OCP\Session\Exceptions\SessionNotAvailableException; + /** * Class Internal * @@ -92,10 +95,11 @@ class Memory extends Session { * Wrapper around session_id * * @return string + * @throws SessionNotAvailableException * @since 9.1.0 */ public function getId() { - throw new \Exception('Memory session does not have an ID'); + throw new SessionNotAvailableException('Memory session does not have an ID'); } /** @@ -108,11 +112,11 @@ class Memory extends Session { /** * In case the session has already been locked an exception will be thrown * - * @throws \Exception + * @throws Exception */ private function validateSession() { 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'); } } } diff --git a/lib/public/Session/Exceptions/SessionNotAvailableException.php b/lib/public/Session/Exceptions/SessionNotAvailableException.php new file mode 100644 index 00000000000..d347e0df15e --- /dev/null +++ b/lib/public/Session/Exceptions/SessionNotAvailableException.php @@ -0,0 +1,32 @@ + + * + * @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 + * + */ + +namespace OCP\Session\Exceptions; + +use Exception; + +/** + * @since 9.1.0 + */ +class SessionNotAvailableException extends Exception { + +} diff --git a/lib/public/isession.php b/lib/public/isession.php index 16c6f9bc6a5..7bc8654a1b9 100644 --- a/lib/public/isession.php +++ b/lib/public/isession.php @@ -100,6 +100,7 @@ interface ISession { * Wrapper around session_id * * @return string + * @throws SessionNotAvailableException * @since 9.1.0 */ public function getId(); diff --git a/tests/lib/session/memory.php b/tests/lib/session/memory.php index 750fcf2ec6f..dbf2737fb3f 100644 --- a/tests/lib/session/memory.php +++ b/tests/lib/session/memory.php @@ -17,7 +17,7 @@ class Memory extends Session { } /** - * @expectedException \Exception + * @expectedException OCP\Session\Exceptions\SessionNotAvailableException */ public function testThrowsExceptionOnGetId() { $this->instance->getId(); -- cgit v1.2.3