MDL-74592 tool_uploaduser: Add option to match on email

This commit is contained in:
Cameron Ball
2022-08-01 13:45:27 +08:00
parent 417d1b918b
commit 2e44102904
5 changed files with 79 additions and 3 deletions
+26 -2
View File
@@ -228,6 +228,15 @@ class process {
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
@@ -414,7 +423,7 @@ class process {
}
// 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++;
@@ -453,7 +462,16 @@ class process {
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);
}
@@ -582,6 +600,12 @@ class process {
// 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()) {