Completed
Push — master ( 8ede3f...f9e201 )
by Roeland
14:09 queued 10s
created

Security::getPriority()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Arthur Schiwon <[email protected]>
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OC\Settings\Personal;
25
26
27
use function array_filter;
28
use function array_map;
29
use function is_null;
30
use OC\Authentication\TwoFactorAuth\Manager as TwoFactorManager;
31
use OC\Authentication\TwoFactorAuth\ProviderLoader;
32
use OCP\AppFramework\Http\TemplateResponse;
33
use OCP\Authentication\TwoFactorAuth\IProvider;
34
use OCP\Authentication\TwoFactorAuth\IProvidesPersonalSettings;
35
use OCP\IUserManager;
36
use OCP\IUserSession;
37
use OCP\Settings\ISettings;
38
39
class Security implements ISettings {
40
41
	/** @var IUserManager */
42
	private $userManager;
43
44
	/** @var TwoFactorManager */
45
	private $twoFactorManager;
46
47
	/** @var ProviderLoader */
48
	private $providerLoader;
49
50
	/** @var IUserSession */
51
	private $userSession;
52
53
54
	public function __construct(IUserManager $userManager,
55
								TwoFactorManager $providerManager,
56
								ProviderLoader $providerLoader,
57
								IUserSession $userSession) {
58
		$this->userManager = $userManager;
59
		$this->twoFactorManager = $providerManager;
60
		$this->providerLoader = $providerLoader;
61
		$this->userSession = $userSession;
62
	}
63
64
	/**
65
	 * @return TemplateResponse returns the instance with all parameters set, ready to be rendered
66
	 * @since 9.1
67
	 */
68
	public function getForm() {
69
		$user = $this->userManager->get(\OC_User::getUser());
70
		$passwordChangeSupported = false;
71
		if ($user !== null) {
72
			$passwordChangeSupported = $user->canChangePassword();
73
		}
74
75
		return new TemplateResponse('settings', 'settings/personal/security', [
76
			'passwordChangeSupported' => $passwordChangeSupported,
77
			'twoFactorProviderData' => $this->getTwoFactorProviderData(),
78
		]);
79
	}
80
81
	/**
82
	 * @return string the section ID, e.g. 'sharing'
83
	 * @since 9.1
84
	 */
85
	public function getSection() {
86
		return 'security';
87
	}
88
89
	/**
90
	 * @return int whether the form should be rather on the top or bottom of
91
	 * the admin section. The forms are arranged in ascending order of the
92
	 * priority values. It is required to return a value between 0 and 100.
93
	 *
94
	 * E.g.: 70
95
	 * @since 9.1
96
	 */
97
	public function getPriority() {
98
		return 10;
99
	}
100
101
	private function getTwoFactorProviderData(): array {
102
		$user = $this->userSession->getUser();
103
		if (is_null($user)) {
104
			// Actually impossible, but still …
105
			return [];
106
		}
107
108
		return [
109
			'isEnabled' => $this->twoFactorManager->isTwoFactorAuthenticated($user),
110
			'providers' => array_map(function (IProvidesPersonalSettings $provider) use ($user) {
111
				return [
112
					'provider' => $provider,
113
					'settings' => $provider->getPersonalSettings($user)
114
				];
115
			}, array_filter($this->providerLoader->getProviders($user), function (IProvider $provider) {
116
				return $provider instanceof IProvidesPersonalSettings;
117
			}))
118
		];
119
	}
120
}
121