From cf7b00b8cf20d6af829dfe76fb25b717c652c374 Mon Sep 17 00:00:00 2001 From: Don Bowman Date: Thu, 14 May 2020 08:38:20 -0400 Subject: [PATCH] MDL-68696 sessions: Fixed redis session handler for readonly In 39770792ca62 read-only sessions were allowed. In the redis case, as called from the mobile application, this can lead to returning 'false' for session rather than ''. --- lib/classes/session/redis.php | 6 ++++-- lib/tests/session_redis_test.php | 8 ++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/classes/session/redis.php b/lib/classes/session/redis.php index 03679144caf..f70bfbdd5ce 100644 --- a/lib/classes/session/redis.php +++ b/lib/classes/session/redis.php @@ -259,8 +259,10 @@ class redis extends handler { $this->lock_session($id); } $sessiondata = $this->connection->get($id); - if ($sessiondata === false && $this->requires_write_lock()) { - $this->unlock_session($id); + if ($sessiondata === false) { + if ($this->requires_write_lock()) { + $this->unlock_session($id); + } return ''; } $this->connection->expire($id, $this->timeout); diff --git a/lib/tests/session_redis_test.php b/lib/tests/session_redis_test.php index 938491a3cb6..cadbc3b5a34 100644 --- a/lib/tests/session_redis_test.php +++ b/lib/tests/session_redis_test.php @@ -91,6 +91,14 @@ class core_session_redis_testcase extends advanced_testcase { $this->redis->close(); } + public function test_normal_session_read_only() { + $sess = new \core\session\redis(); + $sess->set_requires_write_lock(false); + $sess->init(); + $this->assertSame('', $sess->handler_read('sess1')); + $this->assertTrue($sess->handler_close()); + } + public function test_normal_session_start_stop_works() { $sess = new \core\session\redis(); $sess->init();