|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* @copyright Copyright (c) 2018 John Molakvoæ (skjnldsv) <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* @author Arthur Schiwon <[email protected]> |
|
9
|
|
|
* @author Christoph Wurst <[email protected]> |
|
10
|
|
|
* @author Georg Ehrke <[email protected]> |
|
11
|
|
|
* @author Joas Schilling <[email protected]> |
|
12
|
|
|
* @author John Molakvoæ (skjnldsv) <[email protected]> |
|
13
|
|
|
* @author Roeland Jago Douma <[email protected]> |
|
14
|
|
|
* |
|
15
|
|
|
* @license GNU AGPL version 3 or any later version |
|
16
|
|
|
* |
|
17
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
18
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
19
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
20
|
|
|
* License, or (at your option) any later version. |
|
21
|
|
|
* |
|
22
|
|
|
* This program is distributed in the hope that it will be useful, |
|
23
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
24
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
25
|
|
|
* GNU Affero General Public License for more details. |
|
26
|
|
|
* |
|
27
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
28
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
29
|
|
|
* |
|
30
|
|
|
*/ |
|
31
|
|
|
|
|
32
|
|
|
namespace OCA\Provisioning_API\Controller; |
|
33
|
|
|
|
|
34
|
|
|
use OC\Accounts\AccountManager; |
|
35
|
|
|
use OC\Group\Manager; |
|
36
|
|
|
use OC\User\Backend; |
|
37
|
|
|
use OC\User\NoUserException; |
|
38
|
|
|
use OC_Helper; |
|
39
|
|
|
use OCP\Accounts\IAccountManager; |
|
40
|
|
|
use OCP\AppFramework\OCS\OCSException; |
|
41
|
|
|
use OCP\AppFramework\OCS\OCSNotFoundException; |
|
42
|
|
|
use OCP\AppFramework\OCSController; |
|
43
|
|
|
use OCP\Files\NotFoundException; |
|
44
|
|
|
use OCP\IConfig; |
|
45
|
|
|
use OCP\IGroupManager; |
|
46
|
|
|
use OCP\IRequest; |
|
47
|
|
|
use OCP\IUserManager; |
|
48
|
|
|
use OCP\IUserSession; |
|
49
|
|
|
use OCP\L10N\IFactory; |
|
50
|
|
|
use OCP\User\Backend\ISetDisplayNameBackend; |
|
51
|
|
|
use OCP\User\Backend\ISetPasswordBackend; |
|
52
|
|
|
|
|
53
|
|
|
abstract class AUserData extends OCSController { |
|
54
|
|
|
public const SCOPE_SUFFIX = 'Scope'; |
|
55
|
|
|
|
|
56
|
|
|
/** @var IUserManager */ |
|
57
|
|
|
protected $userManager; |
|
58
|
|
|
/** @var IConfig */ |
|
59
|
|
|
protected $config; |
|
60
|
|
|
/** @var IGroupManager|Manager */ // FIXME Requires a method that is not on the interface |
|
61
|
|
|
protected $groupManager; |
|
62
|
|
|
/** @var IUserSession */ |
|
63
|
|
|
protected $userSession; |
|
64
|
|
|
/** @var AccountManager */ |
|
65
|
|
|
protected $accountManager; |
|
66
|
|
|
/** @var IFactory */ |
|
67
|
|
|
protected $l10nFactory; |
|
68
|
|
|
|
|
69
|
|
|
public function __construct(string $appName, |
|
70
|
|
|
IRequest $request, |
|
71
|
|
|
IUserManager $userManager, |
|
72
|
|
|
IConfig $config, |
|
73
|
|
|
IGroupManager $groupManager, |
|
74
|
|
|
IUserSession $userSession, |
|
75
|
|
|
AccountManager $accountManager, |
|
76
|
|
|
IFactory $l10nFactory) { |
|
77
|
|
|
parent::__construct($appName, $request); |
|
78
|
|
|
|
|
79
|
|
|
$this->userManager = $userManager; |
|
80
|
|
|
$this->config = $config; |
|
81
|
|
|
$this->groupManager = $groupManager; |
|
82
|
|
|
$this->userSession = $userSession; |
|
83
|
|
|
$this->accountManager = $accountManager; |
|
84
|
|
|
$this->l10nFactory = $l10nFactory; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* creates a array with all user data |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $userId |
|
91
|
|
|
* @param bool $includeScopes |
|
92
|
|
|
* @return array |
|
93
|
|
|
* @throws NotFoundException |
|
94
|
|
|
* @throws OCSException |
|
95
|
|
|
* @throws OCSNotFoundException |
|
96
|
|
|
*/ |
|
97
|
|
|
protected function getUserData(string $userId, bool $includeScopes = false): array { |
|
98
|
|
|
$currentLoggedInUser = $this->userSession->getUser(); |
|
99
|
|
|
|
|
100
|
|
|
$data = []; |
|
101
|
|
|
|
|
102
|
|
|
// Check if the target user exists |
|
103
|
|
|
$targetUserObject = $this->userManager->get($userId); |
|
104
|
|
|
if ($targetUserObject === null) { |
|
105
|
|
|
throw new OCSNotFoundException('User does not exist'); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
// Should be at least Admin Or SubAdmin! |
|
109
|
|
|
if ($this->groupManager->isAdmin($currentLoggedInUser->getUID()) |
|
110
|
|
|
|| $this->groupManager->getSubAdmin()->isUserAccessible($currentLoggedInUser, $targetUserObject)) { |
|
|
|
|
|
|
111
|
|
|
$data['enabled'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'enabled', 'true') === 'true'; |
|
112
|
|
|
} else { |
|
113
|
|
|
// Check they are looking up themselves |
|
114
|
|
|
if ($currentLoggedInUser->getUID() !== $targetUserObject->getUID()) { |
|
115
|
|
|
return $data; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
// Get groups data |
|
120
|
|
|
$userAccount = $this->accountManager->getAccount($targetUserObject); |
|
121
|
|
|
$groups = $this->groupManager->getUserGroups($targetUserObject); |
|
122
|
|
|
$gids = []; |
|
123
|
|
|
foreach ($groups as $group) { |
|
124
|
|
|
$gids[] = $group->getGID(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
try { |
|
128
|
|
|
# might be thrown by LDAP due to handling of users disappears |
|
129
|
|
|
# from the external source (reasons unknown to us) |
|
130
|
|
|
# cf. https://github.com/nextcloud/server/issues/12991 |
|
131
|
|
|
$data['storageLocation'] = $targetUserObject->getHome(); |
|
132
|
|
|
} catch (NoUserException $e) { |
|
133
|
|
|
throw new OCSNotFoundException($e->getMessage(), $e); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
// Find the data |
|
137
|
|
|
$data['id'] = $targetUserObject->getUID(); |
|
138
|
|
|
$data['lastLogin'] = $targetUserObject->getLastLogin() * 1000; |
|
139
|
|
|
$data['backend'] = $targetUserObject->getBackendClassName(); |
|
140
|
|
|
$data['subadmin'] = $this->getUserSubAdminGroupsData($targetUserObject->getUID()); |
|
141
|
|
|
$data['quota'] = $this->fillStorageInfo($targetUserObject->getUID()); |
|
142
|
|
|
|
|
143
|
|
|
if ($includeScopes) { |
|
144
|
|
|
$data[IAccountManager::PROPERTY_AVATAR . self::SCOPE_SUFFIX] = $userAccount->getProperty(IAccountManager::PROPERTY_AVATAR)->getScope(); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
$data[IAccountManager::PROPERTY_EMAIL] = $targetUserObject->getEMailAddress(); |
|
148
|
|
|
if ($includeScopes) { |
|
149
|
|
|
$data[IAccountManager::PROPERTY_EMAIL . self::SCOPE_SUFFIX] = $userAccount->getProperty(IAccountManager::PROPERTY_EMAIL)->getScope(); |
|
150
|
|
|
} |
|
151
|
|
|
$data[IAccountManager::PROPERTY_DISPLAYNAME] = $targetUserObject->getDisplayName(); |
|
152
|
|
|
if ($includeScopes) { |
|
153
|
|
|
$data[IAccountManager::PROPERTY_DISPLAYNAME . self::SCOPE_SUFFIX] = $userAccount->getProperty(IAccountManager::PROPERTY_DISPLAYNAME)->getScope(); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
foreach ([ |
|
157
|
|
|
IAccountManager::PROPERTY_PHONE, |
|
158
|
|
|
IAccountManager::PROPERTY_ADDRESS, |
|
159
|
|
|
IAccountManager::PROPERTY_WEBSITE, |
|
160
|
|
|
IAccountManager::PROPERTY_TWITTER, |
|
161
|
|
|
] as $propertyName) { |
|
162
|
|
|
$property = $userAccount->getProperty($propertyName); |
|
163
|
|
|
$data[$propertyName] = $property->getValue(); |
|
164
|
|
|
if ($includeScopes) { |
|
165
|
|
|
$data[$propertyName . self::SCOPE_SUFFIX] = $property->getScope(); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
$data['groups'] = $gids; |
|
170
|
|
|
$data['language'] = $this->l10nFactory->getUserLanguage($targetUserObject); |
|
171
|
|
|
$data['locale'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'locale'); |
|
172
|
|
|
|
|
173
|
|
|
$backend = $targetUserObject->getBackend(); |
|
174
|
|
|
$data['backendCapabilities'] = [ |
|
175
|
|
|
'setDisplayName' => $backend instanceof ISetDisplayNameBackend || $backend->implementsActions(Backend::SET_DISPLAYNAME), |
|
176
|
|
|
'setPassword' => $backend instanceof ISetPasswordBackend || $backend->implementsActions(Backend::SET_PASSWORD), |
|
177
|
|
|
]; |
|
178
|
|
|
|
|
179
|
|
|
return $data; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Get the groups a user is a subadmin of |
|
184
|
|
|
* |
|
185
|
|
|
* @param string $userId |
|
186
|
|
|
* @return array |
|
187
|
|
|
* @throws OCSException |
|
188
|
|
|
*/ |
|
189
|
|
|
protected function getUserSubAdminGroupsData(string $userId): array { |
|
190
|
|
|
$user = $this->userManager->get($userId); |
|
191
|
|
|
// Check if the user exists |
|
192
|
|
|
if ($user === null) { |
|
193
|
|
|
throw new OCSNotFoundException('User does not exist'); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
// Get the subadmin groups |
|
197
|
|
|
$subAdminGroups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($user); |
|
198
|
|
|
$groups = []; |
|
199
|
|
|
foreach ($subAdminGroups as $key => $group) { |
|
200
|
|
|
$groups[] = $group->getGID(); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
return $groups; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* @param string $userId |
|
208
|
|
|
* @return array |
|
209
|
|
|
* @throws OCSException |
|
210
|
|
|
*/ |
|
211
|
|
|
protected function fillStorageInfo(string $userId): array { |
|
212
|
|
|
try { |
|
213
|
|
|
\OC_Util::tearDownFS(); |
|
214
|
|
|
\OC_Util::setupFS($userId); |
|
215
|
|
|
$storage = OC_Helper::getStorageInfo('/'); |
|
216
|
|
|
$data = [ |
|
217
|
|
|
'free' => $storage['free'], |
|
218
|
|
|
'used' => $storage['used'], |
|
219
|
|
|
'total' => $storage['total'], |
|
220
|
|
|
'relative' => $storage['relative'], |
|
221
|
|
|
'quota' => $storage['quota'], |
|
222
|
|
|
]; |
|
223
|
|
|
} catch (NotFoundException $ex) { |
|
224
|
|
|
// User fs is not setup yet |
|
225
|
|
|
$user = $this->userManager->get($userId); |
|
226
|
|
|
if ($user === null) { |
|
227
|
|
|
throw new OCSException('User does not exist', 101); |
|
228
|
|
|
} |
|
229
|
|
|
$quota = $user->getQuota(); |
|
230
|
|
|
if ($quota !== 'none') { |
|
231
|
|
|
$quota = OC_Helper::computerFileSize($quota); |
|
232
|
|
|
} |
|
233
|
|
|
$data = [ |
|
234
|
|
|
'quota' => $quota !== false ? $quota : 'none', |
|
235
|
|
|
'used' => 0 |
|
236
|
|
|
]; |
|
237
|
|
|
} |
|
238
|
|
|
return $data; |
|
239
|
|
|
} |
|
240
|
|
|
} |
|
241
|
|
|
|