Skip to content

Commit

Permalink
[user:*] Update user commands. (#3565)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmolivas committed Oct 30, 2017
1 parent 8ec8a99 commit 8fc0ac6
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 298 deletions.
6 changes: 3 additions & 3 deletions config/services/user.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ services:
- { name: drupal.command }
console.user_login_clear_attempts:
class: Drupal\Console\Command\User\LoginCleanAttemptsCommand
arguments: ['@database']
arguments: ['@database', '@entity_type.manager']
tags:
- { name: drupal.command }
console.user_login_url:
Expand All @@ -21,12 +21,12 @@ services:
- { name: drupal.command }
console.user_password_reset:
class: Drupal\Console\Command\User\PasswordResetCommand
arguments: ['@database', '@console.chain_queue']
arguments: ['@database', '@console.chain_queue', '@entity_type.manager']
tags:
- { name: drupal.command }
console.user_role:
class: Drupal\Console\Command\User\RoleCommand
arguments: ['@console.drupal_api']
arguments: ['@console.drupal_api', '@entity_type.manager']
tags:
- { name: drupal.command }
console.user_create:
Expand Down
11 changes: 9 additions & 2 deletions src/Command/Debug/UserCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,18 @@ protected function execute(InputInterface $input, OutputInterface $output)
foreach ($users as $userId => $user) {
$userRoles = [];
foreach ($user->getRoles() as $userRole) {
$userRoles[] = $systemRoles[$userRole];
if ($systemRoles[$userRole]) {
$userRoles[] = $systemRoles[$userRole];
}
}

$status = $user->isActive()?$this->trans('commands.common.status.enabled'):$this->trans('commands.common.status.disabled');
$tableRows[] = [$userId, $user->getUsername(), implode(', ', $userRoles), $status];
$tableRows[] = [
$userId,
$user->getUsername(),
implode(', ', $userRoles),
$status
];
}

$io->table($tableHeader, $tableRows);
Expand Down
48 changes: 23 additions & 25 deletions src/Command/User/CreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
$email = $input->getOption('email');
$status = $input->getOption('status');

$user = $this->createUser($username, $password, $roles, $email, $status);
$user = $this->createUser(
$username,
$password,
$roles,
$email,
$status
);

$tableHeader = ['Field', 'Value'];

Expand All @@ -141,6 +147,7 @@ function ($field, $value) {
);

$io->table($tableHeader, $tableData);

$io->success(
sprintf(
$this->trans('commands.user.create.messages.user-created'),
Expand All @@ -153,6 +160,8 @@ function ($field, $value) {

if ($user['error']) {
$io->error($user['error']['error']);

return 1;
}
}

Expand All @@ -164,22 +173,22 @@ protected function interact(InputInterface $input, OutputInterface $output)
$io = new DrupalStyle($input, $output);

$username = $input->getArgument('username');
while (!$username) {
$username = $io->askEmpty(
$this->trans('commands.user.create.questions.username'),
null
if (!$username) {
$username = $io->ask(
$this->trans('commands.user.create.questions.username')
);

$input->setArgument('username', $username);
}
$input->setArgument('username', $username);

$password = $input->getArgument('password');
if (!$password) {
$password = $io->askEmpty(
$this->trans('commands.user.create.questions.password'),
null
$this->trans('commands.user.create.questions.password')
);

$input->setArgument('password', $password);
}
$input->setArgument('password', $password);

$roles = $input->getOption('roles');
if (!$roles) {
Expand Down Expand Up @@ -207,8 +216,9 @@ function ($role) use ($systemRoles) {
$this->trans('commands.user.create.questions.email'),
null
);

$input->setOption('email', $email);
}
$input->setOption('email', $email);

$status = $input->getOption('status');
if (!$status) {
Expand All @@ -217,18 +227,18 @@ function ($role) use ($systemRoles) {
[0, 1],
1
);

$input->setOption('status', $status);
}
$input->setOption('status', $status);
}

private function createUser($username, $password, $roles, $email = null, $status = null)
{
$password = $password?:$this->generatePassword();
$user = User::create(
[
'name' => $username,
'mail' => $email ?: $username . '@example.com',
'pass' => $password,
'pass' => $password?:user_password(),
'status' => $status,
'roles' => $roles,
'created' => REQUEST_TIME,
Expand Down Expand Up @@ -264,16 +274,4 @@ private function createUser($username, $password, $roles, $email = null, $status

return $result;
}

private function generatePassword()
{
$length = mt_rand(8, 16);
$str = '';

for ($i = 0; $i < $length; $i++) {
$str .= chr(mt_rand(32, 126));
}

return $str;
}
}
87 changes: 42 additions & 45 deletions src/Command/User/DeleteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Drupal\Console\Core\Command\Command;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Console\Core\Style\DrupalStyle;
Expand All @@ -21,13 +20,8 @@
*
* @package Drupal\Console\Command\User
*/
class DeleteCommand extends Command
class DeleteCommand extends UserBase
{
/**
* @var EntityTypeManagerInterface
*/
protected $entityTypeManager;

/**
* @var QueryFactory
*/
Expand All @@ -50,10 +44,9 @@ public function __construct(
QueryFactory $entityQuery,
DrupalApi $drupalApi
) {
$this->entityTypeManager = $entityTypeManager;
$this->entityQuery = $entityQuery;
$this->drupalApi = $drupalApi;
parent::__construct();
parent::__construct($entityTypeManager);
}

/**
Expand All @@ -65,10 +58,10 @@ protected function configure()
->setName('user:delete')
->setDescription($this->trans('commands.user.delete.description'))
->addOption(
'user-id',
'user',
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.user.delete.options.user-id')
$this->trans('commands.user.delete.options.user')
)
->addOption(
'roles',
Expand All @@ -85,18 +78,18 @@ protected function interact(InputInterface $input, OutputInterface $output)
{
$io = new DrupalStyle($input, $output);

$userId = $input->getOption('user-id');
if (!$userId) {
$userId = $io->askEmpty(
$this->trans('commands.user.delete.questions.user-id'),
$user = $input->getOption('user');
if (!$user) {
$user = $io->askEmpty(
$this->trans('commands.user.delete.questions.user'),
null
);
$input->setOption('user-id', $userId);
$input->setOption('user', $user);
}

$roles = $input->getOption('roles');

if (!$userId && !$roles) {
if (!$user && !$roles) {
$systemRoles = $this->drupalApi->getRoles(false, false, false);
$roles = $io->choice(
$this->trans('commands.user.delete.questions.roles'),
Expand All @@ -123,43 +116,42 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new DrupalStyle($input, $output);

$userId = $input->getOption('user-id');

if ($userId && $userId <= 1) {
$io->error(
sprintf(
$this->trans('commands.user.delete.errors.invalid-user-id'),
$userId
)
);
$user = $input->getOption('user');

return 1;
}
if ($user) {
$userEntity = $this->getUserEntity($user);
if (!$userEntity) {
$io->error(
sprintf(
$this->trans('commands.user.delete.errors.invalid-user'),
$user
)
);

if ($userId) {
$user = $this->entityTypeManager
->getStorage('user')
->load($userId);
return 1;
}

if (!$user) {
if ($userEntity->id() <= 1) {
$io->error(
sprintf(
$this->trans('commands.user.delete.errors.invalid-user'),
$userId
$user
)
);

return 1;
}

try {
$user->delete();
$userEntity->delete();
$io->info(
sprintf(
$this->trans('commands.user.delete.messages.user-deleted'),
$user->getUsername()
$userEntity->getUsername()
)
);

return 0;
} catch (\Exception $e) {
$io->error($e->getMessage());

Expand All @@ -170,27 +162,30 @@ protected function execute(InputInterface $input, OutputInterface $output)
$roles = $input->getOption('roles');

if ($roles) {
$userStorage = $this->entityTypeManager->getStorage('user');
$roles = is_array($roles)?$roles:[$roles];

$query = $this->entityQuery->get('user');
$query->condition('roles', is_array($roles)?$roles:[$roles], 'IN');
$query->condition('uid', 1, '>');
$query = $this->entityQuery
->get('user')
->condition('roles', array_values($roles), 'IN')
->condition('uid', 1, '>');
$results = $query->execute();

$users = $userStorage->loadMultiple($results);
$users = $this->entityTypeManager
->getStorage('user')
->loadMultiple($results);

$tableHeader = [
$this->trans('commands.user.debug.messages.user-id'),
$this->trans('commands.user.debug.messages.username'),
];

$tableRows = [];
foreach ($users as $userId => $user) {
foreach ($users as $user => $userEntity) {
try {
$user->delete();
$tableRows['success'][] = [$userId, $user->getUsername()];
$userEntity->delete();
$tableRows['success'][] = [$user, $userEntity->getUsername()];
} catch (\Exception $e) {
$tableRows['error'][] = [$userId, $user->getUsername()];
$tableRows['error'][] = [$user, $userEntity->getUsername()];
$io->error($e->getMessage());

return 1;
Expand All @@ -205,6 +200,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
count($tableRows['success'])
)
);

return 0;
}
}
}
Expand Down

0 comments on commit 8fc0ac6

Please sign in to comment.