fix: move user preferences dashboard and twofactor out of users table (#6286)

* fix: move user preferences dashboard and twofactor out of users table
This allows them to work with any authentication method
Add set_user_pref() and get_user_pref() helper functions

* fix edit users for other users

* Fix updated_at default timestamp

* Update and rename 183.sql to 184.sql

* removed commented out debug
This commit is contained in:
Tony Murray
2017-04-01 16:18:00 -05:00
committed by Neil Lathwood
parent 98e8e7e407
commit 4b9f3f37d7
13 changed files with 209 additions and 102 deletions

View File

@@ -1672,3 +1672,75 @@ function is_ip($string, $ver = 'ipv4ipv6')
}
return false;
}
/**
* Fetch a user preference from the database
* Do not use strict comparison as results could be strings
*
* @param string $name preference name
* @param mixed $default value to return if the preference is not set
* @param int $user_id for this user_id otherwise, the currently logged in user
* @return mixed value of this preference
*/
function get_user_pref($name, $default = null, $user_id = null)
{
global $user_prefs;
if (array_key_exists($name, $user_prefs)) {
return $user_prefs[$name];
}
if (is_null($user_id)) {
$user_id = $_SESSION['user_id'];
}
$pref = dbFetchCell(
'SELECT `value` FROM `users_prefs` WHERE `user_id`=? AND `pref`=?',
array($user_id, $name)
);
if (!is_null($pref)) {
$pref = json_decode($pref, true);
$user_prefs[$name] = $pref;
return $pref;
}
return $default;
}
/**
* Set a user preference value
*
* @param string $name preference name
* @param mixed $value value of this preference
* @param int $user_id for this user_id otherwise, the currently logged in user
* @return bool whether the setting was changed or not
*/
function set_user_pref($name, $value, $user_id = null)
{
global $user_prefs;
if (is_null($user_id)) {
$user_id = $_SESSION['user_id'];
}
$pref = array(
'user_id' => $user_id,
'pref' => $name,
'value' => json_encode($value),
);
if (dbFetchCell('SELECT count(*) FROM `users_prefs` WHERE `user_id`=? AND `pref`=?', array($user_id, $name))) {
$update = array('value' => json_encode($value));
$params = array($user_id, $name);
$result = dbUpdate($update, 'users_prefs', '`user_id`=? AND `pref`=?', $params) > 0;
} else {
$result = dbInsert($pref, 'users_prefs') !== null;
}
if ($result) {
$user_prefs[$name] = $value;
}
return $result;
}