Completed
Push — master ( e3be9e...9444a3 )
by Morris
73:40 queued 52:27
created

State::execute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 2
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
/**
6
 * @copyright 2018 Christoph Wurst <[email protected]>
7
 *
8
 * @author 2018 Christoph Wurst <[email protected]>
9
 *
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OC\Core\Command\TwoFactorAuth;
28
29
use OC\Core\Command\Base;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, OC\Core\Command\TwoFactorAuth\Base.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
30
use OCP\Authentication\TwoFactorAuth\IRegistry;
31
use OCP\IUserManager;
32
use Symfony\Component\Console\Input\InputArgument;
33
use Symfony\Component\Console\Input\InputInterface;
34
use Symfony\Component\Console\Output\OutputInterface;
35
36
class State extends Base {
37
38
	/** @var IRegistry */
39
	private $registry;
40
41
	/** @var IUserManager */
42
	private $userManager;
43
44
	public function __construct(IRegistry $registry, IUserManager $userManager) {
45
		parent::__construct('twofactorauth:state');
46
47
		$this->registry = $registry;
48
		$this->userManager = $userManager;
49
	}
50
51
	protected function configure() {
52
		parent::configure();
53
54
		$this->setName('twofactorauth:state');
55
		$this->setDescription('Get the two-factor authentication (2FA) state of a user');
56
		$this->addArgument('uid', InputArgument::REQUIRED);
57
	}
58
59
	protected function execute(InputInterface $input, OutputInterface $output) {
60
		$uid = $input->getArgument('uid');
61
		$user = $this->userManager->get($uid);
62
		if (is_null($user)) {
63
			$output->writeln("<error>Invalid UID</error>");
64
			return;
65
		}
66
67
		$providerStates = $this->registry->getProviderStates($user);
68
		$filtered = $this->filterEnabledDisabledUnknownProviders($providerStates);
69
		list ($enabled, $disabled) = $filtered;
70
71
		if (!empty($enabled)) {
72
			$output->writeln("Two-factor authentication is enabled for user $uid");
73
		} else {
74
			$output->writeln("Two-factor authentication is not enabled for user $uid");
75
		}
76
77
		$output->writeln("");
78
		$this->printProviders("Enabled providers", $enabled, $output);
79
		$this->printProviders("Disabled providers", $disabled, $output);
80
	}
81
82
	private function filterEnabledDisabledUnknownProviders(array $providerStates): array {
83
		$enabled = [];
84
		$disabled = [];
85
86
		foreach ($providerStates as $providerId => $isEnabled) {
87
			if ($isEnabled) {
88
				$enabled[] = $providerId;
89
			} else {
90
				$disabled[] = $providerId;
91
			}
92
		}
93
94
		return [$enabled, $disabled];
95
	}
96
97
	private function printProviders(string $title, array $providers,
98
		OutputInterface $output) {
99
		if (empty($providers)) {
100
			// Ignore and don't print anything
101
			return;
102
		}
103
104
		$output->writeln($title . ":");
105
		foreach ($providers as $provider) {
106
			$output->writeln("- " . $provider);
107
		}
108
	}
109
110
}
111