1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OAuth\Command; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\Collection; |
6
|
|
|
use Exception; |
7
|
|
|
use OAuth\Client; |
8
|
|
|
use OAuth\Repository\ScopeRepository; |
9
|
|
|
use OAuth\Scope; |
10
|
|
|
use OAuth\Service\ClientService; |
11
|
|
|
use Symfony\Component\Console\Command\Command; |
12
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper; |
13
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
14
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
15
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
16
|
|
|
use Symfony\Component\Console\Question\ChoiceQuestion; |
17
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
18
|
|
|
use Symfony\Component\Console\Question\Question; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class ClientCommand |
22
|
|
|
* @package OAuth\Command |
23
|
|
|
*/ |
24
|
|
|
class ClientScopeCommand extends Command |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var ClientService $clientService |
28
|
|
|
*/ |
29
|
|
|
private $clientService; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var ScopeRepository $scopeRepository |
33
|
|
|
*/ |
34
|
|
|
private $scopeRepository; |
35
|
|
|
|
36
|
|
|
/** @var QuestionHelper $helper */ |
37
|
|
|
private $helper; |
|
|
|
|
38
|
|
|
|
39
|
|
|
public function __construct(ClientService $clientService, ScopeRepository $scopeRepository) |
40
|
|
|
{ |
41
|
|
|
$this->clientService = $clientService; |
42
|
|
|
$this->scopeRepository = $scopeRepository; |
43
|
|
|
parent::__construct('client:scope'); |
44
|
|
|
$this->addArgument('operation', InputArgument::REQUIRED, 'list, add, or remove.'); |
45
|
|
|
$this->addArgument('client', InputArgument::OPTIONAL, 'The client identifier.'); |
46
|
|
|
$this->addArgument('scope', InputArgument::OPTIONAL, 'The scope name when adding or removing.'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* configure options |
51
|
|
|
*/ |
52
|
|
|
protected function configure() |
53
|
|
|
{ |
54
|
|
|
$this->setDescription('Add, remove, or list scopes for each client.'); |
55
|
|
|
$this->setHelp('Client scope administration'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param InputInterface $input |
60
|
|
|
* @param OutputInterface $output |
61
|
|
|
* @return int|void|null |
62
|
|
|
* @throws Exception |
63
|
|
|
*/ |
64
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
65
|
|
|
{ |
66
|
|
|
$output->writeln(' '); |
67
|
|
|
$output->writeln('Client scope administration'); |
68
|
|
|
$operation = $input->getArgument('operation'); |
69
|
|
|
switch ($operation) { |
70
|
|
|
case 'list'; |
71
|
|
|
$this->listScopes($input, $output); |
72
|
|
|
break; |
73
|
|
|
case 'add'; |
74
|
|
|
$this->addScope($input, $output); |
75
|
|
|
break; |
76
|
|
|
case 'remove'; |
77
|
|
|
$this->removeScope($input, $output); |
78
|
|
|
break; |
79
|
|
|
} |
80
|
|
|
$output->writeln(' '); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param InputInterface $input |
85
|
|
|
* @param string $argName |
86
|
|
|
* @return string|string[]|null |
87
|
|
|
* @throws Exception |
88
|
|
|
*/ |
89
|
|
|
private function getArgOrGetUpset(InputInterface $input, string $argName) |
90
|
|
|
{ |
91
|
|
|
$value = $input->getArgument($argName); |
92
|
|
|
if (!$value) { |
93
|
|
|
throw new Exception('No ' . $argName . ' provided'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $value; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param InputInterface $input |
101
|
|
|
* @param OutputInterface $output |
102
|
|
|
*/ |
103
|
|
|
private function listScopes(InputInterface $input, OutputInterface $output) |
104
|
|
|
{ |
105
|
|
|
$clientId = $input->getArgument('client'); |
106
|
|
|
|
107
|
|
|
$client = $this->fetchClient($output, $clientId); |
|
|
|
|
108
|
|
|
if (!$client instanceof Client) { |
|
|
|
|
109
|
|
|
$output->writeln('No client found'); |
110
|
|
|
return; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$scopes = $client->getScopes(); |
114
|
|
|
$this->outputScopes($output, $client, $scopes); |
115
|
|
|
|
116
|
|
|
return; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param OutputInterface $output |
121
|
|
|
* @param Collection $scopes |
122
|
|
|
*/ |
123
|
|
|
private function outputScopes(OutputInterface $output, Client $client, Collection $scopes) |
124
|
|
|
{ |
125
|
|
|
$output->writeln('Listing scopes for ' . $client->getName() . '.'); |
126
|
|
|
|
127
|
|
|
if ($scopes->count()) { |
128
|
|
|
/** @var Scope $scope */ |
129
|
|
|
foreach ($scopes as $scope) { |
130
|
|
|
$output->writeln(' - ' . $scope->getIdentifier()); |
131
|
|
|
} |
132
|
|
|
} else { |
133
|
|
|
$output->writeln('No scopes set for ' . $client->getName() . '.'); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param string $id |
139
|
|
|
* @return Client |
140
|
|
|
*/ |
141
|
|
|
private function fetchClient(OutputInterface $output, string $id) |
142
|
|
|
{ |
143
|
|
|
$output->writeln('Fetching client ' . $id .'...'); |
144
|
|
|
/** @var Client $client */ |
145
|
|
|
$client = $this->clientService |
146
|
|
|
->getClientRepository() |
147
|
|
|
->getClientEntity( $id, null, null, false); |
148
|
|
|
|
149
|
|
|
return $client; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param InputInterface $input |
154
|
|
|
* @param OutputInterface $output |
155
|
|
|
* @throws Exception |
156
|
|
|
*/ |
157
|
|
|
private function addScope(InputInterface $input, OutputInterface $output) |
158
|
|
|
{ |
159
|
|
|
$clientId = $input->getArgument('client'); |
160
|
|
|
$scopeId = $this->getArgOrGetUpset($input, 'scope'); |
161
|
|
|
|
162
|
|
|
$client = $this->fetchClient($output, $clientId); |
|
|
|
|
163
|
|
|
if (!$client instanceof Client) { |
|
|
|
|
164
|
|
|
$output->writeln('No client found'); |
165
|
|
|
return; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$scope = $this->scopeRepository->getScopeEntityByIdentifier($scopeId); |
|
|
|
|
169
|
|
|
if (!$scope instanceof Scope) { |
|
|
|
|
170
|
|
|
$output->writeln('No scope found.'); |
171
|
|
|
return; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
$output->writeln('Adding '. $scopeId . ' scope to ' . $client->getName() . '...'); |
|
|
|
|
175
|
|
|
$client->getScopes()->add($scope); |
176
|
|
|
$this->clientService->getClientRepository()->save($client); |
177
|
|
|
$output->writeln($scopeId . ' scope added.'); |
178
|
|
|
|
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param InputInterface $input |
183
|
|
|
* @param OutputInterface $output |
184
|
|
|
* @throws Exception |
185
|
|
|
*/ |
186
|
|
|
private function removeScope(InputInterface $input, OutputInterface $output) |
187
|
|
|
{ |
188
|
|
|
$clientId = $input->getArgument('client'); |
189
|
|
|
$scopeId = $this->getArgOrGetUpset($input, 'scope'); |
190
|
|
|
|
191
|
|
|
$client = $this->fetchClient($output, $clientId); |
|
|
|
|
192
|
|
|
if (!$client instanceof Client) { |
|
|
|
|
193
|
|
|
$output->writeln('No client found'); |
194
|
|
|
return; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
$scopes = $client->getScopes(); |
198
|
|
|
/** @var Scope $scope */ |
199
|
|
|
foreach ($scopes as $key => $scope) { |
200
|
|
|
if ($scope->getIdentifier() == $scopeId) { |
201
|
|
|
$scopes->remove($key); |
202
|
|
|
break; |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
$this->clientService->getClientRepository()->save($client); |
207
|
|
|
$output->writeln($scopeId . ' scope removed.'); |
208
|
|
|
} |
209
|
|
|
} |