|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* SPDX-FileCopyrightText: 2024 Christoph Wurst <[email protected]> |
|
7
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace OCA\TwoFactorGateway\Command; |
|
11
|
|
|
|
|
12
|
|
|
use OCA\TwoFactorGateway\Exception\InvalidProviderException; |
|
13
|
|
|
use OCA\TwoFactorGateway\Provider\Gateway\AGateway; |
|
14
|
|
|
use OCA\TwoFactorGateway\Provider\Gateway\Factory; |
|
15
|
|
|
use Symfony\Component\Console\Command\Command; |
|
16
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper; |
|
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\Question\ChoiceQuestion; |
|
21
|
|
|
|
|
22
|
|
|
class Configure extends Command { |
|
23
|
|
|
/** @var AGateway[] */ |
|
24
|
|
|
private array $gateways = []; |
|
25
|
|
|
|
|
26
|
1 |
|
public function __construct( |
|
27
|
|
|
private Factory $gatewayFactory, |
|
28
|
|
|
) { |
|
29
|
1 |
|
parent::__construct('twofactorauth:gateway:configure'); |
|
30
|
|
|
|
|
31
|
1 |
|
$fqcnList = $this->gatewayFactory->getFqcnList(); |
|
32
|
1 |
|
foreach ($fqcnList as $fqcn) { |
|
33
|
1 |
|
$gateway = $this->gatewayFactory->get($fqcn); |
|
34
|
1 |
|
$this->gateways[$gateway->getProviderId()] = $gateway; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
1 |
|
$this->addArgument( |
|
38
|
1 |
|
'gateway', |
|
39
|
1 |
|
InputArgument::OPTIONAL, |
|
40
|
1 |
|
'The name of the gateway: ' . implode(', ', array_keys($this->gateways)) |
|
41
|
1 |
|
); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
1 |
|
#[\Override] |
|
45
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) { |
|
46
|
1 |
|
$gatewayName = strtolower((string)$input->getArgument('gateway')); |
|
47
|
1 |
|
if (!array_key_exists($gatewayName, $this->gateways)) { |
|
48
|
1 |
|
$helper = new QuestionHelper(); |
|
49
|
1 |
|
$choiceQuestion = new ChoiceQuestion('Please choose a provider:', array_keys($this->gateways)); |
|
50
|
1 |
|
$selectedIndex = $helper->ask($input, $output, $choiceQuestion); |
|
51
|
1 |
|
$gateway = $this->gateways[$selectedIndex]; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
try { |
|
55
|
1 |
|
return $gateway->cliConfigure($input, $output); |
|
|
|
|
|
|
56
|
|
|
} catch (InvalidProviderException $e) { |
|
57
|
|
|
$output->writeln("<error>Invalid gateway $gatewayName</error>"); |
|
58
|
|
|
return 1; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|