|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jafar\Bundle\GuardedAuthenticationBundle\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
|
6
|
|
|
use Symfony\Component\Console\Command\Command; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
9
|
|
|
use Symfony\Component\Console\Question\Question; |
|
10
|
|
|
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; |
|
11
|
|
|
use \Exception; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class PasswordEncryptorCommand. |
|
16
|
|
|
* |
|
17
|
|
|
* @author Jafar Jabr <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class PasswordEncryptorCommand extends Command |
|
20
|
|
|
{ |
|
21
|
|
|
protected static $defaultName = 'jafar:encrypt-password'; |
|
22
|
|
|
|
|
23
|
|
|
/** @var UserPasswordEncoderInterface $encoder */ |
|
24
|
|
|
private $encoder; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct(UserPasswordEncoderInterface $encoder) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->encoder = $encoder; |
|
29
|
|
|
parent::__construct(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* {@inheritdoc} |
|
34
|
|
|
*/ |
|
35
|
|
|
protected function configure() |
|
36
|
|
|
{ |
|
37
|
|
|
$this |
|
38
|
|
|
->setName('jafar:encrypt-password') |
|
39
|
|
|
->setDescription('encrypt a password for first use before to have the regisration') |
|
40
|
|
|
->setHelp('encrypt a password for first use before to have the regisration'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* {@inheritdoc} |
|
45
|
|
|
*/ |
|
46
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
47
|
|
|
{ |
|
48
|
|
|
$io = new SymfonyStyle($input, $output); |
|
49
|
|
|
$io->title('Thank you for using Jafar:GuardedAuthenticationBundle'); |
|
50
|
|
|
$question = new Question('Please enter password to be encrypted ? '); |
|
51
|
|
|
$question->setHiddenFallback(false); |
|
52
|
|
|
$question->setValidator(function ($answer) { |
|
53
|
|
|
if (strlen($answer) < 6) { |
|
54
|
|
|
throw new RuntimeException('The password can not be less than 6 characters'); |
|
|
|
|
|
|
55
|
|
|
} elseif (strlen($answer) > 50) { |
|
56
|
|
|
throw new RuntimeException('The password can not be more than 50 characters'); |
|
57
|
|
|
} |
|
58
|
|
|
return $answer; |
|
59
|
|
|
}); |
|
60
|
|
|
$helper = $this->getHelper('question'); |
|
61
|
|
|
$plainPassword = $helper->ask($input, $output, $question); |
|
62
|
|
|
$user = null; |
|
63
|
|
|
if(class_exists('\App\Entity\User')){ |
|
64
|
|
|
$user = new \App\Entity\User(); |
|
|
|
|
|
|
65
|
|
|
} elseif(class_exists('\App\Entity\Users')){ |
|
66
|
|
|
$user = new \App\Entity\Users(); |
|
|
|
|
|
|
67
|
|
|
}else{ |
|
68
|
|
|
throw new Exception('No User Entity found, searched in \'App\Entity\User\' and \'App\Entity\Users\''); |
|
69
|
|
|
} |
|
70
|
|
|
$enc = $this->encoder->encodePassword( |
|
71
|
|
|
$user, |
|
72
|
|
|
$plainPassword |
|
73
|
|
|
); |
|
74
|
|
|
$output->writeln('<info>'.$enc.'</info>'); |
|
75
|
|
|
|
|
76
|
|
|
return 0; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|