public function testSavePreferencesToDBAndResetPreferences()
 {
     $user = new User();
     $user->retrieve('1');
     $userPreference = new UserPreference($user);
     //create a Preference record, save it to DB
     $userPreference->setPreference('test', 'test val', 'test_category');
     $userPreference->savePreferencesToDB();
     //retrieve it back and verify
     $result = $userPreference->retrieve_by_string_fields(array('assigned_user_id' => $user->id, 'category' => 'test_category'));
     $this->assertTrue(isset($result->id));
     //reset the preferences and verify that it is deleted
     $userPreference->resetPreferences();
     $result = $userPreference->retrieve_by_string_fields(array('assigned_user_id' => $user->id, 'category' => 'test_category'));
     $this->assertEquals(null, $result);
 }
 /**
  * Get the registered dashlet list of a determined user.
  * @param \User $user
  * @return array
  */
 public function getDashletsByUser($user)
 {
     $orderBy = "";
     $where = "category='HOME' AND assigned_user_id='{$user->id}'";
     $preferences = $this->preferenceRetriever->get_full_list($orderBy, $where);
     $result = array();
     if (!empty($preferences)) {
         foreach ($preferences as $preference) {
             $result[] = $this->processDashletData($preference->assigned_user_id, $preference->contents);
         }
     }
     return $result;
 }
 /**
  * Delete a filter from previously used
  * @param RestService $api 
  * @param array $args 
  * @return array of formatted Beans
  */
 public function deleteUsed($api, $args)
 {
     $data = array();
     $user_preference = new UserPreference($GLOBALS['current_user']);
     $used_filters = $user_preference->getPreference($args['module_name'], 'filters');
     if (isset($args['record']) && !empty($args['record'])) {
         // if the record exists unset it
         $key = array_search($args['record'], $used_filters);
         if ($key !== false) {
             unset($used_filters[$key]);
         }
     } else {
         // delete them all
         $used_filters = array();
     }
     $user_preference->setPreference($args['module_name'], $used_filters, 'filters');
     $user_preference->savePreferencesToDB(true);
     if (!empty($used_filters)) {
         $beans = $this->loadFilters($used_filters);
         $data = $this->formatBeans($api, $args, $beans);
     }
     return $data;
 }
 /**
  *  Create a user in the seed data.
  */
 function _create_seed_user($id, $last_name, $first_name, $user_name, $title, $is_admin, $reports_to, $reports_to_name, $email)
 {
     $u = new User();
     $u->id = $id;
     $u->new_with_id = true;
     $u->last_name = $last_name;
     $u->first_name = $first_name;
     $u->user_name = $user_name;
     $u->title = $title;
     $u->status = 'Active';
     $u->employee_status = 'Active';
     $u->is_admin = $is_admin;
     //$u->user_password = $u->encrypt_password($user_name);
     $u->user_hash = strtolower(md5($user_name));
     $u->reports_to_id = $reports_to;
     $u->reports_to_name = $reports_to_name;
     //$u->email1 = $email;
     $u->emailAddress->addAddress($email, true);
     $u->emailAddress->addAddress("reply." . $email, false, true);
     $u->emailAddress->addAddress("alias." . $email);
     // bug 15371 tyoung set a user preference so that Users/DetailView.php can find something without repeatedly querying the db in vain
     $u->setPreference('max_tabs', '12');
     UserPreference::savePreferencesToDB($u, true);
     $u->save();
 }
 /**
  * Saves all preferences into the database that are in the session. Expensive, this is called by default in
  * sugar_cleanup if a setPreference has been called during one round trip.
  *
  * @global user will use current_user if no user specificed in $user param
  * @param user $user User object to retrieve, otherwise user current_user
  * @param bool $all save all of the preferences? (Dangerous)
  *
  */
 public function savePreferencesToDB($all = false)
 {
     global $sugar_config;
     $GLOBALS['savePreferencesToDB'] = false;
     $user = $this->_userFocus;
     // these are not the preferences you are looking for [ hand waving ]
     if (empty($GLOBALS['installing']) && !empty($_SESSION['unique_key']) && $_SESSION['unique_key'] != $sugar_config['unique_key']) {
         return;
     }
     $GLOBALS['log']->debug('Saving Preferences to DB ' . $user->user_name);
     if (isset($_SESSION[$user->user_name . '_PREFERENCES']) && is_array($_SESSION[$user->user_name . '_PREFERENCES'])) {
         $GLOBALS['log']->debug("Saving Preferences to DB: {$user->user_name}");
         // only save the categories that have been modified or all?
         if (!$all && isset($GLOBALS['savePreferencesToDBCats']) && is_array($GLOBALS['savePreferencesToDBCats'])) {
             $catsToSave = array();
             foreach ($GLOBALS['savePreferencesToDBCats'] as $category => $value) {
                 if (isset($_SESSION[$user->user_name . '_PREFERENCES'][$category])) {
                     $catsToSave[$category] = $_SESSION[$user->user_name . '_PREFERENCES'][$category];
                 }
             }
         } else {
             $catsToSave = $_SESSION[$user->user_name . '_PREFERENCES'];
         }
         foreach ($catsToSave as $category => $contents) {
             $focus = new UserPreference($this->_userFocus);
             $result = $focus->retrieve_by_string_fields(array('assigned_user_id' => $user->id, 'category' => $category));
             $focus->assigned_user_id = $user->id;
             // MFH Bug #13862
             $focus->deleted = 0;
             $focus->contents = base64_encode(serialize($contents));
             $focus->category = $category;
             $focus->save();
         }
     }
 }
 function uninstall_user_prefs($module)
 {
     UserPreference::updateAllUserPrefs('display_tabs', $module, '', true, true);
     UserPreference::updateAllUserPrefs('hide_tabs', $module, '', true, true);
     UserPreference::updateAllUserPrefs('remove_tabs', $module, '', true, true);
 }
