You are here

function rules_action_user_create in Rules 6

Action: Creates a user.

Related topics

File

rules/modules/user.rules.inc, line 225
rules integration for the user module

Code

function rules_action_user_create($user_name, $user_mail) {
  $password = user_password();
  $user_data = array(
    'name' => $user_name,
    'mail' => $user_mail,
    'status' => 1,
    'pass' => $password,
  );

  //validate name input.
  if (user_validate_name($user_data['name'])) {
    rules_log(t('No appropriate user name. No user has been created.'));
  }
  elseif (drupal_is_denied('user', $user_data['name'])) {
    rules_log(t('The name %name has been denied access. No user has been created.', array(
      '%name' => $user_data['name'],
    )));
  }
  elseif ($account = user_load(array(
    'name' => $user_data['name'],
  ))) {
    rules_log(t('User !name already exists. No user has been created.', array(
      '!name' => $user_data['name'],
    )));
  }
  elseif ($error = user_validate_mail($user_data['mail'])) {
    rules_log(t('No appropriate mail address. No user has been created.'));
  }
  elseif (drupal_is_denied('mail', $user_data['mail'])) {
    rules_log(t('The e-mail address %email has been denied access. No user has been created.', array(
      '%email' => $user_data['mail'],
    )));
  }
  elseif ($account = user_load(array(
    'mail' => $user_data['mail'],
  ))) {
    rules_log(t('The e-mail address %email is already registered. No user has been created.', array(
      '%email' => $user_data['mail'],
    )));
  }
  else {
    $user = user_save(NULL, $user_data);
    return array(
      'user_added' => $user,
      'user_added_pw' => $password,
    );
  }
}