Passed
Push — master ( ccd89f...c92d35 )
by Morris
12:58
created
settings/Controller/UsersController.php 2 patches
Indentation   +372 added lines, -372 removed lines patch added patch discarded remove patch
@@ -65,407 +65,407 @@
 block discarded – undo
65 65
  * @package OC\Settings\Controller
66 66
  */
67 67
 class UsersController extends Controller {
68
-	/** @var IUserManager */
69
-	private $userManager;
70
-	/** @var IGroupManager */
71
-	private $groupManager;
72
-	/** @var IUserSession */
73
-	private $userSession;
74
-	/** @var IConfig */
75
-	private $config;
76
-	/** @var bool */
77
-	private $isAdmin;
78
-	/** @var IL10N */
79
-	private $l10n;
80
-	/** @var IMailer */
81
-	private $mailer;
82
-	/** @var IFactory */
83
-	private $l10nFactory;
84
-	/** @var IAppManager */
85
-	private $appManager;
86
-	/** @var AccountManager */
87
-	private $accountManager;
88
-	/** @var Manager */
89
-	private $keyManager;
90
-	/** @var IJobList */
91
-	private $jobList;
92
-	/** @var IManager */
93
-	private $encryptionManager;
68
+    /** @var IUserManager */
69
+    private $userManager;
70
+    /** @var IGroupManager */
71
+    private $groupManager;
72
+    /** @var IUserSession */
73
+    private $userSession;
74
+    /** @var IConfig */
75
+    private $config;
76
+    /** @var bool */
77
+    private $isAdmin;
78
+    /** @var IL10N */
79
+    private $l10n;
80
+    /** @var IMailer */
81
+    private $mailer;
82
+    /** @var IFactory */
83
+    private $l10nFactory;
84
+    /** @var IAppManager */
85
+    private $appManager;
86
+    /** @var AccountManager */
87
+    private $accountManager;
88
+    /** @var Manager */
89
+    private $keyManager;
90
+    /** @var IJobList */
91
+    private $jobList;
92
+    /** @var IManager */
93
+    private $encryptionManager;
94 94
 
95 95
 
96
-	public function __construct(string $appName,
97
-								IRequest $request,
98
-								IUserManager $userManager,
99
-								IGroupManager $groupManager,
100
-								IUserSession $userSession,
101
-								IConfig $config,
102
-								bool $isAdmin,
103
-								IL10N $l10n,
104
-								IMailer $mailer,
105
-								IFactory $l10nFactory,
106
-								IAppManager $appManager,
107
-								AccountManager $accountManager,
108
-								Manager $keyManager,
109
-								IJobList $jobList,
110
-								IManager $encryptionManager) {
111
-		parent::__construct($appName, $request);
112
-		$this->userManager = $userManager;
113
-		$this->groupManager = $groupManager;
114
-		$this->userSession = $userSession;
115
-		$this->config = $config;
116
-		$this->isAdmin = $isAdmin;
117
-		$this->l10n = $l10n;
118
-		$this->mailer = $mailer;
119
-		$this->l10nFactory = $l10nFactory;
120
-		$this->appManager = $appManager;
121
-		$this->accountManager = $accountManager;
122
-		$this->keyManager = $keyManager;
123
-		$this->jobList = $jobList;
124
-		$this->encryptionManager = $encryptionManager;
125
-	}
96
+    public function __construct(string $appName,
97
+                                IRequest $request,
98
+                                IUserManager $userManager,
99
+                                IGroupManager $groupManager,
100
+                                IUserSession $userSession,
101
+                                IConfig $config,
102
+                                bool $isAdmin,
103
+                                IL10N $l10n,
104
+                                IMailer $mailer,
105
+                                IFactory $l10nFactory,
106
+                                IAppManager $appManager,
107
+                                AccountManager $accountManager,
108
+                                Manager $keyManager,
109
+                                IJobList $jobList,
110
+                                IManager $encryptionManager) {
111
+        parent::__construct($appName, $request);
112
+        $this->userManager = $userManager;
113
+        $this->groupManager = $groupManager;
114
+        $this->userSession = $userSession;
115
+        $this->config = $config;
116
+        $this->isAdmin = $isAdmin;
117
+        $this->l10n = $l10n;
118
+        $this->mailer = $mailer;
119
+        $this->l10nFactory = $l10nFactory;
120
+        $this->appManager = $appManager;
121
+        $this->accountManager = $accountManager;
122
+        $this->keyManager = $keyManager;
123
+        $this->jobList = $jobList;
124
+        $this->encryptionManager = $encryptionManager;
125
+    }
126 126
 
127 127
 
128
-	/**
129
-	 * @NoCSRFRequired
130
-	 * @NoAdminRequired
131
-	 * 
132
-	 * Display users list template
133
-	 * 
134
-	 * @return TemplateResponse
135
-	 */
136
-	public function usersListByGroup() {
137
-		return $this->usersList();
138
-	}
128
+    /**
129
+     * @NoCSRFRequired
130
+     * @NoAdminRequired
131
+     * 
132
+     * Display users list template
133
+     * 
134
+     * @return TemplateResponse
135
+     */
136
+    public function usersListByGroup() {
137
+        return $this->usersList();
138
+    }
139 139
 
140
-	/**
141
-	 * @NoCSRFRequired
142
-	 * @NoAdminRequired
143
-	 * 
144
-	 * Display users list template
145
-	 * 
146
-	 * @return TemplateResponse
147
-	 */
148
-	public function usersList() {
149
-		$user = $this->userSession->getUser();
150
-		$uid = $user->getUID();
140
+    /**
141
+     * @NoCSRFRequired
142
+     * @NoAdminRequired
143
+     * 
144
+     * Display users list template
145
+     * 
146
+     * @return TemplateResponse
147
+     */
148
+    public function usersList() {
149
+        $user = $this->userSession->getUser();
150
+        $uid = $user->getUID();
151 151
 
152
-		\OC::$server->getNavigationManager()->setActiveEntry('core_users');
152
+        \OC::$server->getNavigationManager()->setActiveEntry('core_users');
153 153
 				
154
-		/* SORT OPTION: SORT_USERCOUNT or SORT_GROUPNAME */
155
-		$sortGroupsBy = \OC\Group\MetaData::SORT_USERCOUNT;
156
-		$isLDAPUsed = false;
157
-		if ($this->config->getSystemValue('sort_groups_by_name', false)) {
158
-			$sortGroupsBy = \OC\Group\MetaData::SORT_GROUPNAME;
159
-		} else {
160
-			if ($this->appManager->isEnabledForUser('user_ldap')) {
161
-				$isLDAPUsed =
162
-					$this->groupManager->isBackendUsed('\OCA\User_LDAP\Group_Proxy');
163
-				if ($isLDAPUsed) {
164
-					// LDAP user count can be slow, so we sort by group name here
165
-					$sortGroupsBy = \OC\Group\MetaData::SORT_GROUPNAME;
166
-				}
167
-			}
168
-		}
154
+        /* SORT OPTION: SORT_USERCOUNT or SORT_GROUPNAME */
155
+        $sortGroupsBy = \OC\Group\MetaData::SORT_USERCOUNT;
156
+        $isLDAPUsed = false;
157
+        if ($this->config->getSystemValue('sort_groups_by_name', false)) {
158
+            $sortGroupsBy = \OC\Group\MetaData::SORT_GROUPNAME;
159
+        } else {
160
+            if ($this->appManager->isEnabledForUser('user_ldap')) {
161
+                $isLDAPUsed =
162
+                    $this->groupManager->isBackendUsed('\OCA\User_LDAP\Group_Proxy');
163
+                if ($isLDAPUsed) {
164
+                    // LDAP user count can be slow, so we sort by group name here
165
+                    $sortGroupsBy = \OC\Group\MetaData::SORT_GROUPNAME;
166
+                }
167
+            }
168
+        }
169 169
 		
170
-		/* ENCRYPTION CONFIG */
171
-		$isEncryptionEnabled = $this->encryptionManager->isEnabled();
172
-		$useMasterKey = $this->config->getAppValue('encryption', 'useMasterKey', true);
173
-		// If masterKey enabled, then you can change password. This is to avoid data loss!
174
-		$canChangePassword = ($isEncryptionEnabled && $useMasterKey) || $useMasterKey;
170
+        /* ENCRYPTION CONFIG */
171
+        $isEncryptionEnabled = $this->encryptionManager->isEnabled();
172
+        $useMasterKey = $this->config->getAppValue('encryption', 'useMasterKey', true);
173
+        // If masterKey enabled, then you can change password. This is to avoid data loss!
174
+        $canChangePassword = ($isEncryptionEnabled && $useMasterKey) || $useMasterKey;
175 175
 		
176 176
 		
177
-		/* GROUPS */		
178
-		$groupsInfo = new \OC\Group\MetaData(
179
-			$uid,
180
-			$this->isAdmin,
181
-			$this->groupManager,
182
-			$this->userSession
183
-		);
177
+        /* GROUPS */		
178
+        $groupsInfo = new \OC\Group\MetaData(
179
+            $uid,
180
+            $this->isAdmin,
181
+            $this->groupManager,
182
+            $this->userSession
183
+        );
184 184
 		
185
-		$groupsInfo->setSorting($sortGroupsBy);
186
-		list($adminGroup, $groups) = $groupsInfo->get();
185
+        $groupsInfo->setSorting($sortGroupsBy);
186
+        list($adminGroup, $groups) = $groupsInfo->get();
187 187
 
188
-		if(!$isLDAPUsed && $this->appManager->isEnabledForUser('user_ldap')) {
189
-			$isLDAPUsed = (bool)array_reduce($this->userManager->getBackends(), function ($ldapFound, $backend) {
190
-				return $ldapFound || $backend instanceof User_Proxy;
191
-			});
192
-		}
188
+        if(!$isLDAPUsed && $this->appManager->isEnabledForUser('user_ldap')) {
189
+            $isLDAPUsed = (bool)array_reduce($this->userManager->getBackends(), function ($ldapFound, $backend) {
190
+                return $ldapFound || $backend instanceof User_Proxy;
191
+            });
192
+        }
193 193
 		
194
-		if ($this->isAdmin) {
195
-			$disabledUsers = $isLDAPUsed ? -1 : $this->userManager->countDisabledUsers();
196
-			$userCount = $isLDAPUsed ? 0 : array_reduce($this->userManager->countUsers(), function($v, $w) {
197
-				return $v + (int)$w;
198
-			}, 0);
199
-		} else {
200
-			// User is subadmin !
201
-			// Map group list to names to retrieve the countDisabledUsersOfGroups
202
-			$userGroups = $this->groupManager->getUserGroups($user);
203
-			$groupsNames = [];
204
-			$userCount = 0;
194
+        if ($this->isAdmin) {
195
+            $disabledUsers = $isLDAPUsed ? -1 : $this->userManager->countDisabledUsers();
196
+            $userCount = $isLDAPUsed ? 0 : array_reduce($this->userManager->countUsers(), function($v, $w) {
197
+                return $v + (int)$w;
198
+            }, 0);
199
+        } else {
200
+            // User is subadmin !
201
+            // Map group list to names to retrieve the countDisabledUsersOfGroups
202
+            $userGroups = $this->groupManager->getUserGroups($user);
203
+            $groupsNames = [];
204
+            $userCount = 0;
205 205
 
206
-			foreach($groups as $key => $group) {
207
-				// $userCount += (int)$group['usercount'];
208
-				array_push($groupsNames, $group['name']);
209
-				// we prevent subadmins from looking up themselves
210
-				// so we lower the count of the groups he belongs to
211
-				if (array_key_exists($group['id'], $userGroups)) {
212
-					$groups[$key]['usercount']--;
213
-					$userCount = -1; // we also lower from one the total count
214
-				}
215
-			};
216
-			$userCount += $isLDAPUsed ? 0 : $this->userManager->countUsersOfGroups($groupsInfo->getGroups());
217
-			$disabledUsers = $isLDAPUsed ? -1 : $this->userManager->countDisabledUsersOfGroups($groupsNames);
218
-		}
219
-		$disabledUsersGroup = [
220
-			'id' => 'disabled',
221
-			'name' => 'Disabled users',
222
-			'usercount' => $disabledUsers
223
-		];
206
+            foreach($groups as $key => $group) {
207
+                // $userCount += (int)$group['usercount'];
208
+                array_push($groupsNames, $group['name']);
209
+                // we prevent subadmins from looking up themselves
210
+                // so we lower the count of the groups he belongs to
211
+                if (array_key_exists($group['id'], $userGroups)) {
212
+                    $groups[$key]['usercount']--;
213
+                    $userCount = -1; // we also lower from one the total count
214
+                }
215
+            };
216
+            $userCount += $isLDAPUsed ? 0 : $this->userManager->countUsersOfGroups($groupsInfo->getGroups());
217
+            $disabledUsers = $isLDAPUsed ? -1 : $this->userManager->countDisabledUsersOfGroups($groupsNames);
218
+        }
219
+        $disabledUsersGroup = [
220
+            'id' => 'disabled',
221
+            'name' => 'Disabled users',
222
+            'usercount' => $disabledUsers
223
+        ];
224 224
 		
225
-		/* QUOTAS PRESETS */
226
-		$quotaPreset = $this->config->getAppValue('files', 'quota_preset', '1 GB, 5 GB, 10 GB');
227
-		$quotaPreset = explode(',', $quotaPreset);
228
-		foreach ($quotaPreset as &$preset) {
229
-			$preset = trim($preset);
230
-		}
231
-		$quotaPreset = array_diff($quotaPreset, array('default', 'none'));
232
-		$defaultQuota = $this->config->getAppValue('files', 'default_quota', 'none');
225
+        /* QUOTAS PRESETS */
226
+        $quotaPreset = $this->config->getAppValue('files', 'quota_preset', '1 GB, 5 GB, 10 GB');
227
+        $quotaPreset = explode(',', $quotaPreset);
228
+        foreach ($quotaPreset as &$preset) {
229
+            $preset = trim($preset);
230
+        }
231
+        $quotaPreset = array_diff($quotaPreset, array('default', 'none'));
232
+        $defaultQuota = $this->config->getAppValue('files', 'default_quota', 'none');
233 233
 		
234
-		\OC::$server->getEventDispatcher()->dispatch('OC\Settings\Users::loadAdditionalScripts');
234
+        \OC::$server->getEventDispatcher()->dispatch('OC\Settings\Users::loadAdditionalScripts');
235 235
 		
236
-		/* LANGUAGES */
237
-		$languages = $this->l10nFactory->getLanguages();
236
+        /* LANGUAGES */
237
+        $languages = $this->l10nFactory->getLanguages();
238 238
 		
239
-		/* FINAL DATA */
240
-		$serverData = array();
241
-		// groups
242
-		$serverData['groups'] = array_merge_recursive($adminGroup, [$disabledUsersGroup], $groups);
243
-		// Various data
244
-		$serverData['isAdmin'] = $this->isAdmin;
245
-		$serverData['sortGroups'] = $sortGroupsBy;
246
-		$serverData['quotaPreset'] = $quotaPreset;
247
-		$serverData['userCount'] = $userCount - $disabledUsers;
248
-		$serverData['languages'] = $languages;
249
-		$serverData['defaultLanguage'] = $this->config->getSystemValue('default_language', 'en');
250
-		// Settings
251
-		$serverData['defaultQuota'] = $defaultQuota;
252
-		$serverData['canChangePassword'] = $canChangePassword;
239
+        /* FINAL DATA */
240
+        $serverData = array();
241
+        // groups
242
+        $serverData['groups'] = array_merge_recursive($adminGroup, [$disabledUsersGroup], $groups);
243
+        // Various data
244
+        $serverData['isAdmin'] = $this->isAdmin;
245
+        $serverData['sortGroups'] = $sortGroupsBy;
246
+        $serverData['quotaPreset'] = $quotaPreset;
247
+        $serverData['userCount'] = $userCount - $disabledUsers;
248
+        $serverData['languages'] = $languages;
249
+        $serverData['defaultLanguage'] = $this->config->getSystemValue('default_language', 'en');
250
+        // Settings
251
+        $serverData['defaultQuota'] = $defaultQuota;
252
+        $serverData['canChangePassword'] = $canChangePassword;
253 253
 
254
-		return new TemplateResponse('settings', 'settings-vue', ['serverData' => $serverData]);
255
-	}
254
+        return new TemplateResponse('settings', 'settings-vue', ['serverData' => $serverData]);
255
+    }
256 256
 
257
-	/**
258
-	 * @NoAdminRequired
259
-	 * @NoSubadminRequired
260
-	 * @PasswordConfirmationRequired
261
-	 *
262
-	 * @param string $avatarScope
263
-	 * @param string $displayname
264
-	 * @param string $displaynameScope
265
-	 * @param string $phone
266
-	 * @param string $phoneScope
267
-	 * @param string $email
268
-	 * @param string $emailScope
269
-	 * @param string $website
270
-	 * @param string $websiteScope
271
-	 * @param string $address
272
-	 * @param string $addressScope
273
-	 * @param string $twitter
274
-	 * @param string $twitterScope
275
-	 * @return DataResponse
276
-	 */
277
-	public function setUserSettings($avatarScope,
278
-									$displayname,
279
-									$displaynameScope,
280
-									$phone,
281
-									$phoneScope,
282
-									$email,
283
-									$emailScope,
284
-									$website,
285
-									$websiteScope,
286
-									$address,
287
-									$addressScope,
288
-									$twitter,
289
-									$twitterScope
290
-	) {
291
-		if (!empty($email) && !$this->mailer->validateMailAddress($email)) {
292
-			return new DataResponse(
293
-				[
294
-					'status' => 'error',
295
-					'data' => [
296
-						'message' => $this->l10n->t('Invalid mail address')
297
-					]
298
-				],
299
-				Http::STATUS_UNPROCESSABLE_ENTITY
300
-			);
301
-		}
302
-		$user = $this->userSession->getUser();
303
-		$data = $this->accountManager->getUser($user);
304
-		$data[AccountManager::PROPERTY_AVATAR] = ['scope' => $avatarScope];
305
-		if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) {
306
-			$data[AccountManager::PROPERTY_DISPLAYNAME] = ['value' => $displayname, 'scope' => $displaynameScope];
307
-			$data[AccountManager::PROPERTY_EMAIL] = ['value' => $email, 'scope' => $emailScope];
308
-		}
309
-		if ($this->appManager->isEnabledForUser('federatedfilesharing')) {
310
-			$federatedFileSharing = new \OCA\FederatedFileSharing\AppInfo\Application();
311
-			$shareProvider = $federatedFileSharing->getFederatedShareProvider();
312
-			if ($shareProvider->isLookupServerUploadEnabled()) {
313
-				$data[AccountManager::PROPERTY_WEBSITE] = ['value' => $website, 'scope' => $websiteScope];
314
-				$data[AccountManager::PROPERTY_ADDRESS] = ['value' => $address, 'scope' => $addressScope];
315
-				$data[AccountManager::PROPERTY_PHONE] = ['value' => $phone, 'scope' => $phoneScope];
316
-				$data[AccountManager::PROPERTY_TWITTER] = ['value' => $twitter, 'scope' => $twitterScope];
317
-			}
318
-		}
319
-		try {
320
-			$this->saveUserSettings($user, $data);
321
-			return new DataResponse(
322
-				[
323
-					'status' => 'success',
324
-					'data' => [
325
-						'userId' => $user->getUID(),
326
-						'avatarScope' => $data[AccountManager::PROPERTY_AVATAR]['scope'],
327
-						'displayname' => $data[AccountManager::PROPERTY_DISPLAYNAME]['value'],
328
-						'displaynameScope' => $data[AccountManager::PROPERTY_DISPLAYNAME]['scope'],
329
-						'email' => $data[AccountManager::PROPERTY_EMAIL]['value'],
330
-						'emailScope' => $data[AccountManager::PROPERTY_EMAIL]['scope'],
331
-						'website' => $data[AccountManager::PROPERTY_WEBSITE]['value'],
332
-						'websiteScope' => $data[AccountManager::PROPERTY_WEBSITE]['scope'],
333
-						'address' => $data[AccountManager::PROPERTY_ADDRESS]['value'],
334
-						'addressScope' => $data[AccountManager::PROPERTY_ADDRESS]['scope'],
335
-						'message' => $this->l10n->t('Settings saved')
336
-					]
337
-				],
338
-				Http::STATUS_OK
339
-			);
340
-		} catch (ForbiddenException $e) {
341
-			return new DataResponse([
342
-				'status' => 'error',
343
-				'data' => [
344
-					'message' => $e->getMessage()
345
-				],
346
-			]);
347
-		}
348
-	}
349
-	/**
350
-	 * update account manager with new user data
351
-	 *
352
-	 * @param IUser $user
353
-	 * @param array $data
354
-	 * @throws ForbiddenException
355
-	 */
356
-	protected function saveUserSettings(IUser $user, array $data) {
357
-		// keep the user back-end up-to-date with the latest display name and email
358
-		// address
359
-		$oldDisplayName = $user->getDisplayName();
360
-		$oldDisplayName = is_null($oldDisplayName) ? '' : $oldDisplayName;
361
-		if (isset($data[AccountManager::PROPERTY_DISPLAYNAME]['value'])
362
-			&& $oldDisplayName !== $data[AccountManager::PROPERTY_DISPLAYNAME]['value']
363
-		) {
364
-			$result = $user->setDisplayName($data[AccountManager::PROPERTY_DISPLAYNAME]['value']);
365
-			if ($result === false) {
366
-				throw new ForbiddenException($this->l10n->t('Unable to change full name'));
367
-			}
368
-		}
369
-		$oldEmailAddress = $user->getEMailAddress();
370
-		$oldEmailAddress = is_null($oldEmailAddress) ? '' : $oldEmailAddress;
371
-		if (isset($data[AccountManager::PROPERTY_EMAIL]['value'])
372
-			&& $oldEmailAddress !== $data[AccountManager::PROPERTY_EMAIL]['value']
373
-		) {
374
-			// this is the only permission a backend provides and is also used
375
-			// for the permission of setting a email address
376
-			if (!$user->canChangeDisplayName()) {
377
-				throw new ForbiddenException($this->l10n->t('Unable to change email address'));
378
-			}
379
-			$user->setEMailAddress($data[AccountManager::PROPERTY_EMAIL]['value']);
380
-		}
381
-		$this->accountManager->updateUser($user, $data);
382
-	}
257
+    /**
258
+     * @NoAdminRequired
259
+     * @NoSubadminRequired
260
+     * @PasswordConfirmationRequired
261
+     *
262
+     * @param string $avatarScope
263
+     * @param string $displayname
264
+     * @param string $displaynameScope
265
+     * @param string $phone
266
+     * @param string $phoneScope
267
+     * @param string $email
268
+     * @param string $emailScope
269
+     * @param string $website
270
+     * @param string $websiteScope
271
+     * @param string $address
272
+     * @param string $addressScope
273
+     * @param string $twitter
274
+     * @param string $twitterScope
275
+     * @return DataResponse
276
+     */
277
+    public function setUserSettings($avatarScope,
278
+                                    $displayname,
279
+                                    $displaynameScope,
280
+                                    $phone,
281
+                                    $phoneScope,
282
+                                    $email,
283
+                                    $emailScope,
284
+                                    $website,
285
+                                    $websiteScope,
286
+                                    $address,
287
+                                    $addressScope,
288
+                                    $twitter,
289
+                                    $twitterScope
290
+    ) {
291
+        if (!empty($email) && !$this->mailer->validateMailAddress($email)) {
292
+            return new DataResponse(
293
+                [
294
+                    'status' => 'error',
295
+                    'data' => [
296
+                        'message' => $this->l10n->t('Invalid mail address')
297
+                    ]
298
+                ],
299
+                Http::STATUS_UNPROCESSABLE_ENTITY
300
+            );
301
+        }
302
+        $user = $this->userSession->getUser();
303
+        $data = $this->accountManager->getUser($user);
304
+        $data[AccountManager::PROPERTY_AVATAR] = ['scope' => $avatarScope];
305
+        if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) {
306
+            $data[AccountManager::PROPERTY_DISPLAYNAME] = ['value' => $displayname, 'scope' => $displaynameScope];
307
+            $data[AccountManager::PROPERTY_EMAIL] = ['value' => $email, 'scope' => $emailScope];
308
+        }
309
+        if ($this->appManager->isEnabledForUser('federatedfilesharing')) {
310
+            $federatedFileSharing = new \OCA\FederatedFileSharing\AppInfo\Application();
311
+            $shareProvider = $federatedFileSharing->getFederatedShareProvider();
312
+            if ($shareProvider->isLookupServerUploadEnabled()) {
313
+                $data[AccountManager::PROPERTY_WEBSITE] = ['value' => $website, 'scope' => $websiteScope];
314
+                $data[AccountManager::PROPERTY_ADDRESS] = ['value' => $address, 'scope' => $addressScope];
315
+                $data[AccountManager::PROPERTY_PHONE] = ['value' => $phone, 'scope' => $phoneScope];
316
+                $data[AccountManager::PROPERTY_TWITTER] = ['value' => $twitter, 'scope' => $twitterScope];
317
+            }
318
+        }
319
+        try {
320
+            $this->saveUserSettings($user, $data);
321
+            return new DataResponse(
322
+                [
323
+                    'status' => 'success',
324
+                    'data' => [
325
+                        'userId' => $user->getUID(),
326
+                        'avatarScope' => $data[AccountManager::PROPERTY_AVATAR]['scope'],
327
+                        'displayname' => $data[AccountManager::PROPERTY_DISPLAYNAME]['value'],
328
+                        'displaynameScope' => $data[AccountManager::PROPERTY_DISPLAYNAME]['scope'],
329
+                        'email' => $data[AccountManager::PROPERTY_EMAIL]['value'],
330
+                        'emailScope' => $data[AccountManager::PROPERTY_EMAIL]['scope'],
331
+                        'website' => $data[AccountManager::PROPERTY_WEBSITE]['value'],
332
+                        'websiteScope' => $data[AccountManager::PROPERTY_WEBSITE]['scope'],
333
+                        'address' => $data[AccountManager::PROPERTY_ADDRESS]['value'],
334
+                        'addressScope' => $data[AccountManager::PROPERTY_ADDRESS]['scope'],
335
+                        'message' => $this->l10n->t('Settings saved')
336
+                    ]
337
+                ],
338
+                Http::STATUS_OK
339
+            );
340
+        } catch (ForbiddenException $e) {
341
+            return new DataResponse([
342
+                'status' => 'error',
343
+                'data' => [
344
+                    'message' => $e->getMessage()
345
+                ],
346
+            ]);
347
+        }
348
+    }
349
+    /**
350
+     * update account manager with new user data
351
+     *
352
+     * @param IUser $user
353
+     * @param array $data
354
+     * @throws ForbiddenException
355
+     */
356
+    protected function saveUserSettings(IUser $user, array $data) {
357
+        // keep the user back-end up-to-date with the latest display name and email
358
+        // address
359
+        $oldDisplayName = $user->getDisplayName();
360
+        $oldDisplayName = is_null($oldDisplayName) ? '' : $oldDisplayName;
361
+        if (isset($data[AccountManager::PROPERTY_DISPLAYNAME]['value'])
362
+            && $oldDisplayName !== $data[AccountManager::PROPERTY_DISPLAYNAME]['value']
363
+        ) {
364
+            $result = $user->setDisplayName($data[AccountManager::PROPERTY_DISPLAYNAME]['value']);
365
+            if ($result === false) {
366
+                throw new ForbiddenException($this->l10n->t('Unable to change full name'));
367
+            }
368
+        }
369
+        $oldEmailAddress = $user->getEMailAddress();
370
+        $oldEmailAddress = is_null($oldEmailAddress) ? '' : $oldEmailAddress;
371
+        if (isset($data[AccountManager::PROPERTY_EMAIL]['value'])
372
+            && $oldEmailAddress !== $data[AccountManager::PROPERTY_EMAIL]['value']
373
+        ) {
374
+            // this is the only permission a backend provides and is also used
375
+            // for the permission of setting a email address
376
+            if (!$user->canChangeDisplayName()) {
377
+                throw new ForbiddenException($this->l10n->t('Unable to change email address'));
378
+            }
379
+            $user->setEMailAddress($data[AccountManager::PROPERTY_EMAIL]['value']);
380
+        }
381
+        $this->accountManager->updateUser($user, $data);
382
+    }
383 383
 
