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\Exceptions\InvalidTokenException; |
31
|
|
|
use OC\Authentication\Token\INamedToken; |
32
|
|
|
use OC\Authentication\Token\IProvider as IAuthTokenProvider; |
33
|
|
|
use OC\Authentication\Token\IToken; |
34
|
|
|
use OC\Authentication\TwoFactorAuth\Manager as TwoFactorManager; |
35
|
|
|
use OC\Authentication\TwoFactorAuth\ProviderLoader; |
36
|
|
|
use OCP\AppFramework\Http\TemplateResponse; |
37
|
|
|
use OCP\Authentication\TwoFactorAuth\IProvider; |
38
|
|
|
use OCP\Authentication\TwoFactorAuth\IProvidesPersonalSettings; |
39
|
|
|
use OCP\IInitialStateService; |
40
|
|
|
use OCP\ISession; |
41
|
|
|
use OCP\IUserManager; |
42
|
|
|
use OCP\IUserSession; |
43
|
|
|
use OCP\Session\Exceptions\SessionNotAvailableException; |
44
|
|
|
use OCP\Settings\ISettings; |
45
|
|
|
|
46
|
|
|
class Security implements ISettings { |
47
|
|
|
|
48
|
|
|
/** @var IUserManager */ |
49
|
|
|
private $userManager; |
50
|
|
|
|
51
|
|
|
/** @var TwoFactorManager */ |
52
|
|
|
private $twoFactorManager; |
53
|
|
|
|
54
|
|
|
/** @var IAuthTokenProvider */ |
55
|
|
|
private $tokenProvider; |
56
|
|
|
|
57
|
|
|
/** @var ProviderLoader */ |
58
|
|
|
private $providerLoader; |
59
|
|
|
|
60
|
|
|
/** @var IUserSession */ |
61
|
|
|
private $userSession; |
62
|
|
|
|
63
|
|
|
/** @var ISession */ |
64
|
|
|
private $session; |
65
|
|
|
|
66
|
|
|
/** @var IInitialStateService */ |
67
|
|
|
private $initialStateService; |
68
|
|
|
/** |
69
|
|
|
* @var string|null |
70
|
|
|
*/ |
71
|
|
|
private $uid; |
72
|
|
|
|
73
|
|
|
public function __construct(IUserManager $userManager, |
74
|
|
|
TwoFactorManager $providerManager, |
75
|
|
|
IAuthTokenProvider $tokenProvider, |
76
|
|
|
ProviderLoader $providerLoader, |
77
|
|
|
IUserSession $userSession, |
78
|
|
|
ISession $session, |
79
|
|
|
IInitialStateService $initialStateService, |
80
|
|
|
?string $UserId) { |
81
|
|
|
$this->userManager = $userManager; |
82
|
|
|
$this->twoFactorManager = $providerManager; |
83
|
|
|
$this->tokenProvider = $tokenProvider; |
84
|
|
|
$this->providerLoader = $providerLoader; |
85
|
|
|
$this->userSession = $userSession; |
86
|
|
|
$this->session = $session; |
87
|
|
|
$this->initialStateService = $initialStateService; |
88
|
|
|
$this->uid = $UserId; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return TemplateResponse returns the instance with all parameters set, ready to be rendered |
93
|
|
|
* @since 9.1 |
94
|
|
|
*/ |
95
|
|
|
public function getForm() { |
96
|
|
|
$user = $this->userManager->get($this->uid); |
97
|
|
|
$passwordChangeSupported = false; |
98
|
|
|
if ($user !== null) { |
99
|
|
|
$passwordChangeSupported = $user->canChangePassword(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$this->initialStateService->provideInitialState( |
103
|
|
|
'settings', |
104
|
|
|
'app_tokens', |
105
|
|
|
$this->getAppTokens() |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
return new TemplateResponse('settings', 'settings/personal/security', [ |
109
|
|
|
'passwordChangeSupported' => $passwordChangeSupported, |
110
|
|
|
'twoFactorProviderData' => $this->getTwoFactorProviderData(), |
111
|
|
|
]); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return string the section ID, e.g. 'sharing' |
116
|
|
|
* @since 9.1 |
117
|
|
|
*/ |
118
|
|
|
public function getSection() { |
119
|
|
|
return 'security'; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return int whether the form should be rather on the top or bottom of |
124
|
|
|
* the admin section. The forms are arranged in ascending order of the |
125
|
|
|
* priority values. It is required to return a value between 0 and 100. |
126
|
|
|
* |
127
|
|
|
* E.g.: 70 |
128
|
|
|
* @since 9.1 |
129
|
|
|
*/ |
130
|
|
|
public function getPriority() { |
131
|
|
|
return 10; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
private function getTwoFactorProviderData(): array { |
135
|
|
|
$user = $this->userSession->getUser(); |
136
|
|
|
if (is_null($user)) { |
137
|
|
|
// Actually impossible, but still … |
138
|
|
|
return []; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return [ |
142
|
|
|
'providers' => array_map(function (IProvidesPersonalSettings $provider) use ($user) { |
143
|
|
|
return [ |
144
|
|
|
'provider' => $provider, |
145
|
|
|
'settings' => $provider->getPersonalSettings($user) |
146
|
|
|
]; |
147
|
|
|
}, array_filter($this->providerLoader->getProviders($user), function (IProvider $provider) { |
148
|
|
|
return $provider instanceof IProvidesPersonalSettings; |
149
|
|
|
})) |
150
|
|
|
]; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
private function getAppTokens(): array { |
154
|
|
|
$tokens = $this->tokenProvider->getTokenByUser($this->uid); |
155
|
|
|
|
156
|
|
|
try { |
157
|
|
|
$sessionId = $this->session->getId(); |
158
|
|
|
} catch (SessionNotAvailableException $ex) { |
159
|
|
|
return []; |
160
|
|
|
} |
161
|
|
|
try { |
162
|
|
|
$sessionToken = $this->tokenProvider->getToken($sessionId); |
163
|
|
|
} catch (InvalidTokenException $ex) { |
164
|
|
|
return []; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return array_map(function (IToken $token) use ($sessionToken) { |
168
|
|
|
$data = $token->jsonSerialize(); |
169
|
|
|
$data['canDelete'] = true; |
170
|
|
|
$data['canRename'] = $token instanceof INamedToken; |
171
|
|
|
if ($sessionToken->getId() === $token->getId()) { |
172
|
|
|
$data['canDelete'] = false; |
173
|
|
|
$data['canRename'] = false; |
174
|
|
|
$data['current'] = true; |
175
|
|
|
} |
176
|
|
|
return $data; |
177
|
|
|
}, $tokens); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
} |
181
|
|
|
|