dev/core#4074 make CRM_Core_BAO_CMSUser CMS agnostic
authorHerb v/d Dool <herb@3speedhub.com>
Wed, 11 Jan 2023 18:06:31 +0000 (13:06 -0500)
committerHerb v/d Dool <herb@3speedhub.com>
Fri, 27 Jan 2023 22:16:29 +0000 (17:16 -0500)
CRM/Core/BAO/CMSUser.php
CRM/Utils/System/Backdrop.php
CRM/Utils/System/Base.php
CRM/Utils/System/Drupal.php
CRM/Utils/System/Drupal8.php
CRM/Utils/System/DrupalBase.php
CRM/Utils/System/Joomla.php
CRM/Utils/System/WordPress.php

index c53d1e799909454de445fc781e026016ac0a13ea..a07b61a5f0c254670c4b715746f7403e1dadcdd2 100644 (file)
@@ -144,71 +144,47 @@ class CRM_Core_BAO_CMSUser {
 
     $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;
   }
 
index 9f674eec9ea7cc1456f3ef9dccb2738d07dfc4eb..014144bc0f9290b80823eaff5939fb9b940986cb 100644 (file)
@@ -92,16 +92,9 @@ class CRM_Utils_System_Backdrop extends CRM_Utils_System_DrupalBase {
   }
 
   /**
-   * 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']);
index 7a8ad78d4442b48d877c6eeb9d5768343af20dce..78e5f646013c61c1eeba2e1765d07cb70e699138 100644 (file)
@@ -1134,4 +1134,29 @@ abstract class CRM_Utils_System_Base {
     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') {
+  }
+
 }
index eebbcf7fa2d49d4ece9130277f4a9c16d371296c..ee2c36eaad077a6a0aef7389139ae913dc3d07da 100644 (file)
@@ -100,25 +100,13 @@ class CRM_Utils_System_Drupal extends CRM_Utils_System_DrupalBase {
   }
 
   /**
-   * 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'])) {
index 30d2b5f012e5b0c575a74492a5c5d2f628ce68ee..c65589d53acda2d892c3cb20df6b77b29556ac20 100644 (file)
@@ -124,16 +124,9 @@ class CRM_Utils_System_Drupal8 extends CRM_Utils_System_DrupalBase {
   }
 
   /**
-   * 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'];
index 102f3fa76e93c21a33f4668d8b15ad651a478089..d3360c2de01f0754773e7028a3b15c5da757667d 100644 (file)
@@ -733,4 +733,27 @@ abstract class CRM_Utils_System_DrupalBase extends CRM_Utils_System_Base {
     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;
+  }
+
 }
index e2773fe2f25e0f55caf41cfedd275948fac5443e..f5b2bb007bf297e3941f2c3c42c4081cad1e3b29 100644 (file)
@@ -100,14 +100,30 @@ class CRM_Utils_System_Joomla extends CRM_Utils_System_Base {
   }
 
   /**
-   * 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();
index 706049eedbd04c442bc130e1d05f4a7ad24e99fb..533e3078b7b902a7fd5ffcba7f9ddf864188382f 100644 (file)
@@ -972,17 +972,32 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base {
   }
 
   /**
-   * @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");