Merge branch 'MDL-52781-master' of git://github.com/lameze/moodle

This commit is contained in:
Andrew Nicols 2016-04-21 15:43:35 +08:00
commit 6ab41abd3e
26 changed files with 791 additions and 355 deletions

View File

@ -212,7 +212,7 @@ if ($formdata = $mform2->is_cancelled()) {
// init upload progress tracker
$upt = new uu_progress_tracker();
$upt->start(); // start table
$validation = array();
while ($line = $cir->next()) {
$upt->flush();
$linenum++;
@ -280,7 +280,7 @@ if ($formdata = $mform2->is_cancelled()) {
// normalize username
$originalusername = $user->username;
if ($standardusernames) {
$user->username = clean_param($user->username, PARAM_USERNAME);
$user->username = core_user::clean_field($user->username, 'username');
}
// make sure we really have username
@ -295,7 +295,7 @@ if ($formdata = $mform2->is_cancelled()) {
continue;
}
if ($user->username !== clean_param($user->username, PARAM_USERNAME)) {
if ($user->username !== core_user::clean_field($user->username, 'username')) {
$upt->track('status', get_string('invalidusername', 'error', 'username'), 'error');
$upt->track('username', $errorstr, 'error');
$userserrors++;
@ -443,7 +443,7 @@ if ($formdata = $mform2->is_cancelled()) {
}
if ($standardusernames) {
$oldusername = clean_param($user->oldusername, PARAM_USERNAME);
$oldusername = core_user::clean_field($user->oldusername, 'username');
} else {
$oldusername = $user->oldusername;
}
@ -597,7 +597,7 @@ if ($formdata = $mform2->is_cancelled()) {
if (empty($user->lang)) {
// Do not change to not-set value.
continue;
} else if (clean_param($user->lang, PARAM_LANG) === '') {
} else if (core_user::clean_field($user->lang, 'lang') === '') {
$upt->track('status', get_string('cannotfindlang', 'error', $user->lang), 'warning');
continue;
}
@ -774,7 +774,7 @@ if ($formdata = $mform2->is_cancelled()) {
if (empty($user->lang)) {
$user->lang = '';
} else if (clean_param($user->lang, PARAM_LANG) === '') {
} else if (core_user::clean_field($user->lang, 'lang') === '') {
$upt->track('status', get_string('cannotfindlang', 'error', $user->lang), 'warning');
$user->lang = '';
}
@ -1115,9 +1115,14 @@ if ($formdata = $mform2->is_cancelled()) {
}
}
}
$validation[$user->username] = core_user::validate($user);
}
$upt->close(); // close table
if (!empty($validation)) {
foreach ($validation as $username => $error) {
\core\notification::warning(get_string('invaliduserdata', 'tool_uploaduser', s($username)));
}
}
$cir->close();
$cir->cleanup(true);
@ -1177,7 +1182,7 @@ while ($linenum <= $previewrows and $fields = $cir->next()) {
$rowcols['status'] = array();
if (isset($rowcols['username'])) {
$stdusername = clean_param($rowcols['username'], PARAM_USERNAME);
$stdusername = core_user::clean_field($rowcols['username'], 'username');
if ($rowcols['username'] !== $stdusername) {
$rowcols['status'][] = get_string('invalidusernameupload');
}

View File

@ -33,6 +33,7 @@ $string['deleteerrors'] = 'Delete errors';
$string['encoding'] = 'Encoding';
$string['errormnetadd'] = 'Can not add remote users';
$string['errors'] = 'Errors';
$string['invaliduserdata'] = 'Invalid data detected for user {$a} and it has been automatically cleaned.';
$string['nochanges'] = 'No changes';
$string['pluginname'] = 'User upload';
$string['renameerrors'] = 'Rename errors';

View File

@ -226,28 +226,28 @@ class admin_uploaduser_form2 extends moodleform {
$choices = array(0 => get_string('emaildisplayno'), 1 => get_string('emaildisplayyes'), 2 => get_string('emaildisplaycourse'));
$mform->addElement('select', 'maildisplay', get_string('emaildisplay'), $choices);
$mform->setDefault('maildisplay', $CFG->defaultpreference_maildisplay);
$mform->setDefault('maildisplay', core_user::get_property_default('maildisplay'));
$choices = array(0 => get_string('textformat'), 1 => get_string('htmlformat'));
$mform->addElement('select', 'mailformat', get_string('emailformat'), $choices);
$mform->setDefault('mailformat', $CFG->defaultpreference_mailformat);
$mform->setDefault('mailformat', core_user::get_property_default('mailformat'));
$mform->setAdvanced('mailformat');
$choices = array(0 => get_string('emaildigestoff'), 1 => get_string('emaildigestcomplete'), 2 => get_string('emaildigestsubjects'));
$mform->addElement('select', 'maildigest', get_string('emaildigest'), $choices);
$mform->setDefault('maildigest', $CFG->defaultpreference_maildigest);
$mform->setDefault('maildigest', core_user::get_property_default('maildigest'));
$mform->setAdvanced('maildigest');
$choices = array(1 => get_string('autosubscribeyes'), 0 => get_string('autosubscribeno'));
$mform->addElement('select', 'autosubscribe', get_string('autosubscribe'), $choices);
$mform->setDefault('autosubscribe', $CFG->defaultpreference_autosubscribe);
$mform->setDefault('autosubscribe', core_user::get_property_default('autosubscribe'));
$mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="25"');
$mform->setType('city', PARAM_TEXT);
if (empty($CFG->defaultcity)) {
$mform->setDefault('city', $templateuser->city);
} else {
$mform->setDefault('city', $CFG->defaultcity);
$mform->setDefault('city', core_user::get_property_default('city'));
}
$choices = get_string_manager()->get_list_of_countries();
@ -256,7 +256,7 @@ class admin_uploaduser_form2 extends moodleform {
if (empty($CFG->country)) {
$mform->setDefault('country', $templateuser->country);
} else {
$mform->setDefault('country', $CFG->country);
$mform->setDefault('country', core_user::get_property_default('country'));
}
$mform->setAdvanced('country');

View File

@ -41,28 +41,28 @@ class moodle_user_create_users_form extends moodleform {
$data = $this->_customdata;
if ($data['authmethod'] == 'simple') {
$mform->addElement('text', 'wsusername', 'wsusername');
$mform->setType('wsusername', PARAM_USERNAME);
$mform->setType('wsusername', core_user::get_property_type('username'));
$mform->addElement('text', 'wspassword', 'wspassword');
$mform->setType('wspassword', PARAM_RAW);
$mform->setType('wspassword', core_user::get_property_type('password'));
} else if ($data['authmethod'] == 'token') {
$mform->addElement('text', 'token', 'token');
$mform->setType('token', PARAM_RAW_TRIMMED);
}
$mform->addElement('hidden', 'authmethod', $data['authmethod']);
$mform->setType('authmethod', PARAM_SAFEDIR);
$mform->setType('authmethod', core_user::get_property_type('auth'));
/// specific to the create users function
$mform->addElement('text', 'username', 'username');
$mform->setType('username', PARAM_USERNAME);
$mform->setType('username', core_user::get_property_type('username'));
$mform->addElement('text', 'password', 'password');
$mform->setType('password', PARAM_RAW);
$mform->setType('password', core_user::get_property_type('password'));
$mform->addElement('text', 'firstname', 'firstname');
$mform->setType('firstname', PARAM_RAW);
$mform->setType('firstname', core_user::get_property_type('firstname'));
$mform->addElement('text', 'lastname', 'lastname');
$mform->setType('lastname', PARAM_RAW);
$mform->setType('lastname', core_user::get_property_type('lastname'));
$mform->addElement('text', 'email', 'email');
$mform->setType('email', PARAM_EMAIL);
$mform->setType('email', core_user::get_property_type('email'));
$mform->addElement('text', 'customfieldtype', 'customfieldtype');
$mform->setType('customfieldtype', PARAM_RAW);
@ -125,31 +125,31 @@ class moodle_user_update_users_form extends moodleform {
$data = $this->_customdata;
if ($data['authmethod'] == 'simple') {
$mform->addElement('text', 'wsusername', 'wsusername');
$mform->setType('wsusername', PARAM_USERNAME);
$mform->setType('wsusername', core_user::get_property_type('username'));
$mform->addElement('text', 'wspassword', 'wspassword');
$mform->setType('wspassword', PARAM_RAW);
$mform->setType('wspassword', core_user::get_property_type('password'));
} else if ($data['authmethod'] == 'token') {
$mform->addElement('text', 'token', 'token');
$mform->setType('token', PARAM_RAW_TRIMMED);
}
$mform->addElement('hidden', 'authmethod', $data['authmethod']);
$mform->setType('authmethod', PARAM_ALPHA);
$mform->setType('authmethod', core_user::get_property_type('auth'));
/// specific to the create users function
$mform->addElement('text', 'id', 'id');
$mform->addRule('id', get_string('required'), 'required', null, 'client');
$mform->setType('id', PARAM_INT);
$mform->setType('id', core_user::get_property_type('id'));
$mform->addElement('text', 'username', 'username');
$mform->setType('username', PARAM_USERNAME);
$mform->setType('username', core_user::get_property_type('username'));
$mform->addElement('text', 'password', 'password');
$mform->setType('password', PARAM_RAW);
$mform->setType('password', core_user::get_property_type('password'));
$mform->addElement('text', 'firstname', 'firstname');
$mform->setType('firstname', PARAM_RAW);
$mform->setType('firstname', core_user::get_property_type('firstname'));
$mform->addElement('text', 'lastname', 'lastname');
$mform->setType('lastname', PARAM_RAW);
$mform->setType('lastname', core_user::get_property_type('lastname'));
$mform->addElement('text', 'email', 'email');
$mform->setType('email', PARAM_EMAIL);
$mform->setType('email', core_user::get_property_type('email'));
$mform->addElement('text', 'customfieldtype', 'customfieldtype');
@ -219,23 +219,23 @@ class moodle_user_delete_users_form extends moodleform {
$data = $this->_customdata;
if ($data['authmethod'] == 'simple') {
$mform->addElement('text', 'wsusername', 'wsusername');
$mform->setType('wsusername', PARAM_USERNAME);
$mform->setType('wsusername', core_user::get_property_type('username'));
$mform->addElement('text', 'wspassword', 'wspassword');
$mform->setType('wspassword', PARAM_RAW);
$mform->setType('wspassword', core_user::get_property_type('password'));
} else if ($data['authmethod'] == 'token') {
$mform->addElement('text', 'token', 'token');
$mform->setType('token', PARAM_RAW_TRIMMED);
}
$mform->addElement('hidden', 'authmethod', $data['authmethod']);
$mform->setType('authmethod', PARAM_ALPHA);
$mform->setType('authmethod', core_user::get_property_type('auth'));
/// beginning of specific code to the create users function
$mform->addElement('text', 'userids[0]', 'userids[0]');
$mform->addElement('text', 'userids[1]', 'userids[1]');
$mform->addElement('text', 'userids[2]', 'userids[2]');
$mform->addElement('text', 'userids[3]', 'userids[3]');
$mform->setType('userids', PARAM_INT);
$mform->setType('userids', core_user::get_property_type('id'));
/// end of specific code to the create users function
$mform->addElement('hidden', 'function');
@ -291,23 +291,23 @@ class moodle_user_get_users_by_id_form extends moodleform {
$data = $this->_customdata;
if ($data['authmethod'] == 'simple') {
$mform->addElement('text', 'wsusername', 'wsusername');
$mform->setType('wsusername', PARAM_USERNAME);
$mform->setType('wsusername', core_user::get_property_type('username'));
$mform->addElement('text', 'wspassword', 'wspassword');
$mform->setType('wspassword', PARAM_RAW);
$mform->setType('wspassword', core_user::get_property_type('password'));
} else if ($data['authmethod'] == 'token') {
$mform->addElement('text', 'token', 'token');
$mform->setType('token', PARAM_RAW_TRIMMED);
}
$mform->addElement('hidden', 'authmethod', $data['authmethod']);
$mform->setType('authmethod', PARAM_ALPHA);
$mform->setType('authmethod', core_user::get_property_type('auth'));
/// beginning of specific code to the create users function
$mform->addElement('text', 'userids[0]', 'userids[0]');
$mform->addElement('text', 'userids[1]', 'userids[1]');
$mform->addElement('text', 'userids[2]', 'userids[2]');
$mform->addElement('text', 'userids[3]', 'userids[3]');
$mform->setType('userids', PARAM_INT);
$mform->setType('userids', core_user::get_property_type('id'));
/// end of specific code to the create users function
$mform->addElement('hidden', 'function');
@ -364,16 +364,16 @@ class moodle_group_create_groups_form extends moodleform {
$data = $this->_customdata;
if ($data['authmethod'] == 'simple') {
$mform->addElement('text', 'wsusername', 'wsusername');
$mform->setType('wsusername', PARAM_USERNAME);
$mform->setType('wsusername', core_user::get_property_type('username'));
$mform->addElement('text', 'wspassword', 'wspassword');
$mform->setType('wspassword', PARAM_RAW);
$mform->setType('wspassword', core_user::get_property_type('password'));
} else if ($data['authmethod'] == 'token') {
$mform->addElement('text', 'token', 'token');
$mform->setType('token', PARAM_RAW_TRIMMED);
}
$mform->addElement('hidden', 'authmethod', $data['authmethod']);
$mform->setType('authmethod', PARAM_ALPHA);
$mform->setType('authmethod', core_user::get_property_type('auth'));
$mform->addElement('text', 'courseid', 'courseid');
$mform->setType('courseid', PARAM_INT);
@ -430,16 +430,16 @@ class moodle_group_get_groups_form extends moodleform {
$data = $this->_customdata;
if ($data['authmethod'] == 'simple') {
$mform->addElement('text', 'wsusername', 'wsusername');
$mform->setType('wsusername', PARAM_USERNAME);
$mform->setType('wsusername', core_user::get_property_type('username'));
$mform->addElement('text', 'wspassword', 'wspassword');
$mform->setType('wspassword', PARAM_RAW);
$mform->setType('wspassword', core_user::get_property_type('password'));
} else if ($data['authmethod'] == 'token') {
$mform->addElement('text', 'token', 'token');
$mform->setType('token', PARAM_RAW_TRIMMED);
}
$mform->addElement('hidden', 'authmethod', $data['authmethod']);
$mform->setType('authmethod', PARAM_ALPHA);
$mform->setType('authmethod', core_user::get_property_type('auth'));
$mform->addElement('text', 'groupids[0]', 'groupids[0]');
$mform->addElement('text', 'groupids[1]', 'groupids[1]');
$mform->addElement('text', 'groupids[2]', 'groupids[2]');
@ -493,16 +493,16 @@ class moodle_group_get_course_groups_form extends moodleform {
$data = $this->_customdata;
if ($data['authmethod'] == 'simple') {
$mform->addElement('text', 'wsusername', 'wsusername');
$mform->setType('wsusername', PARAM_USERNAME);
$mform->setType('wsusername', core_user::get_property_type('username'));
$mform->addElement('text', 'wspassword', 'wspassword');
$mform->setType('wspassword', PARAM_RAW);
$mform->setType('wspassword', core_user::get_property_type('password'));
} else if ($data['authmethod'] == 'token') {
$mform->addElement('text', 'token', 'token');
$mform->setType('token', PARAM_RAW_TRIMMED);
}
$mform->addElement('hidden', 'authmethod', $data['authmethod']);
$mform->setType('authmethod', PARAM_ALPHA);
$mform->setType('authmethod', core_user::get_property_type('auth'));
$mform->addElement('text', 'courseid', 'courseid');
$mform->addElement('hidden', 'function');
@ -546,16 +546,16 @@ class moodle_group_delete_groups_form extends moodleform {
$data = $this->_customdata;
if ($data['authmethod'] == 'simple') {
$mform->addElement('text', 'wsusername', 'wsusername');
$mform->setType('wsusername', PARAM_USERNAME);
$mform->setType('wsusername', core_user::get_property_type('username'));
$mform->addElement('text', 'wspassword', 'wspassword');
$mform->setType('wspassword', PARAM_RAW);
$mform->setType('wspassword', core_user::get_property_type('password'));
} else if ($data['authmethod'] == 'token') {
$mform->addElement('text', 'token', 'token');
$mform->setType('token', PARAM_RAW_TRIMMED);
}
$mform->addElement('hidden', 'authmethod', $data['authmethod']);
$mform->setType('authmethod', PARAM_ALPHA);
$mform->setType('authmethod', core_user::get_property_type('auth'));
$mform->addElement('text', 'groupids[0]', 'groupids[0]');
$mform->addElement('text', 'groupids[1]', 'groupids[1]');
$mform->addElement('text', 'groupids[2]', 'groupids[2]');
@ -611,16 +611,16 @@ class moodle_group_get_groupmembers_form extends moodleform {
$data = $this->_customdata;
if ($data['authmethod'] == 'simple') {
$mform->addElement('text', 'wsusername', 'wsusername');
$mform->setType('wsusername', PARAM_USERNAME);
$mform->setType('wsusername', core_user::get_property_type('username'));
$mform->addElement('text', 'wspassword', 'wspassword');
$mform->setType('wspassword', PARAM_RAW);
$mform->setType('wspassword', core_user::get_property_type('password'));
} else if ($data['authmethod'] == 'token') {
$mform->addElement('text', 'token', 'token');
$mform->setType('token', PARAM_RAW_TRIMMED);
}
$mform->addElement('hidden', 'authmethod', $data['authmethod']);
$mform->setType('authmethod', PARAM_ALPHA);
$mform->setType('authmethod', core_user::get_property_type('auth'));
$mform->addElement('text', 'groupids[0]', 'groupids[0]');
$mform->addElement('text', 'groupids[1]', 'groupids[1]');
$mform->addElement('text', 'groupids[2]', 'groupids[2]');
@ -674,21 +674,21 @@ class moodle_group_add_groupmembers_form extends moodleform {
$data = $this->_customdata;
if ($data['authmethod'] == 'simple') {
$mform->addElement('text', 'wsusername', 'wsusername');
$mform->setType('wsusername', PARAM_USERNAME);
$mform->setType('wsusername', core_user::get_property_type('username'));
$mform->addElement('text', 'wspassword', 'wspassword');
$mform->setType('wspassword', PARAM_RAW);
$mform->setType('wspassword', core_user::get_property_type('password'));
} else if ($data['authmethod'] == 'token') {
$mform->addElement('text', 'token', 'token');
$mform->setType('token', PARAM_RAW_TRIMMED);
}
$mform->addElement('hidden', 'authmethod', $data['authmethod']);
$mform->setType('authmethod', PARAM_SAFEDIR);
$mform->setType('authmethod', core_user::get_property_type('auth'));
$mform->addElement('text', 'userid[0]', 'userid[0]');
$mform->addElement('text', 'groupid[0]', 'groupid[0]');
$mform->addElement('text', 'userid[1]', 'userid[1]');
$mform->addElement('text', 'groupid[1]', 'groupid[1]');
$mform->setType('userid', PARAM_INT);
$mform->setType('userid', core_user::get_property_type('id'));
$mform->setType('groupids', PARAM_INT);
$mform->addElement('hidden', 'function');
@ -738,16 +738,16 @@ class moodle_group_delete_groupmembers_form extends moodleform {
$data = $this->_customdata;
if ($data['authmethod'] == 'simple') {
$mform->addElement('text', 'wsusername', 'wsusername');
$mform->setType('wsusername', PARAM_USERNAME);
$mform->setType('wsusername', core_user::get_property_type('username'));
$mform->addElement('text', 'wspassword', 'wspassword');
$mform->setType('wspassword', PARAM_RAW);
$mform->setType('wspassword', core_user::get_property_type('password'));
} else if ($data['authmethod'] == 'token') {
$mform->addElement('text', 'token', 'token');
$mform->setType('token', PARAM_RAW_TRIMMED);
}
$mform->addElement('hidden', 'authmethod', $data['authmethod']);
$mform->setType('authmethod', PARAM_ALPHA);
$mform->setType('authmethod', core_user::get_property_type('auth'));
$mform->addElement('text', 'userid[0]', 'userid[0]');
$mform->addElement('text', 'groupid[0]', 'groupid[0]');
$mform->addElement('text', 'userid[1]', 'userid[1]');
@ -812,16 +812,16 @@ class core_course_create_categories_form extends moodleform {
$data = $this->_customdata;
if ($data['authmethod'] == 'simple') {
$mform->addElement('text', 'wsusername', 'wsusername');
$mform->setType('wsusername', PARAM_USERNAME);
$mform->setType('wsusername', core_user::get_property_type('username'));
$mform->addElement('text', 'wspassword', 'wspassword');
$mform->setType('wspassword', PARAM_RAW);
$mform->setType('wspassword', core_user::get_property_type('password'));
} else if ($data['authmethod'] == 'token') {
$mform->addElement('text', 'token', 'token');
$mform->setType('token', PARAM_RAW_TRIMMED);
}
$mform->addElement('hidden', 'authmethod', $data['authmethod']);
$mform->setType('authmethod', PARAM_ALPHA);
$mform->setType('authmethod', core_user::get_property_type('auth'));
$mform->addElement('text', 'name[0]', 'name[0]');
$mform->addElement('text', 'parent[0]', 'parent[0]');
$mform->addElement('text', 'idnumber[0]', 'idnumber[0]');
@ -830,10 +830,10 @@ class core_course_create_categories_form extends moodleform {
$mform->addElement('text', 'parent[1]', 'parent[1]');
$mform->addElement('text', 'idnumber[1]', 'idnumber[1]');
$mform->addElement('text', 'description[1]', 'description[1]');
$mform->setType('name', PARAM_TEXT);
$mform->setType('parent', PARAM_INT);
$mform->setType('idnumber', PARAM_RAW);
$mform->setType('description', PARAM_TEXT);
$mform->setType('name', core_user::get_property_type('firstname'));
$mform->setType('parent', core_user::get_property_type('id'));
$mform->setType('idnumber', core_user::get_property_type('idnumber'));
$mform->setType('description', core_user::get_property_type('description'));
$mform->addElement('hidden', 'function');
$mform->setType('function', PARAM_PLUGIN);
@ -896,23 +896,23 @@ class core_course_delete_categories_form extends moodleform {
$data = $this->_customdata;
if ($data['authmethod'] == 'simple') {
$mform->addElement('text', 'wsusername', 'wsusername');
$mform->setType('wsusername', PARAM_USERNAME);
$mform->setType('wsusername', core_user::get_property_type('username'));
$mform->addElement('text', 'wspassword', 'wspassword');
$mform->setType('wspassword', PARAM_RAW);
$mform->setType('wspassword', core_user::get_property_type('password'));
} else if ($data['authmethod'] == 'token') {
$mform->addElement('text', 'token', 'token');
$mform->setType('token', PARAM_RAW_TRIMMED);
}
$mform->addElement('hidden', 'authmethod', $data['authmethod']);
$mform->setType('authmethod', PARAM_ALPHA);
$mform->setType('authmethod', core_user::get_property_type('auth'));
$mform->addElement('text', 'id[0]', 'id[0]');
$mform->addElement('text', 'newparent[0]', 'newparent[0]');
$mform->addElement('text', 'recursive[0]', 'recursive[0]');
$mform->addElement('text', 'id[1]', 'id[1]');
$mform->addElement('text', 'newparent[1]', 'newparent[1]');
$mform->addElement('text', 'recursive[1]', 'recursive[1]');
$mform->setType('id', PARAM_INT);
$mform->setType('id', core_user::get_property_type('id'));
$mform->setType('newparent', PARAM_INT);
$mform->setType('recursive', PARAM_BOOL);
@ -984,16 +984,16 @@ class core_course_update_categories_form extends moodleform {
$data = $this->_customdata;
if ($data['authmethod'] == 'simple') {
$mform->addElement('text', 'wsusername', 'wsusername');
$mform->setType('wsusername', PARAM_USERNAME);
$mform->setType('wsusername', core_user::get_property_type('username'));
$mform->addElement('text', 'wspassword', 'wspassword');
$mform->setType('wspassword', PARAM_RAW);
$mform->setType('wspassword', core_user::get_property_type('password'));
} else if ($data['authmethod'] == 'token') {
$mform->addElement('text', 'token', 'token');
$mform->setType('token', PARAM_RAW_TRIMMED);
}
$mform->addElement('hidden', 'authmethod', $data['authmethod']);
$mform->setType('authmethod', PARAM_ALPHA);
$mform->setType('authmethod', core_user::get_property_type('auth'));
$mform->addElement('text', 'id[0]', 'id[0]');
$mform->addElement('text', 'name[0]', 'name[0]');
$mform->addElement('text', 'parent[0]', 'parent[0]');
@ -1004,11 +1004,11 @@ class core_course_update_categories_form extends moodleform {
$mform->addElement('text', 'parent[1]', 'parent[1]');
$mform->addElement('text', 'idnumber[1]', 'idnumber[1]');
$mform->addElement('text', 'description[1]', 'description[1]');
$mform->setType('id', PARAM_INT);
$mform->setType('name', PARAM_TEXT);
$mform->setType('id', core_user::get_property_type('id'));
$mform->setType('name', core_user::get_property_type('firstname'));
$mform->setType('parent', PARAM_INT);
$mform->setType('idnumber', PARAM_RAW);
$mform->setType('description', PARAM_TEXT);
$mform->setType('idnumber', core_user::get_property_type('idnumber'));
$mform->setType('description', core_user::get_property_type('description'));
$mform->addElement('hidden', 'function');
$mform->setType('function', PARAM_PLUGIN);

View File

@ -328,7 +328,6 @@ class auth_plugin_db extends auth_plugin_base {
$updateuser = new stdClass();
$updateuser->id = $user->id;
$updateuser->suspended = 1;
$updateuser = $this->clean_data($updateuser);
user_update_user($updateuser, false);
$trace->output(get_string('auth_dbsuspenduser', 'auth_db', array('name'=>$user->username, 'id'=>$user->id)), 1);
}
@ -415,7 +414,6 @@ class auth_plugin_db extends auth_plugin_base {
$updateuser = new stdClass();
$updateuser->id = $olduser->id;
$updateuser->suspended = 0;
$updateuser = $this->clean_data($updateuser);
user_update_user($updateuser);
$trace->output(get_string('auth_dbreviveduser', 'auth_db', array('name' => $username,
'id' => $olduser->id)), 1);
@ -438,7 +436,6 @@ class auth_plugin_db extends auth_plugin_base {
$trace->output(get_string('auth_dbinsertuserduplicate', 'auth_db', array('username'=>$user->username, 'auth'=>$collision->auth)), 1);
continue;
}
$user = $this->clean_data($user);
try {
$id = user_create_user($user, false); // It is truly a new user.
$trace->output(get_string('auth_dbinsertuser', 'auth_db', array('name'=>$user->username, 'id'=>$id)), 1);
@ -580,7 +577,6 @@ class auth_plugin_db extends auth_plugin_base {
}
if ($needsupdate) {
require_once($CFG->dirroot . '/user/lib.php');
$updateuser = $this->clean_data($updateuser);
user_update_user($updateuser);
}
return $DB->get_record('user', array('id'=>$userid, 'deleted'=>0));
@ -913,26 +909,14 @@ class auth_plugin_db extends auth_plugin_base {
/**
* Clean the user data that comes from an external database.
*
* @deprecated since 3.1, please use core_user::clean_data() instead.
* @param array $user the user data to be validated against properties definition.
* @return stdClass $user the cleaned user data.
*/
public function clean_data($user) {
if (empty($user)) {
return $user;
}
foreach ($user as $field => $value) {
// Get the property parameter type and do the cleaning.
try {
$property = core_user::get_property_definition($field);
$user->$field = clean_param($value, $property['type']);
} catch (coding_exception $e) {
debugging("The property '$field' could not be cleaned.", DEBUG_DEVELOPER);
}
}
return $user;
debugging('The method clean_data() has been deprecated, please use core_user::clean_data() instead.',
DEBUG_DEVELOPER);
return core_user::clean_data($user);
}
}

View File

@ -121,7 +121,9 @@ class auth_db_testcase extends advanced_testcase {
set_config('table', $CFG->prefix.'auth_db_users', 'auth/db');
set_config('fielduser', 'name', 'auth/db');
set_config('fieldpass', 'pass', 'auth/db');
set_config('field_map_lastname', 'lastname', 'auth/db');
set_config('field_updatelocal_lastname', 'oncreate', 'auth/db');
set_config('field_lock_lastname', 'unlocked', 'auth/db');
// Setu up field mappings.
set_config('field_map_email', 'email', 'auth/db');
@ -149,7 +151,7 @@ class auth_db_testcase extends advanced_testcase {
public function test_plugin() {
global $DB, $CFG;
$this->resetAfterTest(false);
$this->resetAfterTest(true);
// NOTE: It is strongly discouraged to create new tables in advanced_testcase classes,
// but there is no other simple way to test ext database enrol sync, so let's
@ -416,60 +418,31 @@ class auth_db_testcase extends advanced_testcase {
$extdbuser1 = (object)array('name'=>'u1', 'pass'=>'heslo', 'email'=>'u1@example.com');
$extdbuser1->id = $DB->insert_record('auth_db_users', $extdbuser1);
// User with malicious data on the name.
// User with malicious data on the name (won't be imported).
$extdbuser2 = (object)array('name'=>'user<script>alert(1);</script>xss', 'pass'=>'heslo', 'email'=>'xssuser@example.com');
$extdbuser2->id = $DB->insert_record('auth_db_users', $extdbuser2);
$extdbuser3 = (object)array('name'=>'u3', 'pass'=>'heslo', 'email'=>'u3@example.com',
'lastname' => 'user<script>alert(1);</script>xss');
$extdbuser3->id = $DB->insert_record('auth_db_users', $extdbuser3);
$trace = new null_progress_trace();
// Let's test user sync make sure still works as expected..
$auth->sync_users($trace, true);
// Get the user on moodle user table.
$user2 = $DB->get_record('user', array('email'=> $extdbuser2->email, 'auth'=>'db'));
// The malicious code should be sanitized.
$this->assertEquals($user2->username, 'userscriptalert1scriptxss');
$this->assertNotEquals($user2->username, $extdbuser2->name);
$this->assertDebuggingCalled("The property 'lastname' has invalid data and has been cleaned.");
// User with correct data, should be equal to external db.
$user1 = $DB->get_record('user', array('email'=> $extdbuser1->email, 'auth'=>'db'));
$this->assertEquals($extdbuser1->name, $user1->username);
$this->assertEquals($extdbuser1->email, $user1->email);
// Now, let's update the name.
$extdbuser2->name = 'user no xss anymore';
$DB->update_record('auth_db_users', $extdbuser2);
// Get the user on moodle user table.
$user2 = $DB->get_record('user', array('email'=> $extdbuser2->email, 'auth'=>'db'));
$user3 = $DB->get_record('user', array('email'=> $extdbuser3->email, 'auth'=>'db'));
// Run sync again to update the user data.
$auth->sync_users($trace, true);
$this->assertEmpty($user2);
$this->assertEquals($extdbuser3->name, $user3->username);
$this->assertEquals('useralert(1);xss', $user3->lastname);
// The user information should be updated.
$user2 = $DB->get_record('user', array('username' => 'usernoxssanymore', 'auth' => 'db'));
// The spaces should be removed, as it's the username.
$this->assertEquals($user2->username, 'usernoxssanymore');
// Now let's test just the clean_data() method isolated.
// Testing PARAM_USERNAME, PARAM_NOTAGS, PARAM_RAW_TRIMMED and others.
$user3 = new stdClass();
$user3->firstname = 'John <script>alert(1)</script> Doe';
$user3->username = 'john%#&~%*_doe';
$user3->email = ' john@testing.com ';
$user3->deleted = 'no';
$user3->description = '<b>A description <script>alert(123)</script>about myself.</b>';
$user3cleaned = $auth->clean_data($user3);
// Expected results.
$this->assertEquals($user3cleaned->firstname, 'John alert(1) Doe');
$this->assertEquals($user3cleaned->email, 'john@testing.com');
$this->assertEquals($user3cleaned->deleted, 0);
$this->assertEquals($user3->description, '<b>A description about myself.</b>');
$this->assertEquals($user3->username, 'john_doe');
// Try to clean an invalid property (fullname).
$user3->fullname = 'John Doe';
$auth->clean_data($user3);
$this->assertDebuggingCalled("The property 'fullname' could not be cleaned.");
$this->cleanup_auth_database();
}
}

View File

@ -1,6 +1,12 @@
This files describes API changes in /auth/db/*,
information provided here is intended especially for developers.
=== 3.1 ===
* The auth_plugin_db::clean_data() has been deprecated and will be removed
in a future version. Please update to use core_user::clean_data()
instead.
=== 2.9 ===
Some alterations have been made to the handling of case sensitity handling of passwords

View File

@ -365,7 +365,7 @@ class auth_ldap_plugin_testcase extends advanced_testcase {
'email' => 'usersignuptest1@example.com',
'description' => 'This is a description for user 1',
'city' => 'Perth',
'country' => 'au',
'country' => 'AU',
'mnethostid' => $CFG->mnet_localhost_id,
'auth' => 'ldap'
);

View File

@ -352,6 +352,7 @@ $string['invalidurl'] = 'Invalid URL';
$string['invaliduser'] = 'Invalid user';
$string['invaliduserid'] = 'Invalid user id';
$string['invaliduserfield'] = 'Invalid user field: {$a}';
$string['invaliduserdata'] = 'Invalid user data: {$a}';
$string['invalidusername'] = 'The given username contains invalid characters';
$string['invalidxmlfile'] = '"{$a}" is not a valid XML file';
$string['iplookupfailed'] = 'Cannot find geo information about this IP address {$a}';

View File

@ -281,10 +281,21 @@ class core_user {
/**
* Definition of user profile fields and the expected parameter type for data validation.
*
* array(
* 'property_name' => array( // The user property to be checked. Should match the field on the user table.
* 'null' => NULL_ALLOWED, // Defaults to NULL_NOT_ALLOWED. Takes NULL_NOT_ALLOWED or NULL_ALLOWED.
* 'type' => PARAM_TYPE, // Expected parameter type of the user field.
* 'choices' => array(1, 2..) // An array of accepted values of the user field.
* 'default' => $CFG->setting // An default value for the field.
* )
* )
*
* The fields choices and default are optional.
*
* @return void
*/
protected static function fill_properties_cache() {
global $CFG;
if (self::$propertiescache !== null) {
return;
}
@ -292,60 +303,70 @@ class core_user {
// Array of user fields properties and expected parameters.
// Every new field on the user table should be added here otherwise it won't be validated.
$fields = array();
$fields['id'] = array('type' => PARAM_INT);
$fields['auth'] = array('type' => PARAM_NOTAGS);
$fields['confirmed'] = array('type' => PARAM_BOOL);
$fields['policyagreed'] = array('type' => PARAM_BOOL);
$fields['deleted'] = array('type' => PARAM_BOOL);
$fields['suspended'] = array('type' => PARAM_BOOL);
$fields['mnethostid'] = array('type' => PARAM_BOOL);
$fields['username'] = array('type' => PARAM_USERNAME);
$fields['password'] = array('type' => PARAM_NOTAGS);
$fields['idnumber'] = array('type' => PARAM_NOTAGS);
$fields['firstname'] = array('type' => PARAM_NOTAGS);
$fields['lastname'] = array('type' => PARAM_NOTAGS);
$fields['surname'] = array('type' => PARAM_NOTAGS);
$fields['email'] = array('type' => PARAM_RAW_TRIMMED);
$fields['emailstop'] = array('type' => PARAM_INT);
$fields['icq'] = array('type' => PARAM_NOTAGS);
$fields['skype'] = array('type' => PARAM_NOTAGS);
$fields['aim'] = array('type' => PARAM_NOTAGS);
$fields['yahoo'] = array('type' => PARAM_NOTAGS);
$fields['msn'] = array('type' => PARAM_NOTAGS);
$fields['phone1'] = array('type' => PARAM_NOTAGS);
$fields['phone2'] = array('type' => PARAM_NOTAGS);
$fields['institution'] = array('type' => PARAM_TEXT);
$fields['department'] = array('type' => PARAM_TEXT);
$fields['address'] = array('type' => PARAM_TEXT);
$fields['city'] = array('type' => PARAM_TEXT);
$fields['country'] = array('type' => PARAM_TEXT);
$fields['lang'] = array('type' => PARAM_TEXT);
$fields['calendartype'] = array('type' => PARAM_NOTAGS);
$fields['theme'] = array('type' => PARAM_NOTAGS);
$fields['timezones'] = array('type' => PARAM_TEXT);
$fields['firstaccess'] = array('type' => PARAM_INT);
$fields['lastaccess'] = array('type' => PARAM_INT);
$fields['lastlogin'] = array('type' => PARAM_INT);
$fields['currentlogin'] = array('type' => PARAM_INT);
$fields['lastip'] = array('type' => PARAM_NOTAGS);
$fields['secret'] = array('type' => PARAM_TEXT);
$fields['picture'] = array('type' => PARAM_INT);
$fields['url'] = array('type' => PARAM_URL);
$fields['description'] = array('type' => PARAM_CLEANHTML);
$fields['descriptionformat'] = array('type' => PARAM_INT);
$fields['mailformat'] = array('type' => PARAM_INT);
$fields['maildigest'] = array('type' => PARAM_INT);
$fields['maildisplay'] = array('type' => PARAM_INT);
$fields['autosubscribe'] = array('type' => PARAM_INT);
$fields['trackforums'] = array('type' => PARAM_INT);
$fields['timecreated'] = array('type' => PARAM_INT);
$fields['timemodified'] = array('type' => PARAM_INT);
$fields['trustbitmask'] = array('type' => PARAM_INT);
$fields['imagealt'] = array('type' => PARAM_TEXT);
$fields['lastnamephonetic'] = array('type' => PARAM_NOTAGS);
$fields['firstnamephonetic'] = array('type' => PARAM_NOTAGS);
$fields['middlename'] = array('type' => PARAM_NOTAGS);
$fields['alternatename'] = array('type' => PARAM_NOTAGS);
$fields['id'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
$fields['auth'] = array('type' => PARAM_AUTH, 'null' => NULL_NOT_ALLOWED);
$fields['confirmed'] = array('type' => PARAM_BOOL, 'null' => NULL_NOT_ALLOWED);
$fields['policyagreed'] = array('type' => PARAM_BOOL, 'null' => NULL_NOT_ALLOWED);
$fields['deleted'] = array('type' => PARAM_BOOL, 'null' => NULL_NOT_ALLOWED);
$fields['suspended'] = array('type' => PARAM_BOOL, 'null' => NULL_NOT_ALLOWED);
$fields['mnethostid'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
$fields['username'] = array('type' => PARAM_USERNAME, 'null' => NULL_NOT_ALLOWED);
$fields['password'] = array('type' => PARAM_RAW, 'null' => NULL_NOT_ALLOWED);
$fields['idnumber'] = array('type' => PARAM_RAW, 'null' => NULL_NOT_ALLOWED);
$fields['firstname'] = array('type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED);
$fields['lastname'] = array('type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED);
$fields['surname'] = array('type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED);
$fields['email'] = array('type' => PARAM_RAW_TRIMMED, 'null' => NULL_NOT_ALLOWED);
$fields['emailstop'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
$fields['icq'] = array('type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED);
$fields['skype'] = array('type' => PARAM_NOTAGS, 'null' => NULL_ALLOWED);
$fields['aim'] = array('type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED);
$fields['yahoo'] = array('type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED);
$fields['msn'] = array('type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED);
$fields['phone1'] = array('type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED);
$fields['phone2'] = array('type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED);
$fields['institution'] = array('type' => PARAM_TEXT, 'null' => NULL_NOT_ALLOWED);
$fields['department'] = array('type' => PARAM_TEXT, 'null' => NULL_NOT_ALLOWED);
$fields['address'] = array('type' => PARAM_TEXT, 'null' => NULL_NOT_ALLOWED);
$fields['city'] = array('type' => PARAM_TEXT, 'null' => NULL_NOT_ALLOWED, 'default' => $CFG->defaultcity);
$fields['country'] = array('type' => PARAM_ALPHA, 'null' => NULL_NOT_ALLOWED, 'default' => $CFG->country,
'choices' => array_merge(array('' => ''), get_string_manager()->get_list_of_countries(true, true)));
$fields['lang'] = array('type' => PARAM_LANG, 'null' => NULL_NOT_ALLOWED, 'default' => $CFG->lang,
'choices' => array_merge(array('' => ''), get_string_manager()->get_list_of_languages()));
$fields['calendartype'] = array('type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED, 'default' => $CFG->calendartype,
'choices' => array_merge(array('' => ''), \core_calendar\type_factory::get_list_of_calendar_types()));
$fields['theme'] = array('type' => PARAM_THEME, 'null' => NULL_NOT_ALLOWED,
'default' => theme_config::DEFAULT_THEME, 'choices' => array_merge(array('' => ''), get_list_of_themes()));
$fields['timezone'] = array('type' => PARAM_TIMEZONE, 'null' => NULL_NOT_ALLOWED, 'default' => $CFG->timezone,
'choices' => core_date::get_list_of_timezones(null, true));
$fields['firstaccess'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
$fields['lastaccess'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
$fields['lastlogin'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
$fields['currentlogin'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
$fields['lastip'] = array('type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED);
$fields['secret'] = array('type' => PARAM_RAW, 'null' => NULL_NOT_ALLOWED);
$fields['picture'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
$fields['url'] = array('type' => PARAM_URL, 'null' => NULL_NOT_ALLOWED);
$fields['description'] = array('type' => PARAM_RAW, 'null' => NULL_ALLOWED);
$fields['descriptionformat'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
$fields['mailformat'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED,
'default' => $CFG->defaultpreference_mailformat);
$fields['maildigest'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED,
'default' => $CFG->defaultpreference_maildigest);
$fields['maildisplay'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED,
'default' => $CFG->defaultpreference_maildisplay);
$fields['autosubscribe'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED,
'default' => $CFG->defaultpreference_autosubscribe);
$fields['trackforums'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED,
'default' => $CFG->defaultpreference_trackforums);
$fields['timecreated'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
$fields['timemodified'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
$fields['trustbitmask'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
$fields['imagealt'] = array('type' => PARAM_TEXT, 'null' => NULL_ALLOWED);
$fields['lastnamephonetic'] = array('type' => PARAM_NOTAGS, 'null' => NULL_ALLOWED);
$fields['firstnamephonetic'] = array('type' => PARAM_NOTAGS, 'null' => NULL_ALLOWED);
$fields['middlename'] = array('type' => PARAM_NOTAGS, 'null' => NULL_ALLOWED);
$fields['alternatename'] = array('type' => PARAM_NOTAGS, 'null' => NULL_ALLOWED);
self::$propertiescache = $fields;
}
@ -368,6 +389,38 @@ class core_user {
return self::$propertiescache[$property];
}
/**
* Validate user data.
*
* This method just validates each user field and return an array of errors. It doesn't clean the data,
* the methods clean() and clean_field() should be used for this purpose.
*
* @param stdClass|array $data user data object or array to be validated.
* @return array|true $errors array of errors found on the user object, true if the validation passed.
*/
public static function validate($data) {
// Get all user profile fields definition.
self::fill_properties_cache();
foreach ($data as $property => $value) {
try {
if (isset(self::$propertiescache[$property])) {
validate_param($value, self::$propertiescache[$property]['type'], self::$propertiescache[$property]['null']);
}
// Check that the value is part of a list of allowed values.
if (!empty(self::$propertiescache[$property]['choices']) &&
!isset(self::$propertiescache[$property]['choices'][$data->$property]) &&
!array_key_exists($data->$property, self::$propertiescache[$property]['choices'])) {
throw new invalid_parameter_exception($value);
}
} catch (invalid_parameter_exception $e) {
$errors[$property] = $e->getMessage();
}
}
return empty($errors) ? true : $errors;
}
/**
* Clean the properties cache.
*
@ -377,4 +430,149 @@ class core_user {
public static function reset_caches() {
self::$propertiescache = null;
}
/**
* Clean the user data.
*
* @param stdClass|array $user the user data to be validated against properties definition.
* @return stdClass $user the cleaned user data.
*/
public static function clean_data($user) {
if (empty($user)) {
return $user;
}
foreach ($user as $field => $value) {
// Get the property parameter type and do the cleaning.
try {
if (isset(self::$propertiescache[$field]['choices'])) {
if (!array_key_exists($value, self::$propertiescache[$field]['choices'])) {
if (isset(self::$propertiescache[$field]['default'])) {
$user->$field = self::$propertiescache[$field]['default'];
} else {
$user->$field = '';
}
}
} else {
$user->$field = core_user::clean_field($value, $field);
}
} catch (coding_exception $e) {
debugging("The property '$field' could not be cleaned.", DEBUG_DEVELOPER);
}
}
return $user;
}
/**
* Clean a specific user field.
*
* @param string $data the user field data to be cleaned.
* @param string $field the user field name on the property definition cache.
* @return string the cleaned user data.
*/
public static function clean_field($data, $field) {
if (empty($data) || empty($field)) {
return $data;
}
try {
$type = core_user::get_property_type($field);
if (isset(self::$propertiescache[$field]['choices'])) {
if (!array_key_exists($data, self::$propertiescache[$field]['choices'])) {
if (isset(self::$propertiescache[$field]['default'])) {
$data = self::$propertiescache[$field]['default'];
} else {
$data = '';
}
}
} else {
$data = clean_param($data, $type);
}
} catch (coding_exception $e) {
debugging("The property '$field' could not be cleaned.", DEBUG_DEVELOPER);
}
return $data;
}
/**
* Get the parameter type of the property.
*
* @param string $property property name to be retrieved.
* @throws coding_exception if the requested property name is invalid.
* @return int the property parameter type.
*/
public static function get_property_type($property) {
self::fill_properties_cache();
if (!array_key_exists($property, self::$propertiescache)) {
throw new coding_exception('Invalid property requested: ' . $property);
}
return self::$propertiescache[$property]['type'];
}
/**
* Discover if the property is NULL_ALLOWED or NULL_NOT_ALLOWED.
*
* @param string $property property name to be retrieved.
* @throws coding_exception if the requested property name is invalid.
* @return bool true if the property is NULL_ALLOWED, false otherwise.
*/
public static function get_property_null($property) {
self::fill_properties_cache();
if (!array_key_exists($property, self::$propertiescache)) {
throw new coding_exception('Invalid property requested: ' . $property);
}
return self::$propertiescache[$property]['null'];
}
/**
* Get the choices of the property.
*
* This is a helper method to validate a value against a list of acceptable choices.
* For instance: country, timezone, language, themes and etc.
*
* @param string $property property name to be retrieved.
* @throws coding_exception if the requested property name is invalid or if it does not has a list of choices.
* @return array the property parameter type.
*/
public static function get_property_choices($property) {
self::fill_properties_cache();
if (!array_key_exists($property, self::$propertiescache) && !array_key_exists('choices',
self::$propertiescache[$property])) {
throw new coding_exception('Invalid property requested, or the property does not has a list of choices.');
}
return self::$propertiescache[$property]['choices'];
}
/**
* Get the property default.
*
* This method gets the default value of a field (if exists).
*
* @param string $property property name to be retrieved.
* @throws coding_exception if the requested property name is invalid or if it does not has a default value.
* @return string the property default value.
*/
public static function get_property_default($property) {
self::fill_properties_cache();
if (!array_key_exists($property, self::$propertiescache) || !isset(self::$propertiescache[$property]['default'])) {
throw new coding_exception('Invalid property requested, or the property does not has a default value.');
}
return self::$propertiescache[$property]['default'];
}
}

View File

@ -1999,5 +1999,11 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2016041500.66);
}
if ($oldversion < 2016042100.00) {
// Update all countries to upper case.
$DB->execute("UPDATE {user} SET country = UPPER(country)");
// Main savepoint reached.
upgrade_main_savepoint(true, 2016042100.00);
}
return true;
}

View File

@ -64,8 +64,8 @@ class core_test_generator_testcase extends advanced_testcase {
$this->setCurrentTimeStart();
$user = $generator->create_user();
$this->assertEquals($count + 1, $DB->count_records('user'));
$this->assertSame($user->username, clean_param($user->username, PARAM_USERNAME));
$this->assertSame($user->email, clean_param($user->email, PARAM_EMAIL));
$this->assertSame($user->username, core_user::clean_field($user->username, 'username'));
$this->assertSame($user->email, core_user::clean_field($user->email, 'email'));
$this->assertSame(AUTH_PASSWORD_NOT_CACHED, $user->password);
$this->assertNotEmpty($user->firstnamephonetic);
$this->assertNotEmpty($user->lastnamephonetic);

View File

@ -71,7 +71,7 @@ class core_datalib_testcase extends advanced_testcase {
'descriptionformat' => FORMAT_MOODLE,
'city' => 'Perth',
'url' => 'http://moodle.org',
'country' => 'au'
'country' => 'AU'
);
$user1 = self::getDataGenerator()->create_user($user1);
$user2 = array(
@ -94,7 +94,7 @@ class core_datalib_testcase extends advanced_testcase {
'descriptionformat' => FORMAT_MOODLE,
'city' => 'Perth',
'url' => 'http://moodle.org',
'country' => 'au'
'country' => 'AU'
);
$user2 = self::getDataGenerator()->create_user($user2);

View File

@ -209,4 +209,207 @@ class core_user_testcase extends advanced_testcase {
$this->assertRegExp('/Invalid property requested./', $e->getMessage());
}
}
/**
* Test validate() method.
*/
public function test_validate() {
// Create user with just with username and firstname.
$record = array('username' => 's10', 'firstname' => 'Bebe Stevens');
$validation = core_user::validate((object)$record);
// Validate the user, should return true as the user data is correct.
$this->assertTrue($validation);
// Create user with incorrect data (invalid country and theme).
$record = array('username' => 's1', 'firstname' => 'Eric Cartman', 'country' => 'UU', 'theme' => 'beise');
// Should return an array with 2 errors.
$validation = core_user::validate((object)$record);
$this->assertArrayHasKey('country', $validation);
$this->assertArrayHasKey('theme', $validation);
$this->assertCount(2, $validation);
// Create user with malicious data (xss).
$record = array('username' => 's3', 'firstname' => 'Kyle<script>alert(1);<script> Broflovski');
// Should return an array with 1 error.
$validation = core_user::validate((object)$record);
$this->assertCount(1, $validation);
$this->assertArrayHasKey('firstname', $validation);
}
/**
* Test clean_data() method.
*/
public function test_clean_data() {
$this->resetAfterTest(false);
$user = new stdClass();
$user->firstname = 'John <script>alert(1)</script> Doe';
$user->username = 'john%#&~%*_doe';
$user->email = ' john@testing.com ';
$user->deleted = 'no';
$user->description = '<b>A description <script>alert(123);</script>about myself.</b>';
$usercleaned = core_user::clean_data($user);
// Expected results.
$this->assertEquals('John alert(1) Doe', $usercleaned->firstname);
$this->assertEquals('john@testing.com', $usercleaned->email);
$this->assertEquals(0, $usercleaned->deleted);
$this->assertEquals('<b>A description <script>alert(123);</script>about myself.</b>', $user->description);
$this->assertEquals('john_doe', $user->username);
// Try to clean an invalid property (userfullname).
$user->userfullname = 'John Doe';
core_user::clean_data($user);
$this->assertDebuggingCalled("The property 'userfullname' could not be cleaned.");
}
/**
* Test clean_field() method.
*/
public function test_clean_field() {
// Create a 'malicious' user object/
$user = new stdClass();
$user->firstname = 'John <script>alert(1)</script> Doe';
$user->username = 'john%#&~%*_doe';
$user->email = ' john@testing.com ';
$user->deleted = 'no';
$user->description = '<b>A description <script>alert(123);</script>about myself.</b>';
$user->userfullname = 'John Doe';
// Expected results.
$this->assertEquals('John alert(1) Doe', core_user::clean_field($user->firstname, 'firstname'));
$this->assertEquals('john_doe', core_user::clean_field($user->username, 'username'));
$this->assertEquals('john@testing.com', core_user::clean_field($user->email, 'email'));
$this->assertEquals(0, core_user::clean_field($user->deleted, 'deleted'));
$this->assertEquals('<b>A description <script>alert(123);</script>about myself.</b>', core_user::clean_field($user->description, 'description'));
// Try to clean an invalid property (fullname).
core_user::clean_field($user->userfullname, 'fullname');
$this->assertDebuggingCalled("The property 'fullname' could not be cleaned.");
}
/**
* Test get_property_type() method.
*/
public function test_get_property_type() {
// Fetch valid properties and verify if the type is correct.
$type = core_user::get_property_type('username');
$this->assertEquals(PARAM_USERNAME, $type);
$type = core_user::get_property_type('email');
$this->assertEquals(PARAM_RAW_TRIMMED, $type);
$type = core_user::get_property_type('timezone');
$this->assertEquals(PARAM_TIMEZONE, $type);
// Try to fetch type of a non-existent properties.
$nonexistingproperty = 'userfullname';
$this->setExpectedException('coding_exception', 'Invalid property requested: ' . $nonexistingproperty);
core_user::get_property_type($nonexistingproperty);
$nonexistingproperty = 'mobilenumber';
$this->setExpectedException('coding_exception', 'Invalid property requested: ' . $nonexistingproperty);
core_user::get_property_type($nonexistingproperty);
}
/**
* Test get_property_null() method.
*/
public function test_get_property_null() {
// Fetch valid properties and verify if it is NULL_ALLOWED or NULL_NOT_ALLOWED.
$property = core_user::get_property_null('username');
$this->assertEquals(NULL_NOT_ALLOWED, $property);
$property = core_user::get_property_null('password');
$this->assertEquals(NULL_NOT_ALLOWED, $property);
$property = core_user::get_property_null('imagealt');
$this->assertEquals(NULL_ALLOWED, $property);
$property = core_user::get_property_null('middlename');
$this->assertEquals(NULL_ALLOWED, $property);
// Try to fetch type of a non-existent properties.
$nonexistingproperty = 'lastnamefonetic';
$this->setExpectedException('coding_exception', 'Invalid property requested: ' . $nonexistingproperty);
core_user::get_property_null($nonexistingproperty);
$nonexistingproperty = 'midlename';
$this->setExpectedException('coding_exception', 'Invalid property requested: ' . $nonexistingproperty);
core_user::get_property_null($nonexistingproperty);
}
/**
* Test get_property_choices() method.
*/
public function test_get_property_choices() {
// Test against country property choices.
$choices = core_user::get_property_choices('country');
$this->assertArrayHasKey('AU', $choices);
$this->assertArrayHasKey('BR', $choices);
$this->assertArrayNotHasKey('WW', $choices);
$this->assertArrayNotHasKey('TX', $choices);
// Test against lang property choices.
$choices = core_user::get_property_choices('lang');
$this->assertArrayHasKey('en', $choices);
$this->assertArrayHasKey('ko', $choices);
$this->assertArrayHasKey('ru', $choices);
$this->assertArrayNotHasKey('ww', $choices);
$this->assertArrayNotHasKey('yy', $choices);
// Test against theme property choices.
$choices = core_user::get_property_choices('theme');
$this->assertArrayHasKey('base', $choices);
$this->assertArrayHasKey('clean', $choices);
$this->assertArrayNotHasKey('unknowntheme', $choices);
$this->assertArrayNotHasKey('wrongtheme', $choices);
// Test against timezone property choices.
$choices = core_user::get_property_choices('timezone');
$this->assertArrayHasKey('America/Sao_Paulo', $choices);
$this->assertArrayHasKey('Australia/Perth', $choices);
$this->assertArrayHasKey('99', $choices);
$this->assertArrayHasKey('UTC', $choices);
$this->assertArrayNotHasKey('North Korea', $choices);
$this->assertArrayNotHasKey('New york', $choices);
// Try to fetch type of a non-existent properties.
$nonexistingproperty = 'language';
$this->setExpectedException('coding_exception', 'Invalid property requested: ' . $nonexistingproperty);
core_user::get_property_null($nonexistingproperty);
$nonexistingproperty = 'coutries';
$this->setExpectedException('coding_exception', 'Invalid property requested: ' . $nonexistingproperty);
core_user::get_property_null($nonexistingproperty);
}
/**
* Test get_property_default().
*/
public function test_get_property_default() {
global $CFG;
$country = core_user::get_property_default('country');
$this->assertEquals($CFG->country, $country);
set_config('country', 'AU');
core_user::reset_caches();
$country = core_user::get_property_default('country');
$this->assertEquals($CFG->country, $country);
$lang = core_user::get_property_default('lang');
$this->assertEquals($CFG->lang, $lang);
set_config('lang', 'en');
$lang = core_user::get_property_default('lang');
$this->assertEquals($CFG->lang, $lang);
$timezone = core_user::get_property_default('timezone');
$this->assertEquals($CFG->timezone, $timezone);
set_config('timezone', 99);
core_user::reset_caches();
$timezone = core_user::get_property_default('timezone');
$this->assertEquals(99, $timezone);
$this->setExpectedException('coding_exception', 'Invalid property requested, or the property does not has a default value.');
core_user::get_property_default('firstname');
}
}

View File

@ -137,7 +137,7 @@ if ($frm and isset($frm->username)) { // Login WITH
$frm->username = trim(core_text::strtolower($frm->username));
if (is_enabled_auth('none') ) {
if ($frm->username !== clean_param($frm->username, PARAM_USERNAME)) {
if ($frm->username !== core_user::clean_field($frm->username, 'username')) {
$errormsg = get_string('username').': '.get_string("invalidusername");
$errorcode = 2;
$user = null;
@ -312,6 +312,7 @@ if (!isset($frm) or !is_object($frm)) {
if (empty($frm->username) && $authsequence[0] != 'shibboleth') { // See bug 5184
if (!empty($_GET["username"])) {
// we do not want data from _POST here
$frm->username = clean_param($_GET["username"], PARAM_RAW); // we do not want data from _POST here
} else {
$frm->username = get_moodle_cookie();

View File

@ -40,30 +40,30 @@ class login_signup_form extends moodleform {
$mform->addElement('text', 'username', get_string('username'), 'maxlength="100" size="12"');
$mform->setType('username', PARAM_NOTAGS);
$mform->setType('username', core_user::get_property_type('username'));
$mform->addRule('username', get_string('missingusername'), 'required', null, 'client');
if (!empty($CFG->passwordpolicy)){
$mform->addElement('static', 'passwordpolicyinfo', '', print_password_policy());
}
$mform->addElement('passwordunmask', 'password', get_string('password'), 'maxlength="32" size="12"');
$mform->setType('password', PARAM_RAW);
$mform->setType('password', core_user::get_property_type('password'));
$mform->addRule('password', get_string('missingpassword'), 'required', null, 'client');
$mform->addElement('header', 'supplyinfo', get_string('supplyinfo'),'');
$mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="25"');
$mform->setType('email', PARAM_RAW_TRIMMED);
$mform->setType('email', core_user::get_property_type('email'));
$mform->addRule('email', get_string('missingemail'), 'required', null, 'client');
$mform->addElement('text', 'email2', get_string('emailagain'), 'maxlength="100" size="25"');
$mform->setType('email2', PARAM_RAW_TRIMMED);
$mform->setType('email2', core_user::get_property_type('email'));
$mform->addRule('email2', get_string('missingemail'), 'required', null, 'client');
$namefields = useredit_get_required_name_fields();
foreach ($namefields as $field) {
$mform->addElement('text', $field, get_string($field), 'maxlength="100" size="30"');
$mform->setType($field, PARAM_NOTAGS);
$mform->setType($field, core_user::get_property_type('firstname'));
$stringid = 'missing' . $field;
if (!get_string_manager()->string_exists($stringid, 'moodle')) {
$stringid = 'required';
@ -72,7 +72,7 @@ class login_signup_form extends moodleform {
}
$mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="20"');
$mform->setType('city', PARAM_TEXT);
$mform->setType('city', core_user::get_property_type('city'));
if (!empty($CFG->defaultcity)) {
$mform->setDefault('city', $CFG->defaultcity);
}
@ -132,7 +132,7 @@ class login_signup_form extends moodleform {
if ($data['username'] !== core_text::strtolower($data['username'])) {
$errors['username'] = get_string('usernamelowercase');
} else {
if ($data['username'] !== clean_param($data['username'], PARAM_USERNAME)) {
if ($data['username'] !== core_user::clean_field($data['username'], 'username')) {
$errors['username'] = get_string('invalidusername');
}

View File

@ -60,7 +60,7 @@ class user_editadvanced_form extends moodleform {
// Add some extra hidden fields.
$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);
$mform->setType('id', core_user::get_property_type('id'));
$mform->addElement('hidden', 'course', $COURSE->id);
$mform->setType('course', PARAM_INT);
@ -69,7 +69,7 @@ class user_editadvanced_form extends moodleform {
$mform->addElement('text', 'username', get_string('username'), 'size="20"');
$mform->addRule('username', $strrequired, 'required', null, 'client');
$mform->setType('username', PARAM_RAW);
$mform->setType('username', core_user::get_property_type('username'));
$auths = core_component::get_plugin_list('auth');
$enabled = get_string('pluginenabled', 'core_plugin');
@ -107,7 +107,7 @@ class user_editadvanced_form extends moodleform {
}
$mform->addElement('passwordunmask', 'newpassword', get_string('newpassword'), 'size="20"');
$mform->addHelpButton('newpassword', 'newpassword');
$mform->setType('newpassword', PARAM_RAW);
$mform->setType('newpassword', core_user::get_property_type('password'));
$mform->disabledIf('newpassword', 'createpassword', 'checked');
$mform->disabledIf('newpassword', 'auth', 'in', $cannotchangepass);
@ -259,7 +259,7 @@ class user_editadvanced_form extends moodleform {
if ($usernew->username !== core_text::strtolower($usernew->username)) {
$err['username'] = get_string('usernamelowercase');
} else {
if ($usernew->username !== clean_param($usernew->username, PARAM_USERNAME)) {
if ($usernew->username !== core_user::clean_field($usernew->username, 'username')) {
$err['username'] = get_string('invalidusername');
}
}

View File

@ -322,7 +322,7 @@ function useredit_shared_definition(&$mform, $editoroptions, $filemanageroptions
$choices['1'] = get_string('emaildisplayyes');
$choices['2'] = get_string('emaildisplaycourse');
$mform->addElement('select', 'maildisplay', get_string('emaildisplay'), $choices);
$mform->setDefault('maildisplay', $CFG->defaultpreference_maildisplay);
$mform->setDefault('maildisplay', core_user::get_property_default('maildisplay'));
$mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="21"');
$mform->setType('city', PARAM_TEXT);
@ -334,14 +334,14 @@ function useredit_shared_definition(&$mform, $editoroptions, $filemanageroptions
$choices = array('' => get_string('selectacountry') . '...') + $choices;
$mform->addElement('select', 'country', get_string('selectacountry'), $choices);
if (!empty($CFG->country)) {
$mform->setDefault('country', $CFG->country);
$mform->setDefault('country', core_user::get_property_default('country'));
}
if (isset($CFG->forcetimezone) and $CFG->forcetimezone != 99) {
$choices = core_date::get_list_of_timezones($CFG->forcetimezone);
$mform->addElement('static', 'forcedtimezone', get_string('timezone'), $choices[$CFG->forcetimezone]);
$mform->addElement('hidden', 'timezone');
$mform->setType('timezone', PARAM_TIMEZONE);
$mform->setType('timezone', core_user::get_property_type('timezone'));
} else {
$choices = core_date::get_list_of_timezones($user->timezone, true);
$mform->addElement('select', 'timezone', get_string('timezone'), $choices);
@ -413,40 +413,40 @@ function useredit_shared_definition(&$mform, $editoroptions, $filemanageroptions
$mform->addElement('header', 'moodle_optional', get_string('optional', 'form'));
$mform->addElement('text', 'url', get_string('webpage'), 'maxlength="255" size="50"');
$mform->setType('url', PARAM_URL);
$mform->setType('url', core_user::get_property_type('url'));
$mform->addElement('text', 'icq', get_string('icqnumber'), 'maxlength="15" size="25"');
$mform->setType('icq', PARAM_NOTAGS);
$mform->setType('icq', core_user::get_property_type('icq'));
$mform->addElement('text', 'skype', get_string('skypeid'), 'maxlength="50" size="25"');
$mform->setType('skype', PARAM_NOTAGS);
$mform->setType('skype', core_user::get_property_type('skype'));
$mform->addElement('text', 'aim', get_string('aimid'), 'maxlength="50" size="25"');
$mform->setType('aim', PARAM_NOTAGS);
$mform->setType('aim', core_user::get_property_type('aim'));
$mform->addElement('text', 'yahoo', get_string('yahooid'), 'maxlength="50" size="25"');
$mform->setType('yahoo', PARAM_NOTAGS);
$mform->setType('yahoo', core_user::get_property_type('yahoo'));
$mform->addElement('text', 'msn', get_string('msnid'), 'maxlength="50" size="25"');
$mform->setType('msn', PARAM_NOTAGS);
$mform->setType('msn', core_user::get_property_type('msn'));
$mform->addElement('text', 'idnumber', get_string('idnumber'), 'maxlength="255" size="25"');
$mform->setType('idnumber', PARAM_NOTAGS);
$mform->setType('idnumber', core_user::get_property_type('idnumber'));
$mform->addElement('text', 'institution', get_string('institution'), 'maxlength="255" size="25"');
$mform->setType('institution', PARAM_TEXT);
$mform->setType('institution', core_user::get_property_type('institution'));
$mform->addElement('text', 'department', get_string('department'), 'maxlength="255" size="25"');
$mform->setType('department', PARAM_TEXT);
$mform->setType('department', core_user::get_property_type('department'));
$mform->addElement('text', 'phone1', get_string('phone1'), 'maxlength="20" size="25"');
$mform->setType('phone1', PARAM_NOTAGS);
$mform->setType('phone1', core_user::get_property_type('phone1'));
$mform->addElement('text', 'phone2', get_string('phone2'), 'maxlength="20" size="25"');
$mform->setType('phone2', PARAM_NOTAGS);
$mform->setType('phone2', core_user::get_property_type('phone2'));
$mform->addElement('text', 'address', get_string('address'), 'maxlength="255" size="25"');
$mform->setType('address', PARAM_TEXT);
$mform->setType('address', core_user::get_property_type('address'));
}
/**

View File

@ -51,53 +51,53 @@ class core_user_external extends external_api {
new external_single_structure(
array(
'username' =>
new external_value(PARAM_USERNAME, 'Username policy is defined in Moodle security config.'),
new external_value(core_user::get_property_type('username'), 'Username policy is defined in Moodle security config.'),
'password' =>
new external_value(PARAM_RAW, 'Plain text password consisting of any characters', VALUE_OPTIONAL),
new external_value(core_user::get_property_type('password'), 'Plain text password consisting of any characters', VALUE_OPTIONAL),
'createpassword' =>
new external_value(PARAM_BOOL, 'True if password should be created and mailed to user.',
VALUE_OPTIONAL),
'firstname' =>
new external_value(PARAM_NOTAGS, 'The first name(s) of the user'),
new external_value(core_user::get_property_type('firstname'), 'The first name(s) of the user'),
'lastname' =>
new external_value(PARAM_NOTAGS, 'The family name of the user'),
new external_value(core_user::get_property_type('lastname'), 'The family name of the user'),
'email' =>
new external_value(PARAM_EMAIL, 'A valid and unique email address'),
new external_value(core_user::get_property_type('email'), 'A valid and unique email address'),
'auth' =>
new external_value(PARAM_PLUGIN, 'Auth plugins include manual, ldap, imap, etc', VALUE_DEFAULT,
'manual', NULL_NOT_ALLOWED),
new external_value(core_user::get_property_type('auth'), 'Auth plugins include manual, ldap, imap, etc', VALUE_DEFAULT,
'manual', core_user::get_property_null('auth')),
'idnumber' =>
new external_value(PARAM_RAW, 'An arbitrary ID code number perhaps from the institution',
new external_value(core_user::get_property_type('idnumber'), 'An arbitrary ID code number perhaps from the institution',
VALUE_DEFAULT, ''),
'lang' =>
new external_value(PARAM_SAFEDIR, 'Language code such as "en", must exist on server', VALUE_DEFAULT,
$CFG->lang, NULL_NOT_ALLOWED),
new external_value(core_user::get_property_type('lang'), 'Language code such as "en", must exist on server', VALUE_DEFAULT,
core_user::get_property_default('lang'), core_user::get_property_null('lang')),
'calendartype' =>
new external_value(PARAM_PLUGIN, 'Calendar type such as "gregorian", must exist on server',
new external_value(core_user::get_property_type('calendartype'), 'Calendar type such as "gregorian", must exist on server',
VALUE_DEFAULT, $CFG->calendartype, VALUE_OPTIONAL),
'theme' =>
new external_value(PARAM_PLUGIN, 'Theme name such as "standard", must exist on server',
new external_value(core_user::get_property_type('theme'), 'Theme name such as "standard", must exist on server',
VALUE_OPTIONAL),
'timezone' =>
new external_value(PARAM_TIMEZONE, 'Timezone code such as Australia/Perth, or 99 for default',
new external_value(core_user::get_property_type('timezone'), 'Timezone code such as Australia/Perth, or 99 for default',
VALUE_OPTIONAL),
'mailformat' =>
new external_value(PARAM_INT, 'Mail format code is 0 for plain text, 1 for HTML etc',
new external_value(core_user::get_property_type('mailformat'), 'Mail format code is 0 for plain text, 1 for HTML etc',
VALUE_OPTIONAL),
'description' =>
new external_value(PARAM_TEXT, 'User profile description, no HTML', VALUE_OPTIONAL),
new external_value(core_user::get_property_type('description'), 'User profile description, no HTML', VALUE_OPTIONAL),
'city' =>
new external_value(PARAM_NOTAGS, 'Home city of the user', VALUE_OPTIONAL),
new external_value(core_user::get_property_type('city'), 'Home city of the user', VALUE_OPTIONAL),
'country' =>
new external_value(PARAM_ALPHA, 'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL),
new external_value(core_user::get_property_type('country'), 'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL),
'firstnamephonetic' =>
new external_value(PARAM_NOTAGS, 'The first name(s) phonetically of the user', VALUE_OPTIONAL),
new external_value(core_user::get_property_type('firstnamephonetic'), 'The first name(s) phonetically of the user', VALUE_OPTIONAL),
'lastnamephonetic' =>
new external_value(PARAM_NOTAGS, 'The family name phonetically of the user', VALUE_OPTIONAL),
new external_value(core_user::get_property_type('lastnamephonetic'), 'The family name phonetically of the user', VALUE_OPTIONAL),
'middlename' =>
new external_value(PARAM_NOTAGS, 'The middle name of the user', VALUE_OPTIONAL),
new external_value(core_user::get_property_type('middlename'), 'The middle name of the user', VALUE_OPTIONAL),
'alternatename' =>
new external_value(PARAM_NOTAGS, 'The alternate name of the user', VALUE_OPTIONAL),
new external_value(core_user::get_property_type('alternatename'), 'The alternate name of the user', VALUE_OPTIONAL),
'preferences' => new external_multiple_structure(
new external_single_structure(
array(
@ -252,8 +252,8 @@ class core_user_external extends external_api {
return new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'user id'),
'username' => new external_value(PARAM_USERNAME, 'user name'),
'id' => new external_value(core_user::get_property_type('id'), 'user id'),
'username' => new external_value(core_user::get_property_type('username'), 'user name'),
)
)
);
@ -269,7 +269,7 @@ class core_user_external extends external_api {
public static function delete_users_parameters() {
return new external_function_parameters(
array(
'userids' => new external_multiple_structure(new external_value(PARAM_INT, 'user ID')),
'userids' => new external_multiple_structure(new external_value(core_user::get_property_type('id'), 'user ID')),
)
);
}
@ -336,56 +336,56 @@ class core_user_external extends external_api {
new external_single_structure(
array(
'id' =>
new external_value(PARAM_INT, 'ID of the user'),
new external_value(core_user::get_property_type('id'), 'ID of the user'),
'username' =>
new external_value(PARAM_USERNAME, 'Username policy is defined in Moodle security config.',
new external_value(core_user::get_property_type('username'), 'Username policy is defined in Moodle security config.',
VALUE_OPTIONAL, '', NULL_NOT_ALLOWED),
'password' =>
new external_value(PARAM_RAW, 'Plain text password consisting of any characters', VALUE_OPTIONAL,
new external_value(core_user::get_property_type('password'), 'Plain text password consisting of any characters', VALUE_OPTIONAL,
'', NULL_NOT_ALLOWED),
'firstname' =>
new external_value(PARAM_NOTAGS, 'The first name(s) of the user', VALUE_OPTIONAL, '',
new external_value(core_user::get_property_type('firstname'), 'The first name(s) of the user', VALUE_OPTIONAL, '',
NULL_NOT_ALLOWED),
'lastname' =>
new external_value(PARAM_NOTAGS, 'The family name of the user', VALUE_OPTIONAL),
new external_value(core_user::get_property_type('lastname'), 'The family name of the user', VALUE_OPTIONAL),
'email' =>
new external_value(PARAM_EMAIL, 'A valid and unique email address', VALUE_OPTIONAL, '',
new external_value(core_user::get_property_type('email'), 'A valid and unique email address', VALUE_OPTIONAL, '',
NULL_NOT_ALLOWED),
'auth' =>
new external_value(PARAM_PLUGIN, 'Auth plugins include manual, ldap, imap, etc', VALUE_OPTIONAL, '',
new external_value(core_user::get_property_type('auth'), 'Auth plugins include manual, ldap, imap, etc', VALUE_OPTIONAL, '',
NULL_NOT_ALLOWED),
'idnumber' =>
new external_value(PARAM_RAW, 'An arbitrary ID code number perhaps from the institution',
new external_value(core_user::get_property_type('idnumber'), 'An arbitrary ID code number perhaps from the institution',
VALUE_OPTIONAL),
'lang' =>
new external_value(PARAM_SAFEDIR, 'Language code such as "en", must exist on server',
new external_value(core_user::get_property_type('lang'), 'Language code such as "en", must exist on server',
VALUE_OPTIONAL, '', NULL_NOT_ALLOWED),
'calendartype' =>
new external_value(PARAM_PLUGIN, 'Calendar type such as "gregorian", must exist on server',
new external_value(core_user::get_property_type('calendartype'), 'Calendar type such as "gregorian", must exist on server',
VALUE_OPTIONAL, '', NULL_NOT_ALLOWED),
'theme' =>
new external_value(PARAM_PLUGIN, 'Theme name such as "standard", must exist on server',
new external_value(core_user::get_property_type('theme'), 'Theme name such as "standard", must exist on server',
VALUE_OPTIONAL),
'timezone' =>
new external_value(PARAM_TIMEZONE, 'Timezone code such as Australia/Perth, or 99 for default',
new external_value(core_user::get_property_type('timezone'), 'Timezone code such as Australia/Perth, or 99 for default',
VALUE_OPTIONAL),
'mailformat' =>
new external_value(PARAM_INT, 'Mail format code is 0 for plain text, 1 for HTML etc',
new external_value(core_user::get_property_type('mailformat'), 'Mail format code is 0 for plain text, 1 for HTML etc',
VALUE_OPTIONAL),
'description' =>
new external_value(PARAM_TEXT, 'User profile description, no HTML', VALUE_OPTIONAL),
new external_value(core_user::get_property_type('description'), 'User profile description, no HTML', VALUE_OPTIONAL),
'city' =>
new external_value(PARAM_NOTAGS, 'Home city of the user', VALUE_OPTIONAL),
new external_value(core_user::get_property_type('city'), 'Home city of the user', VALUE_OPTIONAL),
'country' =>
new external_value(PARAM_ALPHA, 'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL),
new external_value(core_user::get_property_type('country'), 'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL),
'firstnamephonetic' =>
new external_value(PARAM_NOTAGS, 'The first name(s) phonetically of the user', VALUE_OPTIONAL),
new external_value(core_user::get_property_type('firstnamephonetic'), 'The first name(s) phonetically of the user', VALUE_OPTIONAL),
'lastnamephonetic' =>
new external_value(PARAM_NOTAGS, 'The family name phonetically of the user', VALUE_OPTIONAL),
new external_value(core_user::get_property_type('lastnamephonetic'), 'The family name phonetically of the user', VALUE_OPTIONAL),
'middlename' =>
new external_value(PARAM_NOTAGS, 'The middle name of the user', VALUE_OPTIONAL),
new external_value(core_user::get_property_type('middlename'), 'The middle name of the user', VALUE_OPTIONAL),
'alternatename' =>
new external_value(PARAM_NOTAGS, 'The alternate name of the user', VALUE_OPTIONAL),
new external_value(core_user::get_property_type('alternatename'), 'The alternate name of the user', VALUE_OPTIONAL),
'customfields' => new external_multiple_structure(
new external_single_structure(
array(
@ -507,16 +507,16 @@ class core_user_external extends external_api {
switch ($field) {
case 'id':
$paramtype = PARAM_INT;
$paramtype = core_user::get_property_type('id');
break;
case 'idnumber':
$paramtype = PARAM_RAW;
$paramtype = core_user::get_property_type('idnumber');
break;
case 'username':
$paramtype = PARAM_RAW;
$paramtype = core_user::get_property_type('username');
break;
case 'email':
$paramtype = PARAM_EMAIL;
$paramtype = core_user::get_property_type('email');
break;
default:
throw new coding_exception('invalid field parameter',
@ -638,24 +638,24 @@ class core_user_external extends external_api {
$paramtype = PARAM_RAW;
switch ($criteria['key']) {
case 'id':
$paramtype = PARAM_INT;
$paramtype = core_user::get_property_type('id');
break;
case 'idnumber':
$paramtype = PARAM_RAW;
$paramtype = core_user::get_property_type('idnumber');
break;
case 'username':
$paramtype = PARAM_RAW;
$paramtype = core_user::get_property_type('username');
break;
case 'email':
// We use PARAM_RAW to allow searches with %.
$paramtype = PARAM_RAW;
$paramtype = core_user::get_property_type('email');
break;
case 'auth':
$paramtype = PARAM_AUTH;
$paramtype = core_user::get_property_type('auth');
break;
case 'lastname':
case 'firstname':
$paramtype = PARAM_TEXT;
$paramtype = core_user::get_property_type('firstname');
break;
default:
// Send back a warning that this search key is not supported in this version.
@ -752,7 +752,9 @@ class core_user_external extends external_api {
public static function get_users_by_id_parameters() {
return new external_function_parameters(
array(
'userids' => new external_multiple_structure(new external_value(PARAM_INT, 'user ID')),
'userids' => new external_multiple_structure(
new external_value(core_user::get_property_type('id'), 'user ID')
),
)
);
}
@ -857,7 +859,7 @@ class core_user_external extends external_api {
'userlist' => new external_multiple_structure(
new external_single_structure(
array(
'userid' => new external_value(PARAM_INT, 'userid'),
'userid' => new external_value(core_user::get_property_type('id'), 'userid'),
'courseid' => new external_value(PARAM_INT, 'courseid'),
)
)
@ -977,38 +979,38 @@ class core_user_external extends external_api {
*/
public static function user_description($additionalfields = array()) {
$userfields = array(
'id' => new external_value(PARAM_INT, 'ID of the user'),
'username' => new external_value(PARAM_RAW, 'The username', VALUE_OPTIONAL),
'firstname' => new external_value(PARAM_NOTAGS, 'The first name(s) of the user', VALUE_OPTIONAL),
'lastname' => new external_value(PARAM_NOTAGS, 'The family name of the user', VALUE_OPTIONAL),
'fullname' => new external_value(PARAM_NOTAGS, 'The fullname of the user'),
'email' => new external_value(PARAM_TEXT, 'An email address - allow email as root@localhost', VALUE_OPTIONAL),
'address' => new external_value(PARAM_TEXT, 'Postal address', VALUE_OPTIONAL),
'phone1' => new external_value(PARAM_NOTAGS, 'Phone 1', VALUE_OPTIONAL),
'phone2' => new external_value(PARAM_NOTAGS, 'Phone 2', VALUE_OPTIONAL),
'icq' => new external_value(PARAM_NOTAGS, 'icq number', VALUE_OPTIONAL),
'skype' => new external_value(PARAM_NOTAGS, 'skype id', VALUE_OPTIONAL),
'yahoo' => new external_value(PARAM_NOTAGS, 'yahoo id', VALUE_OPTIONAL),
'aim' => new external_value(PARAM_NOTAGS, 'aim id', VALUE_OPTIONAL),
'msn' => new external_value(PARAM_NOTAGS, 'msn number', VALUE_OPTIONAL),
'department' => new external_value(PARAM_TEXT, 'department', VALUE_OPTIONAL),
'institution' => new external_value(PARAM_TEXT, 'institution', VALUE_OPTIONAL),
'idnumber' => new external_value(PARAM_RAW, 'An arbitrary ID code number perhaps from the institution', VALUE_OPTIONAL),
'id' => new external_value(core_user::get_property_type('id'), 'ID of the user'),
'username' => new external_value(core_user::get_property_type('username'), 'The username', VALUE_OPTIONAL),
'firstname' => new external_value(core_user::get_property_type('firstname'), 'The first name(s) of the user', VALUE_OPTIONAL),
'lastname' => new external_value(core_user::get_property_type('lastname'), 'The family name of the user', VALUE_OPTIONAL),
'fullname' => new external_value(core_user::get_property_type('firstname'), 'The fullname of the user'),
'email' => new external_value(core_user::get_property_type('email'), 'An email address - allow email as root@localhost', VALUE_OPTIONAL),
'address' => new external_value(core_user::get_property_type('address'), 'Postal address', VALUE_OPTIONAL),
'phone1' => new external_value(core_user::get_property_type('phone1'), 'Phone 1', VALUE_OPTIONAL),
'phone2' => new external_value(core_user::get_property_type('phone2'), 'Phone 2', VALUE_OPTIONAL),
'icq' => new external_value(core_user::get_property_type('icq'), 'icq number', VALUE_OPTIONAL),
'skype' => new external_value(core_user::get_property_type('skype'), 'skype id', VALUE_OPTIONAL),
'yahoo' => new external_value(core_user::get_property_type('yahoo'), 'yahoo id', VALUE_OPTIONAL),
'aim' => new external_value(core_user::get_property_type('aim'), 'aim id', VALUE_OPTIONAL),
'msn' => new external_value(core_user::get_property_type('msn'), 'msn number', VALUE_OPTIONAL),
'department' => new external_value(core_user::get_property_type('department'), 'department', VALUE_OPTIONAL),
'institution' => new external_value(core_user::get_property_type('institution'), 'institution', VALUE_OPTIONAL),
'idnumber' => new external_value(core_user::get_property_type('idnumber'), 'An arbitrary ID code number perhaps from the institution', VALUE_OPTIONAL),
'interests' => new external_value(PARAM_TEXT, 'user interests (separated by commas)', VALUE_OPTIONAL),
'firstaccess' => new external_value(PARAM_INT, 'first access to the site (0 if never)', VALUE_OPTIONAL),
'lastaccess' => new external_value(PARAM_INT, 'last access to the site (0 if never)', VALUE_OPTIONAL),
'auth' => new external_value(PARAM_PLUGIN, 'Auth plugins include manual, ldap, imap, etc', VALUE_OPTIONAL),
'confirmed' => new external_value(PARAM_INT, 'Active user: 1 if confirmed, 0 otherwise', VALUE_OPTIONAL),
'lang' => new external_value(PARAM_SAFEDIR, 'Language code such as "en", must exist on server', VALUE_OPTIONAL),
'calendartype' => new external_value(PARAM_PLUGIN, 'Calendar type such as "gregorian", must exist on server', VALUE_OPTIONAL),
'theme' => new external_value(PARAM_PLUGIN, 'Theme name such as "standard", must exist on server', VALUE_OPTIONAL),
'timezone' => new external_value(PARAM_TIMEZONE, 'Timezone code such as Australia/Perth, or 99 for default', VALUE_OPTIONAL),
'mailformat' => new external_value(PARAM_INT, 'Mail format code is 0 for plain text, 1 for HTML etc', VALUE_OPTIONAL),
'description' => new external_value(PARAM_RAW, 'User profile description', VALUE_OPTIONAL),
'descriptionformat' => new external_format_value('description', VALUE_OPTIONAL),
'city' => new external_value(PARAM_NOTAGS, 'Home city of the user', VALUE_OPTIONAL),
'url' => new external_value(PARAM_URL, 'URL of the user', VALUE_OPTIONAL),
'country' => new external_value(PARAM_ALPHA, 'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL),
'firstaccess' => new external_value(core_user::get_property_type('firstaccess'), 'first access to the site (0 if never)', VALUE_OPTIONAL),
'lastaccess' => new external_value(core_user::get_property_type('lastaccess'), 'last access to the site (0 if never)', VALUE_OPTIONAL),
'auth' => new external_value(core_user::get_property_type('auth'), 'Auth plugins include manual, ldap, imap, etc', VALUE_OPTIONAL),
'confirmed' => new external_value(core_user::get_property_type('confirmed'), 'Active user: 1 if confirmed, 0 otherwise', VALUE_OPTIONAL),
'lang' => new external_value(core_user::get_property_type('lang'), 'Language code such as "en", must exist on server', VALUE_OPTIONAL),
'calendartype' => new external_value(core_user::get_property_type('calendartype'), 'Calendar type such as "gregorian", must exist on server', VALUE_OPTIONAL),
'theme' => new external_value(core_user::get_property_type('theme'), 'Theme name such as "standard", must exist on server', VALUE_OPTIONAL),
'timezone' => new external_value(core_user::get_property_type('timezone'), 'Timezone code such as Australia/Perth, or 99 for default', VALUE_OPTIONAL),
'mailformat' => new external_value(core_user::get_property_type('mailformat'), 'Mail format code is 0 for plain text, 1 for HTML etc', VALUE_OPTIONAL),
'description' => new external_value(core_user::get_property_type('description'), 'User profile description', VALUE_OPTIONAL),
'descriptionformat' => new external_format_value(core_user::get_property_type('descriptionformat'), VALUE_OPTIONAL),
'city' => new external_value(core_user::get_property_type('city'), 'Home city of the user', VALUE_OPTIONAL),
'url' => new external_value(core_user::get_property_type('url'), 'URL of the user', VALUE_OPTIONAL),
'country' => new external_value(core_user::get_property_type('country'), 'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL),
'profileimageurlsmall' => new external_value(PARAM_URL, 'User image profile URL - small version'),
'profileimageurl' => new external_value(PARAM_URL, 'User image profile URL - big version'),
'customfields' => new external_multiple_structure(

View File

@ -52,21 +52,21 @@ class user_edit_forum_form extends moodleform {
$choices['1'] = get_string('emaildigestcomplete');
$choices['2'] = get_string('emaildigestsubjects');
$mform->addElement('select', 'maildigest', get_string('emaildigest'), $choices);
$mform->setDefault('maildigest', $CFG->defaultpreference_maildigest);
$mform->setDefault('maildigest', core_user::get_property_default('maildigest'));
$mform->addHelpButton('maildigest', 'emaildigest');
$choices = array();
$choices['1'] = get_string('autosubscribeyes');
$choices['0'] = get_string('autosubscribeno');
$mform->addElement('select', 'autosubscribe', get_string('autosubscribe'), $choices);
$mform->setDefault('autosubscribe', $CFG->defaultpreference_autosubscribe);
$mform->setDefault('autosubscribe', core_user::get_property_default('autosubscribe'));
if (!empty($CFG->forum_trackreadposts)) {
$choices = array();
$choices['0'] = get_string('trackforumsno');
$choices['1'] = get_string('trackforumsyes');
$mform->addElement('select', 'trackforums', get_string('trackforums'), $choices);
$mform->setDefault('trackforums', $CFG->defaultpreference_trackforums);
$mform->setDefault('trackforums', core_user::get_property_default('trackforums'));
}
// Add some extra hidden fields.

View File

@ -46,7 +46,7 @@ if ($languageform->is_cancelled()) {
$lang = $data->lang;
// If the specified language does not exist, use the site default.
if (!get_string_manager()->translation_exists($lang, false)) {
$lang = $CFG->lang;
$lang = core_user::get_property_default('lang');
}
$user->lang = $lang;

View File

@ -58,7 +58,7 @@ class user_edit_language_form extends moodleform {
$mform->setType('course', PARAM_INT);
$mform->addElement('select', 'lang', get_string('preferredlanguage'), get_string_manager()->get_list_of_translations());
$mform->setDefault('lang', $CFG->lang);
$mform->setDefault('lang', core_user::get_property_default('lang'));
$this->add_action_buttons(true, get_string('savechanges'));
}
@ -77,7 +77,7 @@ class user_edit_language_form extends moodleform {
// Check lang exists.
if (!get_string_manager()->translation_exists($lang, false)) {
$langel =& $mform->getElement('lang');
$langel->setValue($CFG->lang);
$langel->setValue(core_user::get_property_default('lang'));
}
}

View File

@ -33,7 +33,7 @@
* @return int id of the newly created user
*/
function user_create_user($user, $updatepassword = true, $triggerevent = true) {
global $CFG, $DB;
global $DB;
// Set the timecreate field to the current time.
if (!is_object($user)) {
@ -44,7 +44,7 @@ function user_create_user($user, $updatepassword = true, $triggerevent = true) {
if ($user->username !== core_text::strtolower($user->username)) {
throw new moodle_exception('usernamelowercase');
} else {
if ($user->username !== clean_param($user->username, PARAM_USERNAME)) {
if ($user->username !== core_user::clean_field($user->username, 'username')) {
throw new moodle_exception('invalidusername');
}
}
@ -61,39 +61,41 @@ function user_create_user($user, $updatepassword = true, $triggerevent = true) {
unset($user->password);
}
// Make sure calendartype, if set, is valid.
if (!empty($user->calendartype)) {
$availablecalendartypes = \core_calendar\type_factory::get_list_of_calendar_types();
if (empty($availablecalendartypes[$user->calendartype])) {
$user->calendartype = $CFG->calendartype;
}
} else {
$user->calendartype = $CFG->calendartype;
}
// Apply default values for user preferences that are stored in users table.
if (!isset($user->calendartype)) {
$user->calendartype = core_user::get_property_default('calendartype');
}
if (!isset($user->maildisplay)) {
$user->maildisplay = $CFG->defaultpreference_maildisplay;
$user->maildisplay = core_user::get_property_default('maildisplay');
}
if (!isset($user->mailformat)) {
$user->mailformat = $CFG->defaultpreference_mailformat;
$user->mailformat = core_user::get_property_default('mailformat');
}
if (!isset($user->maildigest)) {
$user->maildigest = $CFG->defaultpreference_maildigest;
$user->maildigest = core_user::get_property_default('maildigest');
}
if (!isset($user->autosubscribe)) {
$user->autosubscribe = $CFG->defaultpreference_autosubscribe;
$user->autosubscribe = core_user::get_property_default('autosubscribe');
}
if (!isset($user->trackforums)) {
$user->trackforums = $CFG->defaultpreference_trackforums;
$user->trackforums = core_user::get_property_default('trackforums');
}
if (!isset($user->lang)) {
$user->lang = $CFG->lang;
$user->lang = core_user::get_property_default('lang');
}
$user->timecreated = time();
$user->timemodified = $user->timecreated;
// Validate user data object.
$uservalidation = core_user::validate($user);
if ($uservalidation !== true) {
foreach ($uservalidation as $field => $message) {
debugging("The property '$field' has invalid data and has been cleaned.", DEBUG_DEVELOPER);
$user->$field = core_user::clean_field($user->$field, $field);
}
}
// Insert the user into the database.
$newuserid = $DB->insert_record('user', $user);
@ -138,7 +140,7 @@ function user_update_user($user, $updatepassword = true, $triggerevent = true) {
if ($user->username !== core_text::strtolower($user->username)) {
throw new moodle_exception('usernamelowercase');
} else {
if ($user->username !== clean_param($user->username, PARAM_USERNAME)) {
if ($user->username !== core_user::clean_field($user->username, 'username')) {
throw new moodle_exception('invalidusername');
}
}
@ -157,18 +159,22 @@ function user_update_user($user, $updatepassword = true, $triggerevent = true) {
}
// Make sure calendartype, if set, is valid.
if (!empty($user->calendartype)) {
$availablecalendartypes = \core_calendar\type_factory::get_list_of_calendar_types();
// If it doesn't exist, then unset this value, we do not want to update the user's value.
if (empty($availablecalendartypes[$user->calendartype])) {
unset($user->calendartype);
}
} else {
if (empty($user->calendartype)) {
// Unset this variable, must be an empty string, which we do not want to update the calendartype to.
unset($user->calendartype);
}
$user->timemodified = time();
// Validate user data object.
$uservalidation = core_user::validate($user);
if ($uservalidation !== true) {
foreach ($uservalidation as $field => $message) {
debugging("The property '$field' has invalid data and has been cleaned.", DEBUG_DEVELOPER);
$user->$field = core_user::clean_field($user->$field, $field);
}
}
$DB->update_record('user', $user);
if ($updatepassword) {

View File

@ -64,7 +64,7 @@ class core_user_externallib_testcase extends externallib_advanced_testcase {
'descriptionformat' => FORMAT_MOODLE,
'city' => 'Perth',
'url' => 'http://moodle.org',
'country' => 'au'
'country' => 'AU'
);
$user1 = self::getDataGenerator()->create_user($user1);
@ -222,7 +222,7 @@ class core_user_externallib_testcase extends externallib_advanced_testcase {
'descriptionformat' => FORMAT_MOODLE,
'city' => 'Perth',
'url' => 'http://moodle.org',
'country' => 'au'
'country' => 'AU'
);
$user1 = self::getDataGenerator()->create_user($user1);
if (!empty($CFG->usetags)) {
@ -383,7 +383,7 @@ class core_user_externallib_testcase extends externallib_advanced_testcase {
'descriptionformat' => FORMAT_MOODLE,
'city' => 'Perth',
'url' => 'http://moodle.org',
'country' => 'au'
'country' => 'AU'
);
$return->user1 = self::getDataGenerator()->create_user($return->user1);
if (!empty($CFG->usetags)) {
@ -494,7 +494,7 @@ class core_user_externallib_testcase extends externallib_advanced_testcase {
'email' => 'usertest1@example.com',
'description' => 'This is a description for user 1',
'city' => 'Perth',
'country' => 'au'
'country' => 'AU'
);
$context = context_system::instance();
@ -586,7 +586,7 @@ class core_user_externallib_testcase extends externallib_advanced_testcase {
'descriptionformat' => FORMAT_MOODLE,
'city' => 'Perth',
'url' => 'http://moodle.org',
'country' => 'au'
'country' => 'AU'
);
$user1 = self::getDataGenerator()->create_user($user1);
if (!empty($CFG->usetags)) {
@ -672,7 +672,7 @@ class core_user_externallib_testcase extends externallib_advanced_testcase {
'email' => 'usertest1@example.com',
'description' => 'This is a description for user 1',
'city' => 'Perth',
'country' => 'au'
'country' => 'AU'
);
$context = context_system::instance();

View File

@ -92,6 +92,29 @@ class core_userliblib_testcase extends advanced_testcase {
$this->assertCount(1, $events);
$event = array_pop($events);
$this->assertInstanceOf('\core\event\user_password_updated', $event);
// Test user data validation.
$user->username = 'johndoe123';
$user->auth = 'shibolth';
$user->country = 'WW';
$user->lang = 'xy';
$user->theme = 'somewrongthemename';
$user->timezone = 'Paris';
$user->url = 'wwww.somewrong@#$url.com.aus';
$debugmessages = $this->getDebuggingMessages();
user_update_user($user, true, false);
$this->assertDebuggingCalledCount(6, $debugmessages);
// Now, with valid user data.
$user->username = 'johndoe321';
$user->auth = 'shibboleth';
$user->country = 'AU';
$user->lang = 'en';
$user->theme = 'clean';
$user->timezone = 'Australia/Perth';
$user->url = 'www.moodle.org';
user_update_user($user, true, false);
$this->assertDebuggingNotCalled();
}
/**
@ -115,7 +138,7 @@ class core_userliblib_testcase extends advanced_testcase {
'email' => 'usertest1@example.com',
'description' => 'This is a description for user 1',
'city' => 'Perth',
'country' => 'au'
'country' => 'AU'
);
// Create user and capture event.
@ -152,6 +175,33 @@ class core_userliblib_testcase extends advanced_testcase {
$events = $sink->get_events();
$sink->close();
$this->assertCount(0, $events);
// Test user data validation, first some invalid data.
$user['username'] = 'johndoe123';
$user['auth'] = 'shibolth';
$user['country'] = 'WW';
$user['lang'] = 'xy';
$user['theme'] = 'somewrongthemename';
$user['timezone'] = 'Paris';
$user['url'] = 'wwww.somewrong@#$url.com.aus';
$debugmessages = $this->getDebuggingMessages();
$user['id'] = user_create_user($user, true, false);
$this->assertDebuggingCalledCount(6, $debugmessages);
$dbuser = $DB->get_record('user', array('id' => $user['id']));
$this->assertEquals($dbuser->country, 0);
$this->assertEquals($dbuser->lang, 'en');
$this->assertEquals($dbuser->timezone, 'Australia/Perth');
// Now, with valid user data.
$user['username'] = 'johndoe321';
$user['auth'] = 'shibboleth';
$user['country'] = 'AU';
$user['lang'] = 'en';
$user['theme'] = 'clean';
$user['timezone'] = 'Australia/Perth';
$user['url'] = 'www.moodle.org';
user_create_user($user, true, false);
$this->assertDebuggingNotCalled();
}
/**

View File

@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2016041500.67; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2016042100.00; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.