Bugfixing bad UIDs for PConfig

This commit is contained in:
Philipp Holzer 2019-07-15 21:11:38 +02:00
parent ebf00e32a1
commit d5de5b6789
No known key found for this signature in database
GPG key ID: D8365C3D36B77D90
5 changed files with 62 additions and 20 deletions

View file

@ -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]);

View file

@ -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])) {

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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'));
}
}