|
6 | 6 |
|
7 | 7 | namespace Magento\Setup\Console\Command;
|
8 | 8 |
|
9 |
| -use Magento\Setup\Model\AdminAccount; |
10 | 9 | use Magento\Framework\Setup\ConsoleLogger;
|
| 10 | +use Magento\Setup\Model\AdminAccount; |
11 | 11 | use Magento\Setup\Model\InstallerFactory;
|
12 | 12 | use Magento\User\Model\UserValidationRules;
|
13 |
| -use Symfony\Component\Console\Input\InputOption; |
14 | 13 | use Symfony\Component\Console\Input\InputInterface;
|
| 14 | +use Symfony\Component\Console\Input\InputOption; |
15 | 15 | use Symfony\Component\Console\Output\OutputInterface;
|
| 16 | +use Symfony\Component\Console\Question\Question; |
16 | 17 |
|
17 | 18 | class AdminUserCreateCommand extends AbstractSetupCommand
|
18 | 19 | {
|
@@ -50,14 +51,106 @@ protected function configure()
|
50 | 51 | parent::configure();
|
51 | 52 | }
|
52 | 53 |
|
| 54 | + /** |
| 55 | + * @param \Symfony\Component\Console\Input\InputInterface $input |
| 56 | + * @param \Symfony\Component\Console\Output\OutputInterface $output |
| 57 | + * |
| 58 | + * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
| 59 | + */ |
| 60 | + protected function interact(InputInterface $input, OutputInterface $output) |
| 61 | + { |
| 62 | + /** @var \Symfony\Component\Console\Helper\QuestionHelper $questionHelper */ |
| 63 | + $questionHelper = $this->getHelper('question'); |
| 64 | + |
| 65 | + if (!$input->getOption(AdminAccount::KEY_USER)) { |
| 66 | + $question = new Question('<question>Admin user:</question> ', ''); |
| 67 | + $this->addNotEmptyValidator($question); |
| 68 | + |
| 69 | + $input->setOption( |
| 70 | + AdminAccount::KEY_USER, |
| 71 | + $questionHelper->ask($input, $output, $question) |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + if (!$input->getOption(AdminAccount::KEY_PASSWORD)) { |
| 76 | + $question = new Question('<question>Admin password:</question> ', ''); |
| 77 | + $question->setHidden(true); |
| 78 | + |
| 79 | + $question->setValidator(function ($value) use ($output) { |
| 80 | + $user = new \Magento\Framework\DataObject(); |
| 81 | + $user->setPassword($value); |
| 82 | + |
| 83 | + $validator = new \Magento\Framework\Validator\DataObject(); |
| 84 | + $this->validationRules->addPasswordRules($validator); |
| 85 | + |
| 86 | + $validator->isValid($user); |
| 87 | + foreach ($validator->getMessages() as $message) { |
| 88 | + throw new \Exception($message); |
| 89 | + } |
| 90 | + |
| 91 | + return $value; |
| 92 | + }); |
| 93 | + |
| 94 | + $input->setOption( |
| 95 | + AdminAccount::KEY_PASSWORD, |
| 96 | + $questionHelper->ask($input, $output, $question) |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + if (!$input->getOption(AdminAccount::KEY_EMAIL)) { |
| 101 | + $question = new Question('<question>Admin email:</question> ', ''); |
| 102 | + $this->addNotEmptyValidator($question); |
| 103 | + |
| 104 | + $input->setOption( |
| 105 | + AdminAccount::KEY_EMAIL, |
| 106 | + $questionHelper->ask($input, $output, $question) |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + if (!$input->getOption(AdminAccount::KEY_FIRST_NAME)) { |
| 111 | + $question = new Question('<question>Admin first name:</question> ', ''); |
| 112 | + $this->addNotEmptyValidator($question); |
| 113 | + |
| 114 | + $input->setOption( |
| 115 | + AdminAccount::KEY_FIRST_NAME, |
| 116 | + $questionHelper->ask($input, $output, $question) |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + if (!$input->getOption(AdminAccount::KEY_LAST_NAME)) { |
| 121 | + $question = new Question('<question>Admin last name:</question> ', ''); |
| 122 | + $this->addNotEmptyValidator($question); |
| 123 | + |
| 124 | + $input->setOption( |
| 125 | + AdminAccount::KEY_LAST_NAME, |
| 126 | + $questionHelper->ask($input, $output, $question) |
| 127 | + ); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * @param \Symfony\Component\Console\Question\Question $question |
| 133 | + * @return void |
| 134 | + */ |
| 135 | + private function addNotEmptyValidator(Question $question) |
| 136 | + { |
| 137 | + $question->setValidator(function ($value) { |
| 138 | + if (trim($value) == '') { |
| 139 | + throw new \Exception('The value cannot be empty'); |
| 140 | + } |
| 141 | + |
| 142 | + return $value; |
| 143 | + }); |
| 144 | + } |
| 145 | + |
53 | 146 | /**
|
54 | 147 | * {@inheritdoc}
|
55 | 148 | */
|
56 | 149 | protected function execute(InputInterface $input, OutputInterface $output)
|
57 | 150 | {
|
58 | 151 | $errors = $this->validate($input);
|
59 | 152 | if ($errors) {
|
60 |
| - $output->writeln('<error>' . implode('</error>' . PHP_EOL . '<error>', $errors) . '</error>'); |
| 153 | + $output->writeln('<error>' . implode('</error>' . PHP_EOL . '<error>', $errors) . '</error>'); |
61 | 154 | // we must have an exit code higher than zero to indicate something was wrong
|
62 | 155 | return \Magento\Framework\Console\Cli::RETURN_FAILURE;
|
63 | 156 | }
|
@@ -113,7 +206,7 @@ public function validate(InputInterface $input)
|
113 | 206 | ? '' : $input->getOption(AdminAccount::KEY_PASSWORD)
|
114 | 207 | );
|
115 | 208 |
|
116 |
| - $validator = new \Magento\Framework\Validator\DataObject; |
| 209 | + $validator = new \Magento\Framework\Validator\DataObject(); |
117 | 210 | $this->validationRules->addUserInfoRules($validator);
|
118 | 211 | $this->validationRules->addPasswordRules($validator);
|
119 | 212 |
|
|
0 commit comments