Exemple #7
0
 /**
  * Unconditionally reloads user preferences from the DB and updates the session
  * @param string $category name of the category to retreive, defaults to global scope
  * @return bool successful?
  */
 public function reloadPreferences($category = 'global')
 {
     return $this->_userPreferenceFocus->reloadPreferences($category = 'global');
 }
function sugar_cleanup($exit = false)
{
    if (!empty($GLOBALS['savePreferencesToDB']) && $GLOBALS['savePreferencesToDB']) {
        require_once 'modules/UserPreferences/UserPreference.php';
        UserPreference::savePreferencesToDB();
    }
    pre_login_check();
    if (class_exists('PearDatabase')) {
        $db =& PearDatabase::getInstance();
        $db->disconnect();
        if ($exit) {
            exit;
        }
    }
}
 /**
  * Saves all preferences into the database that are in the session. Expensive, this is called by default in
  * sugar_cleanup if a setPreference has been called during one round trip.
  *
  * @global user will use current_user if no user specificed in $user param
  * @param user $user User object to retrieve, otherwise user current_user
  * @param bool $all save all of the preferences? (Dangerous)
  *
  */
 public function savePreferencesToDB($all = false)
 {
     global $sugar_config;
     $GLOBALS['savePreferencesToDB'] = false;
     $user = $this->_userFocus;
     // these are not the preferences you are looking for [ hand waving ]
     if (!empty($_SESSION['unique_key']) && $_SESSION['unique_key'] != $sugar_config['unique_key']) {
         return;
     }
     $GLOBALS['log']->debug('Saving Preferences to DB ' . $user->user_name);
     if (isset($_SESSION[$user->user_name . '_PREFERENCES']) && is_array($_SESSION[$user->user_name . '_PREFERENCES'])) {
         $GLOBALS['log']->debug("Saving Preferences to DB: {$user->user_name}");
         // only save the categories that have been modified or all?
         if (!$all && isset($GLOBALS['savePreferencesToDBCats']) && is_array($GLOBALS['savePreferencesToDBCats'])) {
             $catsToSave = array();
             foreach ($GLOBALS['savePreferencesToDBCats'] as $category => $value) {
                 if (isset($_SESSION[$user->user_name . '_PREFERENCES'][$category])) {
                     $catsToSave[$category] = $_SESSION[$user->user_name . '_PREFERENCES'][$category];
                 }
             }
         } else {
             $catsToSave = $_SESSION[$user->user_name . '_PREFERENCES'];
         }
         foreach ($catsToSave as $category => $contents) {
             $focus = new UserPreference($this->_userFocus);
             $result = $focus->retrieve_by_string_fields(array('assigned_user_id' => $user->id, 'category' => $category));
             $focus->assigned_user_id = $user->id;
             // MFH Bug #13862
             $focus->deleted = 0;
             $focus->contents = base64_encode(serialize($contents));
             $focus->category = $category;
             //save if length is under column max
             if (strlen($focus->contents) > 65535) {
                 //log error and add to error message
                 $GLOBALS['log']->fatal("USERPREFERENCE ERROR:: User preference  for user: '******' and category '{$focus->category}' is " . strlen($focus->contents) . " characters long which is too big to save");
                 $errors[] = 'category: ' . $focus->category . ' is ' . strlen($focus->contents) . ' characters long which is too big and will cause the query to fail.';
                 //set global flag to indicate error has ocurred.  This will cause sugar_cleanup() in utils.php to flash a warning message to user.
                 $_SESSION['USER_PREFRENCE_ERRORS'] = true;
             } else {
                 $focus->save();
             }
         }
     }
 }
