Completed
Push — master ( ba9b17...94004c )
by Lukas
05:19 queued 04:59
created

UsersController::setUserSettings()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 73
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 53
nc 4
nop 13
dl 0
loc 73
rs 8.6829
c 0
b 0
f 0

How to fix   Long Method    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 * @author Clark Tomlinson <[email protected]>
7
 * @author Joas Schilling <[email protected]>
8
 * @author Lukas Reschke <[email protected]>
9
 * @author Morris Jobke <[email protected]>
10
 * @author Robin Appelman <[email protected]>
11
 * @author Roeland Jago Douma <[email protected]>
12
 * @author Thomas Müller <[email protected]>
13
 * @author Vincent Petry <[email protected]>
14
 *
15
 * @license AGPL-3.0
16
 *
17
 * This code is free software: you can redistribute it and/or modify
18
 * it under the terms of the GNU Affero General Public License, version 3,
19
 * as published by the Free Software Foundation.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License, version 3,
27
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
28
 *
29
 */
30
31
namespace OC\Settings\Controller;
32
33
use OC\Accounts\AccountManager;
34
use OC\AppFramework\Http;
35
use OC\ForbiddenException;
36
use OC\User\User;
37
use OCP\App\IAppManager;
38
use OCP\AppFramework\Controller;
39
use OCP\AppFramework\Http\DataResponse;
40
use OCP\AppFramework\Http\TemplateResponse;
41
use OCP\IConfig;
42
use OCP\IGroupManager;
43
use OCP\IL10N;
44
use OCP\ILogger;
45
use OCP\IRequest;
46
use OCP\IURLGenerator;
47
use OCP\IUser;
48
use OCP\IUserManager;
49
use OCP\IUserSession;
50
use OCP\Mail\IMailer;
51
use OCP\IAvatarManager;
52
use Punic\Exception;
53
54
/**
55
 * @package OC\Settings\Controller
56
 */
