Completed
Push — master ( 34c17b...548f4e )
by Roeland
63:38 queued 45:49
created

ProviderManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 4
rs 10
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\Authentication\TwoFactorAuth;
28
29
use OC\Authentication\Exceptions\InvalidProviderException;
30
use OCP\Authentication\TwoFactorAuth\IActivatableByAdmin;
31
use OCP\Authentication\TwoFactorAuth\IDeactivableByAdmin;
32
use OCP\Authentication\TwoFactorAuth\IDeactivatableByAdmin;
33
use OCP\Authentication\TwoFactorAuth\IProvider;
34
use OCP\Authentication\TwoFactorAuth\IRegistry;
35
use OCP\IUser;
36
37
class ProviderManager {
38
39
	/** @var ProviderLoader */
40
	private $providerLoader;
41
42
	/** @var IRegistry */
43
	private $providerRegistry;
44
45
	public function __construct(ProviderLoader $providerLoader, IRegistry $providerRegistry) {
46
		$this->providerLoader = $providerLoader;
47
		$this->providerRegistry = $providerRegistry;
48
	}
49
50
	private function getProvider(string $providerId, IUser $user): IProvider {
51
		$providers = $this->providerLoader->getProviders($user);
52
53
		if (!isset($providers[$providerId])) {
54
			throw new InvalidProviderException($providerId);
55
		}
56
57
		return $providers[$providerId];
58
	}
59
60
	/**
61
	 * Try to enable the provider with the given id for the given user
62
	 *
63
	 * @param IUser $user
64
	 *
65
	 * @return bool whether the provider supports this operation
66
	 */
67
	public function tryEnableProviderFor(string $providerId, IUser $user): bool {
68
		$provider = $this->getProvider($providerId, $user);
69
70
		if ($provider instanceof IActivatableByAdmin) {
71
			$provider->enableFor($user);
72
			$this->providerRegistry->enableProviderFor($provider, $user);
73
			return true;
74
		} else {
75
			return false;
76
		}
77
	}
78
79
	/**
80
	 * Try to disable the provider with the given id for the given user
81
	 *
82
	 * @param IUser $user
83
	 *
84
	 * @return bool whether the provider supports this operation
85
	 */
86
	public function tryDisableProviderFor(string $providerId, IUser $user): bool {
87
		$provider = $this->getProvider($providerId, $user);
88
89
		if ($provider instanceof IDeactivatableByAdmin) {
90
			$provider->disableFor($user);
91
			$this->providerRegistry->disableProviderFor($provider, $user);
92
			return true;
93
		} else {
94
			return false;
95
		}
96
	}
97
98
}
99