<?php
namespace App\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Style\SymfonyStyle;
class PcgcChangepasswordUserCommand extends ContainerAwareCommand
{
protected static $defaultName = 'pcgc:changepassword:user';
protected function configure()
{
$this->setDescription('Change a CMS user password')
->setHelp('This command allows you to change a users password...duh!')
;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$io->title('FlightCMS Change Password Command');
$em = $this->getContainer()->get('doctrine')->getManager();
$helper = $this->getHelper('question');
//build & capture email address question
$questionEmail = new Question('Please enter the users email address: ');
$questionEmail->setValidator(function ($value) {
if ('' == trim($value)) {
throw new \Exception('The email must not be blank');
}
return $value;
});
$questionEmail->setMaxAttempts(3);
$email = $helper->ask($input, $output, $questionEmail);
//check if email exists
$output->writeln(sprintf('Checking %s.....', $email));
$user = $em->getRepository('App:User')->findOneByEmail($email);
if (!$user) {
$io->error('The email does not exist');
return 0;
}
$output->writeln(sprintf('Found user : %s', $user->getUsername()));
//build & capture password question
$questionPassword = new Question('Enter new password: ');
$questionPassword->setValidator(function ($value) {
if ('' == trim($value)) {
throw new \Exception('The password must not be blank');
}
return $value;
});
$questionPassword->setMaxAttempts(3);
$password = $helper->ask($input, $output, $questionPassword);
//Change password in Database
$password_encoder = $this->getContainer()->get('security.password_encoder');
$encryedPassword = $password_encoder->encodePassword($user, $password);
$user->setEmailresetkey(null);
$user->setPassword($encryedPassword);
$em->persist($user);
$em->flush();
$io->success('Password for %s has been changed', $email);
return 0;
}
}