/** * Sets the specified account settings to the current user. * A table with | Setting label | value | is expected. * * @Given /^I set the following account settings values:$/ * @param TableNode $table */ public function i_set_account_settings(TableNode $table) { global $USER; $prefs = array(); foreach ($table->getHash() as $accountpref) { $prefs[$accountpref['field']] = $accountpref['value']; } // Validate the settings if (isset($prefs['urlid']) && get_config('cleanurls') && $prefs['urlid'] != $USER->get('urlid')) { if (strlen($prefs['urlid']) < 3) { throw new Exception("Invalid urlid: " . get_string('rule.minlength.minlength', 'pieforms', 3)); } else { if (record_exists('usr', 'urlid', $prefs['urlid'])) { throw new Exception("Invalid urlid: " . get_string('urlalreadytaken', 'account')); } } } if (get_config('allowmobileuploads')) { foreach ($prefs['mobileuploadtoken'] as $k => $text) { if (strlen($text) > 0 && !preg_match('/^[a-zA-Z0-9 !@#$%^&*()\\-_=+\\[{\\]};:\'",<\\.>\\/?]{6,}$/', $text)) { throw new Exception("Invalid mobileuploadtoken: " . get_string('badmobileuploadtoken', 'account')); } } } // Update user's account settings db_begin(); // use this as looping through values is not safe. $expectedprefs = expected_account_preferences(); if (isset($prefs['maildisabled']) && $prefs['maildisabled'] == 0 && get_account_preference($USER->get('id'), 'maildisabled') == 1) { // Reset the sent and bounce counts otherwise mail will be disabled // on the next send attempt $u = new StdClass(); $u->email = $USER->get('email'); $u->id = $USER->get('id'); update_bounce_count($u, true); update_send_count($u, true); } // Remember the user's language & theme prefs, so we can reload the page if they change them $oldlang = $USER->get_account_preference('lang'); $oldtheme = $USER->get_account_preference('theme'); $oldgroupsideblockmaxgroups = $USER->get_account_preference('groupsideblockmaxgroups'); $oldgroupsideblocksortby = $USER->get_account_preference('groupsideblocksortby'); if (get_config('allowmobileuploads') && isset($prefs['mobileuploadtoken'])) { // Make sure the mobile token is formatted / saved correctly $prefs['mobileuploadtoken'] = array_filter($prefs['mobileuploadtoken']); $new_token_pref = '|' . join('|', $prefs['mobileuploadtoken']) . '|'; $USER->set_account_preference('mobileuploadtoken', $new_token_pref); unset($prefs['mobileuploadtoken']); } // Set user account preferences foreach ($expectedprefs as $eprefkey => $epref) { if (isset($prefs[$eprefkey]) && $prefs[$eprefkey] !== get_account_preference($USER->get('id'), $eprefkey)) { $USER->set_account_preference($eprefkey, $prefs[$eprefkey]); } } db_commit(); }
$options[$authinstance->id] = $authinstance->displayname . ': ' . $authinstance->instancename; if ($authinstance->authname != 'internal') { $external = true; } } } } $elements = array('firstname' => array('type' => 'text', 'title' => get_string('firstname'), 'rules' => array('required' => true), 'class' => 'form-control text'), 'lastname' => array('type' => 'text', 'title' => get_string('lastname'), 'rules' => array('required' => true), 'class' => 'form-control text'), 'email' => array('type' => 'text', 'title' => get_string('email'), 'rules' => array('required' => true), 'class' => 'form-control text'), 'leap2afile' => array('type' => 'file', 'class' => 'leap2aupload', 'title' => ''), 'username' => array('type' => 'text', 'title' => get_string('username'), 'rules' => array('required' => true, 'maxlength' => 236)), 'password' => array('type' => 'password', 'title' => get_string('password'), 'rules' => array('required' => true)), 'staff' => array('type' => 'switchbox', 'title' => get_string('sitestaff', 'admin'), 'ignore' => !$USER->get('admin')), 'admin' => array('type' => 'switchbox', 'title' => get_string('siteadmin', 'admin'), 'ignore' => !$USER->get('admin')), 'quota' => array('type' => 'bytes', 'title' => get_string('filequota1', 'admin'), 'rules' => array('integer' => true, 'minvalue' => 0), 'defaultvalue' => get_config_plugin('artefact', 'file', 'defaultquota')), 'authinstance' => array('type' => 'select', 'title' => get_string('institution'), 'options' => $options, 'defaultvalue' => 1, 'rules' => array('required' => true), 'ignore' => !$authinstancecount), 'institutionadmin' => array('type' => 'switchbox', 'class' => 'last', 'title' => get_string('institutionadministrator', 'admin'), 'ignore' => !$authinstancecount), 'submit' => array('type' => 'submit', 'value' => get_string('createuser', 'admin'), 'class' => 'btn-success btn-lg btn-block mtm')); if (!$USER->get('admin')) { unset($elements['authinstance']['defaultvalue']); } if (!($USER->get('admin') || get_config_plugin('artefact', 'file', 'institutionaloverride'))) { $elements['quota'] = array('type' => 'text', 'disabled' => true, 'title' => get_string('filequota1', 'admin'), 'description' => get_string('filequotadescription', 'admin'), 'value' => display_size(get_config_plugin('artefact', 'file', 'defaultquota'))); } // Add general account options $prefs = (object) expected_account_preferences(); $elements = array_merge($elements, general_account_prefs_form_elements($prefs)); unset($prefs); $form = pieform(array('name' => 'adduser', 'class' => 'panel panel-default panel-body pts', 'autofocus' => false, 'template' => 'adduser.php', 'templatedir' => pieform_template_dir('adduser.php'), 'plugintype' => 'core', 'pluginname' => 'admin', 'class' => 'form-condensed', 'elements' => $elements)); function adduser_validate(Pieform $form, $values) { global $USER, $TRANSPORTER; $authobj = AuthFactory::create($values['authinstance']); $institution = $authobj->institution; // Institutional admins can only set their own institutions' authinstances if (!$USER->get('admin') && !$USER->is_institutional_admin($authobj->institution)) { $form->set_error('authinstance', get_string('notadminforinstitution', 'admin')); return; } $institution = new Institution($authobj->institution); // Don't exceed max user accounts for the institution
/** * gets an account preference for the user, * or the default if not set for that user, * as specified in {@link expected_account_preferences} * * @param int $userid id of user * @param string $field preference to get */ function get_account_preference($userid, $field) { if ($pref = get_record('usr_account_preference', 'usr', $userid, 'field', $field)) { return $pref->value; } $expected = expected_account_preferences(); return $expected[$field]; }
function accountprefs_submit(Pieform $form, $values) { global $USER; $authobj = AuthFactory::create($USER->authinstance); db_begin(); if (isset($values['password1']) && $values['password1'] !== '') { global $authclass; $password = $authobj->change_password($USER, $values['password1']); $USER->password = $password; $USER->passwordchange = 0; $USER->commit(); } // use this as looping through values is not safe. $expectedprefs = expected_account_preferences(); foreach (array_keys($expectedprefs) as $pref) { if (isset($values[$pref])) { $USER->set_account_preference($pref, $values[$pref]); } } $returndata = array(); if (isset($values['username']) && $values['username'] != $USER->get('username')) { $USER->username = $values['username']; $USER->commit(); $returndata['username'] = $values['username']; } db_commit(); $returndata['message'] = get_string('prefssaved', 'account'); $form->json_reply(PIEFORM_OK, $returndata); }
/** * Update user * * @param object $user stdclass for the usr table * @param object $profile profile field/values to set * @param string $remotename username on the remote site * @param array $accountprefs user account preferences to set * @param bool $forceupdateremote force delete of remotename before update attempted * @return array list of updated fields */ function update_user($user, $profile, $remotename = null, $accountprefs = array(), $forceupdateremote = false, $quickhash = false) { require_once get_config('docroot') . 'auth/session.php'; if (!empty($user->id)) { $oldrecord = get_record('usr', 'id', $user->id); } else { $oldrecord = get_record('usr', 'username', $user->username); } $userid = $oldrecord->id; db_begin(); // Log the user out, otherwise they can overwrite all this on the next request remove_user_sessions($userid); $updated = array(); $newrecord = new StdClass(); foreach (get_object_vars($user) as $k => $v) { if (!empty($v) && ($k == 'password' || empty($oldrecord->{$k}) || $oldrecord->{$k} != $v)) { $newrecord->{$k} = $v; $updated[$k] = $v; } if (!empty($v) && $k === 'email' && $oldrecord->{$k} != $v) { set_user_primary_email($userid, $v); } } if (count(get_object_vars($newrecord))) { $newrecord->id = $userid; update_record('usr', $newrecord); if (!empty($newrecord->password)) { $newrecord->authinstance = $user->authinstance; reset_password($newrecord, false, $quickhash); } } foreach (get_object_vars($profile) as $k => $v) { if (get_profile_field($userid, $k) != $v) { set_profile_field($userid, $k, $v); $updated[$k] = $v; } } if ($remotename) { $oldremote = get_field('auth_remote_user', 'remoteusername', 'authinstance', $oldrecord->authinstance, 'localusr', $userid); if ($remotename != $oldremote) { $updated['remoteuser'] = $remotename; } delete_records('auth_remote_user', 'authinstance', $user->authinstance, 'localusr', $userid); // force the update of the remoteuser - for the case of a series of user updates swapping the remoteuser name if ($forceupdateremote) { delete_records('auth_remote_user', 'authinstance', $user->authinstance, 'remoteusername', $remotename); } else { // remote username must not already exist if (record_exists('auth_remote_user', 'remoteusername', $remotename, 'authinstance', $user->authinstance)) { throw new InvalidArgumentException("user_update: remoteusername already in use: " . $remotename); } } insert_record('auth_remote_user', (object) array('authinstance' => $user->authinstance, 'remoteusername' => $remotename, 'localusr' => $userid)); } // Update account preferences if (!empty($accountprefs)) { $expectedprefs = expected_account_preferences(); foreach ($expectedprefs as $eprefkey => $epref) { if (isset($accountprefs[$eprefkey]) && $accountprefs[$eprefkey] != get_account_preference($userid, $eprefkey)) { set_account_preference($userid, $eprefkey, $accountprefs[$eprefkey]); $updated[$eprefkey] = $accountprefs[$eprefkey]; } } } db_commit(); return $updated; }
function accountprefs_submit(Pieform $form, $values) { global $USER, $SESSION; $authobj = AuthFactory::create($USER->authinstance); db_begin(); $ispasswordchanged = false; if (isset($values['password1']) && $values['password1'] !== '') { global $authclass; $password = $authobj->change_password($USER, $values['password1']); $USER->password = $password; $USER->passwordchange = 0; $USER->commit(); $ispasswordchanged = true; } // use this as looping through values is not safe. $expectedprefs = expected_account_preferences(); if ($values['maildisabled'] == 0 && get_account_preference($USER->get('id'), 'maildisabled') == 1) { // Reset the sent and bounce counts otherwise mail will be disabled // on the next send attempt $u = new StdClass(); $u->email = $USER->get('email'); $u->id = $USER->get('id'); update_bounce_count($u, true); update_send_count($u, true); } // Remember the user's language & theme prefs, so we can reload the page if they change them $oldlang = $USER->get_account_preference('lang'); $oldtheme = $USER->get_account_preference('theme'); $oldgroupsideblockmaxgroups = $USER->get_account_preference('groupsideblockmaxgroups'); $oldgroupsideblocksortby = $USER->get_account_preference('groupsideblocksortby'); if (get_config('allowmobileuploads')) { // Make sure the mobile token is formatted / saved correctly $values['mobileuploadtoken'] = array_filter($values['mobileuploadtoken']); $new_token_pref = empty($values['mobileuploadtoken']) ? null : '|' . join('|', $values['mobileuploadtoken']) . '|'; $USER->set_account_preference('mobileuploadtoken', $new_token_pref); unset($values['mobileuploadtoken']); } // Set user account preferences foreach ($expectedprefs as $eprefkey => $epref) { if (isset($values[$eprefkey]) && $values[$eprefkey] !== get_account_preference($USER->get('id'), $eprefkey)) { $USER->set_account_preference($eprefkey, $values[$eprefkey]); } } $returndata = array(); if (isset($values['username']) && $values['username'] != $USER->get('username')) { $USER->username = $values['username']; $USER->commit(); $returndata['username'] = $values['username']; } $reload = false; if (get_config('cleanurls') && isset($values['urlid']) && $values['urlid'] != $USER->get('urlid')) { $USER->urlid = $values['urlid']; $USER->commit(); $reload = true; } if ($ispasswordchanged) { // Destroy other sessions of the user require_once get_config('docroot') . 'auth/session.php'; remove_user_sessions($USER->get('id')); } db_commit(); $returndata['message'] = get_string('prefssaved', 'account'); if (isset($values['theme']) && $values['theme'] != $oldtheme) { $USER->update_theme(); $reload = true; } if (isset($values['lang']) && $values['lang'] != $oldlang) { // The session language pref is used when the user has no user pref, // and when logged out. $SESSION->set('lang', $values['lang']); $returndata['message'] = get_string_from_language($values['lang'], 'prefssaved', 'account'); $reload = true; } if (isset($values['groupsideblockmaxgroups']) && $values['groupsideblockmaxgroups'] != $oldgroupsideblockmaxgroups) { $reload = true; } if ($values['groupsideblocksortby'] != $oldgroupsideblocksortby) { $reload = true; } $reload = plugin_account_prefs_submit($form, $values) || $reload; if (!empty($reload)) { // Use PIEFORM_CANCEL here to force a page reload and show the new language. $returndata['location'] = get_config('wwwroot') . 'account/index.php'; $SESSION->add_ok_msg($returndata['message']); $form->json_reply(PIEFORM_CANCEL, $returndata); } $form->json_reply(PIEFORM_OK, $returndata); }
function accountprefs_submit(Pieform $form, $values) { global $USER; $authobj = AuthFactory::create($USER->authinstance); db_begin(); if (isset($values['password1']) && $values['password1'] !== '') { global $authclass; $password = $authobj->change_password($USER, $values['password1']); $USER->password = $password; $USER->passwordchange = 0; $USER->commit(); } // use this as looping through values is not safe. $expectedprefs = expected_account_preferences(); if ($values['maildisabled'] == 0 && get_account_preference($USER->get('id'), 'maildisabled') == 1) { // Reset the sent and bounce counts otherwise mail will be disabled // on the next send attempt $u = new StdClass(); $u->email = $USER->get('email'); $u->id = $USER->get('id'); update_bounce_count($u, true); update_send_count($u, true); } foreach (array_keys($expectedprefs) as $pref) { if (isset($values[$pref])) { $USER->set_account_preference($pref, $values[$pref]); } } $returndata = array(); if (isset($values['username']) && $values['username'] != $USER->get('username')) { $USER->username = $values['username']; $USER->commit(); $returndata['username'] = $values['username']; } db_commit(); $returndata['message'] = get_string('prefssaved', 'account'); $form->json_reply(PIEFORM_OK, $returndata); }