You've already forked php-auth
generated from siteworxpro/Php-Template
Password reset and email
Some checks failed
🧪✨ Tests Workflow / 🛡️ 🔒 Library Audit (push) Failing after 1m19s
🧪✨ Tests Workflow / 📝 ✨ Code Lint (push) Successful in 1m9s
🧪✨ Tests Workflow / 🛡️ 🔒 License Check (push) Failing after 1m19s
🧪✨ Tests Workflow / 🧪 ✨ Database Migrations (push) Successful in 1m34s
🧪✨ Tests Workflow / 🐙 🔍 Code Sniffer (push) Failing after 1m20s
🧪✨ Tests Workflow / 🧪 ✅ Unit Tests (push) Failing after 12s
Some checks failed
🧪✨ Tests Workflow / 🛡️ 🔒 Library Audit (push) Failing after 1m19s
🧪✨ Tests Workflow / 📝 ✨ Code Lint (push) Successful in 1m9s
🧪✨ Tests Workflow / 🛡️ 🔒 License Check (push) Failing after 1m19s
🧪✨ Tests Workflow / 🧪 ✨ Database Migrations (push) Successful in 1m34s
🧪✨ Tests Workflow / 🐙 🔍 Code Sniffer (push) Failing after 1m20s
🧪✨ Tests Workflow / 🧪 ✅ Unit Tests (push) Failing after 12s
This commit is contained in:
77
src/Cli/Commands/User/ResetPassword.php
Normal file
77
src/Cli/Commands/User/ResetPassword.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\App\Cli\Commands\User;
|
||||
|
||||
use Siteworxpro\App\Cli\ClimateOutput;
|
||||
use Siteworxpro\App\Cli\Commands\Command;
|
||||
use Siteworxpro\App\CommandBus\Commands\SendPasswordReset;
|
||||
use Siteworxpro\App\Services\Facades\CommandBus;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Helper\QuestionHelper;
|
||||
use Symfony\Component\Console\Input\ArgvInput;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Question\Question as QuestionInput;
|
||||
|
||||
#[AsCommand('user:reset-password', 'Reset a user\'s password')]
|
||||
class ResetPassword extends Command
|
||||
{
|
||||
protected function configure(): void
|
||||
{
|
||||
$this->addOption(
|
||||
'send-email',
|
||||
's',
|
||||
InputOption::VALUE_NONE,
|
||||
description: 'Send password reset email to the user'
|
||||
);
|
||||
}
|
||||
|
||||
public function __invoke(ArgvInput|InputInterface $input, ClimateOutput|OutputInterface $output): int
|
||||
{
|
||||
$client = $this->askForClient($output, $input);
|
||||
|
||||
if (!$client) {
|
||||
$output->error('Client not found.');
|
||||
return \Symfony\Component\Console\Command\Command::FAILURE;
|
||||
}
|
||||
|
||||
$user = $this->askForUser($client, $output, $input);
|
||||
if (!$user) {
|
||||
$output->error('User not found for the specified client.');
|
||||
return \Symfony\Component\Console\Command\Command::FAILURE;
|
||||
}
|
||||
|
||||
if ($input->getOption('send-email')) {
|
||||
CommandBus::handle(new SendPasswordReset($user, $client));
|
||||
$output->info('Password reset email sent to the user.');
|
||||
|
||||
return \Symfony\Component\Console\Command\Command::SUCCESS;
|
||||
}
|
||||
|
||||
/** @var QuestionHelper $helper */
|
||||
$helper = $this->getHelper('question');
|
||||
$question = new QuestionInput('Enter the new password: ');
|
||||
$question->setValidator(function ($input): string {
|
||||
if (empty($input) || strlen($input) < 8) {
|
||||
throw new \InvalidArgumentException('Password must be at least 8 characters long.');
|
||||
}
|
||||
|
||||
return $input;
|
||||
});
|
||||
|
||||
$question->setHidden(true);
|
||||
$question->setHiddenFallback(false);
|
||||
|
||||
$newPassword = $helper->ask($input, $output, $question);
|
||||
|
||||
$user->password = $newPassword;
|
||||
$user->save();
|
||||
|
||||
$output->info('Password has been reset successfully.');
|
||||
|
||||
return \Symfony\Component\Console\Command\Command::SUCCESS;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user