From d5de5b678964905740af8685d414bedbf83aeca7 Mon Sep 17 00:00:00 2001 From: Philipp Holzer Date: Mon, 15 Jul 2019 21:11:38 +0200 Subject: [PATCH] Bugfixing bad UIDs for PConfig --- src/Core/Config/Cache/PConfigCache.php | 8 +++---- src/Core/Config/JitPConfiguration.php | 24 +++++++++++++++----- src/Core/Config/PConfiguration.php | 8 +++---- src/Core/Config/PreloadPConfiguration.php | 24 +++++++++++++++----- tests/src/Core/Config/PConfigurationTest.php | 18 +++++++++++++++ 5 files changed, 62 insertions(+), 20 deletions(-) diff --git a/src/Core/Config/Cache/PConfigCache.php b/src/Core/Config/Cache/PConfigCache.php index b03d86f41e..d6b1c9b316 100644 --- a/src/Core/Config/Cache/PConfigCache.php +++ b/src/Core/Config/Cache/PConfigCache.php @@ -34,7 +34,7 @@ class PConfigCache * @param int $uid * @param array $config */ - public function load(int $uid, array $config) + public function load($uid, array $config) { $categories = array_keys($config); @@ -62,7 +62,7 @@ class PConfigCache * * @return null|string The value of the config entry or null if not set */ - public function get(int $uid, string $cat, string $key = null) + public function get($uid, string $cat, string $key = null) { if (isset($this->config[$uid][$cat][$key])) { return $this->config[$uid][$cat][$key]; @@ -85,7 +85,7 @@ class PConfigCache * * @return bool Set successful */ - public function set(int $uid, string $cat, string $key, $value) + public function set($uid, string $cat, string $key, $value) { if (!isset($this->config[$uid]) || !is_array($this->config[$uid])) { $this->config[$uid] = []; @@ -116,7 +116,7 @@ class PConfigCache * * @return bool true, if deleted */ - public function delete(int $uid, string $cat, string $key) + public function delete($uid, string $cat, string $key) { if (isset($this->config[$uid][$cat][$key])) { unset($this->config[$uid][$cat][$key]); diff --git a/src/Core/Config/JitPConfiguration.php b/src/Core/Config/JitPConfiguration.php index 8ad65eff1a..82fcbe110c 100644 --- a/src/Core/Config/JitPConfiguration.php +++ b/src/Core/Config/JitPConfiguration.php @@ -32,10 +32,10 @@ class JitPConfiguration extends PConfiguration * {@inheritDoc} * */ - public function load(int $uid, string $cat = 'config') + public function load($uid, string $cat = 'config') { - // If not connected, do nothing - if (!$this->configModel->isConnected()) { + // If not connected or no uid, do nothing + if (!is_int($uid) || !$this->configModel->isConnected()) { return; } @@ -54,8 +54,12 @@ class JitPConfiguration extends PConfiguration /** * {@inheritDoc} */ - public function get(int $uid, string $cat, string $key, $default_value = null, bool $refresh = false) + public function get($uid, string $cat, string $key, $default_value = null, bool $refresh = false) { + if (!is_int($uid)) { + return $default_value; + } + // if the value isn't loaded or refresh is needed, load it to the cache if ($this->configModel->isConnected() && (empty($this->db_loaded[$uid][$cat][$key]) || @@ -80,8 +84,12 @@ class JitPConfiguration extends PConfiguration /** * {@inheritDoc} */ - public function set(int $uid, string $cat, string $key, $value) + public function set($uid, string $cat, string $key, $value) { + if (!is_int($uid)) { + return false; + } + // set the cache first $cached = $this->configCache->set($uid, $cat, $key, $value); @@ -100,8 +108,12 @@ class JitPConfiguration extends PConfiguration /** * {@inheritDoc} */ - public function delete(int $uid, string $cat, string $key) + public function delete($uid, string $cat, string $key) { + if (!is_int($uid)) { + return false; + } + $cacheRemoved = $this->configCache->delete($uid, $cat, $key); if (isset($this->db_loaded[$uid][$cat][$key])) { diff --git a/src/Core/Config/PConfiguration.php b/src/Core/Config/PConfiguration.php index fd4ef2bd3f..a00da819a4 100644 --- a/src/Core/Config/PConfiguration.php +++ b/src/Core/Config/PConfiguration.php @@ -55,7 +55,7 @@ abstract class PConfiguration * @see PConfigCache ) * */ - abstract public function load(int $uid, string $cat = 'config'); + abstract public function load($uid, string $cat = 'config'); /** * Get a particular user's config variable given the category name @@ -73,7 +73,7 @@ abstract class PConfiguration * * @return mixed Stored value or null if it does not exist */ - abstract public function get(int $uid, string $cat, string $key, $default_value = null, bool $refresh = false); + abstract public function get($uid, string $cat, string $key, $default_value = null, bool $refresh = false); /** * Sets a configuration value for a user @@ -90,7 +90,7 @@ abstract class PConfiguration * * @return bool Operation success */ - abstract public function set(int $uid, string $cat, string $key, $value); + abstract public function set($uid, string $cat, string $key, $value); /** * Deletes the given key from the users's configuration. @@ -105,5 +105,5 @@ abstract class PConfiguration * * @return bool */ - abstract public function delete(int $uid, string $cat, string $key); + abstract public function delete($uid, string $cat, string $key); } diff --git a/src/Core/Config/PreloadPConfiguration.php b/src/Core/Config/PreloadPConfiguration.php index 1c2dfba4f7..dd1a72bafd 100644 --- a/src/Core/Config/PreloadPConfiguration.php +++ b/src/Core/Config/PreloadPConfiguration.php @@ -31,10 +31,10 @@ class PreloadPConfiguration extends PConfiguration * This loads all config values everytime load is called * */ - public function load(int $uid, string $cat = 'config') + public function load($uid, string $cat = 'config') { - // Don't load the whole configuration twice - if (!empty($this->config_loaded[$uid])) { + // Don't load the whole configuration twice or with invalid uid + if (!is_int($uid) || !empty($this->config_loaded[$uid])) { return; } @@ -53,8 +53,12 @@ class PreloadPConfiguration extends PConfiguration /** * {@inheritDoc} */ - public function get(int $uid, string $cat, string $key, $default_value = null, bool $refresh = false) + public function get($uid, string $cat, string $key, $default_value = null, bool $refresh = false) { + if (!is_int($uid)) { + return $default_value; + } + if (empty($this->config_loaded[$uid])) { $this->load($uid); } elseif ($refresh) { @@ -75,8 +79,12 @@ class PreloadPConfiguration extends PConfiguration /** * {@inheritDoc} */ - public function set(int $uid, string $cat, string $key, $value) + public function set($uid, string $cat, string $key, $value) { + if (!is_int($uid)) { + return false; + } + if (empty($this->config_loaded[$uid])) { $this->load($uid); } @@ -97,8 +105,12 @@ class PreloadPConfiguration extends PConfiguration /** * {@inheritDoc} */ - public function delete(int $uid, string $cat, string $key) + public function delete($uid, string $cat, string $key) { + if (!is_int($uid)) { + return false; + } + if (empty($this->config_loaded[$uid])) { $this->load($uid); } diff --git a/tests/src/Core/Config/PConfigurationTest.php b/tests/src/Core/Config/PConfigurationTest.php index adc9f267a1..1d5e9c0a17 100644 --- a/tests/src/Core/Config/PConfigurationTest.php +++ b/tests/src/Core/Config/PConfigurationTest.php @@ -452,4 +452,22 @@ abstract class PConfigurationTest extends MockedTest $this->assertConfig($data2['uid'], 'cat1', $data2['data']['cat1']); $this->assertConfig($data2['uid'], 'cat2', $data2['data']['cat2']); } + + /** + * Test when using an invalid UID + * @todo check it the clean way before using the config class + */ + public function testInvalidUid() + { + // bad UID! + $uid = null; + + $this->testedConfig = $this->getInstance(); + + $this->assertNull($this->testedConfig->get($uid, 'cat1', 'cat2')); + $this->assertEquals('fallback!', $this->testedConfig->get($uid, 'cat1', 'cat2', 'fallback!')); + + $this->assertFalse($this->testedConfig->set($uid, 'cat1', 'key1', 'doesn\'t matter!')); + $this->assertFalse($this->testedConfig->delete($uid, 'cat1', 'key1')); + } }