diff --git a/CRM/Core/BAO/CMSUser.php b/CRM/Core/BAO/CMSUser.php index c53d1e799909..a07b61a5f0c2 100644 --- a/CRM/Core/BAO/CMSUser.php +++ b/CRM/Core/BAO/CMSUser.php @@ -144,71 +144,47 @@ public static function formRule($fields, $files, $form) { $config = CRM_Core_Config::singleton(); - $isDrupal = $config->userSystem->is_drupal; - $isJoomla = ucfirst($config->userFramework) == 'Joomla'; - $isWordPress = $config->userFramework == 'WordPress'; - $errors = []; - if ($isDrupal || $isJoomla || $isWordPress) { - $emailName = NULL; - if (!empty($form->_bltID) && array_key_exists("email-{$form->_bltID}", $fields)) { - // this is a transaction related page - $emailName = 'email-' . $form->_bltID; - } - else { - // find the email field in a profile page - foreach ($fields as $name => $dontCare) { - if (substr($name, 0, 5) == 'email') { - $emailName = $name; - break; - } - } - } - if ($emailName == NULL) { - $errors['_qf_default'] = ts('Could not find an email address.'); - return $errors; - } + $emailName = $config->userSystem->getEmailFieldName($form, $fields); - if (empty($fields['cms_name'])) { - $errors['cms_name'] = ts('Please specify a username.'); - } - - if (empty($fields[$emailName])) { - $errors[$emailName] = ts('Please specify a valid email address.'); - } + $params = [ + 'name' => $fields['cms_name'], + 'mail' => isset($fields[$emailName]) ? $fields[$emailName] : '', + 'pass' => isset($fields['cms_pass']) ? $fields['cms_pass'] : '', + ]; - if ($config->userSystem->isPasswordUserGenerated()) { - if (empty($fields['cms_pass']) || - empty($fields['cms_confirm_pass']) - ) { - $errors['cms_pass'] = ts('Please enter a password.'); - } - if ($fields['cms_pass'] != $fields['cms_confirm_pass']) { - $errors['cms_pass'] = ts('Password and Confirm Password values are not the same.'); - } - } + // Verify the password. + if ($config->userSystem->isPasswordUserGenerated()) { + $config->userSystem->verifyPassword($params, $errors); + } - if (!empty($errors)) { - return $errors; - } + // Set generic errors messages. + if ($emailName == '') { + $errors['_qf_default'] = ts('Could not find an email address.'); + } - // now check that the cms db does not have the user name and/or email - if ($isDrupal or $isJoomla or $isWordPress) { - $params = [ - 'name' => $fields['cms_name'], - 'mail' => $fields[$emailName], - 'pass' => $fields['cms_pass'], - ]; - } + if (empty($params['name'])) { + $errors['cms_name'] = ts('Please specify a username.'); + } - $config->userSystem->checkUserNameEmailExists($params, $errors, $emailName); + if (empty($params['mail'])) { + $errors[$emailName] = ts('Please specify a valid email address.'); + } - // Verify the password. - if ($config->userSystem->isPasswordUserGenerated()) { - $config->userSystem->verifyPassword($params, $errors); + if ($config->userSystem->isPasswordUserGenerated()) { + if (empty($fields['cms_pass']) || + empty($fields['cms_confirm_pass']) + ) { + $errors['cms_pass'] = ts('Please enter a password.'); + } + if ($fields['cms_pass'] != $fields['cms_confirm_pass']) { + $errors['cms_pass'] = ts('Password and Confirm Password values are not the same.'); } } + + $config->userSystem->checkUserNameEmailExists($params, $errors, $emailName); + return (!empty($errors)) ? $errors : TRUE; } diff --git a/CRM/Utils/System/Backdrop.php b/CRM/Utils/System/Backdrop.php index 9f674eec9ea7..014144bc0f92 100644 --- a/CRM/Utils/System/Backdrop.php +++ b/CRM/Utils/System/Backdrop.php @@ -92,16 +92,9 @@ public function updateCMSName($ufID, $email) { } /** - * Check if username and email exists in the Backdrop db. - * - * @param array $params - * Array of name and mail values. - * @param array $errors - * Array of errors. - * @param string $emailName - * Field label for the 'email'. + * @inheritdoc */ - public static function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') { + public function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') { if ($backdrop_errors = form_get_errors()) { // unset Backdrop messages to avoid twice display of errors unset($_SESSION['messages']); diff --git a/CRM/Utils/System/Base.php b/CRM/Utils/System/Base.php index 7a8ad78d4442..78e5f646013c 100644 --- a/CRM/Utils/System/Base.php +++ b/CRM/Utils/System/Base.php @@ -1134,4 +1134,29 @@ public function suppressProfileFormErrors():bool { return FALSE; } + /** + * Get email field name from form values + * + * @param CRM_Core_Form $form + * @param array $fields + * + * @return string + */ + public function getEmailFieldName(CRM_Core_Form $form, array $fields):string { + return 'email'; + } + + /** + * Check if username and email exists in the CMS. + * + * @param array $params + * Array of name and mail values. + * @param array $errors + * Array of errors. + * @param string $emailName + * Field label for the 'email'. + */ + public function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') { + } + } diff --git a/CRM/Utils/System/Drupal.php b/CRM/Utils/System/Drupal.php index eebbcf7fa2d4..ee2c36eaad07 100644 --- a/CRM/Utils/System/Drupal.php +++ b/CRM/Utils/System/Drupal.php @@ -100,25 +100,13 @@ public function updateCMSName($ufID, $ufName) { } /** - * Check if username and email exists in the drupal db. - * - * @param array $params - * Array of name and mail values. - * @param array $errors - * Array of errors. - * @param string $emailName - * Field label for the 'email'. + * @inheritdoc */ - public static function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') { - $config = CRM_Core_Config::singleton(); - - $dao = new CRM_Core_DAO(); - $name = $dao->escape(CRM_Utils_Array::value('name', $params)); - $email = $dao->escape(CRM_Utils_Array::value('mail', $params)); - $errors = form_get_errors(); - if ($errors) { - // unset drupal messages to avoid twice display of errors + public function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') { + if ($drupal_errors = form_get_errors()) { + // unset Drupal messages to avoid twice display of errors unset($_SESSION['messages']); + $errors = array_merge($errors, $drupal_errors); } if (!empty($params['name'])) { diff --git a/CRM/Utils/System/Drupal8.php b/CRM/Utils/System/Drupal8.php index 30d2b5f012e5..c65589d53acd 100644 --- a/CRM/Utils/System/Drupal8.php +++ b/CRM/Utils/System/Drupal8.php @@ -124,16 +124,9 @@ public function updateCMSName($ufID, $email) { } /** - * Check if username and email exists in the drupal db. - * - * @param array $params - * Array of name and mail values. - * @param array $errors - * Errors. - * @param string $emailName - * Field label for the 'email'. + * @inheritdoc */ - public static function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') { + public function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') { // If we are given a name, let's check to see if it already exists. if (!empty($params['name'])) { $name = $params['name']; diff --git a/CRM/Utils/System/DrupalBase.php b/CRM/Utils/System/DrupalBase.php index 102f3fa76e93..d3360c2de01f 100644 --- a/CRM/Utils/System/DrupalBase.php +++ b/CRM/Utils/System/DrupalBase.php @@ -733,4 +733,27 @@ public function suppressProfileFormErrors():bool { return FALSE; } + /** + * @inheritdoc + */ + public function getEmailFieldName(CRM_Core_Form $form, array $fields):string { + $emailName = ''; + + if (!empty($form->_bltID) && array_key_exists("email-{$form->_bltID}", $fields)) { + // this is a transaction related page + $emailName = 'email-' . $form->_bltID; + } + else { + // find the email field in a profile page + foreach ($fields as $name => $dontCare) { + if (substr($name, 0, 5) == 'email') { + $emailName = $name; + break; + } + } + } + + return $emailName; + } + } diff --git a/CRM/Utils/System/Joomla.php b/CRM/Utils/System/Joomla.php index e2773fe2f25e..f5b2bb007bf2 100644 --- a/CRM/Utils/System/Joomla.php +++ b/CRM/Utils/System/Joomla.php @@ -100,14 +100,30 @@ public function updateCMSName($ufID, $ufName) { } /** - * Check if username and email exists in the Joomla db. - * - * @param array $params - * Array of name and mail values. - * @param array $errors - * Array of errors. - * @param string $emailName - * Field label for the 'email'. + * @inheritdoc + */ + public function getEmailFieldName(CRM_Core_Form $form, array $fields):string { + $emailName = ''; + + if (!empty($form->_bltID) && array_key_exists("email-{$form->_bltID}", $fields)) { + // this is a transaction related page + $emailName = 'email-' . $form->_bltID; + } + else { + // find the email field in a profile page + foreach ($fields as $name => $dontCare) { + if (substr($name, 0, 5) == 'email') { + $emailName = $name; + break; + } + } + } + + return $emailName; + } + + /** + * @inheritdoc */ public function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') { $config = CRM_Core_Config::singleton(); diff --git a/CRM/Utils/System/WordPress.php b/CRM/Utils/System/WordPress.php index 706049eedbd0..533e3078b7b9 100644 --- a/CRM/Utils/System/WordPress.php +++ b/CRM/Utils/System/WordPress.php @@ -972,17 +972,32 @@ public function updateCMSName($ufID, $ufName) { } /** - * @param array $params - * @param array $errors - * @param string $emailName + * @inheritdoc */ - public function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') { - $config = CRM_Core_Config::singleton(); + public function getEmailFieldName(CRM_Core_Form $form, array $fields):string { + $emailName = ''; - $dao = new CRM_Core_DAO(); - $name = $dao->escape(CRM_Utils_Array::value('name', $params)); - $email = $dao->escape(CRM_Utils_Array::value('mail', $params)); + if (!empty($form->_bltID) && array_key_exists("email-{$form->_bltID}", $fields)) { + // this is a transaction related page + $emailName = 'email-' . $form->_bltID; + } + else { + // find the email field in a profile page + foreach ($fields as $name => $dontCare) { + if (substr($name, 0, 5) == 'email') { + $emailName = $name; + break; + } + } + } + + return $emailName; + } + /** + * @inheritdoc + */ + public function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') { if (!empty($params['name'])) { if (!validate_username($params['name'])) { $errors['cms_name'] = ts("Your username contains invalid characters");