mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
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:
committed by
Neil Lathwood
parent
98e8e7e407
commit
4b9f3f37d7
@@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user