function sugar_cleanup($exit = false)
{
    static $called = false;
    if ($called) {
        return;
    }
    $called = true;
    set_include_path(realpath(dirname(__FILE__) . '/..') . PATH_SEPARATOR . get_include_path());
    chdir(realpath(dirname(__FILE__) . '/..'));
    global $sugar_config;
    LogicHook::initialize();
    $GLOBALS['logic_hook']->call_custom_logic('', 'server_round_trip');
    //added this check to avoid errors during install.
    if (empty($sugar_config['dbconfig'])) {
        if ($exit) {
            exit;
        } else {
            return;
        }
    }
    if (!class_exists('Tracker', true)) {
        require_once 'modules/Trackers/Tracker.php';
    }
    Tracker::logPage();
    // Now write the cached tracker_queries
    if (!empty($GLOBALS['savePreferencesToDB']) && $GLOBALS['savePreferencesToDB']) {
        if (!class_exists('UserPreference', true)) {
        }
        UserPreference::savePreferencesToDB();
    }
    pre_login_check();
    if (class_exists('DBManagerFactory')) {
        $db = DBManagerFactory::getInstance();
        $db->disconnect();
        if ($exit) {
            exit;
        }
    }
}
 public function isCurrentUser()
 {
     return parent::isCurrentUser();
 }
 /**
  * Alias for setPreference in modules/UserPreferences/UserPreference.php
  *    
  */
 function getPreference($name, $category = 'global', $user = null)
 {
     if (isset($user)) {
         return UserPreference::getPreference($name, $category, $user);
     } else {
         return UserPreference::getPreference($name, $category, $this);
     }
 }
Exemple #13
0
 /**
  * Saves all preferences into the database that are in the session. Expensive, this is called by default in
  * sugar_cleanup if a setPreference has been called during one round trip.
  *
  * @global user will use current_user if no user specificed in $user param
  * @param user $user User object to retrieve, otherwise user current_user
  * @param bool $all save all of the preferences? (Dangerous)
  *
  */
 function savePreferencesToDB($user = null, $all = false)
 {
     global $sugar_config;
     $GLOBALS['savePreferencesToDB'] = false;
     global $db;
     if (!isset($user)) {
         if (!empty($GLOBALS['current_user'])) {
             $user = $GLOBALS['current_user'];
         } else {
             $GLOBALS['log']->fatal('No User Defined: UserPreferences::savePreferencesToDB');
             return;
             // no user defined, sad panda
         }
     }
     // these are not the preferences you are looking for [ hand waving ]
     if (!empty($_SESSION['unique_key']) && $_SESSION['unique_key'] != $sugar_config['unique_key']) {
         return;
     }
     $GLOBALS['log']->debug('Saving Preferences to DB ' . $user->user_name);
     if (isset($_SESSION[$user->user_name . '_PREFERENCES']) && is_array($_SESSION[$user->user_name . '_PREFERENCES'])) {
         $GLOBALS['log']->debug("Saving Preferences to DB: {$user->user_name}");
         // only save the categories that have been modified or all?
         if (!$all && isset($GLOBALS['savePreferencesToDBCats']) && is_array($GLOBALS['savePreferencesToDBCats'])) {
             $catsToSave = array();
             foreach ($GLOBALS['savePreferencesToDBCats'] as $category => $value) {
                 if (isset($_SESSION[$user->user_name . '_PREFERENCES'][$category])) {
                     $catsToSave[$category] = $_SESSION[$user->user_name . '_PREFERENCES'][$category];
                 }
             }
         } else {
             $catsToSave = $_SESSION[$user->user_name . '_PREFERENCES'];
         }
         $focus = new UserPreference();
         foreach ($catsToSave as $category => $contents) {
             unset($focus->id);
             $query = "SELECT id, contents FROM user_preferences WHERE deleted = 0 AND assigned_user_id = '" . $user->id . "' AND category = '" . $category . "'";
             $result = $db->query($query);
             $row = $db->fetchByAssoc($result);
             if (!empty($row['id'])) {
                 // update
                 $focus->assigned_user_id = $user->id;
                 // MFH Bug #13862
                 $focus->retrieve($row['id'], true, false);
                 $focus->deleted = 0;
                 $focus->contents = base64_encode(serialize($contents));
             } else {
                 // insert new
                 $focus->assigned_user_id = $user->id;
                 $focus->contents = base64_encode(serialize($contents));
                 $focus->category = $category;
             }
             $focus->save();
         }
     }
 }