1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Zikula package. |
7
|
|
|
* |
8
|
|
|
* Copyright Zikula Foundation - https://ziku.la/ |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Zikula\ZAuthModule\Command; |
15
|
|
|
|
16
|
|
|
use Symfony\Component\Console\Command\Command; |
17
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
18
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
19
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
20
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
21
|
|
|
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface; |
22
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
23
|
|
|
use Zikula\UsersModule\Entity\RepositoryInterface\UserRepositoryInterface; |
24
|
|
|
use Zikula\ZAuthModule\Entity\RepositoryInterface\AuthenticationMappingRepositoryInterface; |
25
|
|
|
|
26
|
|
|
class ZikulaZauthEditCommand extends Command |
27
|
|
|
{ |
28
|
|
|
protected static $defaultName = 'zikula:zauth:edit'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var AuthenticationMappingRepositoryInterface |
32
|
|
|
*/ |
33
|
|
|
private $authenticationMappingRepository; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var UserRepositoryInterface |
37
|
|
|
*/ |
38
|
|
|
private $userRepository; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var TranslatorInterface |
42
|
|
|
*/ |
43
|
|
|
private $translator; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var EncoderFactoryInterface |
47
|
|
|
*/ |
48
|
|
|
private $encoderFactory; |
49
|
|
|
|
50
|
|
|
public function __construct( |
51
|
|
|
AuthenticationMappingRepositoryInterface $authenticationMappingRepository, |
52
|
|
|
UserRepositoryInterface $userRepository, |
53
|
|
|
TranslatorInterface $translator, |
54
|
|
|
EncoderFactoryInterface $encoderFactory |
55
|
|
|
) { |
56
|
|
|
parent::__construct(); |
57
|
|
|
$this->authenticationMappingRepository = $authenticationMappingRepository; |
58
|
|
|
$this->userRepository = $userRepository; |
59
|
|
|
$this->translator = $translator; |
60
|
|
|
$this->encoderFactory = $encoderFactory; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
protected function configure() |
64
|
|
|
{ |
65
|
|
|
$this |
66
|
|
|
->addArgument('id', InputArgument::REQUIRED, 'uid, uname or email') |
67
|
|
|
->setDescription('Edit a ZAuth user mapping') |
68
|
|
|
; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
72
|
|
|
{ |
73
|
|
|
$io = new SymfonyStyle($input, $output); |
74
|
|
|
$id = $input->getArgument('id'); |
75
|
|
|
if (is_numeric($id)) { |
76
|
|
|
$criteria = ['uid' => (int) $id]; |
77
|
|
|
} elseif (filter_var($id, FILTER_VALIDATE_EMAIL)) { |
78
|
|
|
$criteria = ['email' => $id]; |
79
|
|
|
} else { |
80
|
|
|
$criteria = ['uname' => $id]; |
81
|
|
|
} |
82
|
|
|
try { |
83
|
|
|
$mapping = $this->authenticationMappingRepository->findOneBy($criteria); |
84
|
|
|
} catch (\Exception $e) { |
85
|
|
|
$io->error($this->translator->trans('Found more than one user by that criteria. Try a different criteria (uid works best).')); |
86
|
|
|
|
87
|
|
|
return 1; |
88
|
|
|
} |
89
|
|
|
if (empty($mapping)) { |
90
|
|
|
$io->error($this->translator->trans('Found zero users by that criteria. Try a different criteria (uid works best).')); |
91
|
|
|
|
92
|
|
|
return 1; |
93
|
|
|
} |
94
|
|
|
$choice = $io->choice($this->translator->trans('Value to change?'), [ |
95
|
|
|
$this->translator->trans('password'), |
96
|
|
|
$this->translator->trans('email'), |
97
|
|
|
$this->translator->trans('username'), |
98
|
|
|
], $this->translator->trans('password')); |
99
|
|
|
|
100
|
|
|
switch ($choice) { |
101
|
|
|
case 'password': |
102
|
|
|
$value = $io->askHidden($this->translator->trans('New value for %choice%?', ['%choice%' => $choice])); |
103
|
|
|
$mapping->setPass($this->encoderFactory->getEncoder($mapping)->encodePassword($value, null)); |
104
|
|
|
break; |
105
|
|
|
default: |
106
|
|
|
$value = $io->ask($this->translator->trans('New value for %choice%?', ['%choice%' => $choice])); |
107
|
|
|
$methods = ['email' => 'setEmail', 'username' => 'setUname']; |
108
|
|
|
$mapping->{$methods[$choice]}($value); |
109
|
|
|
$userEntity = $this->userRepository->findOneBy($criteria); |
110
|
|
|
$userEntity->{$methods[$choice]}($value); |
111
|
|
|
$this->userRepository->persistAndFlush($userEntity); |
|
|
|
|
112
|
|
|
break; |
113
|
|
|
} |
114
|
|
|
$this->authenticationMappingRepository->persistAndFlush($mapping); |
115
|
|
|
$io->success($this->translator->trans('The %choice% has been changed.', ['%choice%' => $choice])); |
116
|
|
|
|
117
|
|
|
return 0; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|