function civicrm_entity_action_create_user in CiviCRM Entity 7.2
Same name and namespace in other branches
- 7 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 2975
Code
function civicrm_entity_action_create_user($contact, $is_active, $notify = FALSE, $signin = FALSE, $username_format = 'first last') {
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']) || empty($contact['first_name']) || empty($contact['last_name'])) {
$contact = civicrm_api('contact', 'getsingle', array(
'version' => 3,
'id' => $contact['id'],
'sequential' => 1,
'return' => 'email,display_name,first_name,last_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.
if (filter_var($contact['email'], FILTER_VALIDATE_EMAIL) === FALSE) {
drupal_set_message(filter_xss("Cannot create user, email: " . $contact['email'] . " invalid!!"), 'error');
watchdog('civicrm_entity', 'Cannot create user, email %mail invalid.', array(
'%mail' => $contact['email'],
), WATCHDOG_ERROR);
return FALSE;
}
$params = array(
'name' => $contact['display_name'],
'mail' => $contact['email'],
'email' => $contact['email'],
'init' => $contact['email'],
);
switch ($username_format) {
case 'first last':
$params['name'] = _civicrm_entity_clean_login_name(trim($contact['first_name']) . " " . trim($contact['last_name']));
break;
case 'firstlast':
$params['name'] = _civicrm_entity_clean_login_name(trim($contact['first_name']) . " " . trim($contact['last_name']));
break;
case 'first.last':
$params['name'] = _civicrm_entity_clean_login_name(trim($contact['first_name']) . "." . trim($contact['last_name']));
break;
case 'f.last':
$params['name'] = _civicrm_entity_clean_login_name(substr(trim($contact['first_name']), 0, 1) . "." . trim($contact['last_name']));
break;
case 'first.middle.last':
if (!empty($contact['middle_name'])) {
$params['name'] = _civicrm_entity_clean_login_name(trim($contact['first_name']) . "." . trim($contact['middle_name']) . "." . trim($contact['last_name']));
}
else {
$params['name'] = _civicrm_entity_clean_login_name(trim($contact['first_name']) . "." . trim($contact['last_name']));
}
break;
case 'email':
$params['name'] = $params['email'];
break;
case 'display_name':
$params['name'] = _civicrm_entity_clean_login_name(trim($contact['display_name']));
break;
default:
drupal_set_message(filter_xss("username_format " . $username_format . " unknown!!"), 'info');
watchdog('civicrm_entity', 'username_format %format unknown.', array(
'%format' => $username_format,
), WATCHDOG_INFO);
}
if (empty($contact['display_name'])) {
$contact['display_name'] = $contact['email'];
}
// Check if the requested username is available.
$errors = array();
$config = CRM_Core_Config::singleton();
// If username already exists, try appending a numnber
for ($ind = 1; $ind <= 10; $ind++) {
$config->userSystem
->checkUserNameEmailExists($params, $errors);
if (!empty($errors)) {
foreach ($errors as $error) {
//drupal_set_message(check_plain($error), 'error');
watchdog('civicrm_entity', 'Check name %error.', array(
'%error' => $error,
), WATCHDOG_ERROR);
}
$params['name'] = $params['name'] . "{$ind}";
}
else {
$ind = 0;
break;
}
}
// There was errors in the last loop
if ($ind != 0) {
return FALSE;
}
$params['cms_name'] = $user['name'] = $params['name'];
$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);
$config->inCiviCRM = FALSE;
// If selected in action configuration, notify the newly created
// user & send registration link.
if ($notify) {
_user_mail_notify('register_no_approval_required', $user_object);
}
// 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.
// Try to match Individuals, failing that, anything
if (!empty($contact['contact_type']) && $contact['contact_type'] == 'Individual') {
CRM_Core_BAO_UFMatch::synchronizeUFMatch($user_object, $user_object->uid, $contact['email'], 'drupal', NULL, 'Individual', TRUE);
}
else {
CRM_Core_BAO_UFMatch::synchronizeUFMatch($user_object, $user_object->uid, $contact['email'], 'drupal', NULL, NULL, TRUE);
}
drupal_set_message('User with username, ' . $params['cms_name'] . ', has been created.');
// 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,
);
}