First Commit
This commit is contained in:
Executable
+47
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Modules\Auth\Models\User;
|
||||
|
||||
class UserPasswordCommand extends Command
|
||||
{
|
||||
protected static $defaultName = 'user:password';
|
||||
protected static $defaultDescription = 'Altera a senha de um usuário cadastrado no sistema';
|
||||
|
||||
protected function configure(): void
|
||||
{
|
||||
$this
|
||||
->addArgument('user', InputArgument::REQUIRED, 'O ID ou E-mail do usuário')
|
||||
->addArgument('password', InputArgument::REQUIRED, 'A nova senha');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$userInput = $input->getArgument('user');
|
||||
$newPassword = $input->getArgument('password');
|
||||
|
||||
$user = is_numeric($userInput)
|
||||
? User::find($userInput)
|
||||
: User::where('users_email', $userInput)->first();
|
||||
|
||||
if (!$user) {
|
||||
$output->writeln("<error>Usuário '{$userInput}' não encontrado.</error>");
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
try {
|
||||
$user->users_password = password_hash($newPassword, PASSWORD_DEFAULT);
|
||||
$user->save();
|
||||
$output->writeln("<info>Senha do usuário '{$user->users_name}' alterada com sucesso!</info>");
|
||||
return Command::SUCCESS;
|
||||
} catch (\Exception $e) {
|
||||
$output->writeln("<error>Erro ao alterar senha: " . $e->getMessage() . "</error>");
|
||||
return Command::FAILURE;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user