Completed
Pull Request — master (#3869)
by Jan-Christoph
17:04
created
settings/routes.php 1 patch
Indentation   +52 added lines, -52 removed lines patch added patch discarded remove patch
@@ -36,77 +36,77 @@
 block discarded – undo
36 36
 
37 37
 $application = new Application();
38 38
 $application->registerRoutes($this, [
39
-	'resources' => [
40
-		'users' => ['url' => '/settings/users/users'],
41
-		'AuthSettings' => ['url' => '/settings/personal/authtokens'],
42
-	],
43
-	'routes' => [
44
-		['name' => 'MailSettings#setMailSettings', 'url' => '/settings/admin/mailsettings', 'verb' => 'POST'],
45
-		['name' => 'MailSettings#storeCredentials', 'url' => '/settings/admin/mailsettings/credentials', 'verb' => 'POST'],
46
-		['name' => 'MailSettings#sendTestMail', 'url' => '/settings/admin/mailtest', 'verb' => 'POST'],
47
-		['name' => 'Encryption#startMigration', 'url' => '/settings/admin/startmigration', 'verb' => 'POST'],
48
-		['name' => 'AppSettings#listCategories', 'url' => '/settings/apps/categories', 'verb' => 'GET'],
49
-		['name' => 'AppSettings#viewApps', 'url' => '/settings/apps', 'verb' => 'GET'],
50
-		['name' => 'AppSettings#listApps', 'url' => '/settings/apps/list', 'verb' => 'GET'],
51
-		['name' => 'SecuritySettings#trustedDomains', 'url' => '/settings/admin/security/trustedDomains', 'verb' => 'POST'],
52
-		['name' => 'Users#setDisplayName', 'url' => '/settings/users/{username}/displayName', 'verb' => 'POST'],
53
-		['name' => 'Users#setEMailAddress', 'url' => '/settings/users/{id}/mailAddress', 'verb' => 'PUT'],
54
-		['name' => 'Users#setUserSettings', 'url' => '/settings/users/{username}/settings', 'verb' => 'PUT'],
55
-		['name' => 'Users#getVerificationCode', 'url' => '/settings/users/{account}/verify', 'verb' => 'GET'],
56
-		['name' => 'Users#stats', 'url' => '/settings/users/stats', 'verb' => 'GET'],
57
-		['name' => 'LogSettings#setLogLevel', 'url' => '/settings/admin/log/level', 'verb' => 'POST'],
58
-		['name' => 'LogSettings#getEntries', 'url' => '/settings/admin/log/entries', 'verb' => 'GET'],
59
-		['name' => 'LogSettings#download', 'url' => '/settings/admin/log/download', 'verb' => 'GET'],
60
-		['name' => 'CheckSetup#check', 'url' => '/settings/ajax/checksetup', 'verb' => 'GET'],
61
-		['name' => 'CheckSetup#getFailedIntegrityCheckFiles', 'url' => '/settings/integrity/failed', 'verb' => 'GET'],
62
-		['name' => 'CheckSetup#rescanFailedIntegrityCheck', 'url' => '/settings/integrity/rescan', 'verb' => 'GET'],
63
-		['name' => 'Certificate#addPersonalRootCertificate', 'url' => '/settings/personal/certificate', 'verb' => 'POST'],
64
-		['name' => 'Certificate#removePersonalRootCertificate', 'url' => '/settings/personal/certificate/{certificateIdentifier}', 'verb' => 'DELETE'],
65
-		['name' => 'Certificate#addSystemRootCertificate', 'url' => '/settings/admin/certificate', 'verb' => 'POST'],
66
-		['name' => 'Certificate#removeSystemRootCertificate', 'url' => '/settings/admin/certificate/{certificateIdentifier}', 'verb' => 'DELETE'],
67
-		['name' => 'AdminSettings#index', 'url' => '/settings/admin/{section}', 'verb' => 'GET', 'defaults' => ['section' => 'server']],
68
-		['name' => 'AdminSettings#form', 'url' => '/settings/admin/{section}', 'verb' => 'GET'],
69
-		['name' => 'ChangePassword#changePersonalPassword', 'url' => '/settings/personal/changepassword', 'verb' => 'POST'],
70
-		['name' => 'ChangePassword#changeUserPassword', 'url' => '/settings/users/changepassword', 'verb' => 'POST'],
71
-		['name' => 'Personal#setLanguage', 'url' => '/settings/ajax/setlanguage.php', 'verb' => 'POST'],
72
-		['name' => 'Groups#index', 'url' => '/settings/users/groups', 'verb' => 'GET'],
73
-		['name' => 'Groups#show', 'url' => '/settings/users/groups/{id}', 'requirements' => ['id' => '[^?]*'], 'verb' => 'GET'],
74
-		['name' => 'Groups#create', 'url' => '/settings/users/groups', 'verb' => 'POST'],
75
-		['name' => 'Groups#update', 'url' => '/settings/users/groups/{id}', 'requirements' => ['id' => '[^?]*'], 'verb' => 'PUT'],
76
-		['name' => 'Groups#destroy', 'url' => '/settings/users/groups/{id}', 'requirements' => ['id' => '[^?]*'], 'verb' => 'DELETE'],
77
-	]
39
+    'resources' => [
40
+        'users' => ['url' => '/settings/users/users'],
41
+        'AuthSettings' => ['url' => '/settings/personal/authtokens'],
42
+    ],
43
+    'routes' => [
44
+        ['name' => 'MailSettings#setMailSettings', 'url' => '/settings/admin/mailsettings', 'verb' => 'POST'],
45
+        ['name' => 'MailSettings#storeCredentials', 'url' => '/settings/admin/mailsettings/credentials', 'verb' => 'POST'],
46
+        ['name' => 'MailSettings#sendTestMail', 'url' => '/settings/admin/mailtest', 'verb' => 'POST'],
47
+        ['name' => 'Encryption#startMigration', 'url' => '/settings/admin/startmigration', 'verb' => 'POST'],
48
+        ['name' => 'AppSettings#listCategories', 'url' => '/settings/apps/categories', 'verb' => 'GET'],
49
+        ['name' => 'AppSettings#viewApps', 'url' => '/settings/apps', 'verb' => 'GET'],
50
+        ['name' => 'AppSettings#listApps', 'url' => '/settings/apps/list', 'verb' => 'GET'],
51
+        ['name' => 'SecuritySettings#trustedDomains', 'url' => '/settings/admin/security/trustedDomains', 'verb' => 'POST'],
52
+        ['name' => 'Users#setDisplayName', 'url' => '/settings/users/{username}/displayName', 'verb' => 'POST'],
53
+        ['name' => 'Users#setEMailAddress', 'url' => '/settings/users/{id}/mailAddress', 'verb' => 'PUT'],
54
+        ['name' => 'Users#setUserSettings', 'url' => '/settings/users/{username}/settings', 'verb' => 'PUT'],
55
+        ['name' => 'Users#getVerificationCode', 'url' => '/settings/users/{account}/verify', 'verb' => 'GET'],
56
+        ['name' => 'Users#stats', 'url' => '/settings/users/stats', 'verb' => 'GET'],
57
+        ['name' => 'LogSettings#setLogLevel', 'url' => '/settings/admin/log/level', 'verb' => 'POST'],
58
+        ['name' => 'LogSettings#getEntries', 'url' => '/settings/admin/log/entries', 'verb' => 'GET'],
59
+        ['name' => 'LogSettings#download', 'url' => '/settings/admin/log/download', 'verb' => 'GET'],
60
+        ['name' => 'CheckSetup#check', 'url' => '/settings/ajax/checksetup', 'verb' => 'GET'],
61
+        ['name' => 'CheckSetup#getFailedIntegrityCheckFiles', 'url' => '/settings/integrity/failed', 'verb' => 'GET'],
62
+        ['name' => 'CheckSetup#rescanFailedIntegrityCheck', 'url' => '/settings/integrity/rescan', 'verb' => 'GET'],
63
+        ['name' => 'Certificate#addPersonalRootCertificate', 'url' => '/settings/personal/certificate', 'verb' => 'POST'],
64
+        ['name' => 'Certificate#removePersonalRootCertificate', 'url' => '/settings/personal/certificate/{certificateIdentifier}', 'verb' => 'DELETE'],
65
+        ['name' => 'Certificate#addSystemRootCertificate', 'url' => '/settings/admin/certificate', 'verb' => 'POST'],
66
+        ['name' => 'Certificate#removeSystemRootCertificate', 'url' => '/settings/admin/certificate/{certificateIdentifier}', 'verb' => 'DELETE'],
67
+        ['name' => 'AdminSettings#index', 'url' => '/settings/admin/{section}', 'verb' => 'GET', 'defaults' => ['section' => 'server']],
68
+        ['name' => 'AdminSettings#form', 'url' => '/settings/admin/{section}', 'verb' => 'GET'],
69
+        ['name' => 'ChangePassword#changePersonalPassword', 'url' => '/settings/personal/changepassword', 'verb' => 'POST'],
70
+        ['name' => 'ChangePassword#changeUserPassword', 'url' => '/settings/users/changepassword', 'verb' => 'POST'],
71
+        ['name' => 'Personal#setLanguage', 'url' => '/settings/ajax/setlanguage.php', 'verb' => 'POST'],
72
+        ['name' => 'Groups#index', 'url' => '/settings/users/groups', 'verb' => 'GET'],
73
+        ['name' => 'Groups#show', 'url' => '/settings/users/groups/{id}', 'requirements' => ['id' => '[^?]*'], 'verb' => 'GET'],
74
+        ['name' => 'Groups#create', 'url' => '/settings/users/groups', 'verb' => 'POST'],
75
+        ['name' => 'Groups#update', 'url' => '/settings/users/groups/{id}', 'requirements' => ['id' => '[^?]*'], 'verb' => 'PUT'],
76
+        ['name' => 'Groups#destroy', 'url' => '/settings/users/groups/{id}', 'requirements' => ['id' => '[^?]*'], 'verb' => 'DELETE'],
77
+    ]
78 78
 ]);
79 79
 
80 80
 /** @var $this \OCP\Route\IRouter */
81 81
 
82 82
 // Settings pages
83 83
 $this->create('settings_help', '/settings/help')
84
-	->actionInclude('settings/help.php');
84
+    ->actionInclude('settings/help.php');
85 85
 $this->create('settings_personal', '/settings/personal')
86
-	->actionInclude('settings/personal.php');
86
+    ->actionInclude('settings/personal.php');
87 87
 $this->create('settings_users', '/settings/users')
88
-	->actionInclude('settings/users.php');
88
+    ->actionInclude('settings/users.php');
89 89
 // Settings ajax actions
90 90
 // users
91 91
 $this->create('settings_ajax_setquota', '/settings/ajax/setquota.php')
92
-	->actionInclude('settings/ajax/setquota.php');
92
+    ->actionInclude('settings/ajax/setquota.php');
93 93
 $this->create('settings_ajax_togglegroups', '/settings/ajax/togglegroups.php')
94
-	->actionInclude('settings/ajax/togglegroups.php');
94
+    ->actionInclude('settings/ajax/togglegroups.php');
95 95
 $this->create('settings_ajax_togglesubadmins', '/settings/ajax/togglesubadmins.php')
96
-	->actionInclude('settings/ajax/togglesubadmins.php');
96
+    ->actionInclude('settings/ajax/togglesubadmins.php');
97 97
 $this->create('settings_ajax_changegorupname', '/settings/ajax/changegroupname.php')
98
-	->actionInclude('settings/ajax/changegroupname.php');
98
+    ->actionInclude('settings/ajax/changegroupname.php');
99 99
 // apps
100 100
 $this->create('settings_ajax_enableapp', '/settings/ajax/enableapp.php')
101
-	->actionInclude('settings/ajax/enableapp.php');
101
+    ->actionInclude('settings/ajax/enableapp.php');
102 102
 $this->create('settings_ajax_disableapp', '/settings/ajax/disableapp.php')
103
-	->actionInclude('settings/ajax/disableapp.php');
103
+    ->actionInclude('settings/ajax/disableapp.php');
104 104
 $this->create('settings_ajax_updateapp', '/settings/ajax/updateapp.php')
105
-	->actionInclude('settings/ajax/updateapp.php');
105
+    ->actionInclude('settings/ajax/updateapp.php');
106 106
 $this->create('settings_ajax_uninstallapp', '/settings/ajax/uninstallapp.php')
107
-	->actionInclude('settings/ajax/uninstallapp.php');
107
+    ->actionInclude('settings/ajax/uninstallapp.php');
108 108
 $this->create('settings_ajax_navigationdetect', '/settings/ajax/navigationdetect.php')
109
-	->actionInclude('settings/ajax/navigationdetect.php');
109
+    ->actionInclude('settings/ajax/navigationdetect.php');
110 110
 // admin
111 111
 $this->create('settings_ajax_excludegroups', '/settings/ajax/excludegroups.php')
112
-	->actionInclude('settings/ajax/excludegroups.php');
112
+    ->actionInclude('settings/ajax/excludegroups.php');
Please login to merge, or discard this patch.
apps/files_sharing/lib/Controller/ShareesAPIController.php 3 patches
Doc Comments   +4 added lines, -1 removed lines patch added patch discarded remove patch
@@ -385,7 +385,7 @@  discard block
 block discarded – undo
385 385
 	 * split user and remote from federated cloud id
386 386
 	 *
387 387
 	 * @param string $address federated share address
388
-	 * @return array [user, remoteURL]
388
+	 * @return string[] [user, remoteURL]
389 389
 	 * @throws \Exception
390 390
 	 */
391 391
 	public function splitUserRemote($address) {
@@ -652,6 +652,9 @@  discard block
 block discarded – undo
652 652
 		return $result;
653 653
 	}
654 654
 
655
+	/**
656
+	 * @param string $search
657
+	 */
655 658
 	protected function getLookup($search) {
656 659
 		$isEnabled = $this->config->getAppValue('files_sharing', 'lookupServerEnabled', 'no');
657 660
 		$lookupServerUrl = $this->config->getSystemValue('lookup_server', 'https://lookup.nextcloud.com');
Please login to merge, or discard this patch.
Indentation   +670 added lines, -670 removed lines patch added patch discarded remove patch
@@ -43,674 +43,674 @@
 block discarded – undo
43 43
 
44 44
 class ShareesAPIController extends OCSController {
45 45
 
46
-	/** @var IGroupManager */
47
-	protected $groupManager;
48
-
49
-	/** @var IUserManager */
50
-	protected $userManager;
51
-
52
-	/** @var IManager */
53
-	protected $contactsManager;
54
-
55
-	/** @var IConfig */
56
-	protected $config;
57
-
58
-	/** @var IUserSession */
59
-	protected $userSession;
60
-
61
-	/** @var IURLGenerator */
62
-	protected $urlGenerator;
63
-
64
-	/** @var ILogger */
65
-	protected $logger;
66
-
67
-	/** @var \OCP\Share\IManager */
68
-	protected $shareManager;
69
-
70
-	/** @var IClientService */
71
-	protected $clientService;
72
-
73
-	/** @var ICloudIdManager  */
74
-	protected $cloudIdManager;
75
-
76
-	/** @var bool */
77
-	protected $shareWithGroupOnly = false;
78
-
79
-	/** @var bool */
80
-	protected $shareeEnumeration = true;
81
-
82
-	/** @var int */
83
-	protected $offset = 0;
84
-
85
-	/** @var int */
86
-	protected $limit = 10;
87
-
88
-	/** @var array */
89
-	protected $result = [
90
-		'exact' => [
91
-			'users' => [],
92
-			'groups' => [],
93
-			'remotes' => [],
94
-			'emails' => [],
95
-			'circles' => [],
96
-		],
97
-		'users' => [],
98
-		'groups' => [],
99
-		'remotes' => [],
100
-		'emails' => [],
101
-		'lookup' => [],
102
-		'circles' => [],
103
-	];
104
-
105
-	protected $reachedEndFor = [];
106
-
107
-	/**
108
-	 * @param string $appName
109
-	 * @param IRequest $request
110
-	 * @param IGroupManager $groupManager
111
-	 * @param IUserManager $userManager
112
-	 * @param IManager $contactsManager
113
-	 * @param IConfig $config
114
-	 * @param IUserSession $userSession
115
-	 * @param IURLGenerator $urlGenerator
116
-	 * @param ILogger $logger
117
-	 * @param \OCP\Share\IManager $shareManager
118
-	 * @param IClientService $clientService
119
-	 * @param ICloudIdManager $cloudIdManager
120
-	 */
121
-	public function __construct($appName,
122
-								IRequest $request,
123
-								IGroupManager $groupManager,
124
-								IUserManager $userManager,
125
-								IManager $contactsManager,
126
-								IConfig $config,
127
-								IUserSession $userSession,
128
-								IURLGenerator $urlGenerator,
129
-								ILogger $logger,
130
-								\OCP\Share\IManager $shareManager,
131
-								IClientService $clientService,
132
-								ICloudIdManager $cloudIdManager
133
-	) {
134
-		parent::__construct($appName, $request);
135
-
136
-		$this->groupManager = $groupManager;
137
-		$this->userManager = $userManager;
138
-		$this->contactsManager = $contactsManager;
139
-		$this->config = $config;
140
-		$this->userSession = $userSession;
141
-		$this->urlGenerator = $urlGenerator;
142
-		$this->logger = $logger;
143
-		$this->shareManager = $shareManager;
144
-		$this->clientService = $clientService;
145
-		$this->cloudIdManager = $cloudIdManager;
146
-	}
147
-
148
-	/**
149
-	 * @param string $search
150
-	 */
151
-	protected function getUsers($search) {
152
-		$this->result['users'] = $this->result['exact']['users'] = $users = [];
153
-
154
-		$userGroups = [];
155
-		if ($this->shareWithGroupOnly) {
156
-			// Search in all the groups this user is part of
157
-			$userGroups = $this->groupManager->getUserGroupIds($this->userSession->getUser());
158
-			foreach ($userGroups as $userGroup) {
159
-				$usersTmp = $this->groupManager->displayNamesInGroup($userGroup, $search, $this->limit, $this->offset);
160
-				foreach ($usersTmp as $uid => $userDisplayName) {
161
-					$users[$uid] = $userDisplayName;
162
-				}
163
-			}
164
-		} else {
165
-			// Search in all users
166
-			$usersTmp = $this->userManager->searchDisplayName($search, $this->limit, $this->offset);
167
-
168
-			foreach ($usersTmp as $user) {
169
-				$users[$user->getUID()] = $user->getDisplayName();
170
-			}
171
-		}
172
-
173
-		if (!$this->shareeEnumeration || sizeof($users) < $this->limit) {
174
-			$this->reachedEndFor[] = 'users';
175
-		}
176
-
177
-		$foundUserById = false;
178
-		$lowerSearch = strtolower($search);
179
-		foreach ($users as $uid => $userDisplayName) {
180
-			if (strtolower($uid) === $lowerSearch || strtolower($userDisplayName) === $lowerSearch) {
181
-				if (strtolower($uid) === $lowerSearch) {
182
-					$foundUserById = true;
183
-				}
184
-				$this->result['exact']['users'][] = [
185
-					'label' => $userDisplayName,
186
-					'value' => [
187
-						'shareType' => Share::SHARE_TYPE_USER,
188
-						'shareWith' => $uid,
189
-					],
190
-				];
191
-			} else {
192
-				$this->result['users'][] = [
193
-					'label' => $userDisplayName,
194
-					'value' => [
195
-						'shareType' => Share::SHARE_TYPE_USER,
196
-						'shareWith' => $uid,
197
-					],
198
-				];
199
-			}
200
-		}
201
-
202
-		if ($this->offset === 0 && !$foundUserById) {
203
-			// On page one we try if the search result has a direct hit on the
204
-			// user id and if so, we add that to the exact match list
205
-			$user = $this->userManager->get($search);
206
-			if ($user instanceof IUser) {
207
-				$addUser = true;
208
-
209
-				if ($this->shareWithGroupOnly) {
210
-					// Only add, if we have a common group
211
-					$commonGroups = array_intersect($userGroups, $this->groupManager->getUserGroupIds($user));
212
-					$addUser = !empty($commonGroups);
213
-				}
214
-
215
-				if ($addUser) {
216
-					array_push($this->result['exact']['users'], [
217
-						'label' => $user->getDisplayName(),
218
-						'value' => [
219
-							'shareType' => Share::SHARE_TYPE_USER,
220
-							'shareWith' => $user->getUID(),
221
-						],
222
-					]);
223
-				}
224
-			}
225
-		}
226
-
227
-		if (!$this->shareeEnumeration) {
228
-			$this->result['users'] = [];
229
-		}
230
-	}
231
-
232
-	/**
233
-	 * @param string $search
234
-	 */
235
-	protected function getGroups($search) {
236
-		$this->result['groups'] = $this->result['exact']['groups'] = [];
237
-
238
-		$groups = $this->groupManager->search($search, $this->limit, $this->offset);
239
-		$groupIds = array_map(function (IGroup $group) { return $group->getGID(); }, $groups);
240
-
241
-		if (!$this->shareeEnumeration || sizeof($groups) < $this->limit) {
242
-			$this->reachedEndFor[] = 'groups';
243
-		}
244
-
245
-		$userGroups =  [];
246
-		if (!empty($groups) && $this->shareWithGroupOnly) {
247
-			// Intersect all the groups that match with the groups this user is a member of
248
-			$userGroups = $this->groupManager->getUserGroups($this->userSession->getUser());
249
-			$userGroups = array_map(function (IGroup $group) { return $group->getGID(); }, $userGroups);
250
-			$groupIds = array_intersect($groupIds, $userGroups);
251
-		}
252
-
253
-		$lowerSearch = strtolower($search);
254
-		foreach ($groups as $group) {
255
-			// FIXME: use a more efficient approach
256
-			$gid = $group->getGID();
257
-			if (!in_array($gid, $groupIds)) {
258
-				continue;
259
-			}
260
-			if (strtolower($gid) === $lowerSearch || strtolower($group->getDisplayName()) === $lowerSearch) {
261
-				$this->result['exact']['groups'][] = [
262
-					'label' => $group->getDisplayName(),
263
-					'value' => [
264
-						'shareType' => Share::SHARE_TYPE_GROUP,
265
-						'shareWith' => $gid,
266
-					],
267
-				];
268
-			} else {
269
-				$this->result['groups'][] = [
270
-					'label' => $group->getDisplayName(),
271
-					'value' => [
272
-						'shareType' => Share::SHARE_TYPE_GROUP,
273
-						'shareWith' => $gid,
274
-					],
275
-				];
276
-			}
277
-		}
278
-
279
-		if ($this->offset === 0 && empty($this->result['exact']['groups'])) {
280
-			// On page one we try if the search result has a direct hit on the
281
-			// user id and if so, we add that to the exact match list
282
-			$group = $this->groupManager->get($search);
283
-			if ($group instanceof IGroup && (!$this->shareWithGroupOnly || in_array($group->getGID(), $userGroups))) {
284
-				array_push($this->result['exact']['groups'], [
285
-					'label' => $group->getDisplayName(),
286
-					'value' => [
287
-						'shareType' => Share::SHARE_TYPE_GROUP,
288
-						'shareWith' => $group->getGID(),
289
-					],
290
-				]);
291
-			}
292
-		}
293
-
294
-		if (!$this->shareeEnumeration) {
295
-			$this->result['groups'] = [];
296
-		}
297
-	}
298
-
299
-
300
-	/**
301
-	 * @param string $search
302
-	 */
303
-	protected function getCircles($search) {
304
-		$this->result['circles'] = $this->result['exact']['circles'] = [];
305
-
306
-		$result = \OCA\Circles\Api\Sharees::search($search, $this->limit, $this->offset);
307
-		if (array_key_exists('circles', $result['exact'])) {
308
-			$this->result['exact']['circles'] = $result['exact']['circles'];
309
-		}
310
-		if (array_key_exists('circles', $result)) {
311
-			$this->result['circles'] = $result['circles'];
312
-		}
313
-	}
314
-
315
-
316
-	/**
317
-	 * @param string $search
318
-	 * @return array
319
-	 */
320
-	protected function getRemote($search) {
321
-		$result = ['results' => [], 'exact' => []];
322
-
323
-		// Search in contacts
324
-		//@todo Pagination missing
325
-		$addressBookContacts = $this->contactsManager->search($search, ['CLOUD', 'FN']);
326
-		$result['exactIdMatch'] = false;
327
-		foreach ($addressBookContacts as $contact) {
328
-			if (isset($contact['isLocalSystemBook'])) {
329
-				continue;
330
-			}
331
-			if (isset($contact['CLOUD'])) {
332
-				$cloudIds = $contact['CLOUD'];
333
-				if (!is_array($cloudIds)) {
334
-					$cloudIds = [$cloudIds];
335
-				}
336
-				$lowerSearch = strtolower($search);
337
-				foreach ($cloudIds as $cloudId) {
338
-					list(, $serverUrl) = $this->splitUserRemote($cloudId);
339
-					if (strtolower($contact['FN']) === $lowerSearch || strtolower($cloudId) === $lowerSearch) {
340
-						if (strtolower($cloudId) === $lowerSearch) {
341
-							$result['exactIdMatch'] = true;
342
-						}
343
-						$result['exact'][] = [
344
-							'label' => $contact['FN'] . " ($cloudId)",
345
-							'value' => [
346
-								'shareType' => Share::SHARE_TYPE_REMOTE,
347
-								'shareWith' => $cloudId,
348
-								'server' => $serverUrl,
349
-							],
350
-						];
351
-					} else {
352
-						$result['results'][] = [
353
-							'label' => $contact['FN'] . " ($cloudId)",
354
-							'value' => [
355
-								'shareType' => Share::SHARE_TYPE_REMOTE,
356
-								'shareWith' => $cloudId,
357
-								'server' => $serverUrl,
358
-							],
359
-						];
360
-					}
361
-				}
362
-			}
363
-		}
364
-
365
-		if (!$this->shareeEnumeration) {
366
-			$result['results'] = [];
367
-		}
368
-
369
-		if (!$result['exactIdMatch'] && $this->cloudIdManager->isValidCloudId($search) && $this->offset === 0) {
370
-			$result['exact'][] = [
371
-				'label' => $search,
372
-				'value' => [
373
-					'shareType' => Share::SHARE_TYPE_REMOTE,
374
-					'shareWith' => $search,
375
-				],
376
-			];
377
-		}
378
-
379
-		$this->reachedEndFor[] = 'remotes';
380
-
381
-		return $result;
382
-	}
383
-
384
-	/**
385
-	 * split user and remote from federated cloud id
386
-	 *
387
-	 * @param string $address federated share address
388
-	 * @return array [user, remoteURL]
389
-	 * @throws \Exception
390
-	 */
391
-	public function splitUserRemote($address) {
392
-		try {
393
-			$cloudId = $this->cloudIdManager->resolveCloudId($address);
394
-			return [$cloudId->getUser(), $cloudId->getRemote()];
395
-		} catch (\InvalidArgumentException $e) {
396
-			throw new \Exception('Invalid Federated Cloud ID', 0, $e);
397
-		}
398
-	}
399
-
400
-	/**
401
-	 * Strips away a potential file names and trailing slashes:
402
-	 * - http://localhost
403
-	 * - http://localhost/
404
-	 * - http://localhost/index.php
405
-	 * - http://localhost/index.php/s/{shareToken}
406
-	 *
407
-	 * all return: http://localhost
408
-	 *
409
-	 * @param string $remote
410
-	 * @return string
411
-	 */
412
-	protected function fixRemoteURL($remote) {
413
-		$remote = str_replace('\\', '/', $remote);
414
-		if ($fileNamePosition = strpos($remote, '/index.php')) {
415
-			$remote = substr($remote, 0, $fileNamePosition);
416
-		}
417
-		$remote = rtrim($remote, '/');
418
-
419
-		return $remote;
420
-	}
421
-
422
-	/**
423
-	 * @NoAdminRequired
424
-	 *
425
-	 * @param string $search
426
-	 * @param string $itemType
427
-	 * @param int $page
428
-	 * @param int $perPage
429
-	 * @param int|int[] $shareType
430
-	 * @param bool $lookup
431
-	 * @return DataResponse
432
-	 * @throws OCSBadRequestException
433
-	 */
434
-	public function search($search = '', $itemType = null, $page = 1, $perPage = 200, $shareType = null, $lookup = true) {
435
-
436
-		// only search for string larger than a given threshold
437
-		$threshold = (int)$this->config->getSystemValue('sharing.minSearchStringLength', 0);
438
-		if (strlen($search) < $threshold) {
439
-			return new DataResponse($this->result);
440
-		}
441
-
442
-		// never return more than the max. number of results configured in the config.php
443
-		$maxResults = (int)$this->config->getSystemValue('sharing.maxAutocompleteResults', 0);
444
-		if ($maxResults > 0) {
445
-			$perPage = min($perPage, $maxResults);
446
-		}
447
-		if ($perPage <= 0) {
448
-			throw new OCSBadRequestException('Invalid perPage argument');
449
-		}
450
-		if ($page <= 0) {
451
-			throw new OCSBadRequestException('Invalid page');
452
-		}
453
-
454
-		$shareTypes = [
455
-			Share::SHARE_TYPE_USER,
456
-		];
457
-
458
-		if ($itemType === 'file' || $itemType === 'folder') {
459
-			if ($this->shareManager->allowGroupSharing()) {
460
-				$shareTypes[] = Share::SHARE_TYPE_GROUP;
461
-			}
462
-
463
-			if ($this->isRemoteSharingAllowed($itemType)) {
464
-				$shareTypes[] = Share::SHARE_TYPE_REMOTE;
465
-			}
466
-
467
-			if ($this->shareManager->shareProviderExists(Share::SHARE_TYPE_EMAIL)) {
468
-				$shareTypes[] = Share::SHARE_TYPE_EMAIL;
469
-			}
470
-		} else {
471
-			$shareTypes[] = Share::SHARE_TYPE_GROUP;
472
-			$shareTypes[] = Share::SHARE_TYPE_EMAIL;
473
-		}
474
-
475
-		if (\OCP\App::isEnabled('circles')) {
476
-			$shareTypes[] = Share::SHARE_TYPE_CIRCLE;
477
-		}
478
-
479
-		if (isset($_GET['shareType']) && is_array($_GET['shareType'])) {
480
-			$shareTypes = array_intersect($shareTypes, $_GET['shareType']);
481
-			sort($shareTypes);
482
-		} else if (is_numeric($shareType)) {
483
-			$shareTypes = array_intersect($shareTypes, [(int) $shareType]);
484
-			sort($shareTypes);
485
-		}
486
-
487
-		$this->shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
488
-		$this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes';
489
-		$this->limit = (int) $perPage;
490
-		$this->offset = $perPage * ($page - 1);
491
-
492
-		return $this->searchSharees($search, $itemType, $shareTypes, $page, $perPage, $lookup);
493
-	}
494
-
495
-	/**
496
-	 * Method to get out the static call for better testing
497
-	 *
498
-	 * @param string $itemType
499
-	 * @return bool
500
-	 */
501
-	protected function isRemoteSharingAllowed($itemType) {
502
-		try {
503
-			$backend = Share::getBackend($itemType);
504
-			return $backend->isShareTypeAllowed(Share::SHARE_TYPE_REMOTE);
505
-		} catch (\Exception $e) {
506
-			return false;
507
-		}
508
-	}
509
-
510
-	/**
511
-	 * Testable search function that does not need globals
512
-	 *
513
-	 * @param string $search
514
-	 * @param string $itemType
515
-	 * @param array $shareTypes
516
-	 * @param int $page
517
-	 * @param int $perPage
518
-	 * @param bool $lookup
519
-	 * @return DataResponse
520
-	 * @throws OCSBadRequestException
521
-	 */
522
-	protected function searchSharees($search, $itemType, array $shareTypes, $page, $perPage, $lookup) {
523
-		// Verify arguments
524
-		if ($itemType === null) {
525
-			throw new OCSBadRequestException('Missing itemType');
526
-		}
527
-
528
-		// Get users
529
-		if (in_array(Share::SHARE_TYPE_USER, $shareTypes)) {
530
-			$this->getUsers($search);
531
-		}
532
-
533
-		// Get groups
534
-		if (in_array(Share::SHARE_TYPE_GROUP, $shareTypes)) {
535
-			$this->getGroups($search);
536
-		}
537
-
538
-		// Get circles
539
-		if (in_array(Share::SHARE_TYPE_CIRCLE, $shareTypes)) {
540
-			$this->getCircles($search);
541
-		}
542
-
543
-
544
-		// Get remote
545
-		$remoteResults = ['results' => [], 'exact' => [], 'exactIdMatch' => false];
546
-		if (in_array(Share::SHARE_TYPE_REMOTE, $shareTypes)) {
547
-			$remoteResults = $this->getRemote($search);
548
-		}
549
-
550
-		// Get emails
551
-		$mailResults = ['results' => [], 'exact' => [], 'exactIdMatch' => false];
552
-		if (in_array(Share::SHARE_TYPE_EMAIL, $shareTypes)) {
553
-			$mailResults = $this->getEmail($search);
554
-		}
555
-
556
-		// Get from lookup server
557
-		if ($lookup) {
558
-			$this->getLookup($search);
559
-		}
560
-
561
-		// if we have a exact match, either for the federated cloud id or for the
562
-		// email address we only return the exact match. It is highly unlikely
563
-		// that the exact same email address and federated cloud id exists
564
-		if ($mailResults['exactIdMatch'] && !$remoteResults['exactIdMatch']) {
565
-			$this->result['emails'] = $mailResults['results'];
566
-			$this->result['exact']['emails'] = $mailResults['exact'];
567
-		} else if (!$mailResults['exactIdMatch'] && $remoteResults['exactIdMatch']) {
568
-			$this->result['remotes'] = $remoteResults['results'];
569
-			$this->result['exact']['remotes'] = $remoteResults['exact'];
570
-		} else {
571
-			$this->result['remotes'] = $remoteResults['results'];
572
-			$this->result['exact']['remotes'] = $remoteResults['exact'];
573
-			$this->result['emails'] = $mailResults['results'];
574
-			$this->result['exact']['emails'] = $mailResults['exact'];
575
-		}
576
-
577
-		$response = new DataResponse($this->result);
578
-
579
-		if (sizeof($this->reachedEndFor) < 3) {
580
-			$response->addHeader('Link', $this->getPaginationLink($page, [
581
-				'search' => $search,
582
-				'itemType' => $itemType,
583
-				'shareType' => $shareTypes,
584
-				'perPage' => $perPage,
585
-			]));
586
-		}
587
-
588
-		return $response;
589
-	}
590
-
591
-	/**
592
-	 * @param string $search
593
-	 * @return array
594
-	 */
595
-	protected function getEmail($search) {
596
-		$result = ['results' => [], 'exact' => []];
597
-
598
-		// Search in contacts
599
-		//@todo Pagination missing
600
-		$addressBookContacts = $this->contactsManager->search($search, ['EMAIL', 'FN']);
601
-		$result['exactIdMatch'] = false;
602
-		foreach ($addressBookContacts as $contact) {
603
-			if (isset($contact['isLocalSystemBook'])) {
604
-				continue;
605
-			}
606
-			if (isset($contact['EMAIL'])) {
607
-				$emailAddresses = $contact['EMAIL'];
608
-				if (!is_array($emailAddresses)) {
609
-					$emailAddresses = [$emailAddresses];
610
-				}
611
-				foreach ($emailAddresses as $emailAddress) {
612
-					if (strtolower($contact['FN']) === strtolower($search) || strtolower($emailAddress) === strtolower($search)) {
613
-						if (strtolower($emailAddress) === strtolower($search)) {
614
-							$result['exactIdMatch'] = true;
615
-						}
616
-						$result['exact'][] = [
617
-							'label' => $contact['FN'] . " ($emailAddress)",
618
-							'value' => [
619
-								'shareType' => Share::SHARE_TYPE_EMAIL,
620
-								'shareWith' => $emailAddress,
621
-							],
622
-						];
623
-					} else {
624
-						$result['results'][] = [
625
-							'label' => $contact['FN'] . " ($emailAddress)",
626
-							'value' => [
627
-								'shareType' => Share::SHARE_TYPE_EMAIL,
628
-								'shareWith' => $emailAddress,
629
-							],
630
-						];
631
-					}
632
-				}
633
-			}
634
-		}
635
-
636
-		if (!$this->shareeEnumeration) {
637
-			$result['results'] = [];
638
-		}
639
-
640
-		if (!$result['exactIdMatch'] && filter_var($search, FILTER_VALIDATE_EMAIL)) {
641
-			$result['exact'][] = [
642
-				'label' => $search,
643
-				'value' => [
644
-					'shareType' => Share::SHARE_TYPE_EMAIL,
645
-					'shareWith' => $search,
646
-				],
647
-			];
648
-		}
649
-
650
-		$this->reachedEndFor[] = 'emails';
651
-
652
-		return $result;
653
-	}
654
-
655
-	protected function getLookup($search) {
656
-		$isEnabled = $this->config->getAppValue('files_sharing', 'lookupServerEnabled', 'no');
657
-		$lookupServerUrl = $this->config->getSystemValue('lookup_server', 'https://lookup.nextcloud.com');
658
-		$lookupServerUrl = rtrim($lookupServerUrl, '/');
659
-		$result = [];
660
-
661
-		if($isEnabled === 'yes') {
662
-			try {
663
-				$client = $this->clientService->newClient();
664
-				$response = $client->get(
665
-					$lookupServerUrl . '/users?search=' . urlencode($search),
666
-					[
667
-						'timeout' => 10,
668
-						'connect_timeout' => 3,
669
-					]
670
-				);
671
-
672
-				$body = json_decode($response->getBody(), true);
673
-
674
-				$result = [];
675
-				foreach ($body as $lookup) {
676
-					$result[] = [
677
-						'label' => $lookup['federationId'],
678
-						'value' => [
679
-							'shareType' => Share::SHARE_TYPE_REMOTE,
680
-							'shareWith' => $lookup['federationId'],
681
-						],
682
-						'extra' => $lookup,
683
-					];
684
-				}
685
-			} catch (\Exception $e) {}
686
-		}
687
-
688
-		$this->result['lookup'] = $result;
689
-	}
690
-
691
-	/**
692
-	 * Generates a bunch of pagination links for the current page
693
-	 *
694
-	 * @param int $page Current page
695
-	 * @param array $params Parameters for the URL
696
-	 * @return string
697
-	 */
698
-	protected function getPaginationLink($page, array $params) {
699
-		if ($this->isV2()) {
700
-			$url = $this->urlGenerator->getAbsoluteURL('/ocs/v2.php/apps/files_sharing/api/v1/sharees') . '?';
701
-		} else {
702
-			$url = $this->urlGenerator->getAbsoluteURL('/ocs/v1.php/apps/files_sharing/api/v1/sharees') . '?';
703
-		}
704
-		$params['page'] = $page + 1;
705
-		$link = '<' . $url . http_build_query($params) . '>; rel="next"';
706
-
707
-		return $link;
708
-	}
709
-
710
-	/**
711
-	 * @return bool
712
-	 */
713
-	protected function isV2() {
714
-		return $this->request->getScriptName() === '/ocs/v2.php';
715
-	}
46
+    /** @var IGroupManager */
47
+    protected $groupManager;
48
+
49
+    /** @var IUserManager */
50
+    protected $userManager;
51
+
52
+    /** @var IManager */
53
+    protected $contactsManager;
54
+
55
+    /** @var IConfig */
56
+    protected $config;
57
+
58
+    /** @var IUserSession */
59
+    protected $userSession;
60
+
61
+    /** @var IURLGenerator */
62
+    protected $urlGenerator;
63
+
64
+    /** @var ILogger */
65
+    protected $logger;
66
+
67
+    /** @var \OCP\Share\IManager */
68
+    protected $shareManager;
69
+
70
+    /** @var IClientService */
71
+    protected $clientService;
72
+
73
+    /** @var ICloudIdManager  */
74
+    protected $cloudIdManager;
75
+
76
+    /** @var bool */
77
+    protected $shareWithGroupOnly = false;
78
+
79
+    /** @var bool */
80
+    protected $shareeEnumeration = true;
81
+
82
+    /** @var int */
83
+    protected $offset = 0;
84
+
85
+    /** @var int */
86
+    protected $limit = 10;
87
+
88
+    /** @var array */
89
+    protected $result = [
90
+        'exact' => [
91
+            'users' => [],
92
+            'groups' => [],
93
+            'remotes' => [],
94
+            'emails' => [],
95
+            'circles' => [],
96
+        ],
97
+        'users' => [],
98
+        'groups' => [],
99
+        'remotes' => [],
100
+        'emails' => [],
101
+        'lookup' => [],
102
+        'circles' => [],
103
+    ];
104
+
105
+    protected $reachedEndFor = [];
106
+
107
+    /**
108
+     * @param string $appName
109
+     * @param IRequest $request
110
+     * @param IGroupManager $groupManager
111
+     * @param IUserManager $userManager
112
+     * @param IManager $contactsManager
113
+     * @param IConfig $config
114
+     * @param IUserSession $userSession
115
+     * @param IURLGenerator $urlGenerator
116
+     * @param ILogger $logger
117
+     * @param \OCP\Share\IManager $shareManager
118
+     * @param IClientService $clientService
119
+     * @param ICloudIdManager $cloudIdManager
120
+     */
121
+    public function __construct($appName,
122
+                                IRequest $request,
123
+                                IGroupManager $groupManager,
124
+                                IUserManager $userManager,
125
+                                IManager $contactsManager,
126
+                                IConfig $config,
127
+                                IUserSession $userSession,
128
+                                IURLGenerator $urlGenerator,
129
+                                ILogger $logger,
130
+                                \OCP\Share\IManager $shareManager,
131
+                                IClientService $clientService,
132
+                                ICloudIdManager $cloudIdManager
133
+    ) {
134
+        parent::__construct($appName, $request);
135
+
136
+        $this->groupManager = $groupManager;
137
+        $this->userManager = $userManager;
138
+        $this->contactsManager = $contactsManager;
139
+        $this->config = $config;
140
+        $this->userSession = $userSession;
141
+        $this->urlGenerator = $urlGenerator;
142
+        $this->logger = $logger;
143
+        $this->shareManager = $shareManager;
144
+        $this->clientService = $clientService;
145
+        $this->cloudIdManager = $cloudIdManager;
146
+    }
147
+
148
+    /**
149
+     * @param string $search
150
+     */
151
+    protected function getUsers($search) {
152
+        $this->result['users'] = $this->result['exact']['users'] = $users = [];
153
+
154
+        $userGroups = [];
155
+        if ($this->shareWithGroupOnly) {
156
+            // Search in all the groups this user is part of
157
+            $userGroups = $this->groupManager->getUserGroupIds($this->userSession->getUser());
158
+            foreach ($userGroups as $userGroup) {
159
+                $usersTmp = $this->groupManager->displayNamesInGroup($userGroup, $search, $this->limit, $this->offset);
160
+                foreach ($usersTmp as $uid => $userDisplayName) {
161
+                    $users[$uid] = $userDisplayName;
162
+                }
163
+            }
164
+        } else {
165
+            // Search in all users
166
+            $usersTmp = $this->userManager->searchDisplayName($search, $this->limit, $this->offset);
167
+
168
+            foreach ($usersTmp as $user) {
169
+                $users[$user->getUID()] = $user->getDisplayName();
170
+            }
171
+        }
172
+
173
+        if (!$this->shareeEnumeration || sizeof($users) < $this->limit) {
174
+            $this->reachedEndFor[] = 'users';
175
+        }
176
+
177
+        $foundUserById = false;
178
+        $lowerSearch = strtolower($search);
179
+        foreach ($users as $uid => $userDisplayName) {
180
+            if (strtolower($uid) === $lowerSearch || strtolower($userDisplayName) === $lowerSearch) {
181
+                if (strtolower($uid) === $lowerSearch) {
182
+                    $foundUserById = true;
183
+                }
184
+                $this->result['exact']['users'][] = [
185
+                    'label' => $userDisplayName,
186
+                    'value' => [
187
+                        'shareType' => Share::SHARE_TYPE_USER,
188
+                        'shareWith' => $uid,
189
+                    ],
190
+                ];
191
+            } else {
192
+                $this->result['users'][] = [
193
+                    'label' => $userDisplayName,
194
+                    'value' => [
195
+                        'shareType' => Share::SHARE_TYPE_USER,
196
+                        'shareWith' => $uid,
197
+                    ],
198
+                ];
199
+            }
200
+        }
201
+
202
+        if ($this->offset === 0 && !$foundUserById) {
203
+            // On page one we try if the search result has a direct hit on the
204
+            // user id and if so, we add that to the exact match list
205
+            $user = $this->userManager->get($search);
206
+            if ($user instanceof IUser) {
207
+                $addUser = true;
208
+
209
+                if ($this->shareWithGroupOnly) {
210
+                    // Only add, if we have a common group
211
+                    $commonGroups = array_intersect($userGroups, $this->groupManager->getUserGroupIds($user));
212
+                    $addUser = !empty($commonGroups);
213
+                }
214
+
215
+                if ($addUser) {
216
+                    array_push($this->result['exact']['users'], [
217
+                        'label' => $user->getDisplayName(),
218
+                        'value' => [
219
+                            'shareType' => Share::SHARE_TYPE_USER,
220
+                            'shareWith' => $user->getUID(),
221
+                        ],
222
+                    ]);
223
+                }
224
+            }
225
+        }
226
+
227
+        if (!$this->shareeEnumeration) {
228
+            $this->result['users'] = [];
229
+        }
230
+    }
231
+
232
+    /**
233
+     * @param string $search
234
+     */
235
+    protected function getGroups($search) {
236
+        $this->result['groups'] = $this->result['exact']['groups'] = [];
237
+
238
+        $groups = $this->groupManager->search($search, $this->limit, $this->offset);
239
+        $groupIds = array_map(function (IGroup $group) { return $group->getGID(); }, $groups);
240
+
241
+        if (!$this->shareeEnumeration || sizeof($groups) < $this->limit) {
242
+            $this->reachedEndFor[] = 'groups';
243
+        }
244
+
245
+        $userGroups =  [];
246
+        if (!empty($groups) && $this->shareWithGroupOnly) {
247
+            // Intersect all the groups that match with the groups this user is a member of
248
+            $userGroups = $this->groupManager->getUserGroups($this->userSession->getUser());
249
+            $userGroups = array_map(function (IGroup $group) { return $group->getGID(); }, $userGroups);
250
+            $groupIds = array_intersect($groupIds, $userGroups);
251
+        }
252
+
253
+        $lowerSearch = strtolower($search);
254
+        foreach ($groups as $group) {
255
+            // FIXME: use a more efficient approach
256
+            $gid = $group->getGID();
257
+            if (!in_array($gid, $groupIds)) {
258
+                continue;
259
+            }
260
+            if (strtolower($gid) === $lowerSearch || strtolower($group->getDisplayName()) === $lowerSearch) {
261
+                $this->result['exact']['groups'][] = [
262
+                    'label' => $group->getDisplayName(),
263
+                    'value' => [
264
+                        'shareType' => Share::SHARE_TYPE_GROUP,
265
+                        'shareWith' => $gid,
266
+                    ],
267
+                ];
268
+            } else {
269
+                $this->result['groups'][] = [
270
+                    'label' => $group->getDisplayName(),
271
+                    'value' => [
272
+                        'shareType' => Share::SHARE_TYPE_GROUP,
273
+                        'shareWith' => $gid,
274
+                    ],
275
+                ];
276
+            }
277
+        }
278
+
279
+        if ($this->offset === 0 && empty($this->result['exact']['groups'])) {
280
+            // On page one we try if the search result has a direct hit on the
281
+            // user id and if so, we add that to the exact match list
282
+            $group = $this->groupManager->get($search);
283
+            if ($group instanceof IGroup && (!$this->shareWithGroupOnly || in_array($group->getGID(), $userGroups))) {
284
+                array_push($this->result['exact']['groups'], [
285
+                    'label' => $group->getDisplayName(),
286
+                    'value' => [
287
+                        'shareType' => Share::SHARE_TYPE_GROUP,
288
+                        'shareWith' => $group->getGID(),
289
+                    ],
290
+                ]);
291
+            }
292
+        }
293
+
294
+        if (!$this->shareeEnumeration) {
295
+            $this->result['groups'] = [];
296
+        }
297
+    }
298
+
299
+
300
+    /**
301
+     * @param string $search
302
+     */
303
+    protected function getCircles($search) {
304
+        $this->result['circles'] = $this->result['exact']['circles'] = [];
305
+
306
+        $result = \OCA\Circles\Api\Sharees::search($search, $this->limit, $this->offset);
307
+        if (array_key_exists('circles', $result['exact'])) {
308
+            $this->result['exact']['circles'] = $result['exact']['circles'];
309
+        }
310
+        if (array_key_exists('circles', $result)) {
311
+            $this->result['circles'] = $result['circles'];
312
+        }
313
+    }
314
+
315
+
316
+    /**
317
+     * @param string $search
318
+     * @return array
319
+     */
320
+    protected function getRemote($search) {
321
+        $result = ['results' => [], 'exact' => []];
322
+
323
+        // Search in contacts
324
+        //@todo Pagination missing
325
+        $addressBookContacts = $this->contactsManager->search($search, ['CLOUD', 'FN']);
326
+        $result['exactIdMatch'] = false;
327
+        foreach ($addressBookContacts as $contact) {
328
+            if (isset($contact['isLocalSystemBook'])) {
329
+                continue;
330
+            }
331
+            if (isset($contact['CLOUD'])) {
332
+                $cloudIds = $contact['CLOUD'];
333
+                if (!is_array($cloudIds)) {
334
+                    $cloudIds = [$cloudIds];
335
+                }
336
+                $lowerSearch = strtolower($search);
337
+                foreach ($cloudIds as $cloudId) {
338
+                    list(, $serverUrl) = $this->splitUserRemote($cloudId);
339
+                    if (strtolower($contact['FN']) === $lowerSearch || strtolower($cloudId) === $lowerSearch) {
340
+                        if (strtolower($cloudId) === $lowerSearch) {
341
+                            $result['exactIdMatch'] = true;
342
+                        }
343
+                        $result['exact'][] = [
344
+                            'label' => $contact['FN'] . " ($cloudId)",
345
+                            'value' => [
346
+                                'shareType' => Share::SHARE_TYPE_REMOTE,
347
+                                'shareWith' => $cloudId,
348
+                                'server' => $serverUrl,
349
+                            ],
350
+                        ];
351
+                    } else {
352
+                        $result['results'][] = [
353
+                            'label' => $contact['FN'] . " ($cloudId)",
354
+                            'value' => [
355
+                                'shareType' => Share::SHARE_TYPE_REMOTE,
356
+                                'shareWith' => $cloudId,
357
+                                'server' => $serverUrl,
358
+                            ],
359
+                        ];
360
+                    }
361
+                }
362
+            }
363
+        }
364
+
365
+        if (!$this->shareeEnumeration) {
366
+            $result['results'] = [];
367
+        }
368
+
369
+        if (!$result['exactIdMatch'] && $this->cloudIdManager->isValidCloudId($search) && $this->offset === 0) {
370
+            $result['exact'][] = [
371
+                'label' => $search,
372
+                'value' => [
373
+                    'shareType' => Share::SHARE_TYPE_REMOTE,
374
+                    'shareWith' => $search,
375
+                ],
376
+            ];
377
+        }
378
+
379
+        $this->reachedEndFor[] = 'remotes';
380
+
381
+        return $result;
382
+    }
383
+
384
+    /**
385
+     * split user and remote from federated cloud id
386
+     *
387
+     * @param string $address federated share address
388
+     * @return array [user, remoteURL]
389
+     * @throws \Exception
390
+     */
391
+    public function splitUserRemote($address) {
392
+        try {
393
+            $cloudId = $this->cloudIdManager->resolveCloudId($address);
394
+            return [$cloudId->getUser(), $cloudId->getRemote()];
395
+        } catch (\InvalidArgumentException $e) {
396
+            throw new \Exception('Invalid Federated Cloud ID', 0, $e);
397
+        }
398
+    }
399
+
400
+    /**
401
+     * Strips away a potential file names and trailing slashes:
402
+     * - http://localhost
403
+     * - http://localhost/
404
+     * - http://localhost/index.php
405
+     * - http://localhost/index.php/s/{shareToken}
406
+     *
407
+     * all return: http://localhost
408
+     *
409
+     * @param string $remote
410
+     * @return string
411
+     */
412
+    protected function fixRemoteURL($remote) {
413
+        $remote = str_replace('\\', '/', $remote);
414
+        if ($fileNamePosition = strpos($remote, '/index.php')) {
415
+            $remote = substr($remote, 0, $fileNamePosition);
416
+        }
417
+        $remote = rtrim($remote, '/');
418
+
419
+        return $remote;
420
+    }
421
+
422
+    /**
423
+     * @NoAdminRequired
424
+     *
425
+     * @param string $search
426
+     * @param string $itemType
427
+     * @param int $page
428
+     * @param int $perPage
429
+     * @param int|int[] $shareType
430
+     * @param bool $lookup
431
+     * @return DataResponse
432
+     * @throws OCSBadRequestException
433
+     */
434
+    public function search($search = '', $itemType = null, $page = 1, $perPage = 200, $shareType = null, $lookup = true) {
435
+
436
+        // only search for string larger than a given threshold
437
+        $threshold = (int)$this->config->getSystemValue('sharing.minSearchStringLength', 0);
438
+        if (strlen($search) < $threshold) {
439
+            return new DataResponse($this->result);
440
+        }
441
+
442
+        // never return more than the max. number of results configured in the config.php
443
+        $maxResults = (int)$this->config->getSystemValue('sharing.maxAutocompleteResults', 0);
444
+        if ($maxResults > 0) {
445
+            $perPage = min($perPage, $maxResults);
446
+        }
447
+        if ($perPage <= 0) {
448
+            throw new OCSBadRequestException('Invalid perPage argument');
449
+        }
450
+        if ($page <= 0) {
451
+            throw new OCSBadRequestException('Invalid page');
452
+        }
453
+
454
+        $shareTypes = [
455
+            Share::SHARE_TYPE_USER,
456
+        ];
457
+
458
+        if ($itemType === 'file' || $itemType === 'folder') {
459
+            if ($this->shareManager->allowGroupSharing()) {
460
+                $shareTypes[] = Share::SHARE_TYPE_GROUP;
461
+            }
462
+
463
+            if ($this->isRemoteSharingAllowed($itemType)) {
464
+                $shareTypes[] = Share::SHARE_TYPE_REMOTE;
465
+            }
466
+
467
+            if ($this->shareManager->shareProviderExists(Share::SHARE_TYPE_EMAIL)) {
468
+                $shareTypes[] = Share::SHARE_TYPE_EMAIL;
469
+            }
470
+        } else {
471
+            $shareTypes[] = Share::SHARE_TYPE_GROUP;
472
+            $shareTypes[] = Share::SHARE_TYPE_EMAIL;
473
+        }
474
+
475
+        if (\OCP\App::isEnabled('circles')) {
476
+            $shareTypes[] = Share::SHARE_TYPE_CIRCLE;
477
+        }
478
+
479
+        if (isset($_GET['shareType']) && is_array($_GET['shareType'])) {
480
+            $shareTypes = array_intersect($shareTypes, $_GET['shareType']);
481
+            sort($shareTypes);
482
+        } else if (is_numeric($shareType)) {
483
+            $shareTypes = array_intersect($shareTypes, [(int) $shareType]);
484
+            sort($shareTypes);
485
+        }
486
+
487
+        $this->shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
488
+        $this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes';
489
+        $this->limit = (int) $perPage;
490
+        $this->offset = $perPage * ($page - 1);
491
+
492
+        return $this->searchSharees($search, $itemType, $shareTypes, $page, $perPage, $lookup);
493
+    }
494
+
495
+    /**
496
+     * Method to get out the static call for better testing
497
+     *
498
+     * @param string $itemType
499
+     * @return bool
500
+     */
501
+    protected function isRemoteSharingAllowed($itemType) {
502
+        try {
503
+            $backend = Share::getBackend($itemType);
504
+            return $backend->isShareTypeAllowed(Share::SHARE_TYPE_REMOTE);
505
+        } catch (\Exception $e) {
506
+            return false;
507
+        }
508
+    }
509
+
510
+    /**
511
+     * Testable search function that does not need globals
512
+     *
513
+     * @param string $search
514
+     * @param string $itemType
515
+     * @param array $shareTypes
516
+     * @param int $page
517
+     * @param int $perPage
518
+     * @param bool $lookup
519
+     * @return DataResponse
520
+     * @throws OCSBadRequestException
521
+     */
522
+    protected function searchSharees($search, $itemType, array $shareTypes, $page, $perPage, $lookup) {
523
+        // Verify arguments
524
+        if ($itemType === null) {
525
+            throw new OCSBadRequestException('Missing itemType');
526
+        }
527
+
528
+        // Get users
529
+        if (in_array(Share::SHARE_TYPE_USER, $shareTypes)) {
530
+            $this->getUsers($search);
531
+        }
532
+
533
+        // Get groups
534
+        if (in_array(Share::SHARE_TYPE_GROUP, $shareTypes)) {
535
+            $this->getGroups($search);
536
+        }
537
+
538
+        // Get circles
539
+        if (in_array(Share::SHARE_TYPE_CIRCLE, $shareTypes)) {
540
+            $this->getCircles($search);
541
+        }
542
+
543
+
544
+        // Get remote
545
+        $remoteResults = ['results' => [], 'exact' => [], 'exactIdMatch' => false];
546
+        if (in_array(Share::SHARE_TYPE_REMOTE, $shareTypes)) {
547
+            $remoteResults = $this->getRemote($search);
548
+        }
549
+
550
+        // Get emails
551
+        $mailResults = ['results' => [], 'exact' => [], 'exactIdMatch' => false];
552
+        if (in_array(Share::SHARE_TYPE_EMAIL, $shareTypes)) {
553
+            $mailResults = $this->getEmail($search);
554
+        }
555
+
556
+        // Get from lookup server
557
+        if ($lookup) {
558
+            $this->getLookup($search);
559
+        }
560
+
561
+        // if we have a exact match, either for the federated cloud id or for the
562
+        // email address we only return the exact match. It is highly unlikely
563
+        // that the exact same email address and federated cloud id exists
564
+        if ($mailResults['exactIdMatch'] && !$remoteResults['exactIdMatch']) {
565
+            $this->result['emails'] = $mailResults['results'];
566
+            $this->result['exact']['emails'] = $mailResults['exact'];
567
+        } else if (!$mailResults['exactIdMatch'] && $remoteResults['exactIdMatch']) {
568
+            $this->result['remotes'] = $remoteResults['results'];
569
+            $this->result['exact']['remotes'] = $remoteResults['exact'];
570
+        } else {
571
+            $this->result['remotes'] = $remoteResults['results'];
572
+            $this->result['exact']['remotes'] = $remoteResults['exact'];
573
+            $this->result['emails'] = $mailResults['results'];
574
+            $this->result['exact']['emails'] = $mailResults['exact'];
575
+        }
576
+
577
+        $response = new DataResponse($this->result);
578
+
579
+        if (sizeof($this->reachedEndFor) < 3) {
580
+            $response->addHeader('Link', $this->getPaginationLink($page, [
581
+                'search' => $search,
582
+                'itemType' => $itemType,
583
+                'shareType' => $shareTypes,
584
+                'perPage' => $perPage,
585
+            ]));
586
+        }
587
+
588
+        return $response;
589
+    }
590
+
591
+    /**
592
+     * @param string $search
593
+     * @return array
594
+     */
595
+    protected function getEmail($search) {
596
+        $result = ['results' => [], 'exact' => []];
597
+
598
+        // Search in contacts
599
+        //@todo Pagination missing
600
+        $addressBookContacts = $this->contactsManager->search($search, ['EMAIL', 'FN']);
601
+        $result['exactIdMatch'] = false;
602
+        foreach ($addressBookContacts as $contact) {
603
+            if (isset($contact['isLocalSystemBook'])) {
604
+                continue;
605
+            }
606
+            if (isset($contact['EMAIL'])) {
607
+                $emailAddresses = $contact['EMAIL'];
608
+                if (!is_array($emailAddresses)) {
609
+                    $emailAddresses = [$emailAddresses];
610
+                }
611
+                foreach ($emailAddresses as $emailAddress) {
612
+                    if (strtolower($contact['FN']) === strtolower($search) || strtolower($emailAddress) === strtolower($search)) {
613
+                        if (strtolower($emailAddress) === strtolower($search)) {
614
+                            $result['exactIdMatch'] = true;
615
+                        }
616
+                        $result['exact'][] = [
617
+                            'label' => $contact['FN'] . " ($emailAddress)",
618
+                            'value' => [
619
+                                'shareType' => Share::SHARE_TYPE_EMAIL,
620
+                                'shareWith' => $emailAddress,
621
+                            ],
622
+                        ];
623
+                    } else {
624
+                        $result['results'][] = [
625
+                            'label' => $contact['FN'] . " ($emailAddress)",
626
+                            'value' => [
627
+                                'shareType' => Share::SHARE_TYPE_EMAIL,
628
+                                'shareWith' => $emailAddress,
629
+                            ],
630
+                        ];
631
+                    }
632
+                }
633
+            }
634
+        }
635
+
636
+        if (!$this->shareeEnumeration) {
637
+            $result['results'] = [];
638
+        }
639
+
640
+        if (!$result['exactIdMatch'] && filter_var($search, FILTER_VALIDATE_EMAIL)) {
641
+            $result['exact'][] = [
642
+                'label' => $search,
643
+                'value' => [
644
+                    'shareType' => Share::SHARE_TYPE_EMAIL,
645
+                    'shareWith' => $search,
646
+                ],
647
+            ];
648
+        }
649
+
650
+        $this->reachedEndFor[] = 'emails';
651
+
652
+        return $result;
653
+    }
654
+
655
+    protected function getLookup($search) {
656
+        $isEnabled = $this->config->getAppValue('files_sharing', 'lookupServerEnabled', 'no');
657
+        $lookupServerUrl = $this->config->getSystemValue('lookup_server', 'https://lookup.nextcloud.com');
658
+        $lookupServerUrl = rtrim($lookupServerUrl, '/');
659
+        $result = [];
660
+
661
+        if($isEnabled === 'yes') {
662
+            try {
663
+                $client = $this->clientService->newClient();
664
+                $response = $client->get(
665
+                    $lookupServerUrl . '/users?search=' . urlencode($search),
666
+                    [
667
+                        'timeout' => 10,
668
+                        'connect_timeout' => 3,
669
+                    ]
670
+                );
671
+
672
+                $body = json_decode($response->getBody(), true);
673
+
674
+                $result = [];
675
+                foreach ($body as $lookup) {
676
+                    $result[] = [
677
+                        'label' => $lookup['federationId'],
678
+                        'value' => [
679
+                            'shareType' => Share::SHARE_TYPE_REMOTE,
680
+                            'shareWith' => $lookup['federationId'],
681
+                        ],
682
+                        'extra' => $lookup,
683
+                    ];
684
+                }
685
+            } catch (\Exception $e) {}
686
+        }
687
+
688
+        $this->result['lookup'] = $result;
689
+    }
690
+
691
+    /**
692
+     * Generates a bunch of pagination links for the current page
693
+     *
694
+     * @param int $page Current page
695
+     * @param array $params Parameters for the URL
696
+     * @return string
697
+     */
698
+    protected function getPaginationLink($page, array $params) {
699
+        if ($this->isV2()) {
700
+            $url = $this->urlGenerator->getAbsoluteURL('/ocs/v2.php/apps/files_sharing/api/v1/sharees') . '?';
701
+        } else {
702
+            $url = $this->urlGenerator->getAbsoluteURL('/ocs/v1.php/apps/files_sharing/api/v1/sharees') . '?';
703
+        }
704
+        $params['page'] = $page + 1;
705
+        $link = '<' . $url . http_build_query($params) . '>; rel="next"';
706
+
707
+        return $link;
708
+    }
709
+
710
+    /**
711
+     * @return bool
712
+     */
713
+    protected function isV2() {
714
+        return $this->request->getScriptName() === '/ocs/v2.php';
715
+    }
716 716
 }
Please login to merge, or discard this patch.
Spacing   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -236,17 +236,17 @@  discard block
 block discarded – undo
236 236
 		$this->result['groups'] = $this->result['exact']['groups'] = [];
237 237
 
238 238
 		$groups = $this->groupManager->search($search, $this->limit, $this->offset);
239
-		$groupIds = array_map(function (IGroup $group) { return $group->getGID(); }, $groups);
239
+		$groupIds = array_map(function(IGroup $group) { return $group->getGID(); }, $groups);
240 240
 
241 241
 		if (!$this->shareeEnumeration || sizeof($groups) < $this->limit) {
242 242
 			$this->reachedEndFor[] = 'groups';
243 243
 		}
244 244
 
245
-		$userGroups =  [];
245
+		$userGroups = [];
246 246
 		if (!empty($groups) && $this->shareWithGroupOnly) {
247 247
 			// Intersect all the groups that match with the groups this user is a member of
248 248
 			$userGroups = $this->groupManager->getUserGroups($this->userSession->getUser());
249
-			$userGroups = array_map(function (IGroup $group) { return $group->getGID(); }, $userGroups);
249
+			$userGroups = array_map(function(IGroup $group) { return $group->getGID(); }, $userGroups);
250 250
 			$groupIds = array_intersect($groupIds, $userGroups);
251 251
 		}
252 252
 
@@ -341,7 +341,7 @@  discard block
 block discarded – undo
341 341
 							$result['exactIdMatch'] = true;
342 342
 						}
343 343
 						$result['exact'][] = [
344
-							'label' => $contact['FN'] . " ($cloudId)",
344
+							'label' => $contact['FN']." ($cloudId)",
345 345
 							'value' => [
346 346
 								'shareType' => Share::SHARE_TYPE_REMOTE,
347 347
 								'shareWith' => $cloudId,
@@ -350,7 +350,7 @@  discard block
 block discarded – undo
350 350
 						];
351 351
 					} else {
352 352
 						$result['results'][] = [
353
-							'label' => $contact['FN'] . " ($cloudId)",
353
+							'label' => $contact['FN']." ($cloudId)",
354 354
 							'value' => [
355 355
 								'shareType' => Share::SHARE_TYPE_REMOTE,
356 356
 								'shareWith' => $cloudId,
@@ -434,13 +434,13 @@  discard block
 block discarded – undo
434 434
 	public function search($search = '', $itemType = null, $page = 1, $perPage = 200, $shareType = null, $lookup = true) {
435 435
 
436 436
 		// only search for string larger than a given threshold
437
-		$threshold = (int)$this->config->getSystemValue('sharing.minSearchStringLength', 0);
437
+		$threshold = (int) $this->config->getSystemValue('sharing.minSearchStringLength', 0);
438 438
 		if (strlen($search) < $threshold) {
439 439
 			return new DataResponse($this->result);
440 440
 		}
441 441
 
442 442
 		// never return more than the max. number of results configured in the config.php
443
-		$maxResults = (int)$this->config->getSystemValue('sharing.maxAutocompleteResults', 0);
443
+		$maxResults = (int) $this->config->getSystemValue('sharing.maxAutocompleteResults', 0);
444 444
 		if ($maxResults > 0) {
445 445
 			$perPage = min($perPage, $maxResults);
446 446
 		}
@@ -614,7 +614,7 @@  discard block
 block discarded – undo
614 614
 							$result['exactIdMatch'] = true;
615 615
 						}
616 616
 						$result['exact'][] = [
617
-							'label' => $contact['FN'] . " ($emailAddress)",
617
+							'label' => $contact['FN']." ($emailAddress)",
618 618
 							'value' => [
619 619
 								'shareType' => Share::SHARE_TYPE_EMAIL,
620 620
 								'shareWith' => $emailAddress,
@@ -622,7 +622,7 @@  discard block
 block discarded – undo
622 622
 						];
623 623
 					} else {
624 624
 						$result['results'][] = [
625
-							'label' => $contact['FN'] . " ($emailAddress)",
625
+							'label' => $contact['FN']." ($emailAddress)",
626 626
 							'value' => [
627 627
 								'shareType' => Share::SHARE_TYPE_EMAIL,
628 628
 								'shareWith' => $emailAddress,
@@ -658,11 +658,11 @@  discard block
 block discarded – undo
658 658
 		$lookupServerUrl = rtrim($lookupServerUrl, '/');
659 659
 		$result = [];
660 660
 
661
-		if($isEnabled === 'yes') {
661
+		if ($isEnabled === 'yes') {
662 662
 			try {
663 663
 				$client = $this->clientService->newClient();
664 664
 				$response = $client->get(
665
-					$lookupServerUrl . '/users?search=' . urlencode($search),
665
+					$lookupServerUrl.'/users?search='.urlencode($search),
666 666
 					[
667 667
 						'timeout' => 10,
668 668
 						'connect_timeout' => 3,
@@ -697,12 +697,12 @@  discard block
 block discarded – undo
697 697
 	 */
698 698
 	protected function getPaginationLink($page, array $params) {
699 699
 		if ($this->isV2()) {
700
-			$url = $this->urlGenerator->getAbsoluteURL('/ocs/v2.php/apps/files_sharing/api/v1/sharees') . '?';
700
+			$url = $this->urlGenerator->getAbsoluteURL('/ocs/v2.php/apps/files_sharing/api/v1/sharees').'?';
701 701
 		} else {
702
-			$url = $this->urlGenerator->getAbsoluteURL('/ocs/v1.php/apps/files_sharing/api/v1/sharees') . '?';
702
+			$url = $this->urlGenerator->getAbsoluteURL('/ocs/v1.php/apps/files_sharing/api/v1/sharees').'?';
703 703
 		}
704 704
 		$params['page'] = $page + 1;
705
-		$link = '<' . $url . http_build_query($params) . '>; rel="next"';
705
+		$link = '<'.$url.http_build_query($params).'>; rel="next"';
706 706
 
707 707
 		return $link;
708 708
 	}
Please login to merge, or discard this patch.
settings/Controller/UsersController.php 2 patches
Spacing   +45 added lines, -45 removed lines patch added patch discarded remove patch
@@ -157,7 +157,7 @@  discard block
 block discarded – undo
157 157
 
158 158
 		// check for encryption state - TODO see formatUserForIndex
159 159
 		$this->isEncryptionAppEnabled = $appManager->isEnabledForUser('encryption');
160
-		if($this->isEncryptionAppEnabled) {
160
+		if ($this->isEncryptionAppEnabled) {
161 161
 			// putting this directly in empty is possible in PHP 5.5+
162 162
 			$result = $config->getAppValue('encryption', 'recoveryAdminEnabled', 0);
163 163
 			$this->isRestoreEnabled = !empty($result);
@@ -197,7 +197,7 @@  discard block
 block discarded – undo
197 197
 		}
198 198
 
199 199
 		$subAdminGroups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($user);
200
-		foreach($subAdminGroups as $key => $subAdminGroup) {
200
+		foreach ($subAdminGroups as $key => $subAdminGroup) {
201 201
 			$subAdminGroups[$key] = $subAdminGroup->getGID();
202 202
 		}
203 203
 
@@ -254,16 +254,16 @@  discard block
 block discarded – undo
254 254
 	 */
255 255
 	public function index($offset = 0, $limit = 10, $gid = '', $pattern = '', $backend = '') {
256 256
 		// FIXME: The JS sends the group '_everyone' instead of no GID for the "all users" group.
257
-		if($gid === '_everyone') {
257
+		if ($gid === '_everyone') {
258 258
 			$gid = '';
259 259
 		}
260 260
 
261 261
 		// Remove backends
262
-		if(!empty($backend)) {
262
+		if (!empty($backend)) {
263 263
 			$activeBackends = $this->userManager->getBackends();
264 264
 			$this->userManager->clearBackends();
265
-			foreach($activeBackends as $singleActiveBackend) {
266
-				if($backend === get_class($singleActiveBackend)) {
265
+			foreach ($activeBackends as $singleActiveBackend) {
266
+				if ($backend === get_class($singleActiveBackend)) {
267 267
 					$this->userManager->registerBackend($singleActiveBackend);
268 268
 					break;
269 269
 				}
@@ -273,7 +273,7 @@  discard block
 block discarded – undo
273 273
 		$users = [];
274 274
 		if ($this->isAdmin) {
275 275
 
276
-			if($gid !== '') {
276
+			if ($gid !== '') {
277 277
 				$batch = $this->getUsersForUID($this->groupManager->displayNamesInGroup($gid, $pattern, $limit, $offset));
278 278
 			} else {
279 279
 				$batch = $this->userManager->search($pattern, $limit, $offset);
@@ -293,17 +293,17 @@  discard block
 block discarded – undo
293 293
 			$subAdminOfGroups = $gids;
294 294
 
295 295
 			// Set the $gid parameter to an empty value if the subadmin has no rights to access a specific group
296
-			if($gid !== '' && !in_array($gid, $subAdminOfGroups)) {
296
+			if ($gid !== '' && !in_array($gid, $subAdminOfGroups)) {
297 297
 				$gid = '';
298 298
 			}
299 299
 
300 300
 			// Batch all groups the user is subadmin of when a group is specified
301 301
 			$batch = [];
302
-			if($gid === '') {
303
-				foreach($subAdminOfGroups as $group) {
302
+			if ($gid === '') {
303
+				foreach ($subAdminOfGroups as $group) {
304 304
 					$groupUsers = $this->groupManager->displayNamesInGroup($group, $pattern, $limit, $offset);
305 305
 
306
-					foreach($groupUsers as $uid => $displayName) {
306
+					foreach ($groupUsers as $uid => $displayName) {
307 307
 						$batch[$uid] = $displayName;
308 308
 					}
309 309
 				}
@@ -335,11 +335,11 @@  discard block
 block discarded – undo
335 335
 	 * @param string $email
336 336
 	 * @return DataResponse
337 337
 	 */
338
-	public function create($username, $password, array $groups=array(), $email='') {
339
-		if($email !== '' && !$this->mailer->validateMailAddress($email)) {
338
+	public function create($username, $password, array $groups = array(), $email = '') {
339
+		if ($email !== '' && !$this->mailer->validateMailAddress($email)) {
340 340
 			return new DataResponse(
341 341
 				array(
342
-					'message' => (string)$this->l10n->t('Invalid mail address')
342
+					'message' => (string) $this->l10n->t('Invalid mail address')
343 343
 				),
344 344
 				Http::STATUS_UNPROCESSABLE_ENTITY
345 345
 			);
@@ -351,7 +351,7 @@  discard block
 block discarded – undo
351 351
 			if (!empty($groups)) {
352 352
 				foreach ($groups as $key => $group) {
353 353
 					$groupObject = $this->groupManager->get($group);
354
-					if($groupObject === null) {
354
+					if ($groupObject === null) {
355 355
 						unset($groups[$key]);
356 356
 						continue;
357 357
 					}
@@ -375,7 +375,7 @@  discard block
 block discarded – undo
375 375
 		if ($this->userManager->userExists($username)) {
376 376
 			return new DataResponse(
377 377
 				array(
378
-					'message' => (string)$this->l10n->t('A user with that name already exists.')
378
+					'message' => (string) $this->l10n->t('A user with that name already exists.')
379 379
 				),
380 380
 				Http::STATUS_CONFLICT
381 381
 			);
@@ -386,7 +386,7 @@  discard block
 block discarded – undo
386 386
 			if ($email === '') {
387 387
 				return new DataResponse(
388 388
 					array(
389
-						'message' => (string)$this->l10n->t('To send a password link to the user an email address is required.')
389
+						'message' => (string) $this->l10n->t('To send a password link to the user an email address is required.')
390 390
 					),
391 391
 					Http::STATUS_UNPROCESSABLE_ENTITY
392 392
 				);
@@ -411,12 +411,12 @@  discard block
 block discarded – undo
411 411
 			);
412 412
 		}
413 413
 
414
-		if($user instanceof IUser) {
415
-			if($groups !== null) {
416
-				foreach($groups as $groupName) {
414
+		if ($user instanceof IUser) {
415
+			if ($groups !== null) {
416
+				foreach ($groups as $groupName) {
417 417
 					$group = $this->groupManager->get($groupName);
418 418
 
419
-					if(empty($group)) {
419
+					if (empty($group)) {
420 420
 						$group = $this->groupManager->createGroup($groupName);
421 421
 					}
422 422
 					$group->addUser($user);
@@ -425,13 +425,13 @@  discard block
 block discarded – undo
425 425
 			/**
426 426
 			 * Send new user mail only if a mail is set
427 427
 			 */
428
-			if($email !== '') {
428
+			if ($email !== '') {
429 429
 				$user->setEMailAddress($email);
430 430
 				try {
431 431
 					$emailTemplate = $this->newUserMailHelper->generateTemplate($user, $generatePasswordResetToken);
432 432
 					$this->newUserMailHelper->sendMail($user, $emailTemplate);
433
-				} catch(\Exception $e) {
434
-					$this->log->error("Can't send new user mail to $email: " . $e->getMessage(), array('app' => 'settings'));
433
+				} catch (\Exception $e) {
434
+					$this->log->error("Can't send new user mail to $email: ".$e->getMessage(), array('app' => 'settings'));
435 435
 				}
436 436
 			}
437 437
 			// fetch users groups
@@ -445,7 +445,7 @@  discard block
 block discarded – undo
445 445
 
446 446
 		return new DataResponse(
447 447
 			array(
448
-				'message' => (string)$this->l10n->t('Unable to create user.')
448
+				'message' => (string) $this->l10n->t('Unable to create user.')
449 449
 			),
450 450
 			Http::STATUS_FORBIDDEN
451 451
 		);
@@ -463,32 +463,32 @@  discard block
 block discarded – undo
463 463
 		$userId = $this->userSession->getUser()->getUID();
464 464
 		$user = $this->userManager->get($id);
465 465
 
466
-		if($userId === $id) {
466
+		if ($userId === $id) {
467 467
 			return new DataResponse(
468 468
 				array(
469 469
 					'status' => 'error',
470 470
 					'data' => array(
471
-						'message' => (string)$this->l10n->t('Unable to delete user.')
471
+						'message' => (string) $this->l10n->t('Unable to delete user.')
472 472
 					)
473 473
 				),
474 474
 				Http::STATUS_FORBIDDEN
475 475
 			);
476 476
 		}
477 477
 
478
-		if(!$this->isAdmin && !$this->groupManager->getSubAdmin()->isUserAccessible($this->userSession->getUser(), $user)) {
478
+		if (!$this->isAdmin && !$this->groupManager->getSubAdmin()->isUserAccessible($this->userSession->getUser(), $user)) {
479 479
 			return new DataResponse(
480 480
 				array(
481 481
 					'status' => 'error',
482 482
 					'data' => array(
483
-						'message' => (string)$this->l10n->t('Authentication error')
483
+						'message' => (string) $this->l10n->t('Authentication error')
484 484
 					)
485 485
 				),
486 486
 				Http::STATUS_FORBIDDEN
487 487
 			);
488 488
 		}
489 489
 
490
-		if($user) {
491
-			if($user->delete()) {
490
+		if ($user) {
491
+			if ($user->delete()) {
492 492
 				return new DataResponse(
493 493
 					array(
494 494
 						'status' => 'success',
@@ -505,7 +505,7 @@  discard block
 block discarded – undo
505 505
 			array(
506 506
 				'status' => 'error',
507 507
 				'data' => array(
508
-					'message' => (string)$this->l10n->t('Unable to delete user.')
508
+					'message' => (string) $this->l10n->t('Unable to delete user.')
509 509
 				)
510 510
 			),
511 511
 			Http::STATUS_FORBIDDEN
@@ -530,11 +530,11 @@  discard block
 block discarded – undo
530 530
 
531 531
 		$accountData = $this->accountManager->getUser($user);
532 532
 		$cloudId = $user->getCloudId();
533
-		$message = "Use my Federated Cloud ID to share with me: " . $cloudId;
533
+		$message = "Use my Federated Cloud ID to share with me: ".$cloudId;
534 534
 		$signature = $this->signMessage($user, $message);
535 535
 
536
-		$code = $message . ' ' . $signature;
537
-		$codeMd5 = $message . ' ' . md5($signature);
536
+		$code = $message.' '.$signature;
537
+		$codeMd5 = $message.' '.md5($signature);
538 538
 
539 539
 		switch ($account) {
540 540
 			case 'verify-twitter':
@@ -631,12 +631,12 @@  discard block
 block discarded – undo
631 631
 									$twitterScope
632 632
 	) {
633 633
 
634
-		if(!empty($email) && !$this->mailer->validateMailAddress($email)) {
634
+		if (!empty($email) && !$this->mailer->validateMailAddress($email)) {
635 635
 			return new DataResponse(
636 636
 				array(
637 637
 					'status' => 'error',
638 638
 					'data' => array(
639
-						'message' => (string)$this->l10n->t('Invalid mail address')
639
+						'message' => (string) $this->l10n->t('Invalid mail address')
640 640
 					)
641 641
 				),
642 642
 				Http::STATUS_UNPROCESSABLE_ENTITY
@@ -671,7 +671,7 @@  discard block
 block discarded – undo
671 671
 						'websiteScope' => $websiteScope,
672 672
 						'address' => $address,
673 673
 						'addressScope' => $addressScope,
674
-						'message' => (string)$this->l10n->t('Settings saved')
674
+						'message' => (string) $this->l10n->t('Settings saved')
675 675
 					)
676 676
 				),
677 677
 				Http::STATUS_OK
@@ -748,7 +748,7 @@  discard block
 block discarded – undo
748 748
 
749 749
 			$uniqueUsers = [];
750 750
 			foreach ($groups as $group) {
751
-				foreach($group->getUsers() as $uid => $displayName) {
751
+				foreach ($group->getUsers() as $uid => $displayName) {
752 752
 					$uniqueUsers[$uid] = true;
753 753
 				}
754 754
 			}
@@ -842,19 +842,19 @@  discard block
 block discarded – undo
842 842
 				array(
843 843
 					'status' => 'error',
844 844
 					'data' => array(
845
-						'message' => (string)$this->l10n->t('Forbidden')
845
+						'message' => (string) $this->l10n->t('Forbidden')
846 846
 					)
847 847
 				),
848 848
 				Http::STATUS_FORBIDDEN
849 849
 			);
850 850
 		}
851 851
 
852
-		if($mailAddress !== '' && !$this->mailer->validateMailAddress($mailAddress)) {
852
+		if ($mailAddress !== '' && !$this->mailer->validateMailAddress($mailAddress)) {
853 853
 			return new DataResponse(
854 854
 				array(
855 855
 					'status' => 'error',
856 856
 					'data' => array(
857
-						'message' => (string)$this->l10n->t('Invalid mail address')
857
+						'message' => (string) $this->l10n->t('Invalid mail address')
858 858
 					)
859 859
 				),
860 860
 				Http::STATUS_UNPROCESSABLE_ENTITY
@@ -866,7 +866,7 @@  discard block
 block discarded – undo
866 866
 				array(
867 867
 					'status' => 'error',
868 868
 					'data' => array(
869
-						'message' => (string)$this->l10n->t('Invalid user')
869
+						'message' => (string) $this->l10n->t('Invalid user')
870 870
 					)
871 871
 				),
872 872
 				Http::STATUS_UNPROCESSABLE_ENTITY
@@ -879,7 +879,7 @@  discard block
 block discarded – undo
879 879
 				array(
880 880
 					'status' => 'error',
881 881
 					'data' => array(
882
-						'message' => (string)$this->l10n->t('Unable to change mail address')
882
+						'message' => (string) $this->l10n->t('Unable to change mail address')
883 883
 					)
884 884
 				),
885 885
 				Http::STATUS_FORBIDDEN
@@ -897,7 +897,7 @@  discard block
 block discarded – undo
897 897
 					'data' => array(
898 898
 						'username' => $id,
899 899
 						'mailAddress' => $mailAddress,
900
-						'message' => (string)$this->l10n->t('Email saved')
900
+						'message' => (string) $this->l10n->t('Email saved')
901 901
 					)
902 902
 				),
903 903
 				Http::STATUS_OK
Please login to merge, or discard this patch.
Indentation   +853 added lines, -853 removed lines patch added patch discarded remove patch
@@ -58,858 +58,858 @@
 block discarded – undo
58 58
  * @package OC\Settings\Controller
59 59
  */
60 60
 class UsersController extends Controller {
61
-	/** @var IL10N */
62
-	private $l10n;
63
-	/** @var IUserSession */
64
-	private $userSession;
65
-	/** @var bool */
66
-	private $isAdmin;
67
-	/** @var IUserManager */
68
-	private $userManager;
69
-	/** @var IGroupManager */
70
-	private $groupManager;
71
-	/** @var IConfig */
72
-	private $config;
73
-	/** @var ILogger */
74
-	private $log;
75
-	/** @var IMailer */
76
-	private $mailer;
77
-	/** @var bool contains the state of the encryption app */
78
-	private $isEncryptionAppEnabled;
79
-	/** @var bool contains the state of the admin recovery setting */
80
-	private $isRestoreEnabled = false;
81
-	/** @var IAvatarManager */
82
-	private $avatarManager;
83
-	/** @var AccountManager */
84
-	private $accountManager;
85
-	/** @var ISecureRandom */
86
-	private $secureRandom;
87
-	/** @var NewUserMailHelper */
88
-	private $newUserMailHelper;
89
-	/** @var ITimeFactory */
90
-	private $timeFactory;
91
-	/** @var ICrypto */
92
-	private $crypto;
93
-	/** @var Manager */
94
-	private $keyManager;
95
-	/** @var IJobList */
96
-	private $jobList;
97
-
98
-	/**
99
-	 * @param string $appName
100
-	 * @param IRequest $request
101
-	 * @param IUserManager $userManager
102
-	 * @param IGroupManager $groupManager
103
-	 * @param IUserSession $userSession
104
-	 * @param IConfig $config
105
-	 * @param bool $isAdmin
106
-	 * @param IL10N $l10n
107
-	 * @param ILogger $log
108
-	 * @param IMailer $mailer
109
-	 * @param IURLGenerator $urlGenerator
110
-	 * @param IAppManager $appManager
111
-	 * @param IAvatarManager $avatarManager
112
-	 * @param AccountManager $accountManager
113
-	 * @param ISecureRandom $secureRandom
114
-	 * @param NewUserMailHelper $newUserMailHelper
115
-	 * @param ITimeFactory $timeFactory
116
-	 * @param ICrypto $crypto
117
-	 * @param Manager $keyManager
118
-	 * @param IJobList $jobList
119
-	 */
120
-	public function __construct($appName,
121
-								IRequest $request,
122
-								IUserManager $userManager,
123
-								IGroupManager $groupManager,
124
-								IUserSession $userSession,
125
-								IConfig $config,
126
-								$isAdmin,
127
-								IL10N $l10n,
128
-								ILogger $log,
129
-								IMailer $mailer,
130
-								IURLGenerator $urlGenerator,
131
-								IAppManager $appManager,
132
-								IAvatarManager $avatarManager,
133
-								AccountManager $accountManager,
134
-								ISecureRandom $secureRandom,
135
-								NewUserMailHelper $newUserMailHelper,
136
-								ITimeFactory $timeFactory,
137
-								ICrypto $crypto,
138
-								Manager $keyManager,
139
-								IJobList $jobList) {
140
-		parent::__construct($appName, $request);
141
-		$this->userManager = $userManager;
142
-		$this->groupManager = $groupManager;
143
-		$this->userSession = $userSession;
144
-		$this->config = $config;
145
-		$this->isAdmin = $isAdmin;
146
-		$this->l10n = $l10n;
147
-		$this->log = $log;
148
-		$this->mailer = $mailer;
149
-		$this->avatarManager = $avatarManager;
150
-		$this->accountManager = $accountManager;
151
-		$this->secureRandom = $secureRandom;
152
-		$this->newUserMailHelper = $newUserMailHelper;
153
-		$this->timeFactory = $timeFactory;
154
-		$this->crypto = $crypto;
155
-		$this->keyManager = $keyManager;
156
-		$this->jobList = $jobList;
157
-
158
-		// check for encryption state - TODO see formatUserForIndex
159
-		$this->isEncryptionAppEnabled = $appManager->isEnabledForUser('encryption');
160
-		if($this->isEncryptionAppEnabled) {
161
-			// putting this directly in empty is possible in PHP 5.5+
162
-			$result = $config->getAppValue('encryption', 'recoveryAdminEnabled', 0);
163
-			$this->isRestoreEnabled = !empty($result);
164
-		}
165
-	}
166
-
167
-	/**
168
-	 * @param IUser $user
169
-	 * @param array $userGroups
170
-	 * @return array
171
-	 */
172
-	private function formatUserForIndex(IUser $user, array $userGroups = null) {
173
-
174
-		// TODO: eliminate this encryption specific code below and somehow
175
-		// hook in additional user info from other apps
176
-
177
-		// recovery isn't possible if admin or user has it disabled and encryption
178
-		// is enabled - so we eliminate the else paths in the conditional tree
179
-		// below
180
-		$restorePossible = false;
181
-
182
-		if ($this->isEncryptionAppEnabled) {
183
-			if ($this->isRestoreEnabled) {
184
-				// check for the users recovery setting
185
-				$recoveryMode = $this->config->getUserValue($user->getUID(), 'encryption', 'recoveryEnabled', '0');
186
-				// method call inside empty is possible with PHP 5.5+
187
-				$recoveryModeEnabled = !empty($recoveryMode);
188
-				if ($recoveryModeEnabled) {
189
-					// user also has recovery mode enabled
190
-					$restorePossible = true;
191
-				}
192
-			}
193
-		} else {
194
-			// recovery is possible if encryption is disabled (plain files are
195
-			// available)
196
-			$restorePossible = true;
197
-		}
198
-
199
-		$subAdminGroups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($user);
200
-		foreach($subAdminGroups as $key => $subAdminGroup) {
201
-			$subAdminGroups[$key] = $subAdminGroup->getGID();
202
-		}
203
-
204
-		$displayName = $user->getEMailAddress();
205
-		if (is_null($displayName)) {
206
-			$displayName = '';
207
-		}
208
-
209
-		$avatarAvailable = false;
210
-		try {
211
-			$avatarAvailable = $this->avatarManager->getAvatar($user->getUID())->exists();
212
-		} catch (\Exception $e) {
213
-			//No avatar yet
214
-		}
215
-
216
-		return [
217
-			'name' => $user->getUID(),
218
-			'displayname' => $user->getDisplayName(),
219
-			'groups' => (empty($userGroups)) ? $this->groupManager->getUserGroupIds($user) : $userGroups,
220
-			'subadmin' => $subAdminGroups,
221
-			'quota' => $user->getQuota(),
222
-			'storageLocation' => $user->getHome(),
223
-			'lastLogin' => $user->getLastLogin() * 1000,
224
-			'backend' => $user->getBackendClassName(),
225
-			'email' => $displayName,
226
-			'isRestoreDisabled' => !$restorePossible,
227
-			'isAvatarAvailable' => $avatarAvailable,
228
-		];
229
-	}
230
-
231
-	/**
232
-	 * @param array $userIDs Array with schema [$uid => $displayName]
233
-	 * @return IUser[]
234
-	 */
235
-	private function getUsersForUID(array $userIDs) {
236
-		$users = [];
237
-		foreach ($userIDs as $uid => $displayName) {
238
-			$users[$uid] = $this->userManager->get($uid);
239
-		}
240
-		return $users;
241
-	}
242
-
243
-	/**
244
-	 * @NoAdminRequired
245
-	 *
246
-	 * @param int $offset
247
-	 * @param int $limit
248
-	 * @param string $gid GID to filter for
249
-	 * @param string $pattern Pattern to search for in the username
250
-	 * @param string $backend Backend to filter for (class-name)
251
-	 * @return DataResponse
252
-	 *
253
-	 * TODO: Tidy up and write unit tests - code is mainly static method calls
254
-	 */
255
-	public function index($offset = 0, $limit = 10, $gid = '', $pattern = '', $backend = '') {
256
-		// FIXME: The JS sends the group '_everyone' instead of no GID for the "all users" group.
257
-		if($gid === '_everyone') {
258
-			$gid = '';
259
-		}
260
-
261
-		// Remove backends
262
-		if(!empty($backend)) {
263
-			$activeBackends = $this->userManager->getBackends();
264
-			$this->userManager->clearBackends();
265
-			foreach($activeBackends as $singleActiveBackend) {
266
-				if($backend === get_class($singleActiveBackend)) {
267
-					$this->userManager->registerBackend($singleActiveBackend);
268
-					break;
269
-				}
270
-			}
271
-		}
272
-
273
-		$users = [];
274
-		if ($this->isAdmin) {
275
-
276
-			if($gid !== '') {
277
-				$batch = $this->getUsersForUID($this->groupManager->displayNamesInGroup($gid, $pattern, $limit, $offset));
278
-			} else {
279
-				$batch = $this->userManager->search($pattern, $limit, $offset);
280
-			}
281
-
282
-			foreach ($batch as $user) {
283
-				$users[] = $this->formatUserForIndex($user);
284
-			}
285
-
286
-		} else {
287
-			$subAdminOfGroups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($this->userSession->getUser());
288
-			// New class returns IGroup[] so convert back
289
-			$gids = [];
290
-			foreach ($subAdminOfGroups as $group) {
291
-				$gids[] = $group->getGID();
292
-			}
293
-			$subAdminOfGroups = $gids;
294
-
295
-			// Set the $gid parameter to an empty value if the subadmin has no rights to access a specific group
296
-			if($gid !== '' && !in_array($gid, $subAdminOfGroups)) {
297
-				$gid = '';
298
-			}
299
-
300
-			// Batch all groups the user is subadmin of when a group is specified
301
-			$batch = [];
302
-			if($gid === '') {
303
-				foreach($subAdminOfGroups as $group) {
304
-					$groupUsers = $this->groupManager->displayNamesInGroup($group, $pattern, $limit, $offset);
305
-
306
-					foreach($groupUsers as $uid => $displayName) {
307
-						$batch[$uid] = $displayName;
308
-					}
309
-				}
310
-			} else {
311
-				$batch = $this->groupManager->displayNamesInGroup($gid, $pattern, $limit, $offset);
312
-			}
313
-			$batch = $this->getUsersForUID($batch);
314
-
315
-			foreach ($batch as $user) {
316
-				// Only add the groups, this user is a subadmin of
317
-				$userGroups = array_values(array_intersect(
318
-					$this->groupManager->getUserGroupIds($user),
319
-					$subAdminOfGroups
320
-				));
321
-				$users[] = $this->formatUserForIndex($user, $userGroups);
322
-			}
323
-		}
324
-
325
-		return new DataResponse($users);
326
-	}
327
-
328
-	/**
329
-	 * @NoAdminRequired
330
-	 * @PasswordConfirmationRequired
331
-	 *
332
-	 * @param string $username
333
-	 * @param string $password
334
-	 * @param array $groups
335
-	 * @param string $email
336
-	 * @return DataResponse
337
-	 */
338
-	public function create($username, $password, array $groups=array(), $email='') {
339
-		if($email !== '' && !$this->mailer->validateMailAddress($email)) {
340
-			return new DataResponse(
341
-				array(
342
-					'message' => (string)$this->l10n->t('Invalid mail address')
343
-				),
344
-				Http::STATUS_UNPROCESSABLE_ENTITY
345
-			);
346
-		}
347
-
348
-		$currentUser = $this->userSession->getUser();
349
-
350
-		if (!$this->isAdmin) {
351
-			if (!empty($groups)) {
352
-				foreach ($groups as $key => $group) {
353
-					$groupObject = $this->groupManager->get($group);
354
-					if($groupObject === null) {
355
-						unset($groups[$key]);
356
-						continue;
357
-					}
358
-
359
-					if (!$this->groupManager->getSubAdmin()->isSubAdminofGroup($currentUser, $groupObject)) {
360
-						unset($groups[$key]);
361
-					}
362
-				}
363
-			}
364
-
365
-			if (empty($groups)) {
366
-				return new DataResponse(
367
-					array(
368
-						'message' => $this->l10n->t('No valid group selected'),
369
-					),
370
-					Http::STATUS_FORBIDDEN
371
-				);
372
-			}
373
-		}
374
-
375
-		if ($this->userManager->userExists($username)) {
376
-			return new DataResponse(
377
-				array(
378
-					'message' => (string)$this->l10n->t('A user with that name already exists.')
379
-				),
380
-				Http::STATUS_CONFLICT
381
-			);
382
-		}
383
-
384
-		$generatePasswordResetToken = false;
385
-		if ($password === '') {
386
-			if ($email === '') {
387
-				return new DataResponse(
388
-					array(
389
-						'message' => (string)$this->l10n->t('To send a password link to the user an email address is required.')
390
-					),
391
-					Http::STATUS_UNPROCESSABLE_ENTITY
392
-				);
393
-			}
394
-
395
-			$password = $this->secureRandom->generate(32);
396
-			$generatePasswordResetToken = true;
397
-		}
398
-
399
-		try {
400
-			$user = $this->userManager->createUser($username, $password);
401
-		} catch (\Exception $exception) {
402
-			$message = $exception->getMessage();
403
-			if (!$message) {
404
-				$message = $this->l10n->t('Unable to create user.');
405
-			}
406
-			return new DataResponse(
407
-				array(
408
-					'message' => (string) $message,
409
-				),
410
-				Http::STATUS_FORBIDDEN
411
-			);
412
-		}
413
-
414
-		if($user instanceof IUser) {
415
-			if($groups !== null) {
416
-				foreach($groups as $groupName) {
417
-					$group = $this->groupManager->get($groupName);
418
-
419
-					if(empty($group)) {
420
-						$group = $this->groupManager->createGroup($groupName);
421
-					}
422
-					$group->addUser($user);
423
-				}
424
-			}
425
-			/**
426
-			 * Send new user mail only if a mail is set
427
-			 */
428
-			if($email !== '') {
429
-				$user->setEMailAddress($email);
430
-				try {
431
-					$emailTemplate = $this->newUserMailHelper->generateTemplate($user, $generatePasswordResetToken);
432
-					$this->newUserMailHelper->sendMail($user, $emailTemplate);
433
-				} catch(\Exception $e) {
434
-					$this->log->error("Can't send new user mail to $email: " . $e->getMessage(), array('app' => 'settings'));
435
-				}
436
-			}
437
-			// fetch users groups
438
-			$userGroups = $this->groupManager->getUserGroupIds($user);
439
-
440
-			return new DataResponse(
441
-				$this->formatUserForIndex($user, $userGroups),
442
-				Http::STATUS_CREATED
443
-			);
444
-		}
445
-
446
-		return new DataResponse(
447
-			array(
448
-				'message' => (string)$this->l10n->t('Unable to create user.')
449
-			),
450
-			Http::STATUS_FORBIDDEN
451
-		);
452
-
453
-	}
454
-
455
-	/**
456
-	 * @NoAdminRequired
457
-	 * @PasswordConfirmationRequired
458
-	 *
459
-	 * @param string $id
460
-	 * @return DataResponse
461
-	 */
462
-	public function destroy($id) {
463
-		$userId = $this->userSession->getUser()->getUID();
464
-		$user = $this->userManager->get($id);
465
-
466
-		if($userId === $id) {
467
-			return new DataResponse(
468
-				array(
469
-					'status' => 'error',
470
-					'data' => array(
471
-						'message' => (string)$this->l10n->t('Unable to delete user.')
472
-					)
473
-				),
474
-				Http::STATUS_FORBIDDEN
475
-			);
476
-		}
477
-
478
-		if(!$this->isAdmin && !$this->groupManager->getSubAdmin()->isUserAccessible($this->userSession->getUser(), $user)) {
479
-			return new DataResponse(
480
-				array(
481
-					'status' => 'error',
482
-					'data' => array(
483
-						'message' => (string)$this->l10n->t('Authentication error')
484
-					)
485
-				),
486
-				Http::STATUS_FORBIDDEN
487
-			);
488
-		}
489
-
490
-		if($user) {
491
-			if($user->delete()) {
492
-				return new DataResponse(
493
-					array(
494
-						'status' => 'success',
495
-						'data' => array(
496
-							'username' => $id
497
-						)
498
-					),
499
-					Http::STATUS_NO_CONTENT
500
-				);
501
-			}
502
-		}
503
-
504
-		return new DataResponse(
505
-			array(
506
-				'status' => 'error',
507
-				'data' => array(
508
-					'message' => (string)$this->l10n->t('Unable to delete user.')
509
-				)
510
-			),
511
-			Http::STATUS_FORBIDDEN
512
-		);
513
-	}
514
-
515
-	/**
516
-	 * @NoAdminRequired
517
-	 * @NoSubadminRequired
518
-	 * @PasswordConfirmationRequired
519
-	 *
520
-	 * @param string $account
521
-	 * @return DataResponse
522
-	 */
523
-	public function getVerificationCode($account) {
524
-
525
-		$user = $this->userSession->getUser();
526
-
527
-		if ($user === null) {
528
-			return new DataResponse([], Http::STATUS_BAD_REQUEST);
529
-		}
530
-
531
-		$accountData = $this->accountManager->getUser($user);
532
-		$cloudId = $user->getCloudId();
533
-		$message = "Use my Federated Cloud ID to share with me: " . $cloudId;
534
-		$signature = $this->signMessage($user, $message);
535
-
536
-		$code = $message . ' ' . $signature;
537
-		$codeMd5 = $message . ' ' . md5($signature);
538
-
539
-		switch ($account) {
540
-			case 'verify-twitter':
541
-				$accountData[AccountManager::PROPERTY_TWITTER]['verified'] = AccountManager::VERIFICATION_IN_PROGRESS;
542
-				$msg = $this->l10n->t('In order to verify your Twitter account post following tweet on Twitter (please make sure to post it without any line breaks):');
543
-				$code = $codeMd5;
544
-				$type = AccountManager::PROPERTY_TWITTER;
545
-				$data = $accountData[AccountManager::PROPERTY_TWITTER]['value'];
546
-				break;
547
-			case 'verify-website':
548
-				$accountData[AccountManager::PROPERTY_WEBSITE]['verified'] = AccountManager::VERIFICATION_IN_PROGRESS;
549
-				$msg = $this->l10n->t('In order to verify your Website store following content in your web-root at \'CloudIdVerificationCode.txt\' (please make sure that the complete text is in one line):');
550
-				$type = AccountManager::PROPERTY_WEBSITE;
551
-				$data = $accountData[AccountManager::PROPERTY_WEBSITE]['value'];
552
-				break;
553
-			default:
554
-				return new DataResponse([], Http::STATUS_BAD_REQUEST);
555
-		}
556
-
557
-		$this->accountManager->updateUser($user, $accountData);
558
-
559
-
560
-		$this->jobList->add('OC\Settings\BackgroundJobs\VerifyUserData',
561
-			[
562
-				'verificationCode' => $code,
563
-				'data' => $data,
564
-				'type' => $type,
565
-				'uid' => $user->getUID(),
566
-				'try' => 0,
567
-				'lastRun' => $this->getCurrentTime()
568
-			]
569
-		);
570
-
571
-		return new DataResponse(['msg' => $msg, 'code' => $code]);
572
-	}
573
-
574
-	/**
575
-	 * get current timestamp
576
-	 *
577
-	 * @return int
578
-	 */
579
-	protected function getCurrentTime() {
580
-		return time();
581
-	}
582
-
583
-	/**
584
-	 * sign message with users private key
585
-	 *
586
-	 * @param IUser $user
587
-	 * @param string $message
588
-	 *
589
-	 * @return string base64 encoded signature
590
-	 */
591
-	protected function signMessage(IUser $user, $message) {
592
-		$privateKey = $this->keyManager->getKey($user)->getPrivate();
593
-		openssl_sign(json_encode($message), $signature, $privateKey, OPENSSL_ALGO_SHA512);
594
-		$signatureBase64 = base64_encode($signature);
595
-
596
-		return $signatureBase64;
597
-	}
598
-
599
-	/**
600
-	 * @NoAdminRequired
601
-	 * @NoSubadminRequired
602
-	 * @PasswordConfirmationRequired
603
-	 *
604
-	 * @param string $avatarScope
605
-	 * @param string $displayname
606
-	 * @param string $displaynameScope
607
-	 * @param string $phone
608
-	 * @param string $phoneScope
609
-	 * @param string $email
610
-	 * @param string $emailScope
611
-	 * @param string $website
612
-	 * @param string $websiteScope
613
-	 * @param string $address
614
-	 * @param string $addressScope
615
-	 * @param string $twitter
616
-	 * @param string $twitterScope
617
-	 * @return DataResponse
618
-	 */
619
-	public function setUserSettings($avatarScope,
620
-									$displayname,
621
-									$displaynameScope,
622
-									$phone,
623
-									$phoneScope,
624
-									$email,
625
-									$emailScope,
626
-									$website,
627
-									$websiteScope,
628
-									$address,
629
-									$addressScope,
630
-									$twitter,
631
-									$twitterScope
632
-	) {
633
-
634
-		if(!empty($email) && !$this->mailer->validateMailAddress($email)) {
635
-			return new DataResponse(
636
-				array(
637
-					'status' => 'error',
638
-					'data' => array(
639
-						'message' => (string)$this->l10n->t('Invalid mail address')
640
-					)
641
-				),
642
-				Http::STATUS_UNPROCESSABLE_ENTITY
643
-			);
644
-		}
645
-
646
-		$data = [
647
-			AccountManager::PROPERTY_AVATAR =>  ['scope' => $avatarScope],
648
-			AccountManager::PROPERTY_DISPLAYNAME => ['value' => $displayname, 'scope' => $displaynameScope],
649
-			AccountManager::PROPERTY_EMAIL=> ['value' => $email, 'scope' => $emailScope],
650
-			AccountManager::PROPERTY_WEBSITE => ['value' => $website, 'scope' => $websiteScope],
651
-			AccountManager::PROPERTY_ADDRESS => ['value' => $address, 'scope' => $addressScope],
652
-			AccountManager::PROPERTY_PHONE => ['value' => $phone, 'scope' => $phoneScope],
653
-			AccountManager::PROPERTY_TWITTER => ['value' => $twitter, 'scope' => $twitterScope]
654
-		];
655
-
656
-		$user = $this->userSession->getUser();
657
-
658
-		try {
659
-			$this->saveUserSettings($user, $data);
660
-			return new DataResponse(
661
-				array(
662
-					'status' => 'success',
663
-					'data' => array(
664
-						'userId' => $user->getUID(),
665
-						'avatarScope' => $avatarScope,
666
-						'displayname' => $displayname,
667
-						'displaynameScope' => $displaynameScope,
668
-						'email' => $email,
669
-						'emailScope' => $emailScope,
670
-						'website' => $website,
671
-						'websiteScope' => $websiteScope,
672
-						'address' => $address,
673
-						'addressScope' => $addressScope,
674
-						'message' => (string)$this->l10n->t('Settings saved')
675
-					)
676
-				),
677
-				Http::STATUS_OK
678
-			);
679
-		} catch (ForbiddenException $e) {
680
-			return new DataResponse([
681
-				'status' => 'error',
682
-				'data' => [
683
-					'message' => $e->getMessage()
684
-				],
685
-			]);
686
-		}
687
-
688
-	}
689
-
690
-
691
-	/**
692
-	 * update account manager with new user data
693
-	 *
694
-	 * @param IUser $user
695
-	 * @param array $data
696
-	 * @throws ForbiddenException
697
-	 */
698
-	protected function saveUserSettings(IUser $user, $data) {
699
-
700
-		// keep the user back-end up-to-date with the latest display name and email
701
-		// address
702
-		$oldDisplayName = $user->getDisplayName();
703
-		$oldDisplayName = is_null($oldDisplayName) ? '' : $oldDisplayName;
704
-		if (isset($data[AccountManager::PROPERTY_DISPLAYNAME]['value'])
705
-			&& $oldDisplayName !== $data[AccountManager::PROPERTY_DISPLAYNAME]['value']
706
-		) {
707
-			$result = $user->setDisplayName($data[AccountManager::PROPERTY_DISPLAYNAME]['value']);
708
-			if ($result === false) {
709
-				throw new ForbiddenException($this->l10n->t('Unable to change full name'));
710
-			}
711
-		}
712
-
713
-		$oldEmailAddress = $user->getEMailAddress();
714
-		$oldEmailAddress = is_null($oldEmailAddress) ? '' : $oldEmailAddress;
715
-		if (isset($data[AccountManager::PROPERTY_EMAIL]['value'])
716
-			&& $oldEmailAddress !== $data[AccountManager::PROPERTY_EMAIL]['value']
717
-		) {
718
-			// this is the only permission a backend provides and is also used
719
-			// for the permission of setting a email address
720
-			if (!$user->canChangeDisplayName()) {
721
-				throw new ForbiddenException($this->l10n->t('Unable to change email address'));
722
-			}
723
-			$user->setEMailAddress($data[AccountManager::PROPERTY_EMAIL]['value']);
724
-		}
725
-
726
-		$this->accountManager->updateUser($user, $data);
727
-	}
728
-
729
-	/**
730
-	 * Count all unique users visible for the current admin/subadmin.
731
-	 *
732
-	 * @NoAdminRequired
733
-	 *
734
-	 * @return DataResponse
735
-	 */
736
-	public function stats() {
737
-		$userCount = 0;
738
-		if ($this->isAdmin) {
739
-			$countByBackend = $this->userManager->countUsers();
740
-
741
-			if (!empty($countByBackend)) {
742
-				foreach ($countByBackend as $count) {
743
-					$userCount += $count;
744
-				}
745
-			}
746
-		} else {
747
-			$groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($this->userSession->getUser());
748
-
749
-			$uniqueUsers = [];
750
-			foreach ($groups as $group) {
751
-				foreach($group->getUsers() as $uid => $displayName) {
752
-					$uniqueUsers[$uid] = true;
753
-				}
754
-			}
755
-
756
-			$userCount = count($uniqueUsers);
757
-		}
758
-
759
-		return new DataResponse(
760
-			[
761
-				'totalUsers' => $userCount
762
-			]
763
-		);
764
-	}
765
-
766
-
767
-	/**
768
-	 * Set the displayName of a user
769
-	 *
770
-	 * @NoAdminRequired
771
-	 * @NoSubadminRequired
772
-	 * @PasswordConfirmationRequired
773
-	 * @todo merge into saveUserSettings
774
-	 *
775
-	 * @param string $username
776
-	 * @param string $displayName
777
-	 * @return DataResponse
778
-	 */
779
-	public function setDisplayName($username, $displayName) {
780
-		$currentUser = $this->userSession->getUser();
781
-		$user = $this->userManager->get($username);
782
-
783
-		if ($user === null ||
784
-			!$user->canChangeDisplayName() ||
785
-			(
786
-				!$this->groupManager->isAdmin($currentUser->getUID()) &&
787
-				!$this->groupManager->getSubAdmin()->isUserAccessible($currentUser, $user) &&
788
-				$currentUser->getUID() !== $username
789
-
790
-			)
791
-		) {
792
-			return new DataResponse([
793
-				'status' => 'error',
794
-				'data' => [
795
-					'message' => $this->l10n->t('Authentication error'),
796
-				],
797
-			]);
798
-		}
799
-
800
-		$userData = $this->accountManager->getUser($user);
801
-		$userData[AccountManager::PROPERTY_DISPLAYNAME]['value'] = $displayName;
802
-
803
-
804
-		try {
805
-			$this->saveUserSettings($user, $userData);
806
-			return new DataResponse([
807
-				'status' => 'success',
808
-				'data' => [
809
-					'message' => $this->l10n->t('Your full name has been changed.'),
810
-					'username' => $username,
811
-					'displayName' => $displayName,
812
-				],
813
-			]);
814
-		} catch (ForbiddenException $e) {
815
-			return new DataResponse([
816
-				'status' => 'error',
817
-				'data' => [
818
-					'message' => $e->getMessage(),
819
-					'displayName' => $user->getDisplayName(),
820
-				],
821
-			]);
822
-		}
823
-	}
824
-
825
-	/**
826
-	 * Set the mail address of a user
827
-	 *
828
-	 * @NoAdminRequired
829
-	 * @NoSubadminRequired
830
-	 * @PasswordConfirmationRequired
831
-	 *
832
-	 * @param string $id
833
-	 * @param string $mailAddress
834
-	 * @return DataResponse
835
-	 */
836
-	public function setEMailAddress($id, $mailAddress) {
837
-		$user = $this->userManager->get($id);
838
-		if (!$this->isAdmin
839
-			&& !$this->groupManager->getSubAdmin()->isUserAccessible($this->userSession->getUser(), $user)
840
-		) {
841
-			return new DataResponse(
842
-				array(
843
-					'status' => 'error',
844
-					'data' => array(
845
-						'message' => (string)$this->l10n->t('Forbidden')
846
-					)
847
-				),
848
-				Http::STATUS_FORBIDDEN
849
-			);
850
-		}
851
-
852
-		if($mailAddress !== '' && !$this->mailer->validateMailAddress($mailAddress)) {
853
-			return new DataResponse(
854
-				array(
855
-					'status' => 'error',
856
-					'data' => array(
857
-						'message' => (string)$this->l10n->t('Invalid mail address')
858
-					)
859
-				),
860
-				Http::STATUS_UNPROCESSABLE_ENTITY
861
-			);
862
-		}
863
-
864
-		if (!$user) {
865
-			return new DataResponse(
866
-				array(
867
-					'status' => 'error',
868
-					'data' => array(
869
-						'message' => (string)$this->l10n->t('Invalid user')
870
-					)
871
-				),
872
-				Http::STATUS_UNPROCESSABLE_ENTITY
873
-			);
874
-		}
875
-		// this is the only permission a backend provides and is also used
876
-		// for the permission of setting a email address
877
-		if (!$user->canChangeDisplayName()) {
878
-			return new DataResponse(
879
-				array(
880
-					'status' => 'error',
881
-					'data' => array(
882
-						'message' => (string)$this->l10n->t('Unable to change mail address')
883
-					)
884
-				),
885
-				Http::STATUS_FORBIDDEN
886
-			);
887
-		}
888
-
889
-		$userData = $this->accountManager->getUser($user);
890
-		$userData[AccountManager::PROPERTY_EMAIL]['value'] = $mailAddress;
891
-
892
-		try {
893
-			$this->saveUserSettings($user, $userData);
894
-			return new DataResponse(
895
-				array(
896
-					'status' => 'success',
897
-					'data' => array(
898
-						'username' => $id,
899
-						'mailAddress' => $mailAddress,
900
-						'message' => (string)$this->l10n->t('Email saved')
901
-					)
902
-				),
903
-				Http::STATUS_OK
904
-			);
905
-		} catch (ForbiddenException $e) {
906
-			return new DataResponse([
907
-				'status' => 'error',
908
-				'data' => [
909
-					'message' => $e->getMessage()
910
-				],
911
-			]);
912
-		}
913
-	}
61
+    /** @var IL10N */
62
+    private $l10n;
63
+    /** @var IUserSession */
64
+    private $userSession;
65
+    /** @var bool */
66
+    private $isAdmin;
67
+    /** @var IUserManager */
68
+    private $userManager;
69
+    /** @var IGroupManager */
70
+    private $groupManager;
71
+    /** @var IConfig */
72
+    private $config;
73
+    /** @var ILogger */
74
+    private $log;
75
+    /** @var IMailer */
76
+    private $mailer;
77
+    /** @var bool contains the state of the encryption app */
78
+    private $isEncryptionAppEnabled;
79
+    /** @var bool contains the state of the admin recovery setting */
80
+    private $isRestoreEnabled = false;
81
+    /** @var IAvatarManager */
82
+    private $avatarManager;
83
+    /** @var AccountManager */
84
+    private $accountManager;
85
+    /** @var ISecureRandom */
86
+    private $secureRandom;
87
+    /** @var NewUserMailHelper */
88
+    private $newUserMailHelper;
89
+    /** @var ITimeFactory */
90
+    private $timeFactory;
91
+    /** @var ICrypto */
92
+    private $crypto;
93
+    /** @var Manager */
94
+    private $keyManager;
95
+    /** @var IJobList */
96
+    private $jobList;
97
+
98
+    /**
99
+     * @param string $appName
100
+     * @param IRequest $request
101
+     * @param IUserManager $userManager
102
+     * @param IGroupManager $groupManager
103
+     * @param IUserSession $userSession
104
+     * @param IConfig $config
105
+     * @param bool $isAdmin
106
+     * @param IL10N $l10n
107
+     * @param ILogger $log
108
+     * @param IMailer $mailer
109
+     * @param IURLGenerator $urlGenerator
110
+     * @param IAppManager $appManager
111
+     * @param IAvatarManager $avatarManager
112
+     * @param AccountManager $accountManager
113
+     * @param ISecureRandom $secureRandom
114
+     * @param NewUserMailHelper $newUserMailHelper
115
+     * @param ITimeFactory $timeFactory
116
+     * @param ICrypto $crypto
117
+     * @param Manager $keyManager
118
+     * @param IJobList $jobList
119
+     */
120
+    public function __construct($appName,
121
+                                IRequest $request,
122
+                                IUserManager $userManager,
123
+                                IGroupManager $groupManager,
124
+                                IUserSession $userSession,
125
+                                IConfig $config,
126
+                                $isAdmin,
127
+                                IL10N $l10n,
128
+                                ILogger $log,
129
+                                IMailer $mailer,
130
+                                IURLGenerator $urlGenerator,
131
+                                IAppManager $appManager,
132
+                                IAvatarManager $avatarManager,
133
+                                AccountManager $accountManager,
134
+                                ISecureRandom $secureRandom,
135
+                                NewUserMailHelper $newUserMailHelper,
136
+                                ITimeFactory $timeFactory,
137
+                                ICrypto $crypto,
138
+                                Manager $keyManager,
139
+                                IJobList $jobList) {
140
+        parent::__construct($appName, $request);
141
+        $this->userManager = $userManager;
142
+        $this->groupManager = $groupManager;
143
+        $this->userSession = $userSession;
144
+        $this->config = $config;
145
+        $this->isAdmin = $isAdmin;
146
+        $this->l10n = $l10n;
147
+        $this->log = $log;
148
+        $this->mailer = $mailer;
149
+        $this->avatarManager = $avatarManager;
150
+        $this->accountManager = $accountManager;
151
+        $this->secureRandom = $secureRandom;
152
+        $this->newUserMailHelper = $newUserMailHelper;
153
+        $this->timeFactory = $timeFactory;
154
+        $this->crypto = $crypto;
155
+        $this->keyManager = $keyManager;
156
+        $this->jobList = $jobList;
157
+
158
+        // check for encryption state - TODO see formatUserForIndex
159
+        $this->isEncryptionAppEnabled = $appManager->isEnabledForUser('encryption');
160
+        if($this->isEncryptionAppEnabled) {
161
+            // putting this directly in empty is possible in PHP 5.5+
162
+            $result = $config->getAppValue('encryption', 'recoveryAdminEnabled', 0);
163
+            $this->isRestoreEnabled = !empty($result);
164
+        }
165
+    }
166
+
167
+    /**
168
+     * @param IUser $user
169
+     * @param array $userGroups
170
+     * @return array
171
+     */
172
+    private function formatUserForIndex(IUser $user, array $userGroups = null) {
173
+
174
+        // TODO: eliminate this encryption specific code below and somehow
175
+        // hook in additional user info from other apps
176
+
177
+        // recovery isn't possible if admin or user has it disabled and encryption
178
+        // is enabled - so we eliminate the else paths in the conditional tree
179
+        // below
180
+        $restorePossible = false;
181
+
182
+        if ($this->isEncryptionAppEnabled) {
183
+            if ($this->isRestoreEnabled) {
184
+                // check for the users recovery setting
185
+                $recoveryMode = $this->config->getUserValue($user->getUID(), 'encryption', 'recoveryEnabled', '0');
186
+                // method call inside empty is possible with PHP 5.5+
187
+                $recoveryModeEnabled = !empty($recoveryMode);
188
+                if ($recoveryModeEnabled) {
189
+                    // user also has recovery mode enabled
190
+                    $restorePossible = true;
191
+                }
192
+            }
193
+        } else {
194
+            // recovery is possible if encryption is disabled (plain files are
195
+            // available)
196
+            $restorePossible = true;
197
+        }
198
+
199
+        $subAdminGroups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($user);
200
+        foreach($subAdminGroups as $key => $subAdminGroup) {
201
+            $subAdminGroups[$key] = $subAdminGroup->getGID();
202
+        }
203
+
204
+        $displayName = $user->getEMailAddress();
205
+        if (is_null($displayName)) {
206
+            $displayName = '';
207
+        }
208
+
209
+        $avatarAvailable = false;
210
+        try {
211
+            $avatarAvailable = $this->avatarManager->getAvatar($user->getUID())->exists();
212
+        } catch (\Exception $e) {
213
+            //No avatar yet
214
+        }
215
+
216
+        return [
217
+            'name' => $user->getUID(),
218
+            'displayname' => $user->getDisplayName(),
219
+            'groups' => (empty($userGroups)) ? $this->groupManager->getUserGroupIds($user) : $userGroups,
220
+            'subadmin' => $subAdminGroups,
221
+            'quota' => $user->getQuota(),
222
+            'storageLocation' => $user->getHome(),
223
+            'lastLogin' => $user->getLastLogin() * 1000,
224
+            'backend' => $user->getBackendClassName(),
225
+            'email' => $displayName,
226
+            'isRestoreDisabled' => !$restorePossible,
227
+            'isAvatarAvailable' => $avatarAvailable,
228
+        ];
229
+    }
230
+
231
+    /**
232
+     * @param array $userIDs Array with schema [$uid => $displayName]
233
+     * @return IUser[]
234
+     */
235
+    private function getUsersForUID(array $userIDs) {
236
+        $users = [];
237
+        foreach ($userIDs as $uid => $displayName) {
238
+            $users[$uid] = $this->userManager->get($uid);
239
+        }
240
+        return $users;
241
+    }
242
+
243
+    /**
244
+     * @NoAdminRequired
245
+     *
246
+     * @param int $offset
247
+     * @param int $limit
248
+     * @param string $gid GID to filter for
249
+     * @param string $pattern Pattern to search for in the username
250
+     * @param string $backend Backend to filter for (class-name)
251
+     * @return DataResponse
252
+     *
253
+     * TODO: Tidy up and write unit tests - code is mainly static method calls
254
+     */
255
+    public function index($offset = 0, $limit = 10, $gid = '', $pattern = '', $backend = '') {
256
+        // FIXME: The JS sends the group '_everyone' instead of no GID for the "all users" group.
257
+        if($gid === '_everyone') {
258
+            $gid = '';
259
+        }
260
+
261
+        // Remove backends
262
+        if(!empty($backend)) {
263
+            $activeBackends = $this->userManager->getBackends();
264
+            $this->userManager->clearBackends();
265
+            foreach($activeBackends as $singleActiveBackend) {
266
+                if($backend === get_class($singleActiveBackend)) {
267
+                    $this->userManager->registerBackend($singleActiveBackend);
268
+                    break;
269
+                }
270
+            }
271
+        }
272
+
273
+        $users = [];
274
+        if ($this->isAdmin) {
275
+
276
+            if($gid !== '') {
277
+                $batch = $this->getUsersForUID($this->groupManager->displayNamesInGroup($gid, $pattern, $limit, $offset));
278
+            } else {
279
+                $batch = $this->userManager->search($pattern, $limit, $offset);
280
+            }
281
+
282
+            foreach ($batch as $user) {
283
+                $users[] = $this->formatUserForIndex($user);
284
+            }
285
+
286
+        } else {
287
+            $subAdminOfGroups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($this->userSession->getUser());
288
+            // New class returns IGroup[] so convert back
289
+            $gids = [];
290
+            foreach ($subAdminOfGroups as $group) {
291
+                $gids[] = $group->getGID();
292
+            }
293
+            $subAdminOfGroups = $gids;
294
+
295
+            // Set the $gid parameter to an empty value if the subadmin has no rights to access a specific group
296
+            if($gid !== '' && !in_array($gid, $subAdminOfGroups)) {
297
+                $gid = '';
298
+            }
299
+
300
+            // Batch all groups the user is subadmin of when a group is specified
301
+            $batch = [];
302
+            if($gid === '') {
303
+                foreach($subAdminOfGroups as $group) {
304
+                    $groupUsers = $this->groupManager->displayNamesInGroup($group, $pattern, $limit, $offset);
305
+
306
+                    foreach($groupUsers as $uid => $displayName) {
307
+                        $batch[$uid] = $displayName;
308
+                    }
309
+                }
310
+            } else {
311
+                $batch = $this->groupManager->displayNamesInGroup($gid, $pattern, $limit, $offset);
312
+            }
313
+            $batch = $this->getUsersForUID($batch);
314
+
315
+            foreach ($batch as $user) {
316
+                // Only add the groups, this user is a subadmin of
317
+                $userGroups = array_values(array_intersect(
318
+                    $this->groupManager->getUserGroupIds($user),
319
+                    $subAdminOfGroups
320
+                ));
321
+                $users[] = $this->formatUserForIndex($user, $userGroups);
322
+            }
323
+        }
324
+
325
+        return new DataResponse($users);
326
+    }
327
+
328
+    /**
329
+     * @NoAdminRequired
330
+     * @PasswordConfirmationRequired
331
+     *
332
+     * @param string $username
333
+     * @param string $password
334
+     * @param array $groups
335
+     * @param string $email
336
+     * @return DataResponse
337
+     */
338
+    public function create($username, $password, array $groups=array(), $email='') {
339
+        if($email !== '' && !$this->mailer->validateMailAddress($email)) {
340
+            return new DataResponse(
341
+                array(
342
+                    'message' => (string)$this->l10n->t('Invalid mail address')
343
+                ),
344
+                Http::STATUS_UNPROCESSABLE_ENTITY
345
+            );
346
+        }
347
+
348
+        $currentUser = $this->userSession->getUser();
349
+
350
+        if (!$this->isAdmin) {
351
+            if (!empty($groups)) {
352
+                foreach ($groups as $key => $group) {
353
+                    $groupObject = $this->groupManager->get($group);
354
+                    if($groupObject === null) {
355
+                        unset($groups[$key]);
356
+                        continue;
357
+                    }
358
+
359
+                    if (!$this->groupManager->getSubAdmin()->isSubAdminofGroup($currentUser, $groupObject)) {
360
+                        unset($groups[$key]);
361
+                    }
362
+                }
363
+            }
364
+
365
+            if (empty($groups)) {
366
+                return new DataResponse(
367
+                    array(
368
+                        'message' => $this->l10n->t('No valid group selected'),
369
+                    ),
370
+                    Http::STATUS_FORBIDDEN
371
+                );
372
+            }
373
+        }
374
+
375
+        if ($this->userManager->userExists($username)) {
376
+            return new DataResponse(
377
+                array(
378
+                    'message' => (string)$this->l10n->t('A user with that name already exists.')
379
+                ),
380
+                Http::STATUS_CONFLICT
381
+            );
382
+        }
383
+
384
+        $generatePasswordResetToken = false;
385
+        if ($password === '') {
386
+            if ($email === '') {
387
+                return new DataResponse(
388
+                    array(
389
+                        'message' => (string)$this->l10n->t('To send a password link to the user an email address is required.')
390
+                    ),
391
+                    Http::STATUS_UNPROCESSABLE_ENTITY
392
+                );
393
+            }
394
+
395
+            $password = $this->secureRandom->generate(32);
396
+            $generatePasswordResetToken = true;
397
+        }
398
+
399
+        try {
400
+            $user = $this->userManager->createUser($username, $password);
401
+        } catch (\Exception $exception) {
402
+            $message = $exception->getMessage();
403
+            if (!$message) {
404
+                $message = $this->l10n->t('Unable to create user.');
405
+            }
406
+            return new DataResponse(
407
+                array(
408
+                    'message' => (string) $message,
409
+                ),
410
+                Http::STATUS_FORBIDDEN
411
+            );
412
+        }
413
+
414
+        if($user instanceof IUser) {
415
+            if($groups !== null) {
416
+                foreach($groups as $groupName) {
417
+                    $group = $this->groupManager->get($groupName);
418
+
419
+                    if(empty($group)) {
420
+                        $group = $this->groupManager->createGroup($groupName);
421
+                    }
422
+                    $group->addUser($user);
423
+                }
424
+            }
425
+            /**
426
+             * Send new user mail only if a mail is set
427
+             */
428
+            if($email !== '') {
429
+                $user->setEMailAddress($email);
430
+                try {
431
+                    $emailTemplate = $this->newUserMailHelper->generateTemplate($user, $generatePasswordResetToken);
432
+                    $this->newUserMailHelper->sendMail($user, $emailTemplate);
433
+                } catch(\Exception $e) {
434
+                    $this->log->error("Can't send new user mail to $email: " . $e->getMessage(), array('app' => 'settings'));
435
+                }
436
+            }
437
+            // fetch users groups
438
+            $userGroups = $this->groupManager->getUserGroupIds($user);
439
+
440
+            return new DataResponse(
441
+                $this->formatUserForIndex($user, $userGroups),
442
+                Http::STATUS_CREATED
443
+            );
444
+        }
445
+
446
+        return new DataResponse(
447
+            array(
448
+                'message' => (string)$this->l10n->t('Unable to create user.')
449
+            ),
450
+            Http::STATUS_FORBIDDEN
451
+        );
452
+
453
+    }
454
+
455
+    /**
456
+     * @NoAdminRequired
457
+     * @PasswordConfirmationRequired
458
+     *
459
+     * @param string $id
460
+     * @return DataResponse
461
+     */
462
+    public function destroy($id) {
463
+        $userId = $this->userSession->getUser()->getUID();
464
+        $user = $this->userManager->get($id);
465
+
466
+        if($userId === $id) {
467
+            return new DataResponse(
468
+                array(
469
+                    'status' => 'error',
470
+                    'data' => array(
471
+                        'message' => (string)$this->l10n->t('Unable to delete user.')
472
+                    )
473
+                ),
474
+                Http::STATUS_FORBIDDEN
475
+            );
476
+        }
477
+
478
+        if(!$this->isAdmin && !$this->groupManager->getSubAdmin()->isUserAccessible($this->userSession->getUser(), $user)) {
479
+            return new DataResponse(
480
+                array(
481
+                    'status' => 'error',
482
+                    'data' => array(
483
+                        'message' => (string)$this->l10n->t('Authentication error')
484
+                    )
485
+                ),
486
+                Http::STATUS_FORBIDDEN
487
+            );
488
+        }
489
+
490
+        if($user) {
491
+            if($user->delete()) {
492
+                return new DataResponse(
493
+                    array(
494
+                        'status' => 'success',
495
+                        'data' => array(
496
+                            'username' => $id
497
+                        )
498
+                    ),
499
+                    Http::STATUS_NO_CONTENT
500
+                );
501
+            }
502
+        }
503
+
504
+        return new DataResponse(
505
+            array(
506
+                'status' => 'error',
507
+                'data' => array(
508
+                    'message' => (string)$this->l10n->t('Unable to delete user.')
509
+                )
510
+            ),
511
+            Http::STATUS_FORBIDDEN
512
+        );
513
+    }
514
+
515
+    /**
516
+     * @NoAdminRequired
517
+     * @NoSubadminRequired
518
+     * @PasswordConfirmationRequired
519
+     *
520
+     * @param string $account
521
+     * @return DataResponse
522
+     */
523
+    public function getVerificationCode($account) {
524
+
525
+        $user = $this->userSession->getUser();
526
+
527
+        if ($user === null) {
528
+            return new DataResponse([], Http::STATUS_BAD_REQUEST);
529
+        }
530
+
531
+        $accountData = $this->accountManager->getUser($user);
532
+        $cloudId = $user->getCloudId();
533
+        $message = "Use my Federated Cloud ID to share with me: " . $cloudId;
534
+        $signature = $this->signMessage($user, $message);
535
+
536
+        $code = $message . ' ' . $signature;
537
+        $codeMd5 = $message . ' ' . md5($signature);
538
+
539
+        switch ($account) {
540
+            case 'verify-twitter':
541
+                $accountData[AccountManager::PROPERTY_TWITTER]['verified'] = AccountManager::VERIFICATION_IN_PROGRESS;
542
+                $msg = $this->l10n->t('In order to verify your Twitter account post following tweet on Twitter (please make sure to post it without any line breaks):');
543
+                $code = $codeMd5;
544
+                $type = AccountManager::PROPERTY_TWITTER;
545
+                $data = $accountData[AccountManager::PROPERTY_TWITTER]['value'];
546
+                break;
547
+            case 'verify-website':
548
+                $accountData[AccountManager::PROPERTY_WEBSITE]['verified'] = AccountManager::VERIFICATION_IN_PROGRESS;
549
+                $msg = $this->l10n->t('In order to verify your Website store following content in your web-root at \'CloudIdVerificationCode.txt\' (please make sure that the complete text is in one line):');
550
+                $type = AccountManager::PROPERTY_WEBSITE;
551
+                $data = $accountData[AccountManager::PROPERTY_WEBSITE]['value'];
552
+                break;
553
+            default:
554
+                return new DataResponse([], Http::STATUS_BAD_REQUEST);
555
+        }
556
+
557
+        $this->accountManager->updateUser($user, $accountData);
558
+
559
+
560
+        $this->jobList->add('OC\Settings\BackgroundJobs\VerifyUserData',
561
+            [
562
+                'verificationCode' => $code,
563
+                'data' => $data,
564
+                'type' => $type,
565
+                'uid' => $user->getUID(),
566
+                'try' => 0,
567
+                'lastRun' => $this->getCurrentTime()
568
+            ]
569
+        );
570
+
571
+        return new DataResponse(['msg' => $msg, 'code' => $code]);
572
+    }
573
+
574
+    /**
575
+     * get current timestamp
576
+     *
577
+     * @return int
578
+     */
579
+    protected function getCurrentTime() {
580
+        return time();
581
+    }
582
+
583
+    /**
584
+     * sign message with users private key
585
+     *
586
+     * @param IUser $user
587
+     * @param string $message
588
+     *
589
+     * @return string base64 encoded signature
590
+     */
591
+    protected function signMessage(IUser $user, $message) {
592
+        $privateKey = $this->keyManager->getKey($user)->getPrivate();
593
+        openssl_sign(json_encode($message), $signature, $privateKey, OPENSSL_ALGO_SHA512);
594
+        $signatureBase64 = base64_encode($signature);
595
+
596
+        return $signatureBase64;
597
+    }
598
+
599
+    /**
600
+     * @NoAdminRequired
601
+     * @NoSubadminRequired
602
+     * @PasswordConfirmationRequired
603
+     *
604
+     * @param string $avatarScope
605
+     * @param string $displayname
606
+     * @param string $displaynameScope
607
+     * @param string $phone
608
+     * @param string $phoneScope
609
+     * @param string $email
610
+     * @param string $emailScope
611
+     * @param string $website
612
+     * @param string $websiteScope
613
+     * @param string $address
614
+     * @param string $addressScope
615
+     * @param string $twitter
616
+     * @param string $twitterScope
617
+     * @return DataResponse
618
+     */
619
+    public function setUserSettings($avatarScope,
620
+                                    $displayname,
621
+                                    $displaynameScope,
622
+                                    $phone,
623
+                                    $phoneScope,
624
+                                    $email,
625
+                                    $emailScope,
626
+                                    $website,
627
+                                    $websiteScope,
628
+                                    $address,
629
+                                    $addressScope,
630
+                                    $twitter,
631
+                                    $twitterScope
632
+    ) {
633
+
634
+        if(!empty($email) && !$this->mailer->validateMailAddress($email)) {
635
+            return new DataResponse(
636
+                array(
637
+                    'status' => 'error',
638
+                    'data' => array(
639
+                        'message' => (string)$this->l10n->t('Invalid mail address')
640
+                    )
641
+                ),
642
+                Http::STATUS_UNPROCESSABLE_ENTITY
643
+            );
644
+        }
645
+
646
+        $data = [
647
+            AccountManager::PROPERTY_AVATAR =>  ['scope' => $avatarScope],
648
+            AccountManager::PROPERTY_DISPLAYNAME => ['value' => $displayname, 'scope' => $displaynameScope],
649
+            AccountManager::PROPERTY_EMAIL=> ['value' => $email, 'scope' => $emailScope],
650
+            AccountManager::PROPERTY_WEBSITE => ['value' => $website, 'scope' => $websiteScope],
651
+            AccountManager::PROPERTY_ADDRESS => ['value' => $address, 'scope' => $addressScope],
652
+            AccountManager::PROPERTY_PHONE => ['value' => $phone, 'scope' => $phoneScope],
653
+            AccountManager::PROPERTY_TWITTER => ['value' => $twitter, 'scope' => $twitterScope]
654
+        ];
655
+
656
+        $user = $this->userSession->getUser();
657
+
658
+        try {
659
+            $this->saveUserSettings($user, $data);
660
+            return new DataResponse(
661
+                array(
662
+                    'status' => 'success',
663
+                    'data' => array(
664
+                        'userId' => $user->getUID(),
665
+                        'avatarScope' => $avatarScope,
666
+                        'displayname' => $displayname,
667
+                        'displaynameScope' => $displaynameScope,
668
+                        'email' => $email,
669
+                        'emailScope' => $emailScope,
670
+                        'website' => $website,
671
+                        'websiteScope' => $websiteScope,
672
+                        'address' => $address,
673
+                        'addressScope' => $addressScope,
674
+                        'message' => (string)$this->l10n->t('Settings saved')
675
+                    )
676
+                ),
677
+                Http::STATUS_OK
678
+            );
679
+        } catch (ForbiddenException $e) {
680
+            return new DataResponse([
681
+                'status' => 'error',
682
+                'data' => [
683
+                    'message' => $e->getMessage()
684
+                ],
685
+            ]);
686
+        }
687
+
688
+    }
689
+
690
+
691
+    /**
692
+     * update account manager with new user data
693
+     *
694
+     * @param IUser $user
695
+     * @param array $data
696
+     * @throws ForbiddenException
697
+     */
698
+    protected function saveUserSettings(IUser $user, $data) {
699
+
700
+        // keep the user back-end up-to-date with the latest display name and email
701
+        // address
702
+        $oldDisplayName = $user->getDisplayName();
703
+        $oldDisplayName = is_null($oldDisplayName) ? '' : $oldDisplayName;
704
+        if (isset($data[AccountManager::PROPERTY_DISPLAYNAME]['value'])
705
+            && $oldDisplayName !== $data[AccountManager::PROPERTY_DISPLAYNAME]['value']
706
+        ) {
707
+            $result = $user->setDisplayName($data[AccountManager::PROPERTY_DISPLAYNAME]['value']);
708
+            if ($result === false) {
709
+                throw new ForbiddenException($this->l10n->t('Unable to change full name'));
710
+            }
711
+        }
712
+
713
+        $oldEmailAddress = $user->getEMailAddress();
714
+        $oldEmailAddress = is_null($oldEmailAddress) ? '' : $oldEmailAddress;
715
+        if (isset($data[AccountManager::PROPERTY_EMAIL]['value'])
716
+            && $oldEmailAddress !== $data[AccountManager::PROPERTY_EMAIL]['value']
717
+        ) {
718
+            // this is the only permission a backend provides and is also used
719
+            // for the permission of setting a email address
720
+            if (!$user->canChangeDisplayName()) {
721
+                throw new ForbiddenException($this->l10n->t('Unable to change email address'));
722
+            }
723
+            $user->setEMailAddress($data[AccountManager::PROPERTY_EMAIL]['value']);
724
+        }
725
+
726
+        $this->accountManager->updateUser($user, $data);
727
+    }
728
+
729
+    /**
730
+     * Count all unique users visible for the current admin/subadmin.
731
+     *
732
+     * @NoAdminRequired
733
+     *
734
+     * @return DataResponse
735
+     */
736
+    public function stats() {
737
+        $userCount = 0;
738
+        if ($this->isAdmin) {
739
+            $countByBackend = $this->userManager->countUsers();
740
+
741
+            if (!empty($countByBackend)) {
742
+                foreach ($countByBackend as $count) {
743
+                    $userCount += $count;
744
+                }
745
+            }
746
+        } else {
747
+            $groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($this->userSession->getUser());
748
+
749
+            $uniqueUsers = [];
750
+            foreach ($groups as $group) {
751
+                foreach($group->getUsers() as $uid => $displayName) {
752
+                    $uniqueUsers[$uid] = true;
753
+                }
754
+            }
755
+
756
+            $userCount = count($uniqueUsers);
757
+        }
758
+
759
+        return new DataResponse(
760
+            [
761
+                'totalUsers' => $userCount
762
+            ]
763
+        );
764
+    }
765
+
766
+
767
+    /**
768
+     * Set the displayName of a user
769
+     *
770
+     * @NoAdminRequired
771
+     * @NoSubadminRequired
772
+     * @PasswordConfirmationRequired
773
+     * @todo merge into saveUserSettings
774
+     *
775
+     * @param string $username
776
+     * @param string $displayName
777
+     * @return DataResponse
778
+     */
779
+    public function setDisplayName($username, $displayName) {
780
+        $currentUser = $this->userSession->getUser();
781
+        $user = $this->userManager->get($username);
782
+
783
+        if ($user === null ||
784
+            !$user->canChangeDisplayName() ||
785
+            (
786
+                !$this->groupManager->isAdmin($currentUser->getUID()) &&
787
+                !$this->groupManager->getSubAdmin()->isUserAccessible($currentUser, $user) &&
788
+                $currentUser->getUID() !== $username
789
+
790
+            )
791
+        ) {
792
+            return new DataResponse([
793
+                'status' => 'error',
794
+                'data' => [
795
+                    'message' => $this->l10n->t('Authentication error'),
796
+                ],
797
+            ]);
798
+        }
799
+
800
+        $userData = $this->accountManager->getUser($user);
801
+        $userData[AccountManager::PROPERTY_DISPLAYNAME]['value'] = $displayName;
802
+
803
+
804
+        try {
805
+            $this->saveUserSettings($user, $userData);
806
+            return new DataResponse([
807
+                'status' => 'success',
808
+                'data' => [
809
+                    'message' => $this->l10n->t('Your full name has been changed.'),
810
+                    'username' => $username,
811
+                    'displayName' => $displayName,
812
+                ],
813
+            ]);
814
+        } catch (ForbiddenException $e) {
815
+            return new DataResponse([
816
+                'status' => 'error',
817
+                'data' => [
818
+                    'message' => $e->getMessage(),
819
+                    'displayName' => $user->getDisplayName(),
820
+                ],
821
+            ]);
822
+        }
823
+    }
824
+
825
+    /**
826
+     * Set the mail address of a user
827
+     *
828
+     * @NoAdminRequired
829
+     * @NoSubadminRequired
830
+     * @PasswordConfirmationRequired
831
+     *
832
+     * @param string $id
833
+     * @param string $mailAddress
834
+     * @return DataResponse
835
+     */
836
+    public function setEMailAddress($id, $mailAddress) {
837
+        $user = $this->userManager->get($id);
838
+        if (!$this->isAdmin
839
+            && !$this->groupManager->getSubAdmin()->isUserAccessible($this->userSession->getUser(), $user)
840
+        ) {
841
+            return new DataResponse(
842
+                array(
843
+                    'status' => 'error',
844
+                    'data' => array(
845
+                        'message' => (string)$this->l10n->t('Forbidden')
846
+                    )
847
+                ),
848
+                Http::STATUS_FORBIDDEN
849
+            );
850
+        }
851
+
852
+        if($mailAddress !== '' && !$this->mailer->validateMailAddress($mailAddress)) {
853
+            return new DataResponse(
854
+                array(
855
+                    'status' => 'error',
856
+                    'data' => array(
857
+                        'message' => (string)$this->l10n->t('Invalid mail address')
858
+                    )
859
+                ),
860
+                Http::STATUS_UNPROCESSABLE_ENTITY
861
+            );
862
+        }
863
+
864
+        if (!$user) {
865
+            return new DataResponse(
866
+                array(
867
+                    'status' => 'error',
868
+                    'data' => array(
869
+                        'message' => (string)$this->l10n->t('Invalid user')
870
+                    )
871
+                ),
872
+                Http::STATUS_UNPROCESSABLE_ENTITY
873
+            );
874
+        }
875
+        // this is the only permission a backend provides and is also used
876
+        // for the permission of setting a email address
877
+        if (!$user->canChangeDisplayName()) {
878
+            return new DataResponse(
879
+                array(
880
+                    'status' => 'error',
881
+                    'data' => array(
882
+                        'message' => (string)$this->l10n->t('Unable to change mail address')
883
+                    )
884
+                ),
885
+                Http::STATUS_FORBIDDEN
886
+            );
887
+        }
888
+
889
+        $userData = $this->accountManager->getUser($user);
890
+        $userData[AccountManager::PROPERTY_EMAIL]['value'] = $mailAddress;
891
+
892
+        try {
893
+            $this->saveUserSettings($user, $userData);
894
+            return new DataResponse(
895
+                array(
896
+                    'status' => 'success',
897
+                    'data' => array(
898
+                        'username' => $id,
899
+                        'mailAddress' => $mailAddress,
900
+                        'message' => (string)$this->l10n->t('Email saved')
901
+                    )
902
+                ),
903
+                Http::STATUS_OK
904
+            );
905
+        } catch (ForbiddenException $e) {
906
+            return new DataResponse([
907
+                'status' => 'error',
908
+                'data' => [
909
+                    'message' => $e->getMessage()
910
+                ],
911
+            ]);
912
+        }
913
+    }
914 914
 
915 915
 }
Please login to merge, or discard this patch.
lib/private/Accounts/Hooks.php 1 patch
Indentation   +68 added lines, -68 removed lines patch added patch discarded remove patch
@@ -27,73 +27,73 @@
 block discarded – undo
27 27
 
28 28
 class Hooks {
29 29
 
30
-	/** @var  AccountManager */
31
-	private $accountManager = null;
32
-
33
-	/** @var ILogger */
34
-	private $logger;
35
-
36
-	/**
37
-	 * Hooks constructor.
38
-	 *
39
-	 * @param ILogger $logger
40
-	 */
41
-	public function __construct(ILogger $logger) {
42
-		$this->logger = $logger;
43
-	}
44
-
45
-	/**
46
-	 * update accounts table if email address or display name was changed from outside
47
-	 *
48
-	 * @param array $params
49
-	 */
50
-	public function changeUserHook($params) {
51
-
52
-		$accountManager = $this->getAccountManager();
53
-
54
-		/** @var IUser $user */
55
-		$user = isset($params['user']) ? $params['user'] : null;
56
-		$feature = isset($params['feature']) ? $params['feature'] : null;
57
-		$newValue = isset($params['value']) ? $params['value'] : null;
58
-
59
-		if (is_null($user) || is_null($feature) || is_null($newValue)) {
60
-			$this->logger->warning('Missing expected parameters in change user hook');
61
-			return;
62
-		}
63
-
64
-		$accountData = $accountManager->getUser($user);
65
-
66
-		switch ($feature) {
67
-			case 'eMailAddress':
68
-				if ($accountData[AccountManager::PROPERTY_EMAIL]['value'] !== $newValue) {
69
-					$accountData[AccountManager::PROPERTY_EMAIL]['value'] = $newValue;
70
-					$accountManager->updateUser($user, $accountData);
71
-				}
72
-				break;
73
-			case 'displayName':
74
-				if ($accountData[AccountManager::PROPERTY_DISPLAYNAME]['value'] !== $newValue) {
75
-					$accountData[AccountManager::PROPERTY_DISPLAYNAME]['value'] = $newValue;
76
-					$accountManager->updateUser($user, $accountData);
77
-				}
78
-				break;
79
-		}
80
-
81
-	}
82
-
83
-	/**
84
-	 * return instance of accountManager
85
-	 *
86
-	 * @return AccountManager
87
-	 */
88
-	protected function getAccountManager() {
89
-		if (is_null($this->accountManager)) {
90
-			$this->accountManager = new AccountManager(
91
-				\OC::$server->getDatabaseConnection(),
92
-				\OC::$server->getEventDispatcher(),
93
-				\OC::$server->getJobList()
94
-			);
95
-		}
96
-		return $this->accountManager;
97
-	}
30
+    /** @var  AccountManager */
31
+    private $accountManager = null;
32
+
33
+    /** @var ILogger */
34
+    private $logger;
35
+
36
+    /**
37
+     * Hooks constructor.
38
+     *
39
+     * @param ILogger $logger
40
+     */
41
+    public function __construct(ILogger $logger) {
42
+        $this->logger = $logger;
43
+    }
44
+
45
+    /**
46
+     * update accounts table if email address or display name was changed from outside
47
+     *
48
+     * @param array $params
49
+     */
50
+    public function changeUserHook($params) {
51
+
52
+        $accountManager = $this->getAccountManager();
53
+
54
+        /** @var IUser $user */
55
+        $user = isset($params['user']) ? $params['user'] : null;
56
+        $feature = isset($params['feature']) ? $params['feature'] : null;
57
+        $newValue = isset($params['value']) ? $params['value'] : null;
58
+
59
+        if (is_null($user) || is_null($feature) || is_null($newValue)) {
60
+            $this->logger->warning('Missing expected parameters in change user hook');
61
+            return;
62
+        }
63
+
64
+        $accountData = $accountManager->getUser($user);
65
+
66
+        switch ($feature) {
67
+            case 'eMailAddress':
68
+                if ($accountData[AccountManager::PROPERTY_EMAIL]['value'] !== $newValue) {
69
+                    $accountData[AccountManager::PROPERTY_EMAIL]['value'] = $newValue;
70
+                    $accountManager->updateUser($user, $accountData);
71
+                }
72
+                break;
73
+            case 'displayName':
74
+                if ($accountData[AccountManager::PROPERTY_DISPLAYNAME]['value'] !== $newValue) {
75
+                    $accountData[AccountManager::PROPERTY_DISPLAYNAME]['value'] = $newValue;
76
+                    $accountManager->updateUser($user, $accountData);
77
+                }
78
+                break;
79
+        }
80
+
81
+    }
82
+
83
+    /**
84
+     * return instance of accountManager
85
+     *
86
+     * @return AccountManager
87
+     */
88
+    protected function getAccountManager() {
89
+        if (is_null($this->accountManager)) {
90
+            $this->accountManager = new AccountManager(
91
+                \OC::$server->getDatabaseConnection(),
92
+                \OC::$server->getEventDispatcher(),
93
+                \OC::$server->getJobList()
94
+            );
95
+        }
96
+        return $this->accountManager;
97
+    }
98 98
 
99 99
 }
Please login to merge, or discard this patch.
apps/lookup_server_connector/lib/UpdateLookupServer.php 1 patch
Indentation   +95 added lines, -95 removed lines patch added patch discarded remove patch
@@ -35,107 +35,107 @@
 block discarded – undo
35 35
  * @package OCA\LookupServerConnector
36 36
  */
37 37
 class UpdateLookupServer {
38
-	/** @var AccountManager */
39
-	private $accountManager;
40
-	/** @var IClientService */
41
-	private $clientService;
42
-	/** @var Signer */
43
-	private $signer;
44
-	/** @var IJobList */
45
-	private $jobList;
46
-	/** @var string URL point to lookup server */
47
-	private $lookupServer = 'https://lookup.nextcloud.com';
38
+    /** @var AccountManager */
39
+    private $accountManager;
40
+    /** @var IClientService */
41
+    private $clientService;
42
+    /** @var Signer */
43
+    private $signer;
44
+    /** @var IJobList */
45
+    private $jobList;
46
+    /** @var string URL point to lookup server */
47
+    private $lookupServer = 'https://lookup.nextcloud.com';
48 48
 
49
-	/**
50
-	 * @param AccountManager $accountManager
51
-	 * @param IClientService $clientService
52
-	 * @param Signer $signer
53
-	 * @param IJobList $jobList
54
-	 * @param string $lookupServer if nothing is given we use the default lookup server
55
-	 */
56
-	public function __construct(AccountManager $accountManager,
57
-								IClientService $clientService,
58
-								Signer $signer,
59
-								IJobList $jobList,
60
-								$lookupServer = '') {
61
-		$this->accountManager = $accountManager;
62
-		$this->clientService = $clientService;
63
-		$this->signer = $signer;
64
-		$this->jobList = $jobList;
65
-		if ($lookupServer !== '') {
66
-			$this->lookupServer = $lookupServer;
67
-		}
68
-		$this->lookupServer = rtrim($this->lookupServer, '/');
69
-		$this->lookupServer .= '/users';
70
-	}
49
+    /**
50
+     * @param AccountManager $accountManager
51
+     * @param IClientService $clientService
52
+     * @param Signer $signer
53
+     * @param IJobList $jobList
54
+     * @param string $lookupServer if nothing is given we use the default lookup server
55
+     */
56
+    public function __construct(AccountManager $accountManager,
57
+                                IClientService $clientService,
58
+                                Signer $signer,
59
+                                IJobList $jobList,
60
+                                $lookupServer = '') {
61
+        $this->accountManager = $accountManager;
62
+        $this->clientService = $clientService;
63
+        $this->signer = $signer;
64
+        $this->jobList = $jobList;
65
+        if ($lookupServer !== '') {
66
+            $this->lookupServer = $lookupServer;
67
+        }
68
+        $this->lookupServer = rtrim($this->lookupServer, '/');
69
+        $this->lookupServer .= '/users';
70
+    }
71 71
 
72
-	/**
73
-	 * @param IUser $user
74
-	 */
75
-	public function userUpdated(IUser $user) {
76
-		$userData = $this->accountManager->getUser($user);
77
-		$publicData = [];
72
+    /**
73
+     * @param IUser $user
74
+     */
75
+    public function userUpdated(IUser $user) {
76
+        $userData = $this->accountManager->getUser($user);
77
+        $publicData = [];
78 78
 
79
-		foreach ($userData as $key => $data) {
80
-			if ($data['scope'] === AccountManager::VISIBILITY_PUBLIC) {
81
-				$publicData[$key] = $data;
82
-			}
83
-		}
79
+        foreach ($userData as $key => $data) {
80
+            if ($data['scope'] === AccountManager::VISIBILITY_PUBLIC) {
81
+                $publicData[$key] = $data;
82
+            }
83
+        }
84 84
 
85
-		$this->sendToLookupServer($user, $publicData);
86
-	}
85
+        $this->sendToLookupServer($user, $publicData);
86
+    }
87 87
 
88
-	/**
89
-	 * send public user data to the lookup server
90
-	 *
91
-	 * @param IUser $user
92
-	 * @param array $publicData
93
-	 */
94
-	protected function sendToLookupServer(IUser $user, array $publicData) {
88
+    /**
89
+     * send public user data to the lookup server
90
+     *
91
+     * @param IUser $user
92
+     * @param array $publicData
93
+     */
94
+    protected function sendToLookupServer(IUser $user, array $publicData) {
95 95
 
96
-		$dataArray = ['federationId' => $user->getCloudId()];
96
+        $dataArray = ['federationId' => $user->getCloudId()];
97 97
 
98
-		if (!empty($publicData)) {
99
-			$dataArray['name'] = isset($publicData[AccountManager::PROPERTY_DISPLAYNAME]) ? $publicData[AccountManager::PROPERTY_DISPLAYNAME]['value'] : '';
100
-			$dataArray['email'] = isset($publicData[AccountManager::PROPERTY_EMAIL]) ? $publicData[AccountManager::PROPERTY_EMAIL]['value'] : '';
101
-			$dataArray['address'] = isset($publicData[AccountManager::PROPERTY_ADDRESS]) ? $publicData[AccountManager::PROPERTY_ADDRESS]['value'] : '';
102
-			$dataArray['website'] = isset($publicData[AccountManager::PROPERTY_WEBSITE]) ? $publicData[AccountManager::PROPERTY_WEBSITE]['value'] : '';
103
-			$dataArray['twitter'] = isset($publicData[AccountManager::PROPERTY_TWITTER]) ? $publicData[AccountManager::PROPERTY_TWITTER]['value'] : '';
104
-			$dataArray['phone'] = isset($publicData[AccountManager::PROPERTY_PHONE]) ? $publicData[AccountManager::PROPERTY_PHONE]['value'] : '';
105
-			$dataArray['verificationStatus'] =
106
-				[
107
-					AccountManager::PROPERTY_WEBSITE => isset($publicData[AccountManager::PROPERTY_WEBSITE]) ? $publicData[AccountManager::PROPERTY_WEBSITE]['verified'] : '',
108
-					AccountManager::PROPERTY_TWITTER => isset($publicData[AccountManager::PROPERTY_TWITTER]) ? $publicData[AccountManager::PROPERTY_TWITTER]['verified'] : '',
109
-				];
110
-		}
98
+        if (!empty($publicData)) {
99
+            $dataArray['name'] = isset($publicData[AccountManager::PROPERTY_DISPLAYNAME]) ? $publicData[AccountManager::PROPERTY_DISPLAYNAME]['value'] : '';
100
+            $dataArray['email'] = isset($publicData[AccountManager::PROPERTY_EMAIL]) ? $publicData[AccountManager::PROPERTY_EMAIL]['value'] : '';
101
+            $dataArray['address'] = isset($publicData[AccountManager::PROPERTY_ADDRESS]) ? $publicData[AccountManager::PROPERTY_ADDRESS]['value'] : '';
102
+            $dataArray['website'] = isset($publicData[AccountManager::PROPERTY_WEBSITE]) ? $publicData[AccountManager::PROPERTY_WEBSITE]['value'] : '';
103
+            $dataArray['twitter'] = isset($publicData[AccountManager::PROPERTY_TWITTER]) ? $publicData[AccountManager::PROPERTY_TWITTER]['value'] : '';
104
+            $dataArray['phone'] = isset($publicData[AccountManager::PROPERTY_PHONE]) ? $publicData[AccountManager::PROPERTY_PHONE]['value'] : '';
105
+            $dataArray['verificationStatus'] =
106
+                [
107
+                    AccountManager::PROPERTY_WEBSITE => isset($publicData[AccountManager::PROPERTY_WEBSITE]) ? $publicData[AccountManager::PROPERTY_WEBSITE]['verified'] : '',
108
+                    AccountManager::PROPERTY_TWITTER => isset($publicData[AccountManager::PROPERTY_TWITTER]) ? $publicData[AccountManager::PROPERTY_TWITTER]['verified'] : '',
109
+                ];
110
+        }
111 111
 
112
-		$dataArray = $this->signer->sign('lookupserver', $dataArray, $user);
113
-		$httpClient = $this->clientService->newClient();
114
-		try {
115
-			if (empty($publicData)) {
116
-				$httpClient->delete($this->lookupServer,
117
-					[
118
-						'body' => json_encode($dataArray),
119
-						'timeout' => 10,
120
-						'connect_timeout' => 3,
121
-					]
122
-				);
123
-			} else {
124
-				$httpClient->post($this->lookupServer,
125
-					[
126
-						'body' => json_encode($dataArray),
127
-						'timeout' => 10,
128
-						'connect_timeout' => 3,
129
-					]
130
-				);
131
-			}
132
-		} catch (\Exception $e) {
133
-			$this->jobList->add(RetryJob::class,
134
-				[
135
-					'dataArray' => $dataArray,
136
-					'retryNo' => 0,
137
-				]
138
-			);
139
-		}
140
-	}
112
+        $dataArray = $this->signer->sign('lookupserver', $dataArray, $user);
113
+        $httpClient = $this->clientService->newClient();
114
+        try {
115
+            if (empty($publicData)) {
116
+                $httpClient->delete($this->lookupServer,
117
+                    [
118
+                        'body' => json_encode($dataArray),
119
+                        'timeout' => 10,
120
+                        'connect_timeout' => 3,
121
+                    ]
122
+                );
123
+            } else {
124
+                $httpClient->post($this->lookupServer,
125
+                    [
126
+                        'body' => json_encode($dataArray),
127
+                        'timeout' => 10,
128
+                        'connect_timeout' => 3,
129
+                    ]
130
+                );
131
+            }
132
+        } catch (\Exception $e) {
133
+            $this->jobList->add(RetryJob::class,
134
+                [
135
+                    'dataArray' => $dataArray,
136
+                    'retryNo' => 0,
137
+                ]
138
+            );
139
+        }
140
+    }
141 141
 }
Please login to merge, or discard this patch.
apps/lookup_server_connector/appinfo/app.php 1 patch
Indentation   +23 added lines, -23 removed lines patch added patch discarded remove patch
@@ -22,30 +22,30 @@
 block discarded – undo
22 22
 $dispatcher = \OC::$server->getEventDispatcher();
23 23
 
24 24
 $dispatcher->addListener('OC\AccountManager::userUpdated', function(\Symfony\Component\EventDispatcher\GenericEvent $event) {
25
-	$user = $event->getSubject();
25
+    $user = $event->getSubject();
26 26
 
27
-	$keyManager = new \OC\Security\IdentityProof\Manager(
28
-		\OC::$server->getAppDataDir('identityproof'),
29
-		\OC::$server->getCrypto()
30
-	);
27
+    $keyManager = new \OC\Security\IdentityProof\Manager(
28
+        \OC::$server->getAppDataDir('identityproof'),
29
+        \OC::$server->getCrypto()
30
+    );
31 31
 
32
-	$config = \OC::$server->getConfig();
33
-	$lookupServer = $config->getSystemValue('lookup_server', '');
32
+    $config = \OC::$server->getConfig();
33
+    $lookupServer = $config->getSystemValue('lookup_server', '');
34 34
 
35
-	$updateLookupServer = new \OCA\LookupServerConnector\UpdateLookupServer(
36
-		new \OC\Accounts\AccountManager(
37
-			\OC::$server->getDatabaseConnection(),
38
-			\OC::$server->getEventDispatcher(),
39
-			\OC::$server->getJobList()
40
-		),
41
-		\OC::$server->getHTTPClientService(),
42
-		new \OC\Security\IdentityProof\Signer(
43
-			$keyManager,
44
-			new \OC\AppFramework\Utility\TimeFactory(),
45
-			\OC::$server->getUserManager()
46
-		),
47
-		\OC::$server->getJobList(),
48
-		$lookupServer
49
-	);
50
-	$updateLookupServer->userUpdated($user);
35
+    $updateLookupServer = new \OCA\LookupServerConnector\UpdateLookupServer(
36
+        new \OC\Accounts\AccountManager(
37
+            \OC::$server->getDatabaseConnection(),
38
+            \OC::$server->getEventDispatcher(),
39
+            \OC::$server->getJobList()
40
+        ),
41
+        \OC::$server->getHTTPClientService(),
42
+        new \OC\Security\IdentityProof\Signer(
43
+            $keyManager,
44
+            new \OC\AppFramework\Utility\TimeFactory(),
45
+            \OC::$server->getUserManager()
46
+        ),
47
+        \OC::$server->getJobList(),
48
+        $lookupServer
49
+    );
50
+    $updateLookupServer->userUpdated($user);
51 51
 });
Please login to merge, or discard this patch.
settings/BackgroundJobs/VerifyUserData.php 2 patches
Spacing   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -105,9 +105,9 @@  discard block
 block discarded – undo
105 105
 
106 106
 	protected function run($argument) {
107 107
 
108
-		$try = (int)$argument['try'] + 1;
108
+		$try = (int) $argument['try'] + 1;
109 109
 
110
-		switch($argument['type']) {
110
+		switch ($argument['type']) {
111 111
 			case AccountManager::PROPERTY_WEBSITE:
112 112
 				$result = $this->verifyWebsite($argument);
113 113
 				break;
@@ -117,7 +117,7 @@  discard block
 block discarded – undo
117 117
 				break;
118 118
 			default:
119 119
 				// no valid type given, no need to retry
120
-				$this->logger->error($argument['type'] . ' is no valid type for user account data.');
120
+				$this->logger->error($argument['type'].' is no valid type for user account data.');
121 121
 				$result = true;
122 122
 		}
123 123
 
@@ -136,7 +136,7 @@  discard block
 block discarded – undo
136 136
 
137 137
 		$result = false;
138 138
 
139
-		$url = rtrim($argument['data'], '/') . '/well-known/' . 'CloudIdVerificationCode.txt';
139
+		$url = rtrim($argument['data'], '/').'/well-known/'.'CloudIdVerificationCode.txt';
140 140
 
141 141
 		$client = $this->httpClientService->newClient();
142 142
 		try {
@@ -153,7 +153,7 @@  discard block
 block discarded – undo
153 153
 			$user = $this->userManager->get($argument['uid']);
154 154
 			// we don't check a valid user -> give up
155 155
 			if ($user === null) {
156
-				$this->logger->error($argument['uid'] . ' doesn\'t exist, can\'t verify user data.');
156
+				$this->logger->error($argument['uid'].' doesn\'t exist, can\'t verify user data.');
157 157
 				return $result;
158 158
 			}
159 159
 			$userData = $this->accountManager->getUser($user);
@@ -183,7 +183,7 @@  discard block
 block discarded – undo
183 183
 
184 184
 		// we don't check a valid user -> give up
185 185
 		if ($user === null) {
186
-			$this->logger->error($argument['uid'] . ' doesn\'t exist, can\'t verify user data.');
186
+			$this->logger->error($argument['uid'].' doesn\'t exist, can\'t verify user data.');
187 187
 			return true;
188 188
 		}
189 189
 
@@ -222,7 +222,7 @@  discard block
 block discarded – undo
222 222
 		try {
223 223
 			$client = $this->httpClientService->newClient();
224 224
 			$response = $client->get(
225
-				$this->lookupServerUrl . '/users?search=' . urlencode($cloudId) . '&exactCloudId=1',
225
+				$this->lookupServerUrl.'/users?search='.urlencode($cloudId).'&exactCloudId=1',
226 226
 				[
227 227
 					'timeout' => 10,
228 228
 					'connect_timeout' => 3,
@@ -255,7 +255,7 @@  discard block
 block discarded – undo
255 255
 				'data' => $argument['data'],
256 256
 				'type' => $argument['type'],
257 257
 				'uid' => $argument['uid'],
258
-				'try' => (int)$argument['try'] + 1,
258
+				'try' => (int) $argument['try'] + 1,
259 259
 				'lastRun' => time()
260 260
 			]
261 261
 		);
@@ -268,7 +268,7 @@  discard block
 block discarded – undo
268 268
 	 * @return bool
269 269
 	 */
270 270
 	protected function shouldRun(array $argument) {
271
-		$lastRun = (int)$argument['lastRun'];
271
+		$lastRun = (int) $argument['lastRun'];
272 272
 		return ((time() - $lastRun) > $this->interval);
273 273
 	}
274 274
 
Please login to merge, or discard this patch.
Indentation   +234 added lines, -234 removed lines patch added patch discarded remove patch
@@ -35,239 +35,239 @@
 block discarded – undo
35 35
 
36 36
 class VerifyUserData extends Job {
37 37
 
38
-	/** @var  bool */
39
-	private $retainJob = true;
40
-
41
-	/** @var int max number of attempts to send the request */
42
-	private $maxTry = 24;
43
-
44
-	/** @var int how much time should be between two tries (1 hour) */
45
-	private $interval = 3600;
46
-
47
-	/** @var AccountManager */
48
-	private $accountManager;
49
-
50
-	/** @var IUserManager */
51
-	private $userManager;
52
-
53
-	/** @var IClientService */
54
-	private $httpClientService;
55
-
56
-	/** @var ILogger */
57
-	private $logger;
58
-
59
-	/** @var string */
60
-	private $lookupServerUrl;
61
-
62
-	/**
63
-	 * VerifyUserData constructor.
64
-	 *
65
-	 * @param AccountManager $accountManager
66
-	 * @param IUserManager $userManager
67
-	 * @param IClientService $clientService
68
-	 * @param ILogger $logger
69
-	 * @param IConfig $config
70
-	 */
71
-	public function __construct(AccountManager $accountManager,
72
-								IUserManager $userManager,
73
-								IClientService $clientService,
74
-								ILogger $logger,
75
-								IConfig $config
76
-	) {
77
-		$this->accountManager = $accountManager;
78
-		$this->userManager = $userManager;
79
-		$this->httpClientService = $clientService;
80
-		$this->logger = $logger;
81
-
82
-		$lookupServerUrl = $config->getSystemValue('lookup_server', 'https://lookup.nextcloud.com');
83
-		$this->lookupServerUrl = rtrim($lookupServerUrl, '/');
84
-	}
85
-
86
-	/**
87
-	 * run the job, then remove it from the jobList
88
-	 *
89
-	 * @param JobList $jobList
90
-	 * @param ILogger $logger
91
-	 */
92
-	public function execute($jobList, ILogger $logger = null) {
93
-
94
-		if ($this->shouldRun($this->argument)) {
95
-			parent::execute($jobList, $logger);
96
-			$jobList->remove($this, $this->argument);
97
-			if ($this->retainJob) {
98
-				$this->reAddJob($jobList, $this->argument);
99
-			}
100
-		}
101
-
102
-	}
103
-
104
-	protected function run($argument) {
105
-
106
-		$try = (int)$argument['try'] + 1;
107
-
108
-		switch($argument['type']) {
109
-			case AccountManager::PROPERTY_WEBSITE:
110
-				$result = $this->verifyWebsite($argument);
111
-				break;
112
-			case AccountManager::PROPERTY_TWITTER:
113
-			case AccountManager::PROPERTY_EMAIL:
114
-				$result = $this->verifyViaLookupServer($argument, $argument['type']);
115
-				break;
116
-			default:
117
-				// no valid type given, no need to retry
118
-				$this->logger->error($argument['type'] . ' is no valid type for user account data.');
119
-				$result = true;
120
-		}
121
-
122
-		if ($result === true || $try > $this->maxTry) {
123
-			$this->retainJob = false;
124
-		}
125
-	}
126
-
127
-	/**
128
-	 * verify web page
129
-	 *
130
-	 * @param array $argument
131
-	 * @return bool true if we could check the verification code, otherwise false
132
-	 */
133
-	protected function verifyWebsite(array $argument) {
134
-
135
-		$result = false;
136
-
137
-		$url = rtrim($argument['data'], '/') . '/well-known/' . 'CloudIdVerificationCode.txt';
138
-
139
-		$client = $this->httpClientService->newClient();
140
-		try {
141
-			$response = $client->get($url);
142
-		} catch (\Exception $e) {
143
-			return false;
144
-		}
145
-
146
-		if ($response->getStatusCode() === Http::STATUS_OK) {
147
-			$result = true;
148
-			$publishedCode = $response->getBody();
149
-			// remove new lines and spaces
150
-			$publishedCodeSanitized = trim(preg_replace('/\s\s+/', ' ', $publishedCode));
151
-			$user = $this->userManager->get($argument['uid']);
152
-			// we don't check a valid user -> give up
153
-			if ($user === null) {
154
-				$this->logger->error($argument['uid'] . ' doesn\'t exist, can\'t verify user data.');
155
-				return $result;
156
-			}
157
-			$userData = $this->accountManager->getUser($user);
158
-
159
-			if ($publishedCodeSanitized === $argument['verificationCode']) {
160
-				$userData[AccountManager::PROPERTY_WEBSITE]['verified'] = AccountManager::VERIFIED;
161
-			} else {
162
-				$userData[AccountManager::PROPERTY_WEBSITE]['verified'] = AccountManager::NOT_VERIFIED;
163
-			}
164
-
165
-			$this->accountManager->updateUser($user, $userData);
166
-		}
167
-
168
-		return $result;
169
-	}
170
-
171
-	/**
172
-	 * verify email address
173
-	 *
174
-	 * @param array $argument
175
-	 * @param string $dataType
176
-	 * @return bool true if we could check the verification code, otherwise false
177
-	 */
178
-	protected function verifyViaLookupServer(array $argument, $dataType) {
179
-
180
-		$user = $this->userManager->get($argument['uid']);
181
-
182
-		// we don't check a valid user -> give up
183
-		if ($user === null) {
184
-			$this->logger->error($argument['uid'] . ' doesn\'t exist, can\'t verify user data.');
185
-			return true;
186
-		}
187
-
188
-		$localUserData = $this->accountManager->getUser($user);
189
-		$cloudId = $user->getCloudId();
190
-
191
-		// ask lookup-server for user data
192
-		$lookupServerData = $this->queryLookupServer($cloudId);
193
-
194
-		// for some reasons we couldn't read any data from the lookup server, try again later
195
-		if (empty($lookupServerData)) {
196
-			return false;
197
-		}
198
-
199
-		// lookup server has verification data for wrong user data (e.g. email address), try again later
200
-		if ($lookupServerData[$dataType]['value'] !== $argument['data']) {
201
-			return false;
202
-		}
203
-
204
-		// lookup server hasn't verified the email address so far, try again later
205
-		if ($lookupServerData[$dataType]['verified'] === AccountManager::NOT_VERIFIED) {
206
-			return false;
207
-		}
208
-
209
-		$localUserData[$dataType]['verified'] = AccountManager::VERIFIED;
210
-		$this->accountManager->updateUser($user, $localUserData);
211
-
212
-		return true;
213
-	}
214
-
215
-	/**
216
-	 * @param string $cloudId
217
-	 * @return array
218
-	 */
219
-	protected function queryLookupServer($cloudId) {
220
-		try {
221
-			$client = $this->httpClientService->newClient();
222
-			$response = $client->get(
223
-				$this->lookupServerUrl . '/users?search=' . urlencode($cloudId) . '&exactCloudId=1',
224
-				[
225
-					'timeout' => 10,
226
-					'connect_timeout' => 3,
227
-				]
228
-			);
229
-
230
-			$body = json_decode($response->getBody(), true);
231
-
232
-			if ($body['federationId'] === $cloudId) {
233
-				return $body;
234
-			}
235
-
236
-		} catch (\Exception $e) {
237
-			// do nothing, we will just re-try later
238
-		}
239
-
240
-		return [];
241
-	}
242
-
243
-	/**
244
-	 * re-add background job with new arguments
245
-	 *
246
-	 * @param IJobList $jobList
247
-	 * @param array $argument
248
-	 */
249
-	protected function reAddJob(IJobList $jobList, array $argument) {
250
-		$jobList->add('OC\Settings\BackgroundJobs\VerifyUserData',
251
-			[
252
-				'verificationCode' => $argument['verificationCode'],
253
-				'data' => $argument['data'],
254
-				'type' => $argument['type'],
255
-				'uid' => $argument['uid'],
256
-				'try' => (int)$argument['try'] + 1,
257
-				'lastRun' => time()
258
-			]
259
-		);
260
-	}
261
-
262
-	/**
263
-	 * test if it is time for the next run
264
-	 *
265
-	 * @param array $argument
266
-	 * @return bool
267
-	 */
268
-	protected function shouldRun(array $argument) {
269
-		$lastRun = (int)$argument['lastRun'];
270
-		return ((time() - $lastRun) > $this->interval);
271
-	}
38
+    /** @var  bool */
39
+    private $retainJob = true;
40
+
41
+    /** @var int max number of attempts to send the request */
42
+    private $maxTry = 24;
43
+
44
+    /** @var int how much time should be between two tries (1 hour) */
45
+    private $interval = 3600;
46
+
47
+    /** @var AccountManager */
48
+    private $accountManager;
49
+
50
+    /** @var IUserManager */
51
+    private $userManager;
52
+
53
+    /** @var IClientService */
54
+    private $httpClientService;
55
+
56
+    /** @var ILogger */
57
+    private $logger;
58
+
59
+    /** @var string */
60
+    private $lookupServerUrl;
61
+
62
+    /**
63
+     * VerifyUserData constructor.
64
+     *
65
+     * @param AccountManager $accountManager
66
+     * @param IUserManager $userManager
67
+     * @param IClientService $clientService
68
+     * @param ILogger $logger
69
+     * @param IConfig $config
70
+     */
71
+    public function __construct(AccountManager $accountManager,
72
+                                IUserManager $userManager,
73
+                                IClientService $clientService,
74
+                                ILogger $logger,
75
+                                IConfig $config
76
+    ) {
77
+        $this->accountManager = $accountManager;
78
+        $this->userManager = $userManager;
79
+        $this->httpClientService = $clientService;
80
+        $this->logger = $logger;
81
+
82
+        $lookupServerUrl = $config->getSystemValue('lookup_server', 'https://lookup.nextcloud.com');
83
+        $this->lookupServerUrl = rtrim($lookupServerUrl, '/');
84
+    }
85
+
86
+    /**
87
+     * run the job, then remove it from the jobList
88
+     *
89
+     * @param JobList $jobList
90
+     * @param ILogger $logger
91
+     */
92
+    public function execute($jobList, ILogger $logger = null) {
93
+
94
+        if ($this->shouldRun($this->argument)) {
95
+            parent::execute($jobList, $logger);
96
+            $jobList->remove($this, $this->argument);
97
+            if ($this->retainJob) {
98
+                $this->reAddJob($jobList, $this->argument);
99
+            }
100
+        }
101
+
102
+    }
103
+
104
+    protected function run($argument) {
105
+
106
+        $try = (int)$argument['try'] + 1;
107
+
108
+        switch($argument['type']) {
109
+            case AccountManager::PROPERTY_WEBSITE:
110
+                $result = $this->verifyWebsite($argument);
111
+                break;
112
+            case AccountManager::PROPERTY_TWITTER:
113
+            case AccountManager::PROPERTY_EMAIL:
114
+                $result = $this->verifyViaLookupServer($argument, $argument['type']);
115
+                break;
116
+            default:
117
+                // no valid type given, no need to retry
118
+                $this->logger->error($argument['type'] . ' is no valid type for user account data.');
119
+                $result = true;
120
+        }
121
+
122
+        if ($result === true || $try > $this->maxTry) {
123
+            $this->retainJob = false;
124
+        }
125
+    }
126
+
127
+    /**
128
+     * verify web page
129
+     *
130
+     * @param array $argument
131
+     * @return bool true if we could check the verification code, otherwise false
132
+     */
133
+    protected function verifyWebsite(array $argument) {
134
+
135
+        $result = false;
136
+
137
+        $url = rtrim($argument['data'], '/') . '/well-known/' . 'CloudIdVerificationCode.txt';
138
+
139
+        $client = $this->httpClientService->newClient();
140
+        try {
141
+            $response = $client->get($url);
142
+        } catch (\Exception $e) {
143
+            return false;
144
+        }
145
+
146
+        if ($response->getStatusCode() === Http::STATUS_OK) {
147
+            $result = true;
148
+            $publishedCode = $response->getBody();
149
+            // remove new lines and spaces
150
+            $publishedCodeSanitized = trim(preg_replace('/\s\s+/', ' ', $publishedCode));
151
+            $user = $this->userManager->get($argument['uid']);
152
+            // we don't check a valid user -> give up
153
+            if ($user === null) {
154
+                $this->logger->error($argument['uid'] . ' doesn\'t exist, can\'t verify user data.');
155
+                return $result;
156
+            }
157
+            $userData = $this->accountManager->getUser($user);
158
+
159
+            if ($publishedCodeSanitized === $argument['verificationCode']) {
160
+                $userData[AccountManager::PROPERTY_WEBSITE]['verified'] = AccountManager::VERIFIED;
161
+            } else {
162
+                $userData[AccountManager::PROPERTY_WEBSITE]['verified'] = AccountManager::NOT_VERIFIED;
163
+            }
164
+
165
+            $this->accountManager->updateUser($user, $userData);
166
+        }
167
+
168
+        return $result;
169
+    }
170
+
171
+    /**
172
+     * verify email address
173
+     *
174
+     * @param array $argument
175
+     * @param string $dataType
176
+     * @return bool true if we could check the verification code, otherwise false
177
+     */
178
+    protected function verifyViaLookupServer(array $argument, $dataType) {
179
+
180
+        $user = $this->userManager->get($argument['uid']);
181
+
182
+        // we don't check a valid user -> give up
183
+        if ($user === null) {
184
+            $this->logger->error($argument['uid'] . ' doesn\'t exist, can\'t verify user data.');
185
+            return true;
186
+        }
187
+
188
+        $localUserData = $this->accountManager->getUser($user);
189
+        $cloudId = $user->getCloudId();
190
+
191
+        // ask lookup-server for user data
192
+        $lookupServerData = $this->queryLookupServer($cloudId);
193
+
194
+        // for some reasons we couldn't read any data from the lookup server, try again later
195
+        if (empty($lookupServerData)) {
196
+            return false;
197
+        }
198
+
199
+        // lookup server has verification data for wrong user data (e.g. email address), try again later
200
+        if ($lookupServerData[$dataType]['value'] !== $argument['data']) {
201
+            return false;
202
+        }
203
+
204
+        // lookup server hasn't verified the email address so far, try again later
205
+        if ($lookupServerData[$dataType]['verified'] === AccountManager::NOT_VERIFIED) {
206
+            return false;
207
+        }
208
+
209
+        $localUserData[$dataType]['verified'] = AccountManager::VERIFIED;
210
+        $this->accountManager->updateUser($user, $localUserData);
211
+
212
+        return true;
213
+    }
214
+
215
+    /**
216
+     * @param string $cloudId
217
+     * @return array
218
+     */
219
+    protected function queryLookupServer($cloudId) {
220
+        try {
221
+            $client = $this->httpClientService->newClient();
222
+            $response = $client->get(
223
+                $this->lookupServerUrl . '/users?search=' . urlencode($cloudId) . '&exactCloudId=1',
224
+                [
225
+                    'timeout' => 10,
226
+                    'connect_timeout' => 3,
227
+                ]
228
+            );
229
+
230
+            $body = json_decode($response->getBody(), true);
231
+
232
+            if ($body['federationId'] === $cloudId) {
233
+                return $body;
234
+            }
235
+
236
+        } catch (\Exception $e) {
237
+            // do nothing, we will just re-try later
238
+        }
239
+
240
+        return [];
241
+    }
242
+
243
+    /**
244
+     * re-add background job with new arguments
245
+     *
246
+     * @param IJobList $jobList
247
+     * @param array $argument
248
+     */
249
+    protected function reAddJob(IJobList $jobList, array $argument) {
250
+        $jobList->add('OC\Settings\BackgroundJobs\VerifyUserData',
251
+            [
252
+                'verificationCode' => $argument['verificationCode'],
253
+                'data' => $argument['data'],
254
+                'type' => $argument['type'],
255
+                'uid' => $argument['uid'],
256
+                'try' => (int)$argument['try'] + 1,
257
+                'lastRun' => time()
258
+            ]
259
+        );
260
+    }
261
+
262
+    /**
263
+     * test if it is time for the next run
264
+     *
265
+     * @param array $argument
266
+     * @return bool
267
+     */
268
+    protected function shouldRun(array $argument) {
269
+        $lastRun = (int)$argument['lastRun'];
270
+        return ((time() - $lastRun) > $this->interval);
271
+    }
272 272
 
273 273
 }
Please login to merge, or discard this patch.
settings/personal.php 1 patch
Indentation   +67 added lines, -67 removed lines patch added patch discarded remove patch
@@ -41,9 +41,9 @@  discard block
 block discarded – undo
41 41
 $defaults = \OC::$server->getThemingDefaults();
42 42
 $certificateManager = \OC::$server->getCertificateManager();
43 43
 $accountManager = new \OC\Accounts\AccountManager(
44
-	\OC::$server->getDatabaseConnection(),
45
-	\OC::$server->getEventDispatcher(),
46
-	\OC::$server->getJobList()
44
+    \OC::$server->getDatabaseConnection(),
45
+    \OC::$server->getEventDispatcher(),
46
+    \OC::$server->getJobList()
47 47
 );
48 48
 $config = \OC::$server->getConfig();
49 49
 $urlGenerator = \OC::$server->getURLGenerator();
@@ -78,72 +78,72 @@  discard block
 block discarded – undo
78 78
 
79 79
 // array of common languages
80 80
 $commonLangCodes = array(
81
-	'en', 'es', 'fr', 'de', 'de_DE', 'ja', 'ar', 'ru', 'nl', 'it', 'pt_BR', 'pt_PT', 'da', 'fi_FI', 'nb_NO', 'sv', 'tr', 'zh_CN', 'ko'
81
+    'en', 'es', 'fr', 'de', 'de_DE', 'ja', 'ar', 'ru', 'nl', 'it', 'pt_BR', 'pt_PT', 'da', 'fi_FI', 'nb_NO', 'sv', 'tr', 'zh_CN', 'ko'
82 82
 );
83 83
 
84 84
 $languages=array();
85 85
 $commonLanguages = array();
86 86
 foreach($languageCodes as $lang) {
87
-	$l = \OC::$server->getL10N('settings', $lang);
88
-	// TRANSLATORS this is the language name for the language switcher in the personal settings and should be the localized version
89
-	$potentialName = (string) $l->t('__language_name__');
90
-	if($l->getLanguageCode() === $lang && substr($potentialName, 0, 1) !== '_') {//first check if the language name is in the translation file
91
-		$ln = array('code' => $lang, 'name' => $potentialName);
92
-	} elseif ($lang === 'en') {
93
-		$ln = ['code' => $lang, 'name' => 'English (US)'];
94
-	}else{//fallback to language code
95
-		$ln=array('code'=>$lang, 'name'=>$lang);
96
-	}
97
-
98
-	// put appropriate languages into appropriate arrays, to print them sorted
99
-	// used language -> common languages -> divider -> other languages
100
-	if ($lang === $userLang) {
101
-		$userLang = $ln;
102
-	} elseif (in_array($lang, $commonLangCodes)) {
103
-		$commonLanguages[array_search($lang, $commonLangCodes)]=$ln;
104
-	} else {
105
-		$languages[]=$ln;
106
-	}
87
+    $l = \OC::$server->getL10N('settings', $lang);
88
+    // TRANSLATORS this is the language name for the language switcher in the personal settings and should be the localized version
89
+    $potentialName = (string) $l->t('__language_name__');
90
+    if($l->getLanguageCode() === $lang && substr($potentialName, 0, 1) !== '_') {//first check if the language name is in the translation file
91
+        $ln = array('code' => $lang, 'name' => $potentialName);
92
+    } elseif ($lang === 'en') {
93
+        $ln = ['code' => $lang, 'name' => 'English (US)'];
94
+    }else{//fallback to language code
95
+        $ln=array('code'=>$lang, 'name'=>$lang);
96
+    }
97
+
98
+    // put appropriate languages into appropriate arrays, to print them sorted
99
+    // used language -> common languages -> divider -> other languages
100
+    if ($lang === $userLang) {
101
+        $userLang = $ln;
102
+    } elseif (in_array($lang, $commonLangCodes)) {
103
+        $commonLanguages[array_search($lang, $commonLangCodes)]=$ln;
104
+    } else {
105
+        $languages[]=$ln;
106
+    }
107 107
 }
108 108
 
109 109
 // if user language is not available but set somehow: show the actual code as name
110 110
 if (!is_array($userLang)) {
111
-	$userLang = [
112
-		'code' => $userLang,
113
-		'name' => $userLang,
114
-	];
111
+    $userLang = [
112
+        'code' => $userLang,
113
+        'name' => $userLang,
114
+    ];
115 115
 }
116 116
 
117 117
 ksort($commonLanguages);
118 118
 
119 119
 // sort now by displayed language not the iso-code
120 120
 usort( $languages, function ($a, $b) {
121
-	if ($a['code'] === $a['name'] && $b['code'] !== $b['name']) {
122
-		// If a doesn't have a name, but b does, list b before a
123
-		return 1;
124
-	}
125
-	if ($a['code'] !== $a['name'] && $b['code'] === $b['name']) {
126
-		// If a does have a name, but b doesn't, list a before b
127
-		return -1;
128
-	}
129
-	// Otherwise compare the names
130
-	return strcmp($a['name'], $b['name']);
121
+    if ($a['code'] === $a['name'] && $b['code'] !== $b['name']) {
122
+        // If a doesn't have a name, but b does, list b before a
123
+        return 1;
124
+    }
125
+    if ($a['code'] !== $a['name'] && $b['code'] === $b['name']) {
126
+        // If a does have a name, but b doesn't, list a before b
127
+        return -1;
128
+    }
129
+    // Otherwise compare the names
130
+    return strcmp($a['name'], $b['name']);
131 131
 });
132 132
 
133 133
 //links to clients
134 134
 $clients = array(
135
-	'desktop' => $config->getSystemValue('customclient_desktop', $defaults->getSyncClientUrl()),
136
-	'android' => $config->getSystemValue('customclient_android', $defaults->getAndroidClientUrl()),
137
-	'ios'     => $config->getSystemValue('customclient_ios', $defaults->getiOSClientUrl())
135
+    'desktop' => $config->getSystemValue('customclient_desktop', $defaults->getSyncClientUrl()),
136
+    'android' => $config->getSystemValue('customclient_android', $defaults->getAndroidClientUrl()),
137
+    'ios'     => $config->getSystemValue('customclient_ios', $defaults->getiOSClientUrl())
138 138
 );
139 139
 
140 140
 // only show root certificate import if external storages are enabled
141 141
 $enableCertImport = false;
142 142
 $externalStorageEnabled = \OC::$server->getAppManager()->isEnabledForUser('files_external');
143 143
 if ($externalStorageEnabled) {
144
-	/** @var \OCA\Files_External\Service\BackendService $backendService */
145
-	$backendService = \OC_Mount_Config::$app->getContainer()->query('\OCA\Files_External\Service\BackendService');
146
-	$enableCertImport = $backendService->isUserMountingAllowed();
144
+    /** @var \OCA\Files_External\Service\BackendService $backendService */
145
+    $backendService = \OC_Mount_Config::$app->getContainer()->query('\OCA\Files_External\Service\BackendService');
146
+    $enableCertImport = $backendService->isUserMountingAllowed();
147 147
 }
148 148
 
149 149
 
@@ -152,9 +152,9 @@  discard block
 block discarded – undo
152 152
 $tmpl = new OC_Template( 'settings', 'personal', 'user');
153 153
 $tmpl->assign('usage', OC_Helper::humanFileSize($storageInfo['used']));
154 154
 if ($storageInfo['quota'] === \OCP\Files\FileInfo::SPACE_UNLIMITED) {
155
-	$totalSpace = $l->t('Unlimited');
155
+    $totalSpace = $l->t('Unlimited');
156 156
 } else {
157
-	$totalSpace = OC_Helper::humanFileSize($storageInfo['total']);
157
+    $totalSpace = OC_Helper::humanFileSize($storageInfo['total']);
158 158
 }
159 159
 
160 160
 $uid = $user->getUID();
@@ -212,30 +212,30 @@  discard block
 block discarded – undo
212 212
 
213 213
 // add bottom hardcoded forms from the template
214 214
 if ($enableCertImport) {
215
-	$certificatesTemplate = new OC_Template('settings', 'certificates');
216
-	$certificatesTemplate->assign('type', 'personal');
217
-	$certificatesTemplate->assign('uploadRoute', 'settings.Certificate.addPersonalRootCertificate');
218
-	$certificatesTemplate->assign('certs', $certificateManager->listCertificates());
219
-	$certificatesTemplate->assign('urlGenerator', $urlGenerator);
220
-	$forms[] = $certificatesTemplate->fetchPage();
215
+    $certificatesTemplate = new OC_Template('settings', 'certificates');
216
+    $certificatesTemplate->assign('type', 'personal');
217
+    $certificatesTemplate->assign('uploadRoute', 'settings.Certificate.addPersonalRootCertificate');
218
+    $certificatesTemplate->assign('certs', $certificateManager->listCertificates());
219
+    $certificatesTemplate->assign('urlGenerator', $urlGenerator);
220
+    $forms[] = $certificatesTemplate->fetchPage();
221 221
 }
222 222
 
223 223
 $formsMap = array_map(function($form){
224
-	if (preg_match('%(<h2(?P<class>[^>]*)>.*?</h2>)%i', $form, $regs)) {
225
-		$sectionName = str_replace('<h2'.$regs['class'].'>', '', $regs[0]);
226
-		$sectionName = str_replace('</h2>', '', $sectionName);
227
-		$anchor = strtolower($sectionName);
228
-		$anchor = str_replace(' ', '-', $anchor);
229
-
230
-		return array(
231
-			'anchor' => $anchor,
232
-			'section-name' => $sectionName,
233
-			'form' => $form
234
-		);
235
-	}
236
-	return array(
237
-		'form' => $form
238
-	);
224
+    if (preg_match('%(<h2(?P<class>[^>]*)>.*?</h2>)%i', $form, $regs)) {
225
+        $sectionName = str_replace('<h2'.$regs['class'].'>', '', $regs[0]);
226
+        $sectionName = str_replace('</h2>', '', $sectionName);
227
+        $anchor = strtolower($sectionName);
228
+        $anchor = str_replace(' ', '-', $anchor);
229
+
230
+        return array(
231
+            'anchor' => $anchor,
232
+            'section-name' => $sectionName,
233
+            'form' => $form
234
+        );
235
+    }
236
+    return array(
237
+        'form' => $form
238
+    );
239 239
 }, $forms);
240 240
 
241 241
 $formsAndMore = array_merge($formsAndMore, $formsMap);
Please login to merge, or discard this patch.
lib/private/Accounts/AccountManager.php 2 patches
Spacing   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -195,38 +195,38 @@
 block discarded – undo
195 195
 		$emailVerified = isset($oldData[self::PROPERTY_EMAIL]['verified']) && $oldData[self::PROPERTY_EMAIL]['verified'] === self::VERIFIED;
196 196
 
197 197
 		// keep old verification status if we don't have a new one
198
-		if(!isset($newData[self::PROPERTY_TWITTER]['verified'])) {
198
+		if (!isset($newData[self::PROPERTY_TWITTER]['verified'])) {
199 199
 			// keep old verification status if value didn't changed and an old value exists
200 200
 			$keepOldStatus = $newData[self::PROPERTY_TWITTER]['value'] === $oldData[self::PROPERTY_TWITTER]['value'] && isset($oldData[self::PROPERTY_TWITTER]['verified']);
201 201
 			$newData[self::PROPERTY_TWITTER]['verified'] = $keepOldStatus ? $oldData[self::PROPERTY_TWITTER]['verified'] : self::NOT_VERIFIED;
202 202
 		}
203 203
 
204
-		if(!isset($newData[self::PROPERTY_WEBSITE]['verified'])) {
204
+		if (!isset($newData[self::PROPERTY_WEBSITE]['verified'])) {
205 205
 			// keep old verification status if value didn't changed and an old value exists
206 206
 			$keepOldStatus = $newData[self::PROPERTY_WEBSITE]['value'] === $oldData[self::PROPERTY_WEBSITE]['value'] && isset($oldData[self::PROPERTY_WEBSITE]['verified']);
207 207
 			$newData[self::PROPERTY_WEBSITE]['verified'] = $keepOldStatus ? $oldData[self::PROPERTY_WEBSITE]['verified'] : self::NOT_VERIFIED;
208 208
 		}
209 209
 
210
-		if(!isset($newData[self::PROPERTY_EMAIL]['verified'])) {
210
+		if (!isset($newData[self::PROPERTY_EMAIL]['verified'])) {
211 211
 			// keep old verification status if value didn't changed and an old value exists
212 212
 			$keepOldStatus = $newData[self::PROPERTY_EMAIL]['value'] === $oldData[self::PROPERTY_EMAIL]['value'] && isset($oldData[self::PROPERTY_EMAIL]['verified']);
213 213
 			$newData[self::PROPERTY_EMAIL]['verified'] = $keepOldStatus ? $oldData[self::PROPERTY_EMAIL]['verified'] : self::VERIFICATION_IN_PROGRESS;
214 214
 		}
215 215
 
216 216
 		// reset verification status if a value from a previously verified data was changed
217
-		if($twitterVerified &&
217
+		if ($twitterVerified &&
218 218
 			$oldData[self::PROPERTY_TWITTER]['value'] !== $newData[self::PROPERTY_TWITTER]['value']
219 219
 		) {
220 220
 			$newData[self::PROPERTY_TWITTER]['verified'] = self::NOT_VERIFIED;
221 221
 		}
222 222
 
223
-		if($websiteVerified &&
223
+		if ($websiteVerified &&
224 224
 			$oldData[self::PROPERTY_WEBSITE]['value'] !== $newData[self::PROPERTY_WEBSITE]['value']
225 225
 		) {
226 226
 			$newData[self::PROPERTY_WEBSITE]['verified'] = self::NOT_VERIFIED;
227 227
 		}
228 228
 
229
-		if($emailVerified &&
229
+		if ($emailVerified &&
230 230
 			$oldData[self::PROPERTY_EMAIL]['value'] !== $newData[self::PROPERTY_EMAIL]['value']
231 231
 		) {
232 232
 			$newData[self::PROPERTY_EMAIL]['verified'] = self::NOT_VERIFIED;
Please login to merge, or discard this patch.
Indentation   +287 added lines, -287 removed lines patch added patch discarded remove patch
@@ -39,292 +39,292 @@
 block discarded – undo
39 39
  */
40 40
 class AccountManager {
41 41
 
42
-	/** nobody can see my account details */
43
-	const VISIBILITY_PRIVATE = 'private';
44
-	/** only contacts, especially trusted servers can see my contact details */
45
-	const VISIBILITY_CONTACTS_ONLY = 'contacts';
46
-	/** every body ca see my contact detail, will be published to the lookup server */
47
-	const VISIBILITY_PUBLIC = 'public';
48
-
49
-	const PROPERTY_AVATAR = 'avatar';
50
-	const PROPERTY_DISPLAYNAME = 'displayname';
51
-	const PROPERTY_PHONE = 'phone';
52
-	const PROPERTY_EMAIL = 'email';
53
-	const PROPERTY_WEBSITE = 'website';
54
-	const PROPERTY_ADDRESS = 'address';
55
-	const PROPERTY_TWITTER = 'twitter';
56
-
57
-	const NOT_VERIFIED = '0';
58
-	const VERIFICATION_IN_PROGRESS = '1';
59
-	const VERIFIED = '2';
60
-
61
-	/** @var  IDBConnection database connection */
62
-	private $connection;
63
-
64
-	/** @var string table name */
65
-	private $table = 'accounts';
66
-
67
-	/** @var EventDispatcherInterface */
68
-	private $eventDispatcher;
69
-
70
-	/** @var IJobList */
71
-	private $jobList;
72
-
73
-	/**
74
-	 * AccountManager constructor.
75
-	 *
76
-	 * @param IDBConnection $connection
77
-	 * @param EventDispatcherInterface $eventDispatcher
78
-	 * @param IJobList $jobList
79
-	 */
80
-	public function __construct(IDBConnection $connection,
81
-								EventDispatcherInterface $eventDispatcher,
82
-								IJobList $jobList) {
83
-		$this->connection = $connection;
84
-		$this->eventDispatcher = $eventDispatcher;
85
-		$this->jobList = $jobList;
86
-	}
87
-
88
-	/**
89
-	 * update user record
90
-	 *
91
-	 * @param IUser $user
92
-	 * @param $data
93
-	 */
94
-	public function updateUser(IUser $user, $data) {
95
-		$userData = $this->getUser($user);
96
-		$updated = true;
97
-		if (empty($userData)) {
98
-			$this->insertNewUser($user, $data);
99
-		} elseif ($userData !== $data) {
100
-			$data = $this->checkEmailVerification($userData, $data, $user);
101
-			$data = $this->updateVerifyStatus($userData, $data);
102
-			$this->updateExistingUser($user, $data);
103
-		} else {
104
-			// nothing needs to be done if new and old data set are the same
105
-			$updated = false;
106
-		}
107
-
108
-		if ($updated) {
109
-			$this->eventDispatcher->dispatch(
110
-				'OC\AccountManager::userUpdated',
111
-				new GenericEvent($user, $data)
112
-			);
113
-		}
114
-	}
115
-
116
-	/**
117
-	 * get stored data from a given user
118
-	 *
119
-	 * @param IUser $user
120
-	 * @return array
121
-	 */
122
-	public function getUser(IUser $user) {
123
-		$uid = $user->getUID();
124
-		$query = $this->connection->getQueryBuilder();
125
-		$query->select('data')->from($this->table)
126
-			->where($query->expr()->eq('uid', $query->createParameter('uid')))
127
-			->setParameter('uid', $uid);
128
-		$query->execute();
129
-		$result = $query->execute()->fetchAll();
130
-
131
-		if (empty($result)) {
132
-			$userData = $this->buildDefaultUserRecord($user);
133
-			$this->insertNewUser($user, $userData);
134
-			return $userData;
135
-		}
136
-
137
-		$userDataArray = json_decode($result[0]['data'], true);
138
-
139
-		$userDataArray = $this->addMissingDefaultValues($userDataArray);
140
-
141
-		return $userDataArray;
142
-	}
143
-
144
-	/**
145
-	 * check if we need to ask the server for email verification, if yes we create a cronjob
146
-	 *
147
-	 * @param $oldData
148
-	 * @param $newData
149
-	 * @param IUser $user
150
-	 * @return array
151
-	 */
152
-	protected function checkEmailVerification($oldData, $newData, IUser $user) {
153
-		if ($oldData[self::PROPERTY_EMAIL]['value'] !== $newData[self::PROPERTY_EMAIL]['value']) {
154
-			$this->jobList->add('OC\Settings\BackgroundJobs\VerifyUserData',
155
-				[
156
-					'verificationCode' => '',
157
-					'data' => $newData[self::PROPERTY_EMAIL]['value'],
158
-					'type' => self::PROPERTY_EMAIL,
159
-					'uid' => $user->getUID(),
160
-					'try' => 0,
161
-					'lastRun' => time()
162
-				]
163
-			);
164
-			$newData[AccountManager::PROPERTY_EMAIL]['verified'] = AccountManager::VERIFICATION_IN_PROGRESS;
165
-		}
166
-
167
-		return $newData;
168
-	}
169
-
170
-	/**
171
-	 * make sure that all expected data are set
172
-	 *
173
-	 * @param array $userData
174
-	 * @return array
175
-	 */
176
-	protected function addMissingDefaultValues(array $userData) {
177
-
178
-		foreach ($userData as $key => $value) {
179
-			if (!isset($userData[$key]['verified'])) {
180
-				$userData[$key]['verified'] = self::NOT_VERIFIED;
181
-			}
182
-		}
183
-
184
-		return $userData;
185
-	}
186
-
187
-	/**
188
-	 * reset verification status if personal data changed
189
-	 *
190
-	 * @param array $oldData
191
-	 * @param array $newData
192
-	 * @return array
193
-	 */
194
-	protected function updateVerifyStatus($oldData, $newData) {
195
-
196
-		// which account was already verified successfully?
197
-		$twitterVerified = isset($oldData[self::PROPERTY_TWITTER]['verified']) && $oldData[self::PROPERTY_TWITTER]['verified'] === self::VERIFIED;
198
-		$websiteVerified = isset($oldData[self::PROPERTY_WEBSITE]['verified']) && $oldData[self::PROPERTY_WEBSITE]['verified'] === self::VERIFIED;
199
-		$emailVerified = isset($oldData[self::PROPERTY_EMAIL]['verified']) && $oldData[self::PROPERTY_EMAIL]['verified'] === self::VERIFIED;
200
-
201
-		// keep old verification status if we don't have a new one
202
-		if(!isset($newData[self::PROPERTY_TWITTER]['verified'])) {
203
-			// keep old verification status if value didn't changed and an old value exists
204
-			$keepOldStatus = $newData[self::PROPERTY_TWITTER]['value'] === $oldData[self::PROPERTY_TWITTER]['value'] && isset($oldData[self::PROPERTY_TWITTER]['verified']);
205
-			$newData[self::PROPERTY_TWITTER]['verified'] = $keepOldStatus ? $oldData[self::PROPERTY_TWITTER]['verified'] : self::NOT_VERIFIED;
206
-		}
207
-
208
-		if(!isset($newData[self::PROPERTY_WEBSITE]['verified'])) {
209
-			// keep old verification status if value didn't changed and an old value exists
210
-			$keepOldStatus = $newData[self::PROPERTY_WEBSITE]['value'] === $oldData[self::PROPERTY_WEBSITE]['value'] && isset($oldData[self::PROPERTY_WEBSITE]['verified']);
211
-			$newData[self::PROPERTY_WEBSITE]['verified'] = $keepOldStatus ? $oldData[self::PROPERTY_WEBSITE]['verified'] : self::NOT_VERIFIED;
212
-		}
213
-
214
-		if(!isset($newData[self::PROPERTY_EMAIL]['verified'])) {
215
-			// keep old verification status if value didn't changed and an old value exists
216
-			$keepOldStatus = $newData[self::PROPERTY_EMAIL]['value'] === $oldData[self::PROPERTY_EMAIL]['value'] && isset($oldData[self::PROPERTY_EMAIL]['verified']);
217
-			$newData[self::PROPERTY_EMAIL]['verified'] = $keepOldStatus ? $oldData[self::PROPERTY_EMAIL]['verified'] : self::VERIFICATION_IN_PROGRESS;
218
-		}
219
-
220
-		// reset verification status if a value from a previously verified data was changed
221
-		if($twitterVerified &&
222
-			$oldData[self::PROPERTY_TWITTER]['value'] !== $newData[self::PROPERTY_TWITTER]['value']
223
-		) {
224
-			$newData[self::PROPERTY_TWITTER]['verified'] = self::NOT_VERIFIED;
225
-		}
226
-
227
-		if($websiteVerified &&
228
-			$oldData[self::PROPERTY_WEBSITE]['value'] !== $newData[self::PROPERTY_WEBSITE]['value']
229
-		) {
230
-			$newData[self::PROPERTY_WEBSITE]['verified'] = self::NOT_VERIFIED;
231
-		}
232
-
233
-		if($emailVerified &&
234
-			$oldData[self::PROPERTY_EMAIL]['value'] !== $newData[self::PROPERTY_EMAIL]['value']
235
-		) {
236
-			$newData[self::PROPERTY_EMAIL]['verified'] = self::NOT_VERIFIED;
237
-		}
238
-
239
-		return $newData;
240
-
241
-	}
242
-
243
-	/**
244
-	 * add new user to accounts table
245
-	 *
246
-	 * @param IUser $user
247
-	 * @param array $data
248
-	 */
249
-	protected function insertNewUser(IUser $user, $data) {
250
-		$uid = $user->getUID();
251
-		$jsonEncodedData = json_encode($data);
252
-		$query = $this->connection->getQueryBuilder();
253
-		$query->insert($this->table)
254
-			->values(
255
-				[
256
-					'uid' => $query->createNamedParameter($uid),
257
-					'data' => $query->createNamedParameter($jsonEncodedData),
258
-				]
259
-			)
260
-			->execute();
261
-	}
262
-
263
-	/**
264
-	 * update existing user in accounts table
265
-	 *
266
-	 * @param IUser $user
267
-	 * @param array $data
268
-	 */
269
-	protected function updateExistingUser(IUser $user, $data) {
270
-		$uid = $user->getUID();
271
-		$jsonEncodedData = json_encode($data);
272
-		$query = $this->connection->getQueryBuilder();
273
-		$query->update($this->table)
274
-			->set('data', $query->createNamedParameter($jsonEncodedData))
275
-			->where($query->expr()->eq('uid', $query->createNamedParameter($uid)))
276
-			->execute();
277
-	}
278
-
279
-	/**
280
-	 * build default user record in case not data set exists yet
281
-	 *
282
-	 * @param IUser $user
283
-	 * @return array
284
-	 */
285
-	protected function buildDefaultUserRecord(IUser $user) {
286
-		return [
287
-			self::PROPERTY_DISPLAYNAME =>
288
-				[
289
-					'value' => $user->getDisplayName(),
290
-					'scope' => self::VISIBILITY_CONTACTS_ONLY,
291
-					'verified' => self::NOT_VERIFIED,
292
-				],
293
-			self::PROPERTY_ADDRESS =>
294
-				[
295
-					'value' => '',
296
-					'scope' => self::VISIBILITY_PRIVATE,
297
-					'verified' => self::NOT_VERIFIED,
298
-				],
299
-			self::PROPERTY_WEBSITE =>
300
-				[
301
-					'value' => '',
302
-					'scope' => self::VISIBILITY_PRIVATE,
303
-					'verified' => self::NOT_VERIFIED,
304
-				],
305
-			self::PROPERTY_EMAIL =>
306
-				[
307
-					'value' => $user->getEMailAddress(),
308
-					'scope' => self::VISIBILITY_CONTACTS_ONLY,
309
-					'verified' => self::NOT_VERIFIED,
310
-				],
311
-			self::PROPERTY_AVATAR =>
312
-				[
313
-					'scope' => self::VISIBILITY_CONTACTS_ONLY
314
-				],
315
-			self::PROPERTY_PHONE =>
316
-				[
317
-					'value' => '',
318
-					'scope' => self::VISIBILITY_PRIVATE,
319
-					'verified' => self::NOT_VERIFIED,
320
-				],
321
-			self::PROPERTY_TWITTER =>
322
-				[
323
-					'value' => '',
324
-					'scope' => self::VISIBILITY_PRIVATE,
325
-					'verified' => self::NOT_VERIFIED,
326
-				],
327
-		];
328
-	}
42
+    /** nobody can see my account details */
43
+    const VISIBILITY_PRIVATE = 'private';
44
+    /** only contacts, especially trusted servers can see my contact details */
45
+    const VISIBILITY_CONTACTS_ONLY = 'contacts';
46
+    /** every body ca see my contact detail, will be published to the lookup server */
47
+    const VISIBILITY_PUBLIC = 'public';
48
+
49
+    const PROPERTY_AVATAR = 'avatar';
50
+    const PROPERTY_DISPLAYNAME = 'displayname';
51
+    const PROPERTY_PHONE = 'phone';
52
+    const PROPERTY_EMAIL = 'email';
53
+    const PROPERTY_WEBSITE = 'website';
54
+    const PROPERTY_ADDRESS = 'address';
55
+    const PROPERTY_TWITTER = 'twitter';
56
+
57
+    const NOT_VERIFIED = '0';
58
+    const VERIFICATION_IN_PROGRESS = '1';
59
+    const VERIFIED = '2';
60
+
61
+    /** @var  IDBConnection database connection */
62
+    private $connection;
63
+
64
+    /** @var string table name */
65
+    private $table = 'accounts';
66
+
67
+    /** @var EventDispatcherInterface */
68
+    private $eventDispatcher;
69
+
70
+    /** @var IJobList */
71
+    private $jobList;
72
+
73
+    /**
74
+     * AccountManager constructor.
75
+     *
76
+     * @param IDBConnection $connection
77
+     * @param EventDispatcherInterface $eventDispatcher
78
+     * @param IJobList $jobList
79
+     */
80
+    public function __construct(IDBConnection $connection,
81
+                                EventDispatcherInterface $eventDispatcher,
82
+                                IJobList $jobList) {
83
+        $this->connection = $connection;
84
+        $this->eventDispatcher = $eventDispatcher;
85
+        $this->jobList = $jobList;
86
+    }
87
+
88
+    /**
89
+     * update user record
90
+     *
91
+     * @param IUser $user
92
+     * @param $data
93
+     */
94
+    public function updateUser(IUser $user, $data) {
95
+        $userData = $this->getUser($user);
96
+        $updated = true;
97
+        if (empty($userData)) {
98
+            $this->insertNewUser($user, $data);
99
+        } elseif ($userData !== $data) {
100
+            $data = $this->checkEmailVerification($userData, $data, $user);
101
+            $data = $this->updateVerifyStatus($userData, $data);
102
+            $this->updateExistingUser($user, $data);
103
+        } else {
104
+            // nothing needs to be done if new and old data set are the same
105
+            $updated = false;
106
+        }
107
+
108
+        if ($updated) {
109
+            $this->eventDispatcher->dispatch(
110
+                'OC\AccountManager::userUpdated',
111
+                new GenericEvent($user, $data)
112
+            );
113
+        }
114
+    }
115
+
116
+    /**
117
+     * get stored data from a given user
118
+     *
119
+     * @param IUser $user
120
+     * @return array
121
+     */
122
+    public function getUser(IUser $user) {
123
+        $uid = $user->getUID();
124
+        $query = $this->connection->getQueryBuilder();
125
+        $query->select('data')->from($this->table)
126
+            ->where($query->expr()->eq('uid', $query->createParameter('uid')))
127
+            ->setParameter('uid', $uid);
128
+        $query->execute();
129
+        $result = $query->execute()->fetchAll();
130
+
131
+        if (empty($result)) {
132
+            $userData = $this->buildDefaultUserRecord($user);
133
+            $this->insertNewUser($user, $userData);
134
+            return $userData;
135
+        }
136
+
137
+        $userDataArray = json_decode($result[0]['data'], true);
138
+
139
+        $userDataArray = $this->addMissingDefaultValues($userDataArray);
140
+
141
+        return $userDataArray;
142
+    }
143
+
144
+    /**
145
+     * check if we need to ask the server for email verification, if yes we create a cronjob
146
+     *
147
+     * @param $oldData
148
+     * @param $newData
149
+     * @param IUser $user
150
+     * @return array
151
+     */
152
+    protected function checkEmailVerification($oldData, $newData, IUser $user) {
153
+        if ($oldData[self::PROPERTY_EMAIL]['value'] !== $newData[self::PROPERTY_EMAIL]['value']) {
154
+            $this->jobList->add('OC\Settings\BackgroundJobs\VerifyUserData',
155
+                [
156
+                    'verificationCode' => '',
157
+                    'data' => $newData[self::PROPERTY_EMAIL]['value'],
158
+                    'type' => self::PROPERTY_EMAIL,
159
+                    'uid' => $user->getUID(),
160
+                    'try' => 0,
161
+                    'lastRun' => time()
162
+                ]
163
+            );
164
+            $newData[AccountManager::PROPERTY_EMAIL]['verified'] = AccountManager::VERIFICATION_IN_PROGRESS;
165
+        }
166
+
167
+        return $newData;
168
+    }
169
+
170
+    /**
171
+     * make sure that all expected data are set
172
+     *
173
+     * @param array $userData
174
+     * @return array
175
+     */
176
+    protected function addMissingDefaultValues(array $userData) {
177
+
178
+        foreach ($userData as $key => $value) {
179
+            if (!isset($userData[$key]['verified'])) {
180
+                $userData[$key]['verified'] = self::NOT_VERIFIED;
181
+            }
182
+        }
183
+
184
+        return $userData;
185
+    }
186
+
187
+    /**
188
+     * reset verification status if personal data changed
189
+     *
190
+     * @param array $oldData
191
+     * @param array $newData
192
+     * @return array
193
+     */
194
+    protected function updateVerifyStatus($oldData, $newData) {
195
+
196
+        // which account was already verified successfully?
197
+        $twitterVerified = isset($oldData[self::PROPERTY_TWITTER]['verified']) && $oldData[self::PROPERTY_TWITTER]['verified'] === self::VERIFIED;
198
+        $websiteVerified = isset($oldData[self::PROPERTY_WEBSITE]['verified']) && $oldData[self::PROPERTY_WEBSITE]['verified'] === self::VERIFIED;
199
+        $emailVerified = isset($oldData[self::PROPERTY_EMAIL]['verified']) && $oldData[self::PROPERTY_EMAIL]['verified'] === self::VERIFIED;
200
+
201
+        // keep old verification status if we don't have a new one
202
+        if(!isset($newData[self::PROPERTY_TWITTER]['verified'])) {
203
+            // keep old verification status if value didn't changed and an old value exists
204
+            $keepOldStatus = $newData[self::PROPERTY_TWITTER]['value'] === $oldData[self::PROPERTY_TWITTER]['value'] && isset($oldData[self::PROPERTY_TWITTER]['verified']);
205
+            $newData[self::PROPERTY_TWITTER]['verified'] = $keepOldStatus ? $oldData[self::PROPERTY_TWITTER]['verified'] : self::NOT_VERIFIED;
206
+        }
207
+
208
+        if(!isset($newData[self::PROPERTY_WEBSITE]['verified'])) {
209
+            // keep old verification status if value didn't changed and an old value exists
210
+            $keepOldStatus = $newData[self::PROPERTY_WEBSITE]['value'] === $oldData[self::PROPERTY_WEBSITE]['value'] && isset($oldData[self::PROPERTY_WEBSITE]['verified']);
211
+            $newData[self::PROPERTY_WEBSITE]['verified'] = $keepOldStatus ? $oldData[self::PROPERTY_WEBSITE]['verified'] : self::NOT_VERIFIED;
212
+        }
213
+
214
+        if(!isset($newData[self::PROPERTY_EMAIL]['verified'])) {
215
+            // keep old verification status if value didn't changed and an old value exists
216
+            $keepOldStatus = $newData[self::PROPERTY_EMAIL]['value'] === $oldData[self::PROPERTY_EMAIL]['value'] && isset($oldData[self::PROPERTY_EMAIL]['verified']);
217
+            $newData[self::PROPERTY_EMAIL]['verified'] = $keepOldStatus ? $oldData[self::PROPERTY_EMAIL]['verified'] : self::VERIFICATION_IN_PROGRESS;
218
+        }
219
+
220
+        // reset verification status if a value from a previously verified data was changed
221
+        if($twitterVerified &&
222
+            $oldData[self::PROPERTY_TWITTER]['value'] !== $newData[self::PROPERTY_TWITTER]['value']
223
+        ) {
224
+            $newData[self::PROPERTY_TWITTER]['verified'] = self::NOT_VERIFIED;
225
+        }
226
+
227
+        if($websiteVerified &&
228
+            $oldData[self::PROPERTY_WEBSITE]['value'] !== $newData[self::PROPERTY_WEBSITE]['value']
229
+        ) {
230
+            $newData[self::PROPERTY_WEBSITE]['verified'] = self::NOT_VERIFIED;
231
+        }
232
+
233
+        if($emailVerified &&
234
+            $oldData[self::PROPERTY_EMAIL]['value'] !== $newData[self::PROPERTY_EMAIL]['value']
235
+        ) {
236
+            $newData[self::PROPERTY_EMAIL]['verified'] = self::NOT_VERIFIED;
237
+        }
238
+
239
+        return $newData;
240
+
241
+    }
242
+
243
+    /**
244
+     * add new user to accounts table
245
+     *
246
+     * @param IUser $user
247
+     * @param array $data
248
+     */
249
+    protected function insertNewUser(IUser $user, $data) {
250
+        $uid = $user->getUID();
251
+        $jsonEncodedData = json_encode($data);
252
+        $query = $this->connection->getQueryBuilder();
253
+        $query->insert($this->table)
254
+            ->values(
255
+                [
256
+                    'uid' => $query->createNamedParameter($uid),
257
+                    'data' => $query->createNamedParameter($jsonEncodedData),
258
+                ]
259
+            )
260
+            ->execute();
261
+    }
262
+
263
+    /**
264
+     * update existing user in accounts table
265
+     *
266
+     * @param IUser $user
267
+     * @param array $data
268
+     */
269
+    protected function updateExistingUser(IUser $user, $data) {
270
+        $uid = $user->getUID();
271
+        $jsonEncodedData = json_encode($data);
272
+        $query = $this->connection->getQueryBuilder();
273
+        $query->update($this->table)
274
+            ->set('data', $query->createNamedParameter($jsonEncodedData))
275
+            ->where($query->expr()->eq('uid', $query->createNamedParameter($uid)))
276
+            ->execute();
277
+    }
278
+
279
+    /**
280
+     * build default user record in case not data set exists yet
281
+     *
282
+     * @param IUser $user
283
+     * @return array
284
+     */
285
+    protected function buildDefaultUserRecord(IUser $user) {
286
+        return [
287
+            self::PROPERTY_DISPLAYNAME =>
288
+                [
289
+                    'value' => $user->getDisplayName(),
290
+                    'scope' => self::VISIBILITY_CONTACTS_ONLY,
291
+                    'verified' => self::NOT_VERIFIED,
292
+                ],
293
+            self::PROPERTY_ADDRESS =>
294
+                [
295
+                    'value' => '',
296
+                    'scope' => self::VISIBILITY_PRIVATE,
297
+                    'verified' => self::NOT_VERIFIED,
298
+                ],
299
+            self::PROPERTY_WEBSITE =>
300
+                [
301
+                    'value' => '',
302
+                    'scope' => self::VISIBILITY_PRIVATE,
303
+                    'verified' => self::NOT_VERIFIED,
304
+                ],
305
+            self::PROPERTY_EMAIL =>
306
+                [
307
+                    'value' => $user->getEMailAddress(),
308
+                    'scope' => self::VISIBILITY_CONTACTS_ONLY,
309
+                    'verified' => self::NOT_VERIFIED,
310
+                ],
311
+            self::PROPERTY_AVATAR =>
312
+                [
313
+                    'scope' => self::VISIBILITY_CONTACTS_ONLY
314
+                ],
315
+            self::PROPERTY_PHONE =>
316
+                [
317
+                    'value' => '',
318
+                    'scope' => self::VISIBILITY_PRIVATE,
319
+                    'verified' => self::NOT_VERIFIED,
320
+                ],
321
+            self::PROPERTY_TWITTER =>
322
+                [
323
+                    'value' => '',
324
+                    'scope' => self::VISIBILITY_PRIVATE,
325
+                    'verified' => self::NOT_VERIFIED,
326
+                ],
327
+        ];
328
+    }
329 329
 
330 330
 }
Please login to merge, or discard this patch.