Skip to content

Commit

Permalink
Added console command to reset password for a user
Browse files Browse the repository at this point in the history
  • Loading branch information
askvortsov1 committed Mar 3, 2020
1 parent 045ee33 commit 6a46b46
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Foundation/InstalledApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Flarum\Foundation\Console\InfoCommand;
use Flarum\Http\Middleware\DispatchRoute;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\Console\ResetPasswordCommand;
use Illuminate\Contracts\Container\Container;
use Laminas\Stratigility\Middleware\OriginalMessages;
use Laminas\Stratigility\MiddlewarePipe;
Expand Down Expand Up @@ -121,6 +122,7 @@ public function getConsoleCommands()
$this->container->make(MigrateCommand::class),
$this->container->make(ResetCommand::class),
$this->container->make(CacheClearCommand::class),
$this->container->make(ResetPasswordCommand::class)
];
}
}
111 changes: 111 additions & 0 deletions src/User/Console/ResetPasswordCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

namespace Flarum\User\Console;

use Flarum\Console\AbstractCommand;
use Flarum\User\UserRepository;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Question\Question;

class ResetPasswordCommand extends AbstractCommand
{
protected $questionHelper;

protected $userRepository;

/**
* @param UserRepository $userRepository
*/
public function __construct(QuestionHelper $questionHelper, UserRepository $userRepository)
{
$this->questionHelper = $questionHelper;
$this->userRepository = $userRepository;

parent::__construct();
}

/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('password:reset')
->setDescription('Reset a user\'s password');
}

/**
* {@inheritdoc}
*/
protected function fire()
{
$user = $this->getUser();
$user->changePassword($this->askForPassword());
$user->save();
}

private function getUser()
{
while (true) {
$identification = $this->ask('Enter username or email:');
$user = $this->userRepository->findByIdentification($identification);

if ($user) {
return $user;
} else {
$this->validationError('User with this username or email does not exist.');
continue;
}
}
}

private function askForPassword()
{
while (true) {
$password = $this->secret('New password (required >= 8 characters):');

if (strlen($password) < 8) {
$this->validationError('Password must be at least 8 characters.');
continue;
}

$confirmation = $this->secret('New password (confirmation):');

if ($password !== $confirmation) {
$this->validationError('The password did not match its confirmation.');
continue;
}

return $password;
}
}

private function ask($question, $default = null)
{
$question = new Question("<question>$question</question> ", $default);

return $this->questionHelper->ask($this->input, $this->output, $question);
}

private function secret($question)
{
$question = new Question("<question>$question</question> ");

$question->setHidden(true)->setHiddenFallback(true);

return $this->questionHelper->ask($this->input, $this->output, $question);
}

private function validationError($message)
{
$this->output->writeln("<error>$message</error>");
$this->output->writeln('Please try again.');
}
}

0 comments on commit 6a46b46

Please sign in to comment.