384
-	/**
385
-	 * Set the mail address of a user
386
-	 *
387
-	 * @NoAdminRequired
388
-	 * @NoSubadminRequired
389
-	 * @PasswordConfirmationRequired
390
-	 *
391
-	 * @param string $account
392
-	 * @param bool $onlyVerificationCode only return verification code without updating the data
393
-	 * @return DataResponse
394
-	 */
395
-	public function getVerificationCode(string $account, bool $onlyVerificationCode): DataResponse {
384
+    /**
385
+     * Set the mail address of a user
386
+     *
387
+     * @NoAdminRequired
388
+     * @NoSubadminRequired
389
+     * @PasswordConfirmationRequired
390
+     *
391
+     * @param string $account
392
+     * @param bool $onlyVerificationCode only return verification code without updating the data
393
+     * @return DataResponse
394
+     */
395
+    public function getVerificationCode(string $account, bool $onlyVerificationCode): DataResponse {
396 396
 
397
-		$user = $this->userSession->getUser();
397
+        $user = $this->userSession->getUser();
398 398
 
399
-		if ($user === null) {
400
-			return new DataResponse([], Http::STATUS_BAD_REQUEST);
401
-		}
399
+        if ($user === null) {
400
+            return new DataResponse([], Http::STATUS_BAD_REQUEST);
401
+        }
402 402
 
403
-		$accountData = $this->accountManager->getUser($user);
404
-		$cloudId = $user->getCloudId();
405
-		$message = 'Use my Federated Cloud ID to share with me: ' . $cloudId;
406
-		$signature = $this->signMessage($user, $message);
403
+        $accountData = $this->accountManager->getUser($user);
404
+        $cloudId = $user->getCloudId();
405
+        $message = 'Use my Federated Cloud ID to share with me: ' . $cloudId;
406
+        $signature = $this->signMessage($user, $message);
407 407
 
408
-		$code = $message . ' ' . $signature;
409
-		$codeMd5 = $message . ' ' . md5($signature);
408
+        $code = $message . ' ' . $signature;
409
+        $codeMd5 = $message . ' ' . md5($signature);
410 410
 
411
-		switch ($account) {
412
-			case 'verify-twitter':
413
-				$accountData[AccountManager::PROPERTY_TWITTER]['verified'] = AccountManager::VERIFICATION_IN_PROGRESS;
414
-				$msg = $this->l10n->t('In order to verify your Twitter account, post the following tweet on Twitter (please make sure to post it without any line breaks):');
415
-				$code = $codeMd5;
416
-				$type = AccountManager::PROPERTY_TWITTER;
417
-				$data = $accountData[AccountManager::PROPERTY_TWITTER]['value'];
418
-				$accountData[AccountManager::PROPERTY_TWITTER]['signature'] = $signature;
419
-				break;
420
-			case 'verify-website':
421
-				$accountData[AccountManager::PROPERTY_WEBSITE]['verified'] = AccountManager::VERIFICATION_IN_PROGRESS;
422
-				$msg = $this->l10n->t('In order to verify your Website, store the following content in your web-root at \'.well-known/CloudIdVerificationCode.txt\' (please make sure that the complete text is in one line):');
423
-				$type = AccountManager::PROPERTY_WEBSITE;
424
-				$data = $accountData[AccountManager::PROPERTY_WEBSITE]['value'];
425
-				$accountData[AccountManager::PROPERTY_WEBSITE]['signature'] = $signature;
426
-				break;
427
-			default:
428
-				return new DataResponse([], Http::STATUS_BAD_REQUEST);
429
-		}
411
+        switch ($account) {
412
+            case 'verify-twitter':
413
+                $accountData[AccountManager::PROPERTY_TWITTER]['verified'] = AccountManager::VERIFICATION_IN_PROGRESS;
414
+                $msg = $this->l10n->t('In order to verify your Twitter account, post the following tweet on Twitter (please make sure to post it without any line breaks):');
415
+                $code = $codeMd5;
416
+                $type = AccountManager::PROPERTY_TWITTER;
417
+                $data = $accountData[AccountManager::PROPERTY_TWITTER]['value'];
418
+                $accountData[AccountManager::PROPERTY_TWITTER]['signature'] = $signature;
419
+                break;
420
+            case 'verify-website':
421
+                $accountData[AccountManager::PROPERTY_WEBSITE]['verified'] = AccountManager::VERIFICATION_IN_PROGRESS;
422
+                $msg = $this->l10n->t('In order to verify your Website, store the following content in your web-root at \'.well-known/CloudIdVerificationCode.txt\' (please make sure that the complete text is in one line):');
423
+                $type = AccountManager::PROPERTY_WEBSITE;
424
+                $data = $accountData[AccountManager::PROPERTY_WEBSITE]['value'];
425
+                $accountData[AccountManager::PROPERTY_WEBSITE]['signature'] = $signature;
426
+                break;
427
+            default:
428
+                return new DataResponse([], Http::STATUS_BAD_REQUEST);
429
+        }
430 430
 
431
-		if ($onlyVerificationCode === false) {
432
-			$this->accountManager->updateUser($user, $accountData);
431
+        if ($onlyVerificationCode === false) {
432
+            $this->accountManager->updateUser($user, $accountData);
433 433
 
434
-			$this->jobList->add(VerifyUserData::class,
435
-				[
436
-					'verificationCode' => $code,
437
-					'data' => $data,
438
-					'type' => $type,
439
-					'uid' => $user->getUID(),
440
-					'try' => 0,
441
-					'lastRun' => $this->getCurrentTime()
442
-				]
443
-			);
444
-		}
434
+            $this->jobList->add(VerifyUserData::class,
435
+                [
436
+                    'verificationCode' => $code,
437
+                    'data' => $data,
438
+                    'type' => $type,
439
+                    'uid' => $user->getUID(),
440
+                    'try' => 0,
441
+                    'lastRun' => $this->getCurrentTime()
442
+                ]
443
+            );
444
+        }
445 445
 
446
-		return new DataResponse(['msg' => $msg, 'code' => $code]);
447
-	}
446
+        return new DataResponse(['msg' => $msg, 'code' => $code]);
447
+    }
448 448
 
