use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
-use Symfony\Component\Console\Command\Command;
-use Drupal\Console\Core\Command\Shared\CommandTrait;
use Drupal\Core\Database\Connection;
+use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Console\Core\Utils\ChainQueue;
-use Drupal\Console\Command\Shared\ConfirmationTrait;
-use Drupal\user\Entity\User;
-use Drupal\Console\Core\Style\DrupalStyle;
-class PasswordResetCommand extends Command
+class PasswordResetCommand extends UserBase
{
- use CommandTrait;
- use ConfirmationTrait;
-
/**
* @var Connection
*/
/**
* PasswordHashCommand constructor.
*
- * @param Connection $database
- * @param ChainQueue $chainQueue
+ * @param Connection $database
+ * @param ChainQueue $chainQueue
+ * @param EntityTypeManagerInterface $entityTypeManager
*/
public function __construct(
Connection $database,
- ChainQueue $chainQueue
+ ChainQueue $chainQueue,
+ EntityTypeManagerInterface $entityTypeManager
) {
$this->database = $database;
$this->chainQueue = $chainQueue;
- parent::__construct();
+ parent::__construct($entityTypeManager);
}
/**
->setName('user:password:reset')
->setDescription($this->trans('commands.user.password.reset.description'))
->setHelp($this->trans('commands.user.password.reset.help'))
- ->addArgument('user', InputArgument::REQUIRED, $this->trans('commands.user.password.reset.options.user-id'))
- ->addArgument('password', InputArgument::REQUIRED, $this->trans('commands.user.password.reset.options.password'));
+ ->addArgument(
+ 'user',
+ InputArgument::REQUIRED,
+ $this->trans('commands.user.password.reset.options.user')
+ )
+ ->addArgument(
+ 'password',
+ InputArgument::REQUIRED,
+ $this->trans('commands.user.password.reset.options.password')
+ )
+ ->setAliases(['upr']);
}
/**
* {@inheritdoc}
*/
- protected function execute(InputInterface $input, OutputInterface $output)
+ protected function interact(InputInterface $input, OutputInterface $output)
{
- $io = new DrupalStyle($input, $output);
+ $this->getUserArgument();
- $uid = $input->getArgument('user');
+ $password = $input->getArgument('password');
+ if (!$password) {
+ $password = $this->getIo()->ask(
+ $this->trans('commands.user.password.hash.questions.password')
+ );
- $user = User::load($uid);
+ $input->setArgument('password', $password);
+ }
+ }
- if (!$user) {
- $io->error(
+ /**
+ * {@inheritdoc}
+ */
+ protected function execute(InputInterface $input, OutputInterface $output)
+ {
+ $user = $input->getArgument('user');
+
+ $userEntity = $this->getUserEntity($user);
+
+ if (!$userEntity) {
+ $this->getIo()->error(
sprintf(
$this->trans('commands.user.password.reset.errors.invalid-user'),
- $uid
+ $user
)
);
$password = $input->getArgument('password');
if (!$password) {
- $io->error(
+ $this->getIo()->error(
sprintf(
$this->trans('commands.user.password.reset.errors.empty-password'),
- $uid
+ $password
)
);
}
try {
- $user->setPassword($password);
- $user->save();
+ $userEntity->setPassword($password);
+ $userEntity->save();
$schema = $this->database->schema();
$flood = $schema->findTables('flood');
if ($flood) {
- $this-$this->chainQueue
- ->addCommand('user:login:clear:attempts', ['uid' => $uid]);
+ $this->chainQueue
+ ->addCommand('user:login:clear:attempts', ['user' => $user]);
}
+
+ $this->getIo()->success(
+ sprintf(
+ $this->trans('commands.user.password.reset.messages.reset-successful'),
+ $user
+ )
+ );
+ return 0;
} catch (\Exception $e) {
- $io->error($e->getMessage());
+ $this->getIo()->error($e->getMessage());
return 1;
}
-
- $io->success(
- sprintf(
- $this->trans('commands.user.password.reset.messages.reset-successful'),
- $uid
- )
- );
- }
-
- /**
- * {@inheritdoc}
- */
- protected function interact(InputInterface $input, OutputInterface $output)
- {
- $io = new DrupalStyle($input, $output);
-
- $user = $input->getArgument('user');
- if (!$user) {
- while (true) {
- $user = $io->ask(
- $this->trans('commands.user.password.reset.questions.user'),
- '',
- function ($uid) use ($io) {
- if ($uid) {
- $uid = (int) $uid;
- if (is_int($uid) && $uid > 0) {
- return $uid;
- } else {
- $io->error(
- sprintf($this->trans('commands.user.password.reset.questions.invalid-uid'), $uid)
- );
-
- return false;
- }
- }
- }
- );
-
- if ($user) {
- break;
- }
- }
-
- $input->setArgument('user', $user);
- }
-
- $password = $input->getArgument('password');
- if (!$password) {
- while (true) {
- $password = $io->ask(
- $this->trans('commands.user.password.hash.questions.password'),
- '',
- function ($pass) use ($io) {
- if ($pass) {
- if (!empty($pass)) {
- return $pass;
- } else {
- $io->error(
- sprintf($this->trans('commands.user.password.hash.questions.invalid-pass'), $pass)
- );
-
- return false;
- }
- }
- }
- );
-
- if ($password) {
- break;
- }
- }
-
-
- $input->setArgument('password', $password);
- }
}
}