Skip to content

Commit

Permalink
dev/wordpress#82 Add support for creating WordPress accounts from the…
Browse files Browse the repository at this point in the history
… contact record

Also removes the possibility to set a user-defined password for WordPress users.
  • Loading branch information
mlutfy committed Mar 27, 2021
1 parent 8257f28 commit 633d512
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 9 deletions.
14 changes: 10 additions & 4 deletions CRM/Contact/Form/Task/Useradd.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
57 changes: 52 additions & 5 deletions CRM/Utils/System/WordPress.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand All @@ -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;
}

Expand Down Expand Up @@ -870,7 +876,7 @@ public function isUserRegistrationPermitted() {
* @inheritDoc
*/
public function isPasswordUserGenerated() {
return TRUE;
return FALSE;
}

/**
Expand Down Expand Up @@ -990,6 +996,13 @@ public function alterAssetUrl(\Civi\Core\Event\GenericHookEvent $e) {
}
}

/**
* @inheritDoc
*/
public function checkPermissionAddUser() {
return current_user_can('create_users');
}

/**
* @inheritDoc
*/
Expand Down Expand Up @@ -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']);
}

}

0 comments on commit 633d512

Please sign in to comment.