4 * Contains \Drupal\Console\Command\User\CreateCommand.
7 namespace Drupal\Console\Command\User;
9 use Symfony\Component\Console\Input\InputInterface;
10 use Symfony\Component\Console\Input\InputArgument;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Symfony\Component\Console\Command\Command;
14 use Drupal\Console\Core\Command\Shared\CommandTrait;
15 use Drupal\Core\Database\Connection;
16 use Drupal\Core\Entity\EntityTypeManagerInterface;
17 use Drupal\Core\Datetime\DateFormatterInterface;
18 use Drupal\Console\Core\Utils\ChainQueue;
19 use Drupal\Console\Utils\DrupalApi;
20 use Drupal\Console\Command\Shared\ConfirmationTrait;
21 use Drupal\user\Entity\User;
22 use Drupal\Console\Core\Style\DrupalStyle;
24 class CreateCommand extends Command
27 use ConfirmationTrait;
35 * @var EntityTypeManagerInterface
37 protected $entityTypeManager;
40 * @var DateFormatterInterface
42 protected $dateFormatter;
50 * CreateCommand constructor.
52 * @param Connection $database
53 * @param EntityTypeManagerInterface $entityTypeManager
54 * @param DateFormatterInterface $dateFormatter
55 * @param DrupalApi $drupalApi
57 public function __construct(
59 EntityTypeManagerInterface $entityTypeManager,
60 DateFormatterInterface $dateFormatter,
63 $this->database = $database;
64 $this->entityTypeManager = $entityTypeManager;
65 $this->dateFormatter = $dateFormatter;
66 $this->drupalApi = $drupalApi;
67 parent::__construct();
73 protected function configure()
76 ->setName('user:create')
77 ->setDescription($this->trans('commands.user.create.description'))
78 ->setHelp($this->trans('commands.user.create.help'))
79 ->addArgument('username', InputArgument::OPTIONAL, $this->trans('commands.user.create.options.username'))
80 ->addArgument('password', InputArgument::OPTIONAL, $this->trans('commands.user.create.options.password'))
81 ->addOption('roles', null, InputOption::VALUE_OPTIONAL, $this->trans('commands.user.create.options.roles'))
82 ->addOption('email', null, InputOption::VALUE_OPTIONAL, $this->trans('commands.user.create.options.email'))
83 ->addOption('status', null, InputOption::VALUE_OPTIONAL, $this->trans('commands.user.create.options.status'));
89 protected function execute(InputInterface $input, OutputInterface $output)
91 $io = new DrupalStyle($input, $output);
93 $username = $input->getArgument('username');
94 $password = $input->getArgument('password');
95 $roles = $input->getOption('roles');
96 $email = $input->getOption('email');
97 $status = $input->getOption('status');
99 $user = $this->createUser($username, $password, $roles, $email, $status);
101 $tableHeader = ['Field', 'Value'];
104 $this->trans('commands.user.create.messages.user-id'),
105 $this->trans('commands.user.create.messages.username'),
106 $this->trans('commands.user.create.messages.password'),
107 $this->trans('commands.user.create.messages.email'),
108 $this->trans('commands.user.create.messages.roles'),
109 $this->trans('commands.user.create.messages.created'),
110 $this->trans('commands.user.create.messages.status'),
113 if ($user['success']) {
114 $tableData = array_map(
115 function ($field, $value) {
116 return [$field, $value];
122 $io->table($tableHeader, $tableData);
125 $this->trans('commands.user.create.messages.user-created'),
126 $user['success']['username']
133 if ($user['error']) {
134 $io->error($user['error']['error']);
141 protected function interact(InputInterface $input, OutputInterface $output)
143 $io = new DrupalStyle($input, $output);
145 $username = $input->getArgument('username');
147 $username = $io->askEmpty(
148 $this->trans('commands.user.create.questions.username'),
152 $input->setArgument('username', $username);
154 $password = $input->getArgument('password');
156 $password = $io->askEmpty(
157 $this->trans('commands.user.create.questions.password'),
161 $input->setArgument('password', $password);
163 $roles = $input->getOption('roles');
165 $systemRoles = $this->drupalApi->getRoles(false, false, false);
166 $roles = $io->choice(
167 $this->trans('commands.user.create.questions.roles'),
168 array_values($systemRoles),
174 function ($role) use ($systemRoles) {
175 return array_search($role, $systemRoles);
180 $input->setOption('roles', $roles);
183 $email = $input->getOption('email');
185 $email = $io->askEmpty(
186 $this->trans('commands.user.create.questions.email'),
190 $input->setOption('email', $email);
192 $status = $input->getOption('status');
194 $status = $io->choice(
195 $this->trans('commands.user.create.questions.status'),
200 $input->setOption('status', $status);
203 private function createUser($username, $password, $roles, $email = null, $status = null)
205 $password = $password?:$this->generatePassword();
206 $user = User::create(
209 'mail' => $email ?: $username . '@example.com',
213 'created' => REQUEST_TIME,
222 $result['success'] = [
223 'user-id' => $user->id(),
224 'username' => $user->getUsername(),
225 'password' => $password,
226 'email' => $user->getEmail(),
227 'roles' => implode(', ', $roles),
228 'created' => $this->dateFormatter->format(
229 $user->getCreatedTime(),
236 } catch (\Exception $e) {
238 'vid' => $user->id(),
239 'name' => $user->get('name'),
240 'error' => 'Error: ' . get_class($e) . ', code: ' . $e->getCode() . ', message: ' . $e->getMessage()
247 private function generatePassword()
249 $length = mt_rand(8, 16);
252 for ($i = 0; $i < $length; $i++) {
253 $str .= chr(mt_rand(32, 126));