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
|
|
|
private array $ids = []; |
24
|
|
|
|
25
|
|
|
public function __construct( |
26
|
|
|
private Factory $gatewayFactory, |
27
|
|
|
) { |
28
|
|
|
parent::__construct('twofactorauth:gateway:configure'); |
29
|
|
|
|
30
|
|
|
$fqcn = $this->gatewayFactory->getFqcnList(); |
31
|
|
|
foreach ($fqcn as $fqcn) { |
32
|
|
|
$this->ids[] = $fqcn::getProviderId(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$this->addArgument( |
36
|
|
|
'gateway', |
37
|
|
|
InputArgument::OPTIONAL, |
38
|
|
|
'The name of the gateway: ' . implode(', ', $this->ids) |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
#[\Override] |
43
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) { |
44
|
|
|
$gatewayName = strtolower((string)$input->getArgument('gateway')); |
45
|
|
|
if (!in_array($gatewayName, $this->ids, true)) { |
46
|
|
|
$helper = new QuestionHelper(); |
47
|
|
|
$choiceQuestion = new ChoiceQuestion('Please choose a provider:', $this->ids); |
48
|
|
|
$gatewayName = $helper->ask($input, $output, $choiceQuestion); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
try { |
52
|
|
|
/** @var AGateway */ |
53
|
|
|
$gateway = $this->gatewayFactory->get($gatewayName); |
54
|
|
|
return $gateway->cliConfigure($input, $output); |
55
|
|
|
} catch (InvalidProviderException $e) { |
56
|
|
|
$output->writeln("<error>Invalid gateway $gatewayName</error>"); |
57
|
|
|
return 1; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|