diff --git a/CRM/Contact/Form/Task/Useradd.php b/CRM/Contact/Form/Task/Useradd.php index 385405c6a2e1..d75239adac34 100644 --- a/CRM/Contact/Form/Task/Useradd.php +++ b/CRM/Contact/Form/Task/Useradd.php @@ -64,14 +64,20 @@ public function setDefaultValues() { * Build the form object. */ public function buildQuickForm() { + $config = CRM_Core_Config::singleton(); + $element = $this->add('text', 'name', ts('Full Name'), ['class' => 'huge']); $element->freeze(); $this->add('text', 'cms_name', ts('Username'), ['class' => 'huge']); $this->addRule('cms_name', 'Username is required', 'required'); - $this->add('password', 'cms_pass', ts('Password'), ['class' => 'huge']); - $this->add('password', 'cms_confirm_pass', ts('Confirm Password'), ['class' => 'huge']); - $this->addRule('cms_pass', 'Password is required', 'required'); - $this->addRule(['cms_pass', 'cms_confirm_pass'], 'ERROR: Password mismatch', 'compare'); + + if (!$config->userSystem->isUserRegistrationPermitted()) { + $this->add('password', 'cms_pass', ts('Password'), ['class' => 'huge']); + $this->add('password', 'cms_confirm_pass', ts('Confirm Password'), ['class' => 'huge']); + $this->addRule('cms_pass', 'Password is required', 'required'); + $this->addRule(['cms_pass', 'cms_confirm_pass'], 'ERROR: Password mismatch', 'compare'); + } + $this->add('text', 'email', ts('Email:'), ['class' => 'huge'])->freeze(); $this->addRule('email', 'Email is required', 'required'); $this->add('hidden', 'contactID'); diff --git a/CRM/Utils/System/WordPress.php b/CRM/Utils/System/WordPress.php index 2877ffed2ad9..b6311c89dbaa 100644 --- a/CRM/Utils/System/WordPress.php +++ b/CRM/Utils/System/WordPress.php @@ -765,7 +765,6 @@ public function cmsRootPath() { public function createUser(&$params, $mail) { $user_data = [ 'ID' => '', - 'user_pass' => $params['cms_pass'], 'user_login' => $params['cms_name'], 'user_email' => $params[$mail], 'nickname' => $params['cms_name'], @@ -783,15 +782,22 @@ public function createUser(&$params, $mail) { } } + $this->hooks_core_remove(); $uid = wp_insert_user($user_data); $creds = []; $creds['user_login'] = $params['cms_name']; - $creds['user_password'] = $params['cms_pass']; $creds['remember'] = TRUE; - $user = wp_signon($creds, FALSE); - wp_new_user_notification($uid, $user_data['user_pass']); + // Call wp_signon if we aren't already logged in + // For example, we might be creating a new user from the Contact record. + if (!current_user_can('create_users')) { + wp_signon($creds, FALSE); + } + + do_action('register_new_user', $uid); + $this->hooks_core_add(); + return $uid; } @@ -870,7 +876,7 @@ public function isUserRegistrationPermitted() { * @inheritDoc */ public function isPasswordUserGenerated() { - return TRUE; + return FALSE; } /** @@ -990,6 +996,13 @@ public function alterAssetUrl(\Civi\Core\Event\GenericHookEvent $e) { } } + /** + * @inheritDoc + */ + public function checkPermissionAddUser() { + return current_user_can('create_users'); + } + /** * @inheritDoc */ @@ -1252,4 +1265,38 @@ public function getCMSPermissionsUrlParams() { return ['ufAccessURL' => CRM_Utils_System::url('civicrm/admin/access/wp-permissions', 'reset=1')]; } + /** + * Remove CiviCRM's callbacks. + * + * These may cause recursive updates when creating or editing a WordPress + * user. This doesn't seem to have been necessary in the past, but seems + * to be causing trouble when newer versions of BuddyPress and CiviCRM are + * active. + * + * Based on the civicrm-wp-profile-sync plugin by Christian Wach. + * + * @see self::hooks_core_add() + */ + public function hooks_core_remove() { + $civicrm = civi_wp(); + + // Remove current CiviCRM plugin filters. + remove_action('user_register', [$civicrm->users, 'update_user']); + remove_action('profile_update', [$civicrm->users, 'update_user']); + } + + /** + * Add back CiviCRM's callbacks. + * This method undoes the removal of the callbacks above. + * + * @see self::hooks_core_remove() + */ + public function hooks_core_add() { + $civicrm = civi_wp(); + + // Re-add current CiviCRM plugin filters. + add_action('user_register', [$civicrm->users, 'update_user']); + add_action('profile_update', [$civicrm->users, 'update_user']); + } + }