1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2020 SURFnet bv |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Surfnet\StepupMiddleware\MiddlewareBundle\Console\Command; |
20
|
|
|
|
21
|
|
|
use Exception; |
22
|
|
|
use Rhumsaa\Uuid\Uuid; |
23
|
|
|
use Surfnet\Stepup\Identity\Value\Institution; |
24
|
|
|
use Surfnet\Stepup\Identity\Value\NameId; |
25
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\CreateIdentityCommand; |
26
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
27
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
28
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
29
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken; |
30
|
|
|
|
31
|
|
|
final class BootstrapIdentityCommand extends AbstractBootstrapCommand |
32
|
|
|
{ |
33
|
|
View Code Duplication |
protected function configure() |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
$this |
36
|
|
|
->setDescription('Creates an identity') |
37
|
|
|
->addArgument('name-id', InputArgument::REQUIRED, 'The NameID of the identity to create') |
38
|
|
|
->addArgument('institution', InputArgument::REQUIRED, 'The institution of the identity to create') |
39
|
|
|
->addArgument('common-name', InputArgument::REQUIRED, 'The Common Name of the identity to create') |
40
|
|
|
->addArgument('email', InputArgument::REQUIRED, 'The e-mail address of the identity to create') |
41
|
|
|
->addArgument('preferred-locale', InputArgument::REQUIRED, 'The preferred locale of the identity to create') |
42
|
|
|
->addArgument('actor-id', InputArgument::REQUIRED, 'The id of the vetting actor'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
46
|
|
|
{ |
47
|
|
|
$this->tokenStorage->setToken( |
48
|
|
|
new AnonymousToken('cli.bootstrap-identity-with-sms-token', 'cli', ['ROLE_SS']) |
|
|
|
|
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
$nameId = new NameId($input->getArgument('name-id')); |
52
|
|
|
$institutionText = $input->getArgument('institution'); |
53
|
|
|
$institution = new Institution($institutionText); |
|
|
|
|
54
|
|
|
$commonName = $input->getArgument('common-name'); |
55
|
|
|
$email = $input->getArgument('email'); |
56
|
|
|
$preferredLocale = $input->getArgument('preferred-locale'); |
57
|
|
|
$actorId = $input->getArgument('actor-id'); |
58
|
|
|
|
59
|
|
|
$this->enrichEventMetadata($actorId); |
60
|
|
|
|
61
|
|
|
$output->writeln(sprintf('<comment>Adding an identity named: %s</comment>', $commonName)); |
62
|
|
View Code Duplication |
if ($this->tokenBootstrapService->hasIdentityWithNameIdAndInstitution($nameId, $institution)) { |
|
|
|
|
63
|
|
|
$output->writeln( |
64
|
|
|
sprintf( |
65
|
|
|
'<error>An identity with name ID "%s" from institution "%s" already exists</error>', |
66
|
|
|
$nameId->getNameId(), |
67
|
|
|
$institution->getInstitution() |
68
|
|
|
) |
69
|
|
|
); |
70
|
|
|
return; |
71
|
|
|
} |
72
|
|
|
try { |
73
|
|
|
$this->beginTransaction(); |
74
|
|
|
$output->writeln('<info>Creating a new identity</info>'); |
75
|
|
|
$identity = $this->createIdentity($institution, $nameId, $commonName, $email, $preferredLocale); |
76
|
|
|
$this->finishTransaction(); |
77
|
|
|
} catch (Exception $e) { |
78
|
|
|
$output->writeln( |
79
|
|
|
sprintf( |
80
|
|
|
'<error>An Error occurred when trying to bootstrap the identity: "%s"</error>', |
81
|
|
|
$e->getMessage() |
82
|
|
|
) |
83
|
|
|
); |
84
|
|
|
$this->rollback(); |
85
|
|
|
throw $e; |
86
|
|
|
} |
87
|
|
|
$output->writeln( |
88
|
|
|
sprintf('<info>Successfully created identity with UUID %s</info>', $identity->id) |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
protected function createIdentity( |
93
|
|
|
Institution $institution, |
94
|
|
|
NameId $nameId, |
95
|
|
|
$commonName, |
96
|
|
|
$email, |
97
|
|
|
$preferredLocale |
98
|
|
|
) { |
99
|
|
|
$identity = new CreateIdentityCommand(); |
100
|
|
|
$identity->UUID = (string)Uuid::uuid4(); |
101
|
|
|
$identity->id = (string)Uuid::uuid4(); |
102
|
|
|
$identity->institution = $institution->getInstitution(); |
103
|
|
|
$identity->nameId = $nameId->getNameId(); |
104
|
|
|
$identity->commonName = $commonName; |
105
|
|
|
$identity->email = $email; |
106
|
|
|
$identity->preferredLocale = $preferredLocale; |
107
|
|
|
$this->process($identity); |
108
|
|
|
|
109
|
|
|
return $identity; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.