|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Command; |
|
4
|
|
|
|
|
5
|
|
|
use App\Repository\UserRepositoryInterface; |
|
6
|
|
|
use Shapecode\Bundle\CronBundle\Annotation\CronJob; |
|
7
|
|
|
use Symfony\Component\Console\Command\Command; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
10
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
|
11
|
|
|
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @CronJob("*\/2 * * * * ") |
|
15
|
|
|
*/ |
|
16
|
|
|
class ProvisionUserCommand extends Command { |
|
17
|
|
|
|
|
18
|
|
|
private $numberOfUsers; |
|
19
|
|
|
private $userRepository; |
|
20
|
|
|
private $passwordEncoder; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct(int $numberOfUsers, UserPasswordEncoderInterface $passwordEncoder, UserRepositoryInterface $userRepository, string $name = null) { |
|
23
|
|
|
parent::__construct($name); |
|
24
|
|
|
|
|
25
|
|
|
$this->numberOfUsers = $numberOfUsers; |
|
26
|
|
|
$this->userRepository = $userRepository; |
|
27
|
|
|
$this->passwordEncoder = $passwordEncoder; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function configure() { |
|
31
|
|
|
$this |
|
32
|
|
|
->setName('app:user:provision') |
|
33
|
|
|
->setDescription('Provisions the next N users'); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function execute(InputInterface $input, OutputInterface $output) { |
|
37
|
|
|
$io = new SymfonyStyle($input, $output); |
|
38
|
|
|
|
|
39
|
|
|
$users = $this->userRepository->findNextNonProvisionedUsers($this->numberOfUsers); |
|
40
|
|
|
|
|
41
|
|
|
foreach($users as $user) { |
|
42
|
|
|
$user->setPassword( |
|
43
|
|
|
$this->passwordEncoder->encodePassword($user, $user->getPassword()) |
|
44
|
|
|
); |
|
45
|
|
|
|
|
46
|
|
|
$user->setIsProvisioned(true); |
|
47
|
|
|
$this->userRepository->persist($user); |
|
48
|
|
|
|
|
49
|
|
|
$io->text(sprintf('User %s provisioned.', $user->getUsername())); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$io->success(sprintf('Successfully provisioned %d user(s).', count($users))); |
|
53
|
|
|
|
|
54
|
|
|
return 0; |
|
55
|
|
|
} |
|
56
|
|
|
} |