From 133d554916e1f0f9021b62348a0e4309e092d66e Mon Sep 17 00:00:00 2001 From: Roland Gruber Date: Tue, 22 May 2018 19:55:49 +0200 Subject: [PATCH] check for existing email address --- lam/lib/modules.inc | 21 +++++++++++++--- lam/lib/modules/inetOrgPerson.inc | 10 ++++++++ lam/lib/modules/windowsUser.inc | 42 +++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 4 deletions(-) diff --git a/lam/lib/modules.inc b/lam/lib/modules.inc index 6767fdc0..0afbadff 100644 --- a/lam/lib/modules.inc +++ b/lam/lib/modules.inc @@ -522,12 +522,23 @@ function buildUploadAccounts($type, $data, $ids, $selectedModules, htmlResponsiv foreach ($data as $i => $dataRow) { $partialAccounts[$i]['objectClass'] = array(); } + $stopUpload = false; for ($i = 0; $i < sizeof($ordered); $i++) { $module = new $ordered[$i]($type->getScope()); - $errors = $module->build_uploadAccounts($data, $ids, $partialAccounts, $selectedModules, $type); - if (sizeof($errors) > 0) { - array_unshift($errors, array("INFO", _("Displayed account numbers start at \"0\". Add 2 to get the row in your spreadsheet."), "")); - $errors[] = array("ERROR", _("Upload was stopped after errors in %s module!"), "", array($module->get_alias())); + $moduleErrors = $module->build_uploadAccounts($data, $ids, $partialAccounts, $selectedModules, $type); + if (sizeof($moduleErrors) > 0) { + $errors = array_merge($errors, $moduleErrors); + foreach ($moduleErrors as $error) { + if ($error[0] == 'ERROR') { + array_unshift($errors, array("INFO", _("Displayed account numbers start at \"0\". Add 2 to get the row in your spreadsheet."), "")); + $errors[] = array("ERROR", _("Upload was stopped after errors in %s module!"), "", array($module->get_alias())); + // skip other modules if error was found + $stopUpload = true; + break; + } + } + } + if ($stopUpload) { break; } } @@ -537,6 +548,8 @@ function buildUploadAccounts($type, $data, $ids, $selectedModules, htmlResponsiv $values = empty($errors[$i][3]) ? null : $errors[$i][3]; $container->add(new htmlStatusMessage($errors[$i][0], $errors[$i][1], $text, $values), 12); } + } + if ($stopUpload) { return false; } return $partialAccounts; diff --git a/lam/lib/modules/inetOrgPerson.inc b/lam/lib/modules/inetOrgPerson.inc index d0126338..aa877d1c 100644 --- a/lam/lib/modules/inetOrgPerson.inc +++ b/lam/lib/modules/inetOrgPerson.inc @@ -79,6 +79,7 @@ class inetOrgPerson extends baseModule implements passwordService { $this->messages['pager'][1] = array('ERROR', _('Account %s:') . ' inetOrgPerson_pager', _('Please enter a valid telephone number!')); $this->messages['mail'][0] = array('ERROR', _('Email address'), _('Please enter a valid email address!')); $this->messages['mail'][1] = array('WARN', _('Email address'), _('Email "%s" already in use.')); + $this->messages['mail'][2] = array('WARN', _('Account %s:') . ' inetOrgPerson_email', _('Email "%s" already in use.')); $this->messages['email'][1] = array('ERROR', _('Account %s:') . ' inetOrgPerson_email', _('Please enter a valid email address!')); $this->messages['street'][0] = array('ERROR', _('Street'), _('Please enter a valid street name!')); $this->messages['street'][1] = array('ERROR', _('Account %s:') . ' inetOrgPerson_street', _('Please enter a valid street name!')); @@ -2218,6 +2219,10 @@ class inetOrgPerson extends baseModule implements passwordService { for ($e = 0; $e < sizeof($existingUsers); $e++) { $existingUsers[$e] = $existingUsers[$e]['uid'][0]; } + $existingMails = searchLDAPByAttribute('mail', '*', 'inetOrgPerson', array('mail'), array('user')); + for ($e = 0; $e < sizeof($existingMails); $e++) { + $existingMails[$e] = $existingMails[$e]['mail'][0]; + } for ($i = 0; $i < sizeof($rawAccounts); $i++) { if (!in_array("inetOrgPerson", $partialAccounts[$i]['objectClass'])) $partialAccounts[$i]['objectClass'][] = "inetOrgPerson"; // last name @@ -2340,6 +2345,11 @@ class inetOrgPerson extends baseModule implements passwordService { $errors[] = $errMsg; break; } + elseif (in_array($mailList[$x], $existingMails)) { + $errMsg = $this->messages['mail'][2]; + array_push($errMsg, array($i, $mailList[$x])); + $errors[] = $errMsg; + } } } if (isLAMProVersion() && isset($ids['inetOrgPerson_sendPasswordMail']) && ($rawAccounts[$i][$ids['inetOrgPerson_sendPasswordMail']] != "")) { diff --git a/lam/lib/modules/windowsUser.inc b/lam/lib/modules/windowsUser.inc index a9a021b5..b38a05eb 100644 --- a/lam/lib/modules/windowsUser.inc +++ b/lam/lib/modules/windowsUser.inc @@ -977,8 +977,12 @@ class windowsUser extends baseModule implements passwordService { $this->messages['postalCode'][1] = array('ERROR', _('Account %s:') . ' windowsUser_postalCode', _('Please enter a valid postal code!')); $this->messages['mail'][0] = array('ERROR', _('Email address'), _('Please enter a valid email address!')); $this->messages['mail'][1] = array('ERROR', _('Account %s:') . ' windowsUser_mail', _('Please enter a valid email address!')); + $this->messages['mail'][2] = array('WARN', _('Email address'), _('Email "%s" already in use.')); + $this->messages['mail'][3] = array('WARN', _('Account %s:') . ' windowsUser_mail', _('Email "%s" already in use.')); $this->messages['otherMailbox'][0] = array('ERROR', _('Email alias'), _('Email alias is invalid!')); $this->messages['otherMailbox'][1] = array('ERROR', _('Account %s:') . ' windowsUser_otherMailbox', _('Email alias is invalid!')); + $this->messages['otherMailbox'][2] = array('WARN', _('Email alias'), _('Email alias "%s" already in use.')); + $this->messages['otherMailbox'][3] = array('WARN', _('Account %s:') . ' windowsUser_otherMailbox', _('Email alias "%s" already in use.')); $this->messages['profilePath'][0] = array('ERROR', _('Profile path'), _('Profile path is invalid!')); $this->messages['profilePath'][1] = array('ERROR', _('Account %s:') . ' windowsUser_profilePath', _('Profile path is invalid!')); $this->messages['scriptPath'][0] = array('ERROR', _('Logon script'), _('Logon script is invalid!')); @@ -1383,8 +1387,26 @@ class windowsUser extends baseModule implements passwordService { if (!empty($this->attributes['mail'][0]) && !get_preg($this->attributes['mail'][0], 'email')) { $return[] = $this->messages['mail'][0]; } + elseif (!empty($this->attributes['mail'][0]) && $this->getAccountContainer()->isNewAccount) { + $existingMailAccounts = searchLDAPByAttribute('mail', $this->attributes['mail'][0], 'user', array('mail'), array('user')); + if (!empty($existingMailAccounts)) { + $msg = $this->messages['mail'][2]; + $msg[] = array($this->attributes['mail'][0]); + $return[] = $msg; + } + } // email aliases $this->processMultiValueInputTextField('otherMailbox', $return, 'email'); + if (!empty($this->attributes['otherMailbox'][0]) && $this->getAccountContainer()->isNewAccount) { + foreach ($this->attributes['otherMailbox'] as $otherMailbox) { + $existingMailAccounts = searchLDAPByAttribute('otherMailbox', $otherMailbox, 'user', array('otherMailbox'), array('user')); + if (!empty($existingMailAccounts)) { + $msg = $this->messages['otherMailbox'][2]; + $msg[] = array($otherMailbox); + $return[] = $msg; + } + } + } // other telephones $this->processMultiValueInputTextField('otherTelephone', $return, 'telephone'); // fax number @@ -1991,6 +2013,16 @@ class windowsUser extends baseModule implements passwordService { $groupMap[extractRDNValue($dn)] = $dn; } $existingUsers = $this->getUserNames(); + $existingMailAccounts = searchLDAPByAttribute(null, null, 'user', array('mail', 'otherMailbox'), array('user')); + $existingMails = array(); + foreach ($existingMailAccounts as $existingMailAccount) { + if (!empty($existingMailAccount['mail'][0])) { + $existingMails[] = $existingMailAccount['mail'][0]; + } + if (!empty($existingMailAccount['othermailbox'][0])) { + $existingMails = array_merge($existingMails, $existingMailAccount['othermailbox']); + } + } $booleanOptions = array(_('yes') => true, _('no') => false); for ($i = 0; $i < sizeof($rawAccounts); $i++) { // add object class @@ -2116,6 +2148,11 @@ class windowsUser extends baseModule implements passwordService { if ($rawAccounts[$i][$ids['windowsUser_mail']] != "") { if (get_preg($rawAccounts[$i][$ids['windowsUser_mail']], 'email')) { $partialAccounts[$i]['mail'] = $rawAccounts[$i][$ids['windowsUser_mail']]; + if (in_array_ignore_case($partialAccounts[$i]['mail'], $existingMails)) { + $errMsg = $this->messages['mail'][3]; + array_push($errMsg, array($i, $partialAccounts[$i]['mail'])); + $errors[] = $errMsg; + } } else { $errMsg = $this->messages['mail'][1]; @@ -2134,6 +2171,11 @@ class windowsUser extends baseModule implements passwordService { $errors[] = $errMsg; break; } + if (in_array_ignore_case($valueList[$x], $existingMails)) { + $errMsg = $this->messages['otherMailbox'][3]; + array_push($errMsg, array($i, $valueList[$x])); + $errors[] = $errMsg; + } } } // telephone