Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(dev/core#4074) Make CRM_Core_BAO_CMSUser CMS agnostic #25323

Merged
merged 1 commit into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 31 additions & 55 deletions CRM/Core/BAO/CMSUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
herbdool marked this conversation as resolved.
Show resolved Hide resolved
// 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;
}

Expand Down
11 changes: 2 additions & 9 deletions CRM/Utils/System/Backdrop.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down
25 changes: 25 additions & 0 deletions CRM/Utils/System/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
}

}
22 changes: 5 additions & 17 deletions CRM/Utils/System/Drupal.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {
Expand Down
11 changes: 2 additions & 9 deletions CRM/Utils/System/Drupal8.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down
23 changes: 23 additions & 0 deletions CRM/Utils/System/DrupalBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
32 changes: 24 additions & 8 deletions CRM/Utils/System/Joomla.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
31 changes: 23 additions & 8 deletions CRM/Utils/System/WordPress.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down