function drush_user_create

8.0.x user.drush.inc drush_user_create($name)
6.x user.drush.inc drush_user_create($name)
7.x user.drush.inc drush_user_create($name)
4.x user.drush.inc drush_user_create($name)
5.x user.drush.inc drush_user_create($name)
master user.drush.inc drush_user_create($name)

Creates a new user account.

File

commands/user/user.drush.inc, line 312
Drush User Management commands

Code

function drush_user_create($name) {
  $mail = drush_get_option('mail');
  $pass = drush_get_option('password');
  $new_user = array(
    'name' => $name,
    'pass' => $pass,
    'mail' => $mail,
    'access' => '0',
    'status' => 1,
  );
  if (drush_drupal_major_version() >= 7) {
    $result = db_query("SELECT uid FROM {users} WHERE name = :name OR mail = :mail", array(':name' => $name, ':mail' => $new_user['mail']));
  }
  else {
    $result = db_query("SELECT uid FROM {users} WHERE name = '%s' OR mail = '%s'", $name, $new_user['mail']);
  }
  if (drush_db_result($result) === FALSE) {
    if (!drush_get_context('DRUSH_SIMULATE')) {
      if (drush_drupal_major_version() >= 8) {
        $new_user_object = entity_create('user', $new_user);
        $new_user_object->save();
      }
      else {
        $new_user_object = user_save(NULL, $new_user, NULL);
      }
      if ($new_user_object !== FALSE) {
        // See https://github.com/drush-ops/drush/issues/464
        // _drush_user_print_info($new_user_object->uid);
        drush_backend_set_result($new_user_object);
        return TRUE;
      }
      else {
        return drush_set_error("Could not create a new user account with the name " . $name . "!");
      }
    }
  }
  else {
    return drush_set_error("There is already a user account with the name " . $name . " or email address " . $new_user['mail'] . "!");
  }
}