Skip to content

Commit

Permalink
Add check for current user on session preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
jack7anderson7 committed Jul 23, 2024
1 parent 2e8c534 commit ea756b1
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions modules/UserPreferences/UserPreference.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function getPreference(

if ($user->user_name !== $current_user->user_name){
$this->loadPreferences($category);
return $_SESSION[$user->user_name.'_PREFERENCES'][$category][$name];
return $user->user_preferences[$category][$name] ?? $this->getDefaultPreference($name, $category);
}

// if the unique key in session doesn't match the app or prefereces are empty
Expand Down Expand Up @@ -208,13 +208,18 @@ public function setPreference(
public function loadPreferences(
$category = 'global'
) {
global $sugar_config;
global $sugar_config, $current_user;

$user = $this->_userFocus;

if ($user->object_name != 'User') {
return;
}

if ($user->user_name !== $current_user->user_name){
return $this->reloadPreferences($category);
}

if (!empty($user->id) && (!isset($_SESSION[$user->user_name . '_PREFERENCES'][$category]) || (!empty($_SESSION['unique_key']) && $_SESSION['unique_key'] != $sugar_config['unique_key']))) {
// cn: moving this to only log when valid - throwing errors on install
return $this->reloadPreferences($category);
Expand All @@ -235,8 +240,10 @@ public function reloadPreferences($category = 'global')
return false;
}
$GLOBALS['log']->debug('Loading Preferences DB ' . $user->user_name);
if (!isset($_SESSION[$user->user_name . '_PREFERENCES'])) {
$_SESSION[$user->user_name . '_PREFERENCES'] = array();
if ($GLOBALS['current_user']->user_name === $user->user_name){
if (!isset($_SESSION[$user->user_name . '_PREFERENCES'])) {
$_SESSION[$user->user_name . '_PREFERENCES'] = array();
}
}
if (!isset($user->user_preferences) || !is_array($user->user_preferences)) {
$user->user_preferences = array();
Expand All @@ -245,11 +252,15 @@ public function reloadPreferences($category = 'global')
$result = $db->query("SELECT contents FROM user_preferences WHERE assigned_user_id='$user->id' AND category = '" . $category . "' AND deleted = 0", false, 'Failed to load user preferences');
$row = $db->fetchByAssoc($result);
if ($row) {
$_SESSION[$user->user_name . '_PREFERENCES'][$category] = unserialize(base64_decode($row['contents']));
if ($GLOBALS['current_user']->user_name === $user->user_name){
$_SESSION[$user->user_name . '_PREFERENCES'][$category] = unserialize(base64_decode($row['contents']));
}
$user->user_preferences[$category] = unserialize(base64_decode($row['contents']));
return true;
} else {
$_SESSION[$user->user_name . '_PREFERENCES'][$category] = array();
if ($GLOBALS['current_user']->user_name === $user->user_name){
$_SESSION[$user->user_name . '_PREFERENCES'][$category] = array();
}
$user->user_preferences[$category] = array();
}
return false;
Expand Down

0 comments on commit ea756b1

Please sign in to comment.