Skip to content

Commit

Permalink
Merge branch 'MDL-74592-master' of https://github.com/cameron1729/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Aug 3, 2022
2 parents 8f89242 + 2e44102 commit babfdf8
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 3 deletions.
28 changes: 26 additions & 2 deletions admin/tool/uploaduser/classes/process.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,15 @@ protected function get_allow_deletes(): bool {
return (!empty($this->formdata->uuallowdeletes) and $optype != UU_USER_ADDNEW and $optype != UU_USER_ADDINC);
}

/**
* Setting to allow matching user accounts on email
* @return bool
*/
protected function get_match_on_email(): bool {
$optype = $this->get_operation_type();
return (!empty($this->formdata->uumatchemail) && $optype != UU_USER_ADDNEW && $optype != UU_USER_ADDINC);
}

/**
* Setting to allow deletes
* @return bool
Expand Down Expand Up @@ -414,7 +423,7 @@ protected function prepare_user_record(array $line): ?\stdClass {
}

// Make sure we really have username.
if (empty($user->username)) {
if (empty($user->username) && !$this->get_match_on_email()) {
$this->upt->track('status', get_string('missingfield', 'error', 'username'), 'error');
$this->upt->track('username', get_string('error'), 'error');
$this->userserrors++;
Expand Down Expand Up @@ -453,7 +462,16 @@ public function process_line(array $line) {
return;
}

if ($existinguser = $DB->get_record('user', ['username' => $user->username, 'mnethostid' => $user->mnethostid])) {
$matchparam = $this->get_match_on_email() ? ['email' => $user->email] : ['username' => $user->username];
if ($existinguser = $DB->get_records('user', $matchparam + ['mnethostid' => $user->mnethostid])) {
if (is_array($existinguser) && count($existinguser) !== 1) {
$this->upt->track('status', get_string('duplicateemail', 'tool_uploaduser', $user->email), 'warning');
$this->userserrors++;
return;

}

$existinguser = is_array($existinguser) ? array_values($existinguser)[0] : $existinguser;
$this->upt->track('id', $existinguser->id, 'normal', false);
}

Expand Down Expand Up @@ -582,6 +600,12 @@ public function process_line(array $line) {
// We do not need the deleted flag anymore.
unset($user->deleted);

$matchonemailallowrename = $this->get_match_on_email() && $this->get_allow_renames();
if ($matchonemailallowrename && $user->username && ($user->username !== $existinguser->username)) {
$user->oldusername = $existinguser->username;
$existinguser = false;
}

// Renaming requested?
if (!empty($user->oldusername) ) {
if (!$this->get_allow_renames()) {
Expand Down
2 changes: 2 additions & 0 deletions admin/tool/uploaduser/lang/en/tool_uploaduser.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
$string['csvdelimiter'] = 'CSV separator';
$string['defaultvalues'] = 'Default values';
$string['deleteerrors'] = 'Delete errors';
$string['duplicateemail'] = 'Multiple users with email {$a} detected';
$string['encoding'] = 'Encoding';
$string['errormnetadd'] = 'Can not add remote users';
$string['errorprefix'] = 'Error:';
Expand All @@ -51,6 +52,7 @@
$string['invaliduserdata'] = 'Invalid data detected for user {$a} and it has been automatically cleaned.';
$string['invalidtheme'] = 'Theme "{$a}" is not installed and will be ignored.';
$string['linex'] = 'Line {$a}';
$string['matchemail'] = 'Match on email address';
$string['nochanges'] = 'No changes';
$string['notheme'] = 'No theme is defined for this user.';
$string['pluginname'] = 'User upload';
Expand Down
44 changes: 44 additions & 0 deletions admin/tool/uploaduser/tests/behat/upload_users.feature
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,47 @@ Feature: Upload users
And I select "Assign roles" from the "jump" singleselect
And I should see "Course creator"
And I should see "Federico Fellini"

@javascript
Scenario: Update existing users matching them on email
Given the following "users" exist:
| username | firstname | lastname | email |
| bilbob | Blasbo | Blabbins | bilbo@example.com |
| frodob | Frodeo | Baspins | frodo@example.com |
And I log in as "admin"
And I navigate to "Users > Accounts >Upload users" in site administration
When I upload "lib/tests/fixtures/upload_users_email_matching.csv" file to "File" filemanager
And I press "Upload users"
Then I should see "Upload users preview"
And I set the following fields to these values:
| Upload type | Update existing users only |
| Existing user details | Override with file |
| Match on email address | Yes |
And I press "Upload users"
And I press "Continue"
And I navigate to "Users > Accounts > Browse list of users" in site administration
And I should see "Bilbo Baggins"
And I should see "Frodo Baggins"

@javascript
Scenario: Update existing users matching them on email where one email address is associated with multiple users
Given the following "users" exist:
| username | firstname | lastname | email |
| bilbob | Blasbo | Blabbins | bilbo@example.com |
| frodob | Frodeo | Baspins | frodo@example.com |
| fredob | Fredoo | Baspins | frodo@example.com |
And I log in as "admin"
And I navigate to "Users > Accounts > Upload users" in site administration
When I upload "lib/tests/fixtures/upload_users_email_matching.csv" file to "File" filemanager
And I press "Upload users"
Then I should see "Upload users preview"
And I set the following fields to these values:
| Upload type | Update existing users only |
| Existing user details | Override with file |
| Match on email address | Yes |
And I press "Upload users"
And I should see "Multiple users with email [email protected] detected"
And I press "Continue"
And I navigate to "Users > Accounts > Browse list of users" in site administration
And I should see "Bilbo Baggins"
And I should not see "Frodo Baggins"
5 changes: 4 additions & 1 deletion admin/tool/uploaduser/user_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ function definition () {
}
$mform->addElement('select', 'uuforcepasswordchange', get_string('forcepasswordchange', 'core'), $choices);

$mform->addElement('selectyesno', 'uumatchemail', get_string('matchemail', 'tool_uploaduser'));
$mform->setDefault('uumatchemail', 0);
$mform->hideIf('uumatchemail', 'uutype', 'eq', UU_USER_ADDNEW);
$mform->hideIf('uumatchemail', 'uutype', 'eq', UU_USER_ADDINC);

$mform->addElement('selectyesno', 'uuallowrenames', get_string('allowrenames', 'tool_uploaduser'));
$mform->setDefault('uuallowrenames', 0);
Expand Down Expand Up @@ -230,7 +234,6 @@ function definition () {

$mform->addElement('text', 'username', get_string('uuusernametemplate', 'tool_uploaduser'), 'size="20"');
$mform->setType('username', PARAM_RAW); // No cleaning here. The process verifies it later.
$mform->addRule('username', get_string('requiredtemplate', 'tool_uploaduser'), 'required', null, 'client');
$mform->hideIf('username', 'uutype', 'eq', UU_USER_ADD_UPDATE);
$mform->hideIf('username', 'uutype', 'eq', UU_USER_UPDATE);
$mform->setForceLtr('username');
Expand Down
3 changes: 3 additions & 0 deletions lib/tests/fixtures/upload_users_email_matching.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
username,password,firstname,lastname,email,course1,group1
bilbob,verysecret,Bilbo,Baggins,[email protected],math102,Section 1
frodob,somesecret,Frodo,Baggins,[email protected],math102,Section 3

0 comments on commit babfdf8

Please sign in to comment.