449
-	/**
450
-	 * get current timestamp
451
-	 *
452
-	 * @return int
453
-	 */
454
-	protected function getCurrentTime(): int {
455
-		return time();
456
-	}
449
+    /**
450
+     * get current timestamp
451
+     *
452
+     * @return int
453
+     */
454
+    protected function getCurrentTime(): int {
455
+        return time();
456
+    }
457 457
 
458
-	/**
459
-	 * sign message with users private key
460
-	 *
461
-	 * @param IUser $user
462
-	 * @param string $message
463
-	 *
464
-	 * @return string base64 encoded signature
465
-	 */
466
-	protected function signMessage(IUser $user, string $message): string {
467
-		$privateKey = $this->keyManager->getKey($user)->getPrivate();
468
-		openssl_sign(json_encode($message), $signature, $privateKey, OPENSSL_ALGO_SHA512);
469
-		return base64_encode($signature);
470
-	}
458
+    /**
459
+     * sign message with users private key
460
+     *
461
+     * @param IUser $user
462
+     * @param string $message
463
+     *
464
+     * @return string base64 encoded signature
465
+     */
466
+    protected function signMessage(IUser $user, string $message): string {
467
+        $privateKey = $this->keyManager->getKey($user)->getPrivate();
468
+        openssl_sign(json_encode($message), $signature, $privateKey, OPENSSL_ALGO_SHA512);
469
+        return base64_encode($signature);
470
+    }
471 471
 }
