You are here

function civicrm_entity_action_create_user in CiviCRM Entity 7

Same name and namespace in other branches
  1. 7.2 civicrm_entity.module \civicrm_entity_action_create_user()

Given a contact object, load or create then return a drupal user.

Parameters

object $contact: CiviCRM Contact Object

$is_active:

bool $notify:

bool $signin:

Return value

object $user Drupal user object or FALSE.

Throws

Exception

2 calls to civicrm_entity_action_create_user()
civicrm_entity_action_load_create_user in ./civicrm_entity.module
Load or create user as appropriate.
civicrm_entity_user_creatable in ./civicrm_entity.module
Condition Drupal User Account can be created for contact (creates contact).
1 string reference to 'civicrm_entity_action_create_user'
civicrm_entity_rules_action_info in ./civicrm_entity.rules.inc
Implements hook_rules_action_info().

File

./civicrm_entity.module, line 692
Implement CiviCRM entities as a Drupal Entity.

Code

function civicrm_entity_action_create_user($contact, $is_active, $notify = FALSE, $signin = FALSE) {
  if (!is_array($contact)) {

    // Perhaps we should be accepting object rather than array here?
    $contact = (array) $contact;
  }

  // We'll use the civicrm sync mechanism to see if Civi can match the
  // contact to an existing user.
  //
  // Don't think this is a great approach but will use for now - could
  // just create the user but no great support for that yet.
  if (empty($contact['display_name']) || empty($contact['email'])) {
    $contact = civicrm_api('contact', 'getsingle', array(
      'version' => 3,
      'id' => $contact['id'],
      'sequential' => 1,
      'return' => 'email,display_name',
    ));
  }
  if (!is_string($contact['email']) && isset($contact['email'][0]->email)) {
    $contact['email'] = $contact['email'][0]->email;
  }

  // @TODO What happens if they don't have an email at this point?
  // An email is a pre-requisite for a Drupal account, so the action
  // fails if they don't have an email.
  $params = array(
    'name' => $contact['display_name'],
    'mail' => $contact['email'],
    'email' => $contact['email'],
    'init' => $contact['email'],
  );

  // Check if the requested username is available.
  $errors = array();
  $config = CRM_Core_Config::singleton();
  $config->userSystem
    ->checkUserNameEmailExists($params, $errors);
  if (!empty($errors)) {
    foreach ($errors as $error) {
      drupal_set_message(t($error), 'error');
    }
    return FALSE;
  }
  $params['cms_name'] = $params['name'] = $user['name'] = !empty($contact['display_name']) ? $contact['display_name'] : $params['mail'];
  $params['cms_pass'] = $user['pass'] = substr(str_shuffle("abcefghijklmnopqrstuvwxyz"), 0, 8);
  $params['status'] = $is_active;
  if ($notify) {
    $params['notify'] = TRUE;
  }
  $params['roles'] = array(
    DRUPAL_AUTHENTICATED_RID => 'authenticated user',
  );

  // Set $config->inCiviCRM = TRUE to prevent creating a duplicate
  // contact from user_save().
  $config = CRM_Core_Config::singleton();
  $config->inCiviCRM = TRUE;
  $user_object = user_save('', $params);
  $user_object->password = $user['pass'];
  $config->inCiviCRM = FALSE;

  // If selected in action configuration, notify the newly created
  // user & send registration link. Does not contain password in D7.
  if ($notify) {
    drupal_mail('user', 'register_no_approval_required', $params['mail'], NULL, array(
      'account' => $user_object,
    ), variable_get('site_mail', 'noreply@example..com'));
  }

  // CiviCRM doesn't do this when created off CiviCRM Form.
  //
  // Note that we 'pretend' to be logging in to make it do a ufmatch
  // on just the email.
  CRM_Core_BAO_UFMatch::synchronizeUFMatch($user_object, $user_object->uid, $contact['email'], 'drupal', NULL, NULL, TRUE);

  // If selected in action configuration, automatically sign in the
  // current user.
  if ($signin) {
    global $user;
    $user = user_load($user_object->uid);
    watchdog('civicrm_entity', 'User %name logged in via CiviCRM Entity rule execution.', array(
      '%name' => $user->name,
    ), WATCHDOG_INFO);
    $form_values = array(
      'uid' => $user->uid,
    );
    user_login_finalize($form_values);
  }
  return array(
    'civicrm_user' => $user_object,
  );
}