57
class UsersController extends Controller {
58
	/** @var IL10N */
59
	private $l10n;
60
	/** @var IUserSession */
61
	private $userSession;
62
	/** @var bool */
63
	private $isAdmin;
64
	/** @var IUserManager */
65
	private $userManager;
66
	/** @var IGroupManager */
67
	private $groupManager;
68
	/** @var IConfig */
69
	private $config;
70
	/** @var ILogger */
71
	private $log;
72
	/** @var \OC_Defaults */
73
	private $defaults;
74
	/** @var IMailer */
75
	private $mailer;
76
	/** @var string */
77
	private $fromMailAddress;
78
	/** @var IURLGenerator */
79
	private $urlGenerator;
80
	/** @var bool contains the state of the encryption app */
81
	private $isEncryptionAppEnabled;
82
	/** @var bool contains the state of the admin recovery setting */
83
	private $isRestoreEnabled = false;
84
	/** @var IAvatarManager */
85
	private $avatarManager;
86
	/** @var AccountManager */
87
	private $accountManager;
88
89
	/**
90
	 * @param string $appName
91
	 * @param IRequest $request
92
	 * @param IUserManager $userManager
93
	 * @param IGroupManager $groupManager
94
	 * @param IUserSession $userSession
95
	 * @param IConfig $config
96
	 * @param bool $isAdmin
97
	 * @param IL10N $l10n
98
	 * @param ILogger $log
99
	 * @param \OC_Defaults $defaults
100
	 * @param IMailer $mailer
101
	 * @param string $fromMailAddress
102
	 * @param IURLGenerator $urlGenerator
103
	 * @param IAppManager $appManager
104
	 * @param IAvatarManager $avatarManager
105
	 * @param AccountManager $accountManager
106
	 */
107
	public function __construct($appName,
108
								IRequest $request,
109
								IUserManager $userManager,
110
								IGroupManager $groupManager,
111
								IUserSession $userSession,
112
								IConfig $config,
113
								$isAdmin,
114
								IL10N $l10n,
115
								ILogger $log,
116
								\OC_Defaults $defaults,
117
								IMailer $mailer,
118
								$fromMailAddress,
119
								IURLGenerator $urlGenerator,
120
								IAppManager $appManager,
121
								IAvatarManager $avatarManager,
122
								AccountManager $accountManager
123
) {
124
		parent::__construct($appName, $request);
125
		$this->userManager = $userManager;
126
		$this->groupManager = $groupManager;
127
		$this->userSession = $userSession;
128
		$this->config = $config;
129
		$this->isAdmin = $isAdmin;
130
		$this->l10n = $l10n;
131
		$this->log = $log;
132
		$this->defaults = $defaults;
133
		$this->mailer = $mailer;
134
		$this->fromMailAddress = $fromMailAddress;
135
		$this->urlGenerator = $urlGenerator;
136
		$this->avatarManager = $avatarManager;
137
		$this->accountManager = $accountManager;
138
139
		// check for encryption state - TODO see formatUserForIndex
140
		$this->isEncryptionAppEnabled = $appManager->isEnabledForUser('encryption');
141
		if($this->isEncryptionAppEnabled) {
142
			// putting this directly in empty is possible in PHP 5.5+
143
			$result = $config->getAppValue('encryption', 'recoveryAdminEnabled', 0);
144
			$this->isRestoreEnabled = !empty($result);
145
		}
146
	}
147
148
	/**
149
	 * @param IUser $user
150
	 * @param array $userGroups
151
	 * @return array
152
	 */
153
	private function formatUserForIndex(IUser $user, array $userGroups = null) {
154
155
		// TODO: eliminate this encryption specific code below and somehow
156
		// hook in additional user info from other apps
157
158
		// recovery isn't possible if admin or user has it disabled and encryption
159
		// is enabled - so we eliminate the else paths in the conditional tree
160
		// below
161
		$restorePossible = false;
162
163
		if ($this->isEncryptionAppEnabled) {
164
			if ($this->isRestoreEnabled) {
165
				// check for the users recovery setting
166
				$recoveryMode = $this->config->getUserValue($user->getUID(), 'encryption', 'recoveryEnabled', '0');
167
				// method call inside empty is possible with PHP 5.5+
168
				$recoveryModeEnabled = !empty($recoveryMode);
169
				if ($recoveryModeEnabled) {
170
					// user also has recovery mode enabled
171
					$restorePossible = true;
172
				}
173
			}
174
		} else {
175
			// recovery is possible if encryption is disabled (plain files are
176
			// available)
177
			$restorePossible = true;
178
		}
179
180
		$subAdminGroups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($user);
181
		foreach($subAdminGroups as $key => $subAdminGroup) {
182
			$subAdminGroups[$key] = $subAdminGroup->getGID();
183
		}
184
185
		$displayName = $user->getEMailAddress();
186
		if (is_null($displayName)) {
187
			$displayName = '';
188
		}
189
190
		$avatarAvailable = false;
191
		if ($this->config->getSystemValue('enable_avatars', true) === true) {
192
			try {
193
				$avatarAvailable = $this->avatarManager->getAvatar($user->getUID())->exists();
194
			} catch (\Exception $e) {
195
				//No avatar yet
196
			}
197
		}
198
199
		return [
200
			'name' => $user->getUID(),
201
			'displayname' => $user->getDisplayName(),
202
			'groups' => (empty($userGroups)) ? $this->groupManager->getUserGroupIds($user) : $userGroups,
203
			'subadmin' => $subAdminGroups,
204
			'quota' => $user->getQuota(),
205
			'storageLocation' => $user->getHome(),
206
			'lastLogin' => $user->getLastLogin() * 1000,
207
			'backend' => $user->getBackendClassName(),
208
			'email' => $displayName,
209
			'isRestoreDisabled' => !$restorePossible,
210
			'isAvatarAvailable' => $avatarAvailable,
211
		];
212
	}
213
214
	/**
215
	 * @param array $userIDs Array with schema [$uid => $displayName]
216
	 * @return IUser[]
217
	 */
218
	private function getUsersForUID(array $userIDs) {
219
		$users = [];
220
		foreach ($userIDs as $uid => $displayName) {
221
			$users[$uid] = $this->userManager->get($uid);
222
		}
223
		return $users;
224
	}
225
226
	/**
227
	 * @NoAdminRequired
228
	 *
229
	 * @param int $offset
230
	 * @param int $limit
231
	 * @param string $gid GID to filter for
232
	 * @param string $pattern Pattern to search for in the username
233
	 * @param string $backend Backend to filter for (class-name)
234
	 * @return DataResponse
235
	 *
236
	 * TODO: Tidy up and write unit tests - code is mainly static method calls
237
	 */
238
	public function index($offset = 0, $limit = 10, $gid = '', $pattern = '', $backend = '') {
239
		// FIXME: The JS sends the group '_everyone' instead of no GID for the "all users" group.
240
		if($gid === '_everyone') {
241
			$gid = '';
242
		}
243
244
		// Remove backends
245
		if(!empty($backend)) {
246
			$activeBackends = $this->userManager->getBackends();
247
			$this->userManager->clearBackends();
248
			foreach($activeBackends as $singleActiveBackend) {
249
				if($backend === get_class($singleActiveBackend)) {
250
					$this->userManager->registerBackend($singleActiveBackend);
251
					break;
252
				}
253
			}
254
		}
255
256
		$users = [];
257
		if ($this->isAdmin) {
258
259
			if($gid !== '') {
260
				$batch = $this->getUsersForUID($this->groupManager->displayNamesInGroup($gid, $pattern, $limit, $offset));
261
			} else {
262
				$batch = $this->userManager->search($pattern, $limit, $offset);
263
			}
264
265
			foreach ($batch as $user) {
266
				$users[] = $this->formatUserForIndex($user);
267
			}
268
269
		} else {
270
			$subAdminOfGroups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($this->userSession->getUser());
271
			// New class returns IGroup[] so convert back
272
			$gids = [];
273
			foreach ($subAdminOfGroups as $group) {
274
				$gids[] = $group->getGID();
275
			}
276
			$subAdminOfGroups = $gids;
277
278
			// Set the $gid parameter to an empty value if the subadmin has no rights to access a specific group
279
			if($gid !== '' && !in_array($gid, $subAdminOfGroups)) {
280
				$gid = '';
281
			}
282
283
			// Batch all groups the user is subadmin of when a group is specified
284
			$batch = [];
285
			if($gid === '') {
286
				foreach($subAdminOfGroups as $group) {
287
					$groupUsers = $this->groupManager->displayNamesInGroup($group, $pattern, $limit, $offset);
288
289
					foreach($groupUsers as $uid => $displayName) {
290
						$batch[$uid] = $displayName;
291
					}
292
				}
293
			} else {
294
				$batch = $this->groupManager->displayNamesInGroup($gid, $pattern, $limit, $offset);
295
			}
296
			$batch = $this->getUsersForUID($batch);
297
298
			foreach ($batch as $user) {
299
				// Only add the groups, this user is a subadmin of
300
				$userGroups = array_values(array_intersect(
301
					$this->groupManager->getUserGroupIds($user),
302
					$subAdminOfGroups
303
				));
304
				$users[] = $this->formatUserForIndex($user, $userGroups);
305
			}
306
		}
307
308
		return new DataResponse($users);
309
	}
310
311
	/**
312
	 * @NoAdminRequired
313
	 * @PasswordConfirmationRequired
314
	 *
315
	 * @param string $username
316
	 * @param string $password
317
	 * @param array $groups
318
	 * @param string $email
319
	 * @return DataResponse
320
	 */
321
	public function create($username, $password, array $groups=array(), $email='') {
322
		if($email !== '' && !$this->mailer->validateMailAddress($email)) {
323
			return new DataResponse(
324
				array(
325
					'message' => (string)$this->l10n->t('Invalid mail address')
326
				),
327
				Http::STATUS_UNPROCESSABLE_ENTITY
328
			);
329
		}
330
331
		$currentUser = $this->userSession->getUser();
332
333
		if (!$this->isAdmin) {
334
			if (!empty($groups)) {
335
				foreach ($groups as $key => $group) {
336
					$groupObject = $this->groupManager->get($group);
337
					if($groupObject === null) {
338
						unset($groups[$key]);
339
						continue;
340
					}
341
342
					if (!$this->groupManager->getSubAdmin()->isSubAdminofGroup($currentUser, $groupObject)) {
343
						unset($groups[$key]);
344
					}
345
				}
346
			}
347
348
			if (empty($groups)) {
349
				$groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($currentUser);
350
				// New class returns IGroup[] so convert back
351
				$gids = [];
352
				foreach ($groups as $group) {
353
					$gids[] = $group->getGID();
354
				}
355
				$groups = $gids;
356
			}
357
		}
358
359
		if ($this->userManager->userExists($username)) {
360
			return new DataResponse(
361
				array(
362
					'message' => (string)$this->l10n->t('A user with that name already exists.')
363
				),
364
				Http::STATUS_CONFLICT
365
			);
366
		}
367
368
		try {
369
			$user = $this->userManager->createUser($username, $password);
370
		} catch (\Exception $exception) {
371
			$message = $exception->getMessage();
372
			if (!$message) {
373
				$message = $this->l10n->t('Unable to create user.');
374
			}
375
			return new DataResponse(
376
				array(
377
					'message' => (string) $message,
378
				),
379
				Http::STATUS_FORBIDDEN
380
			);
381
		}
382
383
		if($user instanceof User) {
384
			if($groups !== null) {
385
				foreach($groups as $groupName) {
386
					$group = $this->groupManager->get($groupName);
387
388
					if(empty($group)) {
389
						$group = $this->groupManager->createGroup($groupName);
390
					}
391
					$group->addUser($user);
392
				}
393
			}
394
			/**
395
			 * Send new user mail only if a mail is set
396
			 */
397
			if($email !== '') {
398
				$user->setEMailAddress($email);
399
400
				// data for the mail template
401
				$mailData = array(
402
					'username' => $username,
403
					'url' => $this->urlGenerator->getAbsoluteURL('/')
404
				);
405
406
				$mail = new TemplateResponse('settings', 'email.new_user', $mailData, 'blank');
407
				$mailContent = $mail->render();
408
409
				$mail = new TemplateResponse('settings', 'email.new_user_plain_text', $mailData, 'blank');
410
				$plainTextMailContent = $mail->render();
411
412
				$subject = $this->l10n->t('Your %s account was created', [$this->defaults->getName()]);
413
414
				try {
415
					$message = $this->mailer->createMessage();
416
					$message->setTo([$email => $username]);
417
					$message->setSubject($subject);
418
					$message->setHtmlBody($mailContent);
0 ignored issues
show
Bug introduced by
It seems like $mailContent defined by $mail->render() on line 407 can also be of type boolean; however, OC\Mail\Message::setHtmlBody() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
419
					$message->setPlainBody($plainTextMailContent);
0 ignored issues
show
Bug introduced by
It seems like $plainTextMailContent defined by $mail->render() on line 410 can also be of type boolean; however, OC\Mail\Message::setPlainBody() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
420
					$message->setFrom([$this->fromMailAddress => $this->defaults->getName()]);
421
					$this->mailer->send($message);
422
				} catch(\Exception $e) {
423
					$this->log->error("Can't send new user mail to $email: " . $e->getMessage(), array('app' => 'settings'));
424
				}
425
			}
426
			// fetch users groups
427
			$userGroups = $this->groupManager->getUserGroupIds($user);
428
429
			return new DataResponse(
430
				$this->formatUserForIndex($user, $userGroups),
431
				Http::STATUS_CREATED
432
			);
433
		}
434
435
		return new DataResponse(
436
			array(
437
				'message' => (string)$this->l10n->t('Unable to create user.')
438
			),
439
			Http::STATUS_FORBIDDEN
440
		);
441
442
	}
443
444
	/**
445
	 * @NoAdminRequired
446
	 * @PasswordConfirmationRequired
447
	 *
448
	 * @param string $id
449
	 * @return DataResponse
450
	 */
451
	public function destroy($id) {
452
		$userId = $this->userSession->getUser()->getUID();
453
		$user = $this->userManager->get($id);
454
455
		if($userId === $id) {
456
			return new DataResponse(
457
				array(
458
					'status' => 'error',
459
					'data' => array(
460
						'message' => (string)$this->l10n->t('Unable to delete user.')
461
					)
462
				),
463
				Http::STATUS_FORBIDDEN
464
			);
465
		}
466
467
		if(!$this->isAdmin && !$this->groupManager->getSubAdmin()->isUserAccessible($this->userSession->getUser(), $user)) {
468
			return new DataResponse(
469
				array(
470
					'status' => 'error',
471
					'data' => array(
472
						'message' => (string)$this->l10n->t('Authentication error')
473
					)
474
				),
475
				Http::STATUS_FORBIDDEN
476
			);
477
		}
478
479
		if($user) {
480
			if($user->delete()) {
481
				return new DataResponse(
482
					array(
483
						'status' => 'success',
484
						'data' => array(
485
							'username' => $id
486
						)
487
					),
488
					Http::STATUS_NO_CONTENT
489
				);
490
			}
491
		}
492
493
		return new DataResponse(
494
			array(
495
				'status' => 'error',
496
				'data' => array(
497
					'message' => (string)$this->l10n->t('Unable to delete user.')
498
				)
499
			),
500
			Http::STATUS_FORBIDDEN
501
		);
502
	}
503
504
	/**
505
	 * @NoAdminRequired
506
	 * @NoSubadminRequired
507
	 * @PasswordConfirmationRequired
508
	 *
509
	 * @param string $avatarScope
510
	 * @param string $displayname
511
	 * @param string $displaynameScope
512
	 * @param string $phone
513
	 * @param string $phoneScope
514
	 * @param string $email
515
	 * @param string $emailScope
516
	 * @param string $website
517
	 * @param string $websiteScope
518
	 * @param string $address
519
	 * @param string $addressScope
520
	 * @param string $twitter
521
	 * @param string $twitterScope
522
	 * @return DataResponse
523
	 */
524
	public function setUserSettings($avatarScope,
525
									$displayname,
526
									$displaynameScope,
527
									$phone,
528
									$phoneScope,
529
									$email,
530
									$emailScope,
531
									$website,
532
									$websiteScope,
533
									$address,
534
									$addressScope,
535
									$twitter,
536
									$twitterScope
537
	) {
538
539
540
		if(!empty($email) && !$this->mailer->validateMailAddress($email)) {
541
			return new DataResponse(
542
				array(
543
					'status' => 'error',
544
					'data' => array(
545
						'message' => (string)$this->l10n->t('Invalid mail address')
546
					)
547
				),
548
				Http::STATUS_UNPROCESSABLE_ENTITY
549
			);
550
		}
551
552
		$user = $this->userSession->getUser();
553
554
		$data = [
555
			AccountManager::PROPERTY_AVATAR =>  ['scope' => $avatarScope],
556
			AccountManager::PROPERTY_DISPLAYNAME => ['value' => $displayname, 'scope' => $displaynameScope],
557
			AccountManager::PROPERTY_EMAIL=> ['value' => $email, 'scope' => $emailScope],
558
			AccountManager::PROPERTY_WEBSITE => ['value' => $website, 'scope' => $websiteScope],
559
			AccountManager::PROPERTY_ADDRESS => ['value' => $address, 'scope' => $addressScope],
560
			AccountManager::PROPERTY_PHONE => ['value' => $phone, 'scope' => $phoneScope],
561
			AccountManager::PROPERTY_TWITTER => ['value' => $twitter, 'scope' => $twitterScope]
562
		];
563
564
		$this->accountManager->updateUser($user, $data);
0 ignored issues
show
Bug introduced by
It seems like $user defined by $this->userSession->getUser() on line 552 can be null; however, OC\Accounts\AccountManager::updateUser() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
565
566
		try {
567
			$this->saveUserSettings($user, $data);
0 ignored issues
show
Bug introduced by
It seems like $user defined by $this->userSession->getUser() on line 552 can be null; however, OC\Settings\Controller\U...ler::saveUserSettings() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
568
			return new DataResponse(
569
				array(
570
					'status' => 'success',
571
					'data' => array(
572
						'userId' => $user->getUID(),
573
						'avatarScope' => $avatarScope,
574
						'displayname' => $displayname,
575
						'displaynameScope' => $displaynameScope,
576
						'email' => $email,
577
						'emailScope' => $emailScope,
578
						'website' => $website,
579
						'websiteScope' => $websiteScope,
580
						'address' => $address,
581
						'addressScope' => $addressScope,
582
						'message' => (string)$this->l10n->t('Settings saved')
583
					)
584
				),
585
				Http::STATUS_OK
586
			);
587
		} catch (ForbiddenException $e) {
588
			return new DataResponse([
589
				'status' => 'error',
590
				'data' => [
591
					'message' => $e->getMessage()
592
				],
593
			]);
594
		}
595
596
	}
597
598
599
	/**
600
	 * update account manager with new user data
601
	 *
602
	 * @param IUser $user
603
	 * @param array $data
604
	 * @throws ForbiddenException
605
	 */
606
	private function saveUserSettings(IUser $user, $data) {
607
608
		// keep the user back-end up-to-date with the latest display name and email
609
		// address
610
		$oldDisplayName = $user->getDisplayName();
611
		if (isset($data[AccountManager::PROPERTY_DISPLAYNAME]['value'])  && $oldDisplayName !== $data[AccountManager::PROPERTY_DISPLAYNAME]['value']) {
612
			$result = $user->setDisplayName($data[AccountManager::PROPERTY_DISPLAYNAME]['value']);
613
			if ($result === false) {
614
				throw new ForbiddenException($this->l10n->t('Unable to change full name'));
615
			}
616
		}
617
618
		if (isset($data['email'][0]['value']) && $user->getEMailAddress() !== $data['email'][0]['value']) {
619
			$result = $user->setEMailAddress($data['email'][0]['value']);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as $user->setEMailAddress($...a['email'][0]['value']) (which targets OCP\IUser::setEMailAddress()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
620
			if ($result === false) {
621
				throw new ForbiddenException($this->l10n->t('Unable to change mail address'));
622
			}
623
		}
624
625
		$this->accountManager->updateUser($user, $data);
626
	}
627
628
	/**
629
	 * Count all unique users visible for the current admin/subadmin.
630
	 *
631
	 * @NoAdminRequired
632
	 *
633
	 * @return DataResponse
634
	 */
635
	public function stats() {
636
		$userCount = 0;
637
		if ($this->isAdmin) {
638
			$countByBackend = $this->userManager->countUsers();
639
640
			if (!empty($countByBackend)) {
641
				foreach ($countByBackend as $count) {
642
					$userCount += $count;
643
				}
644
			}
645
		} else {
646
			$groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($this->userSession->getUser());
647
648
			$uniqueUsers = [];
649
			foreach ($groups as $group) {
650
				foreach($group->getUsers() as $uid => $displayName) {
651
					$uniqueUsers[$uid] = true;
652
				}
653
			}
654
655
			$userCount = count($uniqueUsers);
656
		}
657
658
		return new DataResponse(
659
			[
660
				'totalUsers' => $userCount
661
			]
662
		);
663
	}
664
665
666
	/**
667
	 * Set the displayName of a user
668
	 *
669
	 * @NoAdminRequired
670
	 * @NoSubadminRequired
671
	 * @PasswordConfirmationRequired
672
	 * @todo merge into saveUserSettings
673
	 *
674
	 * @param string $username
675
	 * @param string $displayName
676
	 * @return DataResponse
677
	 */
678
	public function setDisplayName($username, $displayName) {
679
		$currentUser = $this->userSession->getUser();
680
		$user = $this->userManager->get($username);
681
682
		if ($user === null ||
683
			!$user->canChangeDisplayName() ||
684
			(
685
				!$this->groupManager->isAdmin($currentUser->getUID()) &&
686
				!$this->groupManager->getSubAdmin()->isUserAccessible($currentUser, $user) &&
687
				$currentUser->getUID() !== $username
688
689
			)
690
		) {
691
			return new DataResponse([
692
				'status' => 'error',
693
				'data' => [
694
					'message' => $this->l10n->t('Authentication error'),
695
				],
696
			]);
697
		}
698
699
		$userData = $this->accountManager->getUser($user);
700
		$userData[AccountManager::PROPERTY_DISPLAYNAME]['value'] = $displayName;
701
702
703
		try {
704
			$this->saveUserSettings($user, $userData);
705
			return new DataResponse([
706
				'status' => 'success',
707
				'data' => [
708
					'message' => $this->l10n->t('Your full name has been changed.'),
709
					'username' => $username,
710
					'displayName' => $displayName,
711
				],
712
			]);
713
		} catch (ForbiddenException $e) {
714
			return new DataResponse([
715
				'status' => 'error',
716
				'data' => [
717
					'message' => $e->getMessage(),
718
					'displayName' => $user->getDisplayName(),
719
				],
720
			]);
721
		}
722
	}
723
}
724