Please login to merge, or discard this patch.
Spacing   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -185,8 +185,8 @@  discard block
 block discarded – undo
185 185
 		$groupsInfo->setSorting($sortGroupsBy);
186 186
 		list($adminGroup, $groups) = $groupsInfo->get();
187 187
 
188
-		if(!$isLDAPUsed && $this->appManager->isEnabledForUser('user_ldap')) {
189
-			$isLDAPUsed = (bool)array_reduce($this->userManager->getBackends(), function ($ldapFound, $backend) {
188
+		if (!$isLDAPUsed && $this->appManager->isEnabledForUser('user_ldap')) {
189
+			$isLDAPUsed = (bool) array_reduce($this->userManager->getBackends(), function($ldapFound, $backend) {
190 190
 				return $ldapFound || $backend instanceof User_Proxy;
191 191
 			});
192 192
 		}
@@ -194,7 +194,7 @@  discard block
 block discarded – undo
194 194
 		if ($this->isAdmin) {
195 195
 			$disabledUsers = $isLDAPUsed ? -1 : $this->userManager->countDisabledUsers();
196 196
 			$userCount = $isLDAPUsed ? 0 : array_reduce($this->userManager->countUsers(), function($v, $w) {
197
-				return $v + (int)$w;
197
+				return $v + (int) $w;
198 198
 			}, 0);
199 199
 		} else {
200 200
 			// User is subadmin !
@@ -203,7 +203,7 @@  discard block
 block discarded – undo
203 203
 			$groupsNames = [];
204 204
 			$userCount = 0;
205 205
 
206
-			foreach($groups as $key => $group) {
206
+			foreach ($groups as $key => $group) {
207 207
 				// $userCount += (int)$group['usercount'];
208 208
 				array_push($groupsNames, $group['name']);
209 209
 				// we prevent subadmins from looking up themselves
@@ -402,11 +402,11 @@  discard block
 block discarded – undo
402 402
 
403 403
 		$accountData = $this->accountManager->getUser($user);
404 404
 		$cloudId = $user->getCloudId();
405
-		$message = 'Use my Federated Cloud ID to share with me: ' . $cloudId;
405
+		$message = 'Use my Federated Cloud ID to share with me: '.$cloudId;
406 406
 		$signature = $this->signMessage($user, $message);
407 407
 
408
-		$code = $message . ' ' . $signature;
409
-		$codeMd5 = $message . ' ' . md5($signature);
408
+		$code = $message.' '.$signature;
409
+		$codeMd5 = $message.' '.md5($signature);
410 410
 
411 411
 		switch ($account) {
412 412
 			case 'verify-twitter':
Please login to merge, or discard this patch.