Skip to content

Commit

Permalink
Return new user in user:create. Fixes #5660 (#5661)
Browse files Browse the repository at this point in the history
  • Loading branch information
weitzman committed Jun 15, 2023
1 parent 73cd335 commit fa0112f
Showing 1 changed file with 39 additions and 26 deletions.
65 changes: 39 additions & 26 deletions src/Commands/core/UserCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,31 @@ final class UserCommands extends DrushCommands
const CREATE = 'user:create';
const CANCEL = 'user:cancel';
const PASSWORD = 'user:password';
const INF_LABELS = [
'uid' => 'User ID',
'name' => 'User name',
'pass' => 'Password',
'mail' => 'User mail',
'theme' => 'User theme',
'signature' => 'Signature',
'signature_format' => 'Signature format',
'user_created' => 'User created',
'created' => 'Created',
'user_access' => 'User last access',
'access' => 'Last access',
'user_login' => 'User last login',
'login' => 'Last login',
'user_status' => 'User status',
'status' => 'Status',
'timezone' => 'Time zone',
'picture' => 'User picture',
'init' => 'Initial user mail',
'roles' => 'User roles',
'group_audience' => 'Group Audience',
'langcode' => 'Language code',
'uuid' => 'Uuid',
];
const INF_DEFAULT_FIELDS = ['uid', 'name', 'mail', 'roles', 'user_status'];

public function __construct(protected DateFormatterInterface $dateFormatter)
{
Expand All @@ -54,31 +79,8 @@ public static function create(ContainerInterface $container): self
#[CLI\Usage(name: 'drush user:information --mail=someguy@somegal.com', description: 'Display information for a given email account.')]
#[CLI\Usage(name: 'drush user:information --uid=5', description: 'Display information for a given user id.')]
#[CLI\Usage(name: 'drush uinf --uid=$(drush sqlq "SELECT GROUP_CONCAT(entity_id) FROM user__roles WHERE roles_target_id = \'administrator\'")', description: 'Display information for all administrators.')]
#[CLI\FieldLabels(labels: [
'uid' => 'User ID',
'name' => 'User name',
'pass' => 'Password',
'mail' => 'User mail',
'theme' => 'User theme',
'signature' => 'Signature',
'signature_format' => 'Signature format',
'user_created' => 'User created',
'created' => 'Created',
'user_access' => 'User last access',
'access' => 'Last access',
'user_login' => 'User last login',
'login' => 'Last login',
'user_status' => 'User status',
'status' => 'Status',
'timezone' => 'Time zone',
'picture' => 'User picture',
'init' => 'Initial user mail',
'roles' => 'User roles',
'group_audience' => 'Group Audience',
'langcode' => 'Language code',
'uuid' => 'Uuid',
])]
#[CLI\DefaultTableFields(fields: ['uid', 'name', 'mail', 'roles', 'user_status'])]
#[CLI\FieldLabels(labels: self::INF_LABELS)]
#[CLI\DefaultTableFields(fields: self::INF_DEFAULT_FIELDS)]
#[CLI\FilterDefaultField(field: 'name')]
public function information(string $names = '', $options = ['format' => 'table', 'uid' => self::REQ, 'mail' => self::REQ]): RowsOfFields
{
Expand Down Expand Up @@ -216,8 +218,11 @@ public function removeRole(string $role, string $names = '', $options = ['uid' =
#[CLI\Argument(name: 'name', description: 'The name of the account to add')]
#[CLI\Option(name: 'password', description: 'The password for the new account')]
#[CLI\Option(name: 'mail', description: 'The email address for the new account')]
#[CLI\FieldLabels(labels: self::INF_LABELS)]
#[CLI\DefaultTableFields(fields: self::INF_DEFAULT_FIELDS)]
#[CLI\FilterDefaultField(field: 'name')]
#[CLI\Usage(name: "drush user:create newuser --mail='person@example.com' --password='letmein'", description: 'Create a new user account with the name newuser, the email address person@example.com, and the password letmein')]
public function createUser(string $name, $options = ['password' => self::REQ, 'mail' => self::REQ])
public function createUser(string $name, $options = ['format' => 'table', 'password' => self::REQ, 'mail' => self::REQ]): RowsOfFields|CommandError
{
$new_user = [
'name' => $name,
Expand All @@ -230,10 +235,18 @@ public function createUser(string $name, $options = ['password' => self::REQ, 'm
if ($account = User::create($new_user)) {
$account->save();
$this->logger()->success(dt('Created a new user with uid !uid', ['!uid' => $account->id()]));
$outputs[$account->id()] = $this->infoArray($account);

$result = new RowsOfFields($outputs);
$result->addRendererFunction([$this, 'renderRolesCell']);
return $result;
} else {
return new CommandError("Could not create a new user account with the name " . $name . ".");
}
}
else {
return new RowsOfFields([]);
}
}

/**
Expand Down

0 comments on commit fa0112f

Please sign in to comment.