@@ -53,890 +53,890 @@  | 
                                                    ||
| 53 | 53 | |
| 54 | 54 |  class UsersController extends AUserData { | 
                                                        
| 55 | 55 | |
| 56 | - /** @var IAppManager */  | 
                                                        |
| 57 | - private $appManager;  | 
                                                        |
| 58 | - /** @var ILogger */  | 
                                                        |
| 59 | - private $logger;  | 
                                                        |
| 60 | - /** @var IFactory */  | 
                                                        |
| 61 | - private $l10nFactory;  | 
                                                        |
| 62 | - /** @var NewUserMailHelper */  | 
                                                        |
| 63 | - private $newUserMailHelper;  | 
                                                        |
| 64 | - /** @var FederatedFileSharingFactory */  | 
                                                        |
| 65 | - private $federatedFileSharingFactory;  | 
                                                        |
| 66 | - /** @var ISecureRandom */  | 
                                                        |
| 67 | - private $secureRandom;  | 
                                                        |
| 68 | -  | 
                                                        |
| 69 | - /**  | 
                                                        |
| 70 | - * @param string $appName  | 
                                                        |
| 71 | - * @param IRequest $request  | 
                                                        |
| 72 | - * @param IUserManager $userManager  | 
                                                        |
| 73 | - * @param IConfig $config  | 
                                                        |
| 74 | - * @param IAppManager $appManager  | 
                                                        |
| 75 | - * @param IGroupManager $groupManager  | 
                                                        |
| 76 | - * @param IUserSession $userSession  | 
                                                        |
| 77 | - * @param AccountManager $accountManager  | 
                                                        |
| 78 | - * @param ILogger $logger  | 
                                                        |
| 79 | - * @param IFactory $l10nFactory  | 
                                                        |
| 80 | - * @param NewUserMailHelper $newUserMailHelper  | 
                                                        |
| 81 | - * @param FederatedFileSharingFactory $federatedFileSharingFactory  | 
                                                        |
| 82 | - * @param ISecureRandom $secureRandom  | 
                                                        |
| 83 | - */  | 
                                                        |
| 84 | - public function __construct(string $appName,  | 
                                                        |
| 85 | - IRequest $request,  | 
                                                        |
| 86 | - IUserManager $userManager,  | 
                                                        |
| 87 | - IConfig $config,  | 
                                                        |
| 88 | - IAppManager $appManager,  | 
                                                        |
| 89 | - IGroupManager $groupManager,  | 
                                                        |
| 90 | - IUserSession $userSession,  | 
                                                        |
| 91 | - AccountManager $accountManager,  | 
                                                        |
| 92 | - ILogger $logger,  | 
                                                        |
| 93 | - IFactory $l10nFactory,  | 
                                                        |
| 94 | - NewUserMailHelper $newUserMailHelper,  | 
                                                        |
| 95 | - FederatedFileSharingFactory $federatedFileSharingFactory,  | 
                                                        |
| 96 | -								ISecureRandom $secureRandom) { | 
                                                        |
| 97 | - parent::__construct($appName,  | 
                                                        |
| 98 | - $request,  | 
                                                        |
| 99 | - $userManager,  | 
                                                        |
| 100 | - $config,  | 
                                                        |
| 101 | - $groupManager,  | 
                                                        |
| 102 | - $userSession,  | 
                                                        |
| 103 | - $accountManager);  | 
                                                        |
| 104 | -  | 
                                                        |
| 105 | - $this->appManager = $appManager;  | 
                                                        |
| 106 | - $this->logger = $logger;  | 
                                                        |
| 107 | - $this->l10nFactory = $l10nFactory;  | 
                                                        |
| 108 | - $this->newUserMailHelper = $newUserMailHelper;  | 
                                                        |
| 109 | - $this->federatedFileSharingFactory = $federatedFileSharingFactory;  | 
                                                        |
| 110 | - $this->secureRandom = $secureRandom;  | 
                                                        |
| 111 | - }  | 
                                                        |
| 112 | -  | 
                                                        |
| 113 | - /**  | 
                                                        |
| 114 | - * @NoAdminRequired  | 
                                                        |
| 115 | - *  | 
                                                        |
| 116 | - * returns a list of users  | 
                                                        |
| 117 | - *  | 
                                                        |
| 118 | - * @param string $search  | 
                                                        |
| 119 | - * @param int $limit  | 
                                                        |
| 120 | - * @param int $offset  | 
                                                        |
| 121 | - * @return DataResponse  | 
                                                        |
| 122 | - */  | 
                                                        |
| 123 | -	public function getUsers(string $search = '', $limit = null, $offset = 0): DataResponse { | 
                                                        |
| 124 | - $user = $this->userSession->getUser();  | 
                                                        |
| 125 | - $users = [];  | 
                                                        |
| 126 | -  | 
                                                        |
| 127 | - // Admin? Or SubAdmin?  | 
                                                        |
| 128 | - $uid = $user->getUID();  | 
                                                        |
| 129 | - $subAdminManager = $this->groupManager->getSubAdmin();  | 
                                                        |
| 130 | -		if ($this->groupManager->isAdmin($uid)){ | 
                                                        |
| 131 | - $users = $this->userManager->search($search, $limit, $offset);  | 
                                                        |
| 132 | -		} else if ($subAdminManager->isSubAdmin($user)) { | 
                                                        |
| 133 | - $subAdminOfGroups = $subAdminManager->getSubAdminsGroups($user);  | 
                                                        |
| 134 | -			foreach ($subAdminOfGroups as $key => $group) { | 
                                                        |
| 135 | - $subAdminOfGroups[$key] = $group->getGID();  | 
                                                        |
| 136 | - }  | 
                                                        |
| 137 | -  | 
                                                        |
| 138 | - $users = [];  | 
                                                        |
| 139 | -			foreach ($subAdminOfGroups as $group) { | 
                                                        |
| 140 | - $users = array_merge($users, $this->groupManager->displayNamesInGroup($group, $search, $limit, $offset));  | 
                                                        |
| 141 | - }  | 
                                                        |
| 142 | - }  | 
                                                        |
| 143 | -  | 
                                                        |
| 144 | - $users = array_keys($users);  | 
                                                        |
| 145 | -  | 
                                                        |
| 146 | - return new DataResponse([  | 
                                                        |
| 147 | - 'users' => $users  | 
                                                        |
| 148 | - ]);  | 
                                                        |
| 149 | - }  | 
                                                        |
| 150 | -  | 
                                                        |
| 151 | - /**  | 
                                                        |
| 152 | - * @NoAdminRequired  | 
                                                        |
| 153 | - *  | 
                                                        |
| 154 | - * returns a list of users and their data  | 
                                                        |
| 155 | - */  | 
                                                        |
| 156 | -	public function getUsersDetails(string $search = '', $limit = null, $offset = 0): DataResponse { | 
                                                        |
| 157 | - $currentUser = $this->userSession->getUser();  | 
                                                        |
| 158 | - $users = [];  | 
                                                        |
| 159 | -  | 
                                                        |
| 160 | - // Admin? Or SubAdmin?  | 
                                                        |
| 161 | - $uid = $currentUser->getUID();  | 
                                                        |
| 162 | - $subAdminManager = $this->groupManager->getSubAdmin();  | 
                                                        |
| 163 | -		if ($this->groupManager->isAdmin($uid)){ | 
                                                        |
| 164 | - $users = $this->userManager->search($search, $limit, $offset);  | 
                                                        |
| 165 | - $users = array_keys($users);  | 
                                                        |
| 166 | -		} else if ($subAdminManager->isSubAdmin($currentUser)) { | 
                                                        |
| 167 | - $subAdminOfGroups = $subAdminManager->getSubAdminsGroups($currentUser);  | 
                                                        |
| 168 | -			foreach ($subAdminOfGroups as $key => $group) { | 
                                                        |
| 169 | - $subAdminOfGroups[$key] = $group->getGID();  | 
                                                        |
| 170 | - }  | 
                                                        |
| 171 | -  | 
                                                        |
| 172 | - $users = [];  | 
                                                        |
| 173 | -			foreach ($subAdminOfGroups as $group) { | 
                                                        |
| 174 | - $users[] = array_keys($this->groupManager->displayNamesInGroup($group, $search, $limit, $offset));  | 
                                                        |
| 175 | - }  | 
                                                        |
| 176 | - $users = array_merge(...$users);  | 
                                                        |
| 177 | - }  | 
                                                        |
| 178 | -  | 
                                                        |
| 179 | - $usersDetails = [];  | 
                                                        |
| 180 | -		foreach ($users as $userId) { | 
                                                        |
| 181 | - $userId = (string) $userId;  | 
                                                        |
| 182 | - $userData = $this->getUserData($userId);  | 
                                                        |
| 183 | - // Do not insert empty entry  | 
                                                        |
| 184 | -			if (!empty($userData)) { | 
                                                        |
| 185 | - $usersDetails[$userId] = $userData;  | 
                                                        |
| 186 | -			} else { | 
                                                        |
| 187 | - // Logged user does not have permissions to see this user  | 
                                                        |
| 188 | - // only showing its id  | 
                                                        |
| 189 | - $usersDetails[$userId] = ['id' => $userId];  | 
                                                        |
| 190 | - }  | 
                                                        |
| 191 | - }  | 
                                                        |
| 192 | -  | 
                                                        |
| 193 | - return new DataResponse([  | 
                                                        |
| 194 | - 'users' => $usersDetails  | 
                                                        |
| 195 | - ]);  | 
                                                        |
| 196 | - }  | 
                                                        |
| 197 | -  | 
                                                        |
| 198 | - /**  | 
                                                        |
| 199 | - * @throws OCSException  | 
                                                        |
| 200 | - */  | 
                                                        |
| 201 | -	private function createNewUserId(): string { | 
                                                        |
| 202 | - $attempts = 0;  | 
                                                        |
| 203 | -		do { | 
                                                        |
| 204 | - $uidCandidate = $this->secureRandom->generate(10, ISecureRandom::CHAR_HUMAN_READABLE);  | 
                                                        |
| 205 | -			if (!$this->userManager->userExists($uidCandidate)) { | 
                                                        |
| 206 | - return $uidCandidate;  | 
                                                        |
| 207 | - }  | 
                                                        |
| 208 | - $attempts++;  | 
                                                        |
| 209 | - } while ($attempts < 10);  | 
                                                        |
| 210 | -		throw new OCSException('Could not create non-existing user id', 111); | 
                                                        |
| 211 | - }  | 
                                                        |
| 212 | -  | 
                                                        |
| 213 | - /**  | 
                                                        |
| 214 | - * @PasswordConfirmationRequired  | 
                                                        |
| 215 | - * @NoAdminRequired  | 
                                                        |
| 216 | - *  | 
                                                        |
| 217 | - * @param string $userid  | 
                                                        |
| 218 | - * @param string $password  | 
                                                        |
| 219 | - * @param string $displayName  | 
                                                        |
| 220 | - * @param string $email  | 
                                                        |
| 221 | - * @param array $groups  | 
                                                        |
| 222 | - * @param array $subadmin  | 
                                                        |
| 223 | - * @param string $quota  | 
                                                        |
| 224 | - * @param string $language  | 
                                                        |
| 225 | - * @return DataResponse  | 
                                                        |
| 226 | - * @throws OCSException  | 
                                                        |
| 227 | - */  | 
                                                        |
| 228 | - public function addUser(string $userid,  | 
                                                        |
| 229 | - string $password = '',  | 
                                                        |
| 230 | - string $displayName = '',  | 
                                                        |
| 231 | - string $email = '',  | 
                                                        |
| 232 | - array $groups = [],  | 
                                                        |
| 233 | - array $subadmin = [],  | 
                                                        |
| 234 | - string $quota = '',  | 
                                                        |
| 235 | -							string $language = ''): DataResponse { | 
                                                        |
| 236 | - $user = $this->userSession->getUser();  | 
                                                        |
| 237 | - $isAdmin = $this->groupManager->isAdmin($user->getUID());  | 
                                                        |
| 238 | - $subAdminManager = $this->groupManager->getSubAdmin();  | 
                                                        |
| 239 | -  | 
                                                        |
| 240 | -		if(empty($userid) && $this->config->getAppValue('core', 'newUser.generateUserID', 'no') === 'yes') { | 
                                                        |
| 241 | - $userid = $this->createNewUserId();  | 
                                                        |
| 242 | - }  | 
                                                        |
| 243 | -  | 
                                                        |
| 244 | -		if ($this->userManager->userExists($userid)) { | 
                                                        |
| 245 | -			$this->logger->error('Failed addUser attempt: User already exists.', ['app' => 'ocs_api']); | 
                                                        |
| 246 | -			throw new OCSException('User already exists', 102); | 
                                                        |
| 247 | - }  | 
                                                        |
| 248 | -  | 
                                                        |
| 249 | -		if ($groups !== []) { | 
                                                        |
| 250 | -			foreach ($groups as $group) { | 
                                                        |
| 251 | -				if (!$this->groupManager->groupExists($group)) { | 
                                                        |
| 252 | -					throw new OCSException('group '.$group.' does not exist', 104); | 
                                                        |
| 253 | - }  | 
                                                        |
| 254 | -				if (!$isAdmin && !$subAdminManager->isSubAdminOfGroup($user, $this->groupManager->get($group))) { | 
                                                        |
| 255 | -					throw new OCSException('insufficient privileges for group '. $group, 105); | 
                                                        |
| 256 | - }  | 
                                                        |
| 257 | - }  | 
                                                        |
| 258 | -		} else { | 
                                                        |
| 259 | -			if (!$isAdmin) { | 
                                                        |
| 260 | -				throw new OCSException('no group specified (required for subadmins)', 106); | 
                                                        |
| 261 | - }  | 
                                                        |
| 262 | - }  | 
                                                        |
| 263 | -  | 
                                                        |
| 264 | - $subadminGroups = [];  | 
                                                        |
| 265 | -		if ($subadmin !== []) { | 
                                                        |
| 266 | -			foreach ($subadmin as $groupid) { | 
                                                        |
| 267 | - $group = $this->groupManager->get($groupid);  | 
                                                        |
| 268 | - // Check if group exists  | 
                                                        |
| 269 | -				if ($group === null) { | 
                                                        |
| 270 | -					throw new OCSException('Subadmin group does not exist',  102); | 
                                                        |
| 271 | - }  | 
                                                        |
| 272 | - // Check if trying to make subadmin of admin group  | 
                                                        |
| 273 | -				if ($group->getGID() === 'admin') { | 
                                                        |
| 274 | -					throw new OCSException('Cannot create subadmins for admin group', 103); | 
                                                        |
| 275 | - }  | 
                                                        |
| 276 | - // Check if has permission to promote subadmins  | 
                                                        |
| 277 | -				if (!$subAdminManager->isSubAdminOfGroup($user, $group) && !$isAdmin) { | 
                                                        |
| 278 | -					throw new OCSForbiddenException('No permissions to promote subadmins'); | 
                                                        |
| 279 | - }  | 
                                                        |
| 280 | - $subadminGroups[] = $group;  | 
                                                        |
| 281 | - }  | 
                                                        |
| 282 | - }  | 
                                                        |
| 283 | -  | 
                                                        |
| 284 | - $generatePasswordResetToken = false;  | 
                                                        |
| 285 | -		if ($password === '') { | 
                                                        |
| 286 | -			if ($email === '') { | 
                                                        |
| 287 | -				throw new OCSException('To send a password link to the user an email address is required.', 108); | 
                                                        |
| 288 | - }  | 
                                                        |
| 289 | -  | 
                                                        |
| 290 | - $password = $this->secureRandom->generate(10);  | 
                                                        |
| 291 | - // Make sure we pass the password_policy  | 
                                                        |
| 292 | -			$password .= $this->secureRandom->generate(2, '$!.,;:-~+*[]{}()'); | 
                                                        |
| 293 | - $generatePasswordResetToken = true;  | 
                                                        |
| 294 | - }  | 
                                                        |
| 295 | -  | 
                                                        |
| 296 | -		if ($email === '' && $this->config->getAppValue('core', 'newUser.requireEmail', 'no') === 'yes') { | 
                                                        |
| 297 | -			throw new OCSException('Required email address was not provided', 110); | 
                                                        |
| 298 | - }  | 
                                                        |
| 299 | -  | 
                                                        |
| 300 | -		try { | 
                                                        |
| 301 | - $newUser = $this->userManager->createUser($userid, $password);  | 
                                                        |
| 302 | -			$this->logger->info('Successful addUser call with userid: ' . $userid, ['app' => 'ocs_api']); | 
                                                        |
| 303 | -  | 
                                                        |
| 304 | -			foreach ($groups as $group) { | 
                                                        |
| 305 | - $this->groupManager->get($group)->addUser($newUser);  | 
                                                        |
| 306 | -				$this->logger->info('Added userid ' . $userid . ' to group ' . $group, ['app' => 'ocs_api']); | 
                                                        |
| 307 | - }  | 
                                                        |
| 308 | -			foreach ($subadminGroups as $group) { | 
                                                        |
| 309 | - $subAdminManager->createSubAdmin($newUser, $group);  | 
                                                        |
| 310 | - }  | 
                                                        |
| 311 | -  | 
                                                        |
| 312 | -			if ($displayName !== '') { | 
                                                        |
| 313 | - $this->editUser($userid, 'display', $displayName);  | 
                                                        |
| 314 | - }  | 
                                                        |
| 315 | -  | 
                                                        |
| 316 | -			if ($quota !== '') { | 
                                                        |
| 317 | - $this->editUser($userid, 'quota', $quota);  | 
                                                        |
| 318 | - }  | 
                                                        |
| 319 | -  | 
                                                        |
| 320 | -			if ($language !== '') { | 
                                                        |
| 321 | - $this->editUser($userid, 'language', $language);  | 
                                                        |
| 322 | - }  | 
                                                        |
| 323 | -  | 
                                                        |
| 324 | - // Send new user mail only if a mail is set  | 
                                                        |
| 325 | -			if ($email !== '') { | 
                                                        |
| 326 | - $newUser->setEMailAddress($email);  | 
                                                        |
| 327 | -				try { | 
                                                        |
| 328 | - $emailTemplate = $this->newUserMailHelper->generateTemplate($newUser, $generatePasswordResetToken);  | 
                                                        |
| 329 | - $this->newUserMailHelper->sendMail($newUser, $emailTemplate);  | 
                                                        |
| 330 | -				} catch (\Exception $e) { | 
                                                        |
| 331 | - $this->logger->logException($e, [  | 
                                                        |
| 332 | - 'message' => "Can't send new user mail to $email",  | 
                                                        |
| 333 | - 'level' => ILogger::ERROR,  | 
                                                        |
| 334 | - 'app' => 'ocs_api',  | 
                                                        |
| 335 | - ]);  | 
                                                        |
| 336 | -					throw new OCSException('Unable to send the invitation mail', 109); | 
                                                        |
| 337 | - }  | 
                                                        |
| 338 | - }  | 
                                                        |
| 339 | -  | 
                                                        |
| 340 | - return new DataResponse(['id' => $userid]);  | 
                                                        |
| 341 | -  | 
                                                        |
| 342 | -		} catch (HintException $e) { | 
                                                        |
| 343 | - $this->logger->logException($e, [  | 
                                                        |
| 344 | - 'message' => 'Failed addUser attempt with hint exception.',  | 
                                                        |
| 345 | - 'level' => ILogger::WARN,  | 
                                                        |
| 346 | - 'app' => 'ocs_api',  | 
                                                        |
| 347 | - ]);  | 
                                                        |
| 348 | - throw new OCSException($e->getHint(), 107);  | 
                                                        |
| 349 | -		} catch (OCSException $e) { | 
                                                        |
| 350 | - $this->logger->logException($e, [  | 
                                                        |
| 351 | - 'message' => 'Failed addUser attempt with ocs exeption.',  | 
                                                        |
| 352 | - 'level' => ILogger::ERROR,  | 
                                                        |
| 353 | - 'app' => 'ocs_api',  | 
                                                        |
| 354 | - ]);  | 
                                                        |
| 355 | - throw $e;  | 
                                                        |
| 356 | -		} catch (\Exception $e) { | 
                                                        |
| 357 | - $this->logger->logException($e, [  | 
                                                        |
| 358 | - 'message' => 'Failed addUser attempt with exception.',  | 
                                                        |
| 359 | - 'level' => ILogger::ERROR,  | 
                                                        |
| 360 | - 'app' => 'ocs_api',  | 
                                                        |
| 361 | - ]);  | 
                                                        |
| 362 | -			throw new OCSException('Bad request', 101); | 
                                                        |
| 363 | - }  | 
                                                        |
| 364 | - }  | 
                                                        |
| 365 | -  | 
                                                        |
| 366 | - /**  | 
                                                        |
| 367 | - * @NoAdminRequired  | 
                                                        |
| 368 | - * @NoSubAdminRequired  | 
                                                        |
| 369 | - *  | 
                                                        |
| 370 | - * gets user info  | 
                                                        |
| 371 | - *  | 
                                                        |
| 372 | - * @param string $userId  | 
                                                        |
| 373 | - * @return DataResponse  | 
                                                        |
| 374 | - * @throws OCSException  | 
                                                        |
| 375 | - */  | 
                                                        |
| 376 | -	public function getUser(string $userId): DataResponse { | 
                                                        |
| 377 | - $data = $this->getUserData($userId);  | 
                                                        |
| 378 | - // getUserData returns empty array if not enough permissions  | 
                                                        |
| 379 | -		if (empty($data)) { | 
                                                        |
| 380 | -			throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); | 
                                                        |
| 381 | - }  | 
                                                        |
| 382 | - return new DataResponse($data);  | 
                                                        |
| 383 | - }  | 
                                                        |
| 384 | -  | 
                                                        |
| 385 | - /**  | 
                                                        |
| 386 | - * @NoAdminRequired  | 
                                                        |
| 387 | - * @NoSubAdminRequired  | 
                                                        |
| 388 | - *  | 
                                                        |
| 389 | - * gets user info from the currently logged in user  | 
                                                        |
| 390 | - *  | 
                                                        |
| 391 | - * @return DataResponse  | 
                                                        |
| 392 | - * @throws OCSException  | 
                                                        |
| 393 | - */  | 
                                                        |
| 394 | -	public function getCurrentUser(): DataResponse { | 
                                                        |
| 395 | - $user = $this->userSession->getUser();  | 
                                                        |
| 396 | -		if ($user) { | 
                                                        |
| 397 | - $data = $this->getUserData($user->getUID());  | 
                                                        |
| 398 | - // rename "displayname" to "display-name" only for this call to keep  | 
                                                        |
| 399 | - // the API stable.  | 
                                                        |
| 400 | - $data['display-name'] = $data['displayname'];  | 
                                                        |
| 401 | - unset($data['displayname']);  | 
                                                        |
| 402 | - return new DataResponse($data);  | 
                                                        |
| 403 | -  | 
                                                        |
| 404 | - }  | 
                                                        |
| 405 | -  | 
                                                        |
| 406 | -		throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); | 
                                                        |
| 407 | - }  | 
                                                        |
| 408 | -  | 
                                                        |
| 409 | - /**  | 
                                                        |
| 410 | - * @NoAdminRequired  | 
                                                        |
| 411 | - * @NoSubAdminRequired  | 
                                                        |
| 412 | - */  | 
                                                        |
| 413 | -	public function getEditableFields(): DataResponse { | 
                                                        |
| 414 | - $permittedFields = [];  | 
                                                        |
| 415 | -  | 
                                                        |
| 416 | - // Editing self (display, email)  | 
                                                        |
| 417 | -		if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) { | 
                                                        |
| 418 | - $permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME;  | 
                                                        |
| 419 | - $permittedFields[] = AccountManager::PROPERTY_EMAIL;  | 
                                                        |
| 420 | - }  | 
                                                        |
| 421 | -  | 
                                                        |
| 422 | -		if ($this->appManager->isEnabledForUser('federatedfilesharing')) { | 
                                                        |
| 423 | - $federatedFileSharing = $this->federatedFileSharingFactory->get();  | 
                                                        |
| 424 | - $shareProvider = $federatedFileSharing->getFederatedShareProvider();  | 
                                                        |
| 425 | -			if ($shareProvider->isLookupServerUploadEnabled()) { | 
                                                        |
| 426 | - $permittedFields[] = AccountManager::PROPERTY_PHONE;  | 
                                                        |
| 427 | - $permittedFields[] = AccountManager::PROPERTY_ADDRESS;  | 
                                                        |
| 428 | - $permittedFields[] = AccountManager::PROPERTY_WEBSITE;  | 
                                                        |
| 429 | - $permittedFields[] = AccountManager::PROPERTY_TWITTER;  | 
                                                        |
| 430 | - }  | 
                                                        |
| 431 | - }  | 
                                                        |
| 432 | -  | 
                                                        |
| 433 | - return new DataResponse($permittedFields);  | 
                                                        |
| 434 | - }  | 
                                                        |
| 435 | -  | 
                                                        |
| 436 | - /**  | 
                                                        |
| 437 | - * @NoAdminRequired  | 
                                                        |
| 438 | - * @NoSubAdminRequired  | 
                                                        |
| 439 | - * @PasswordConfirmationRequired  | 
                                                        |
| 440 | - *  | 
                                                        |
| 441 | - * edit users  | 
                                                        |
| 442 | - *  | 
                                                        |
| 443 | - * @param string $userId  | 
                                                        |
| 444 | - * @param string $key  | 
                                                        |
| 445 | - * @param string $value  | 
                                                        |
| 446 | - * @return DataResponse  | 
                                                        |
| 447 | - * @throws OCSException  | 
                                                        |
| 448 | - */  | 
                                                        |
| 449 | -	public function editUser(string $userId, string $key, string $value): DataResponse { | 
                                                        |
| 450 | - $currentLoggedInUser = $this->userSession->getUser();  | 
                                                        |
| 451 | -  | 
                                                        |
| 452 | - $targetUser = $this->userManager->get($userId);  | 
                                                        |
| 453 | -		if ($targetUser === null) { | 
                                                        |
| 454 | -			throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); | 
                                                        |
| 455 | - }  | 
                                                        |
| 456 | -  | 
                                                        |
| 457 | - $permittedFields = [];  | 
                                                        |
| 458 | -		if ($targetUser->getUID() === $currentLoggedInUser->getUID()) { | 
                                                        |
| 459 | - // Editing self (display, email)  | 
                                                        |
| 460 | -			if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) { | 
                                                        |
| 461 | - $permittedFields[] = 'display';  | 
                                                        |
| 462 | - $permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME;  | 
                                                        |
| 463 | - $permittedFields[] = AccountManager::PROPERTY_EMAIL;  | 
                                                        |
| 464 | - }  | 
                                                        |
| 465 | -  | 
                                                        |
| 466 | - $permittedFields[] = 'password';  | 
                                                        |
| 467 | -			if ($this->config->getSystemValue('force_language', false) === false || | 
                                                        |
| 468 | -				$this->groupManager->isAdmin($currentLoggedInUser->getUID())) { | 
                                                        |
| 469 | - $permittedFields[] = 'language';  | 
                                                        |
| 470 | - }  | 
                                                        |
| 471 | -  | 
                                                        |
| 472 | -			if ($this->config->getSystemValue('force_locale', false) === false || | 
                                                        |
| 473 | -				$this->groupManager->isAdmin($currentLoggedInUser->getUID())) { | 
                                                        |
| 474 | - $permittedFields[] = 'locale';  | 
                                                        |
| 475 | - }  | 
                                                        |
| 476 | -  | 
                                                        |
| 477 | -			if ($this->appManager->isEnabledForUser('federatedfilesharing')) { | 
                                                        |
| 478 | - $federatedFileSharing = new \OCA\FederatedFileSharing\AppInfo\Application();  | 
                                                        |
| 479 | - $shareProvider = $federatedFileSharing->getFederatedShareProvider();  | 
                                                        |
| 480 | -				if ($shareProvider->isLookupServerUploadEnabled()) { | 
                                                        |
| 481 | - $permittedFields[] = AccountManager::PROPERTY_PHONE;  | 
                                                        |
| 482 | - $permittedFields[] = AccountManager::PROPERTY_ADDRESS;  | 
                                                        |
| 483 | - $permittedFields[] = AccountManager::PROPERTY_WEBSITE;  | 
                                                        |
| 484 | - $permittedFields[] = AccountManager::PROPERTY_TWITTER;  | 
                                                        |
| 485 | - }  | 
                                                        |
| 486 | - }  | 
                                                        |
| 487 | -  | 
                                                        |
| 488 | - // If admin they can edit their own quota  | 
                                                        |
| 489 | -			if ($this->groupManager->isAdmin($currentLoggedInUser->getUID())) { | 
                                                        |
| 490 | - $permittedFields[] = 'quota';  | 
                                                        |
| 491 | - }  | 
                                                        |
| 492 | -		} else { | 
                                                        |
| 493 | - // Check if admin / subadmin  | 
                                                        |
| 494 | - $subAdminManager = $this->groupManager->getSubAdmin();  | 
                                                        |
| 495 | - if ($subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)  | 
                                                        |
| 496 | -			|| $this->groupManager->isAdmin($currentLoggedInUser->getUID())) { | 
                                                        |
| 497 | - // They have permissions over the user  | 
                                                        |
| 498 | - $permittedFields[] = 'display';  | 
                                                        |
| 499 | - $permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME;  | 
                                                        |
| 500 | - $permittedFields[] = AccountManager::PROPERTY_EMAIL;  | 
                                                        |
| 501 | - $permittedFields[] = 'password';  | 
                                                        |
| 502 | - $permittedFields[] = 'language';  | 
                                                        |
| 503 | - $permittedFields[] = 'locale';  | 
                                                        |
| 504 | - $permittedFields[] = AccountManager::PROPERTY_PHONE;  | 
                                                        |
| 505 | - $permittedFields[] = AccountManager::PROPERTY_ADDRESS;  | 
                                                        |
| 506 | - $permittedFields[] = AccountManager::PROPERTY_WEBSITE;  | 
                                                        |
| 507 | - $permittedFields[] = AccountManager::PROPERTY_TWITTER;  | 
                                                        |
| 508 | - $permittedFields[] = 'quota';  | 
                                                        |
| 509 | -			} else { | 
                                                        |
| 510 | - // No rights  | 
                                                        |
| 511 | -				throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); | 
                                                        |
| 512 | - }  | 
                                                        |
| 513 | - }  | 
                                                        |
| 514 | - // Check if permitted to edit this field  | 
                                                        |
| 515 | -		if (!in_array($key, $permittedFields)) { | 
                                                        |
| 516 | -			throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); | 
                                                        |
| 517 | - }  | 
                                                        |
| 518 | - // Process the edit  | 
                                                        |
| 519 | -		switch($key) { | 
                                                        |
| 520 | - case 'display':  | 
                                                        |
| 521 | - case AccountManager::PROPERTY_DISPLAYNAME:  | 
                                                        |
| 522 | - $targetUser->setDisplayName($value);  | 
                                                        |
| 523 | - break;  | 
                                                        |
| 524 | - case 'quota':  | 
                                                        |
| 525 | - $quota = $value;  | 
                                                        |
| 526 | -				if ($quota !== 'none' && $quota !== 'default') { | 
                                                        |
| 527 | -					if (is_numeric($quota)) { | 
                                                        |
| 528 | - $quota = (float) $quota;  | 
                                                        |
| 529 | -					} else { | 
                                                        |
| 530 | - $quota = \OCP\Util::computerFileSize($quota);  | 
                                                        |
| 531 | - }  | 
                                                        |
| 532 | -					if ($quota === false) { | 
                                                        |
| 533 | -						throw new OCSException('Invalid quota value '.$value, 103); | 
                                                        |
| 534 | - }  | 
                                                        |
| 535 | -					if ($quota === -1) { | 
                                                        |
| 536 | - $quota = 'none';  | 
                                                        |
| 537 | -					} else { | 
                                                        |
| 538 | - $quota = \OCP\Util::humanFileSize($quota);  | 
                                                        |
| 539 | - }  | 
                                                        |
| 540 | - }  | 
                                                        |
| 541 | - $targetUser->setQuota($quota);  | 
                                                        |
| 542 | - break;  | 
                                                        |
| 543 | - case 'password':  | 
                                                        |
| 544 | -				try { | 
                                                        |
| 545 | -					if (!$targetUser->canChangePassword()) { | 
                                                        |
| 546 | -						throw new OCSException('Setting the password is not supported by the users backend', 103); | 
                                                        |
| 547 | - }  | 
                                                        |
| 548 | - $targetUser->setPassword($value);  | 
                                                        |
| 549 | -				} catch (HintException $e) { // password policy error | 
                                                        |
| 550 | - throw new OCSException($e->getMessage(), 103);  | 
                                                        |
| 551 | - }  | 
                                                        |
| 552 | - break;  | 
                                                        |
| 553 | - case 'language':  | 
                                                        |
| 554 | - $languagesCodes = $this->l10nFactory->findAvailableLanguages();  | 
                                                        |
| 555 | -				if (!in_array($value, $languagesCodes, true) && $value !== 'en') { | 
                                                        |
| 556 | -					throw new OCSException('Invalid language', 102); | 
                                                        |
| 557 | - }  | 
                                                        |
| 558 | - $this->config->setUserValue($targetUser->getUID(), 'core', 'lang', $value);  | 
                                                        |
| 559 | - break;  | 
                                                        |
| 560 | - case 'locale':  | 
                                                        |
| 561 | -				if (!$this->l10nFactory->localeExists($value)) { | 
                                                        |
| 562 | -					throw new OCSException('Invalid locale', 102); | 
                                                        |
| 563 | - }  | 
                                                        |
| 564 | - $this->config->setUserValue($targetUser->getUID(), 'core', 'locale', $value);  | 
                                                        |
| 565 | - break;  | 
                                                        |
| 566 | - case AccountManager::PROPERTY_EMAIL:  | 
                                                        |
| 567 | -				if (filter_var($value, FILTER_VALIDATE_EMAIL) || $value === '') { | 
                                                        |
| 568 | - $targetUser->setEMailAddress($value);  | 
                                                        |
| 569 | -				} else { | 
                                                        |
| 570 | -					throw new OCSException('', 102); | 
                                                        |
| 571 | - }  | 
                                                        |
| 572 | - break;  | 
                                                        |
| 573 | - case AccountManager::PROPERTY_PHONE:  | 
                                                        |
| 574 | - case AccountManager::PROPERTY_ADDRESS:  | 
                                                        |
| 575 | - case AccountManager::PROPERTY_WEBSITE:  | 
                                                        |
| 576 | - case AccountManager::PROPERTY_TWITTER:  | 
                                                        |
| 577 | - $userAccount = $this->accountManager->getUser($targetUser);  | 
                                                        |
| 578 | -				if ($userAccount[$key]['value'] !== $value) { | 
                                                        |
| 579 | - $userAccount[$key]['value'] = $value;  | 
                                                        |
| 580 | - $this->accountManager->updateUser($targetUser, $userAccount);  | 
                                                        |
| 581 | - }  | 
                                                        |
| 582 | - break;  | 
                                                        |
| 583 | - default:  | 
                                                        |
| 584 | -				throw new OCSException('', 103); | 
                                                        |
| 585 | - }  | 
                                                        |
| 586 | - return new DataResponse();  | 
                                                        |
| 587 | - }  | 
                                                        |
| 588 | -  | 
                                                        |
| 589 | - /**  | 
                                                        |
| 590 | - * @PasswordConfirmationRequired  | 
                                                        |
| 591 | - * @NoAdminRequired  | 
                                                        |
| 592 | - *  | 
                                                        |
| 593 | - * @param string $userId  | 
                                                        |
| 594 | - * @return DataResponse  | 
                                                        |
| 595 | - * @throws OCSException  | 
                                                        |
| 596 | - */  | 
                                                        |
| 597 | -	public function deleteUser(string $userId): DataResponse { | 
                                                        |
| 598 | - $currentLoggedInUser = $this->userSession->getUser();  | 
                                                        |
| 599 | -  | 
                                                        |
| 600 | - $targetUser = $this->userManager->get($userId);  | 
                                                        |
| 601 | -  | 
                                                        |
| 602 | -		if ($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) { | 
                                                        |
| 603 | -			throw new OCSException('', 101); | 
                                                        |
| 604 | - }  | 
                                                        |
| 605 | -  | 
                                                        |
| 606 | - // If not permitted  | 
                                                        |
| 607 | - $subAdminManager = $this->groupManager->getSubAdmin();  | 
                                                        |
| 608 | -		if (!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) { | 
                                                        |
| 609 | -			throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); | 
                                                        |
| 610 | - }  | 
                                                        |
| 611 | -  | 
                                                        |
| 612 | - // Go ahead with the delete  | 
                                                        |
| 613 | -		if ($targetUser->delete()) { | 
                                                        |
| 614 | - return new DataResponse();  | 
                                                        |
| 615 | -		} else { | 
                                                        |
| 616 | -			throw new OCSException('', 101); | 
                                                        |
| 617 | - }  | 
                                                        |
| 618 | - }  | 
                                                        |
| 619 | -  | 
                                                        |
| 620 | - /**  | 
                                                        |
| 621 | - * @PasswordConfirmationRequired  | 
                                                        |
| 622 | - * @NoAdminRequired  | 
                                                        |
| 623 | - *  | 
                                                        |
| 624 | - * @param string $userId  | 
                                                        |
| 625 | - * @return DataResponse  | 
                                                        |
| 626 | - * @throws OCSException  | 
                                                        |
| 627 | - * @throws OCSForbiddenException  | 
                                                        |
| 628 | - */  | 
                                                        |
| 629 | -	public function disableUser(string $userId): DataResponse { | 
                                                        |
| 630 | - return $this->setEnabled($userId, false);  | 
                                                        |
| 631 | - }  | 
                                                        |
| 632 | -  | 
                                                        |
| 633 | - /**  | 
                                                        |
| 634 | - * @PasswordConfirmationRequired  | 
                                                        |
| 635 | - * @NoAdminRequired  | 
                                                        |
| 636 | - *  | 
                                                        |
| 637 | - * @param string $userId  | 
                                                        |
| 638 | - * @return DataResponse  | 
                                                        |
| 639 | - * @throws OCSException  | 
                                                        |
| 640 | - * @throws OCSForbiddenException  | 
                                                        |
| 641 | - */  | 
                                                        |
| 642 | -	public function enableUser(string $userId): DataResponse { | 
                                                        |
| 643 | - return $this->setEnabled($userId, true);  | 
                                                        |
| 644 | - }  | 
                                                        |
| 645 | -  | 
                                                        |
| 646 | - /**  | 
                                                        |
| 647 | - * @param string $userId  | 
                                                        |
| 648 | - * @param bool $value  | 
                                                        |
| 649 | - * @return DataResponse  | 
                                                        |
| 650 | - * @throws OCSException  | 
                                                        |
| 651 | - */  | 
                                                        |
| 652 | -	private function setEnabled(string $userId, bool $value): DataResponse { | 
                                                        |
| 653 | - $currentLoggedInUser = $this->userSession->getUser();  | 
                                                        |
| 654 | -  | 
                                                        |
| 655 | - $targetUser = $this->userManager->get($userId);  | 
                                                        |
| 656 | -		if ($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) { | 
                                                        |
| 657 | -			throw new OCSException('', 101); | 
                                                        |
| 658 | - }  | 
                                                        |
| 659 | -  | 
                                                        |
| 660 | - // If not permitted  | 
                                                        |
| 661 | - $subAdminManager = $this->groupManager->getSubAdmin();  | 
                                                        |
| 662 | -		if (!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) { | 
                                                        |
| 663 | -			throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); | 
                                                        |
| 664 | - }  | 
                                                        |
| 665 | -  | 
                                                        |
| 666 | - // enable/disable the user now  | 
                                                        |
| 667 | - $targetUser->setEnabled($value);  | 
                                                        |
| 668 | - return new DataResponse();  | 
                                                        |
| 669 | - }  | 
                                                        |
| 670 | -  | 
                                                        |
| 671 | - /**  | 
                                                        |
| 672 | - * @NoAdminRequired  | 
                                                        |
| 673 | - * @NoSubAdminRequired  | 
                                                        |
| 674 | - *  | 
                                                        |
| 675 | - * @param string $userId  | 
                                                        |
| 676 | - * @return DataResponse  | 
                                                        |
| 677 | - * @throws OCSException  | 
                                                        |
| 678 | - */  | 
                                                        |
| 679 | -	public function getUsersGroups(string $userId): DataResponse { | 
                                                        |
| 680 | - $loggedInUser = $this->userSession->getUser();  | 
                                                        |
| 681 | -  | 
                                                        |
| 682 | - $targetUser = $this->userManager->get($userId);  | 
                                                        |
| 683 | -		if ($targetUser === null) { | 
                                                        |
| 684 | -			throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND); | 
                                                        |
| 685 | - }  | 
                                                        |
| 686 | -  | 
                                                        |
| 687 | -		if ($targetUser->getUID() === $loggedInUser->getUID() || $this->groupManager->isAdmin($loggedInUser->getUID())) { | 
                                                        |
| 688 | - // Self lookup or admin lookup  | 
                                                        |
| 689 | - return new DataResponse([  | 
                                                        |
| 690 | - 'groups' => $this->groupManager->getUserGroupIds($targetUser)  | 
                                                        |
| 691 | - ]);  | 
                                                        |
| 692 | -		} else { | 
                                                        |
| 693 | - $subAdminManager = $this->groupManager->getSubAdmin();  | 
                                                        |
| 694 | -  | 
                                                        |
| 695 | - // Looking up someone else  | 
                                                        |
| 696 | -			if ($subAdminManager->isUserAccessible($loggedInUser, $targetUser)) { | 
                                                        |
| 697 | - // Return the group that the method caller is subadmin of for the user in question  | 
                                                        |
| 698 | - /** @var IGroup[] $getSubAdminsGroups */  | 
                                                        |
| 699 | - $getSubAdminsGroups = $subAdminManager->getSubAdminsGroups($loggedInUser);  | 
                                                        |
| 700 | -				foreach ($getSubAdminsGroups as $key => $group) { | 
                                                        |
| 701 | - $getSubAdminsGroups[$key] = $group->getGID();  | 
                                                        |
| 702 | - }  | 
                                                        |
| 703 | - $groups = array_intersect(  | 
                                                        |
| 704 | - $getSubAdminsGroups,  | 
                                                        |
| 705 | - $this->groupManager->getUserGroupIds($targetUser)  | 
                                                        |
| 706 | - );  | 
                                                        |
| 707 | - return new DataResponse(['groups' => $groups]);  | 
                                                        |
| 708 | -			} else { | 
                                                        |
| 709 | - // Not permitted  | 
                                                        |
| 710 | -				throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); | 
                                                        |
| 711 | - }  | 
                                                        |
| 712 | - }  | 
                                                        |
| 713 | -  | 
                                                        |
| 714 | - }  | 
                                                        |
| 715 | -  | 
                                                        |
| 716 | - /**  | 
                                                        |
| 717 | - * @PasswordConfirmationRequired  | 
                                                        |
| 718 | - * @NoAdminRequired  | 
                                                        |
| 719 | - *  | 
                                                        |
| 720 | - * @param string $userId  | 
                                                        |
| 721 | - * @param string $groupid  | 
                                                        |
| 722 | - * @return DataResponse  | 
                                                        |
| 723 | - * @throws OCSException  | 
                                                        |
| 724 | - */  | 
                                                        |
| 725 | -	public function addToGroup(string $userId, string $groupid = ''): DataResponse { | 
                                                        |
| 726 | -		if ($groupid === '') { | 
                                                        |
| 727 | -			throw new OCSException('', 101); | 
                                                        |
| 728 | - }  | 
                                                        |
| 729 | -  | 
                                                        |
| 730 | - $group = $this->groupManager->get($groupid);  | 
                                                        |
| 731 | - $targetUser = $this->userManager->get($userId);  | 
                                                        |
| 732 | -		if ($group === null) { | 
                                                        |
| 733 | -			throw new OCSException('', 102); | 
                                                        |
| 734 | - }  | 
                                                        |
| 735 | -		if ($targetUser === null) { | 
                                                        |
| 736 | -			throw new OCSException('', 103); | 
                                                        |
| 737 | - }  | 
                                                        |
| 738 | -  | 
                                                        |
| 739 | - // If they're not an admin, check they are a subadmin of the group in question  | 
                                                        |
| 740 | - $loggedInUser = $this->userSession->getUser();  | 
                                                        |
| 741 | - $subAdminManager = $this->groupManager->getSubAdmin();  | 
                                                        |
| 742 | -		if (!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminOfGroup($loggedInUser, $group)) { | 
                                                        |
| 743 | -			throw new OCSException('', 104); | 
                                                        |
| 744 | - }  | 
                                                        |
| 745 | -  | 
                                                        |
| 746 | - // Add user to group  | 
                                                        |
| 747 | - $group->addUser($targetUser);  | 
                                                        |
| 748 | - return new DataResponse();  | 
                                                        |
| 749 | - }  | 
                                                        |
| 750 | -  | 
                                                        |
| 751 | - /**  | 
                                                        |
| 752 | - * @PasswordConfirmationRequired  | 
                                                        |
| 753 | - * @NoAdminRequired  | 
                                                        |
| 754 | - *  | 
                                                        |
| 755 | - * @param string $userId  | 
                                                        |
| 756 | - * @param string $groupid  | 
                                                        |
| 757 | - * @return DataResponse  | 
                                                        |
| 758 | - * @throws OCSException  | 
                                                        |
| 759 | - */  | 
                                                        |
| 760 | -	public function removeFromGroup(string $userId, string $groupid): DataResponse { | 
                                                        |
| 761 | - $loggedInUser = $this->userSession->getUser();  | 
                                                        |
| 762 | -  | 
                                                        |
| 763 | -		if ($groupid === null || trim($groupid) === '') { | 
                                                        |
| 764 | -			throw new OCSException('', 101); | 
                                                        |
| 765 | - }  | 
                                                        |
| 766 | -  | 
                                                        |
| 767 | - $group = $this->groupManager->get($groupid);  | 
                                                        |
| 768 | -		if ($group === null) { | 
                                                        |
| 769 | -			throw new OCSException('', 102); | 
                                                        |
| 770 | - }  | 
                                                        |
| 771 | -  | 
                                                        |
| 772 | - $targetUser = $this->userManager->get($userId);  | 
                                                        |
| 773 | -		if ($targetUser === null) { | 
                                                        |
| 774 | -			throw new OCSException('', 103); | 
                                                        |
| 775 | - }  | 
                                                        |
| 776 | -  | 
                                                        |
| 777 | - // If they're not an admin, check they are a subadmin of the group in question  | 
                                                        |
| 778 | - $subAdminManager = $this->groupManager->getSubAdmin();  | 
                                                        |
| 779 | -		if (!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminOfGroup($loggedInUser, $group)) { | 
                                                        |
| 780 | -			throw new OCSException('', 104); | 
                                                        |
| 781 | - }  | 
                                                        |
| 782 | -  | 
                                                        |
| 783 | - // Check they aren't removing themselves from 'admin' or their 'subadmin; group  | 
                                                        |
| 784 | -		if ($targetUser->getUID() === $loggedInUser->getUID()) { | 
                                                        |
| 785 | -			if ($this->groupManager->isAdmin($loggedInUser->getUID())) { | 
                                                        |
| 786 | -				if ($group->getGID() === 'admin') { | 
                                                        |
| 787 | -					throw new OCSException('Cannot remove yourself from the admin group', 105); | 
                                                        |
| 788 | - }  | 
                                                        |
| 789 | -			} else { | 
                                                        |
| 790 | - // Not an admin, so the user must be a subadmin of this group, but that is not allowed.  | 
                                                        |
| 791 | -				throw new OCSException('Cannot remove yourself from this group as you are a SubAdmin', 105); | 
                                                        |
| 792 | - }  | 
                                                        |
| 793 | -  | 
                                                        |
| 794 | -		} else if (!$this->groupManager->isAdmin($loggedInUser->getUID())) { | 
                                                        |
| 795 | - /** @var IGroup[] $subAdminGroups */  | 
                                                        |
| 796 | - $subAdminGroups = $subAdminManager->getSubAdminsGroups($loggedInUser);  | 
                                                        |
| 797 | -			$subAdminGroups = array_map(function (IGroup $subAdminGroup) { | 
                                                        |
| 798 | - return $subAdminGroup->getGID();  | 
                                                        |
| 799 | - }, $subAdminGroups);  | 
                                                        |
| 800 | - $userGroups = $this->groupManager->getUserGroupIds($targetUser);  | 
                                                        |
| 801 | - $userSubAdminGroups = array_intersect($subAdminGroups, $userGroups);  | 
                                                        |
| 802 | -  | 
                                                        |
| 803 | -			if (count($userSubAdminGroups) <= 1) { | 
                                                        |
| 804 | - // Subadmin must not be able to remove a user from all their subadmin groups.  | 
                                                        |
| 805 | -				throw new OCSException('Not viable to remove user from the last group you are SubAdmin of', 105); | 
                                                        |
| 806 | - }  | 
                                                        |
| 807 | - }  | 
                                                        |
| 808 | -  | 
                                                        |
| 809 | - // Remove user from group  | 
                                                        |
| 810 | - $group->removeUser($targetUser);  | 
                                                        |
| 811 | - return new DataResponse();  | 
                                                        |
| 812 | - }  | 
                                                        |
| 813 | -  | 
                                                        |
| 814 | - /**  | 
                                                        |
| 815 | - * Creates a subadmin  | 
                                                        |
| 816 | - *  | 
                                                        |
| 817 | - * @PasswordConfirmationRequired  | 
                                                        |
| 818 | - *  | 
                                                        |
| 819 | - * @param string $userId  | 
                                                        |
| 820 | - * @param string $groupid  | 
                                                        |
| 821 | - * @return DataResponse  | 
                                                        |
| 822 | - * @throws OCSException  | 
                                                        |
| 823 | - */  | 
                                                        |
| 824 | -	public function addSubAdmin(string $userId, string $groupid): DataResponse { | 
                                                        |
| 825 | - $group = $this->groupManager->get($groupid);  | 
                                                        |
| 826 | - $user = $this->userManager->get($userId);  | 
                                                        |
| 827 | -  | 
                                                        |
| 828 | - // Check if the user exists  | 
                                                        |
| 829 | -		if ($user === null) { | 
                                                        |
| 830 | -			throw new OCSException('User does not exist', 101); | 
                                                        |
| 831 | - }  | 
                                                        |
| 832 | - // Check if group exists  | 
                                                        |
| 833 | -		if ($group === null) { | 
                                                        |
| 834 | -			throw new OCSException('Group does not exist',  102); | 
                                                        |
| 835 | - }  | 
                                                        |
| 836 | - // Check if trying to make subadmin of admin group  | 
                                                        |
| 837 | -		if ($group->getGID() === 'admin') { | 
                                                        |
| 838 | -			throw new OCSException('Cannot create subadmins for admin group', 103); | 
                                                        |
| 839 | - }  | 
                                                        |
| 840 | -  | 
                                                        |
| 841 | - $subAdminManager = $this->groupManager->getSubAdmin();  | 
                                                        |
| 842 | -  | 
                                                        |
| 843 | - // We cannot be subadmin twice  | 
                                                        |
| 844 | -		if ($subAdminManager->isSubAdminOfGroup($user, $group)) { | 
                                                        |
| 845 | - return new DataResponse();  | 
                                                        |
| 846 | - }  | 
                                                        |
| 847 | - // Go  | 
                                                        |
| 848 | - $subAdminManager->createSubAdmin($user, $group);  | 
                                                        |
| 849 | - return new DataResponse();  | 
                                                        |
| 850 | - }  | 
                                                        |
| 851 | -  | 
                                                        |
| 852 | - /**  | 
                                                        |
| 853 | - * Removes a subadmin from a group  | 
                                                        |
| 854 | - *  | 
                                                        |
| 855 | - * @PasswordConfirmationRequired  | 
                                                        |
| 856 | - *  | 
                                                        |
| 857 | - * @param string $userId  | 
                                                        |
| 858 | - * @param string $groupid  | 
                                                        |
| 859 | - * @return DataResponse  | 
                                                        |
| 860 | - * @throws OCSException  | 
                                                        |
| 861 | - */  | 
                                                        |
| 862 | -	public function removeSubAdmin(string $userId, string $groupid): DataResponse { | 
                                                        |
| 863 | - $group = $this->groupManager->get($groupid);  | 
                                                        |
| 864 | - $user = $this->userManager->get($userId);  | 
                                                        |
| 865 | - $subAdminManager = $this->groupManager->getSubAdmin();  | 
                                                        |
| 866 | -  | 
                                                        |
| 867 | - // Check if the user exists  | 
                                                        |
| 868 | -		if ($user === null) { | 
                                                        |
| 869 | -			throw new OCSException('User does not exist', 101); | 
                                                        |
| 870 | - }  | 
                                                        |
| 871 | - // Check if the group exists  | 
                                                        |
| 872 | -		if ($group === null) { | 
                                                        |
| 873 | -			throw new OCSException('Group does not exist', 101); | 
                                                        |
| 874 | - }  | 
                                                        |
| 875 | - // Check if they are a subadmin of this said group  | 
                                                        |
| 876 | -		if (!$subAdminManager->isSubAdminOfGroup($user, $group)) { | 
                                                        |
| 877 | -			throw new OCSException('User is not a subadmin of this group', 102); | 
                                                        |
| 878 | - }  | 
                                                        |
| 879 | -  | 
                                                        |
| 880 | - // Go  | 
                                                        |
| 881 | - $subAdminManager->deleteSubAdmin($user, $group);  | 
                                                        |
| 882 | - return new DataResponse();  | 
                                                        |
| 883 | - }  | 
                                                        |
| 884 | -  | 
                                                        |
| 885 | - /**  | 
                                                        |
| 886 | - * Get the groups a user is a subadmin of  | 
                                                        |
| 887 | - *  | 
                                                        |
| 888 | - * @param string $userId  | 
                                                        |
| 889 | - * @return DataResponse  | 
                                                        |
| 890 | - * @throws OCSException  | 
                                                        |
| 891 | - */  | 
                                                        |
| 892 | -	public function getUserSubAdminGroups(string $userId): DataResponse { | 
                                                        |
| 893 | - $groups = $this->getUserSubAdminGroupsData($userId);  | 
                                                        |
| 894 | - return new DataResponse($groups);  | 
                                                        |
| 895 | - }  | 
                                                        |
| 896 | -  | 
                                                        |
| 897 | - /**  | 
                                                        |
| 898 | - * @NoAdminRequired  | 
                                                        |
| 899 | - * @PasswordConfirmationRequired  | 
                                                        |
| 900 | - *  | 
                                                        |
| 901 | - * resend welcome message  | 
                                                        |
| 902 | - *  | 
                                                        |
| 903 | - * @param string $userId  | 
                                                        |
| 904 | - * @return DataResponse  | 
                                                        |
| 905 | - * @throws OCSException  | 
                                                        |
| 906 | - */  | 
                                                        |
| 907 | -	public function resendWelcomeMessage(string $userId): DataResponse { | 
                                                        |
| 908 | - $currentLoggedInUser = $this->userSession->getUser();  | 
                                                        |
| 909 | -  | 
                                                        |
| 910 | - $targetUser = $this->userManager->get($userId);  | 
                                                        |
| 911 | -		if ($targetUser === null) { | 
                                                        |
| 912 | -			throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND); | 
                                                        |
| 913 | - }  | 
                                                        |
| 914 | -  | 
                                                        |
| 915 | - // Check if admin / subadmin  | 
                                                        |
| 916 | - $subAdminManager = $this->groupManager->getSubAdmin();  | 
                                                        |
| 917 | - if (!$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)  | 
                                                        |
| 918 | -			&& !$this->groupManager->isAdmin($currentLoggedInUser->getUID())) { | 
                                                        |
| 919 | - // No rights  | 
                                                        |
| 920 | -			throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); | 
                                                        |
| 921 | - }  | 
                                                        |
| 922 | -  | 
                                                        |
| 923 | - $email = $targetUser->getEMailAddress();  | 
                                                        |
| 924 | -		if ($email === '' || $email === null) { | 
                                                        |
| 925 | -			throw new OCSException('Email address not available', 101); | 
                                                        |
| 926 | - }  | 
                                                        |
| 927 | -  | 
                                                        |
| 928 | -		try { | 
                                                        |
| 929 | - $emailTemplate = $this->newUserMailHelper->generateTemplate($targetUser, false);  | 
                                                        |
| 930 | - $this->newUserMailHelper->sendMail($targetUser, $emailTemplate);  | 
                                                        |
| 931 | -		} catch(\Exception $e) { | 
                                                        |
| 932 | - $this->logger->logException($e, [  | 
                                                        |
| 933 | - 'message' => "Can't send new user mail to $email",  | 
                                                        |
| 934 | - 'level' => ILogger::ERROR,  | 
                                                        |
| 935 | - 'app' => 'settings',  | 
                                                        |
| 936 | - ]);  | 
                                                        |
| 937 | -			throw new OCSException('Sending email failed', 102); | 
                                                        |
| 938 | - }  | 
                                                        |
| 939 | -  | 
                                                        |
| 940 | - return new DataResponse();  | 
                                                        |
| 941 | - }  | 
                                                        |
| 56 | + /** @var IAppManager */  | 
                                                        |
| 57 | + private $appManager;  | 
                                                        |
| 58 | + /** @var ILogger */  | 
                                                        |
| 59 | + private $logger;  | 
                                                        |
| 60 | + /** @var IFactory */  | 
                                                        |
| 61 | + private $l10nFactory;  | 
                                                        |
| 62 | + /** @var NewUserMailHelper */  | 
                                                        |
| 63 | + private $newUserMailHelper;  | 
                                                        |
| 64 | + /** @var FederatedFileSharingFactory */  | 
                                                        |
| 65 | + private $federatedFileSharingFactory;  | 
                                                        |
| 66 | + /** @var ISecureRandom */  | 
                                                        |
| 67 | + private $secureRandom;  | 
                                                        |
| 68 | +  | 
                                                        |
| 69 | + /**  | 
                                                        |
| 70 | + * @param string $appName  | 
                                                        |
| 71 | + * @param IRequest $request  | 
                                                        |
| 72 | + * @param IUserManager $userManager  | 
                                                        |
| 73 | + * @param IConfig $config  | 
                                                        |
| 74 | + * @param IAppManager $appManager  | 
                                                        |
| 75 | + * @param IGroupManager $groupManager  | 
                                                        |
| 76 | + * @param IUserSession $userSession  | 
                                                        |
| 77 | + * @param AccountManager $accountManager  | 
                                                        |
| 78 | + * @param ILogger $logger  | 
                                                        |
| 79 | + * @param IFactory $l10nFactory  | 
                                                        |
| 80 | + * @param NewUserMailHelper $newUserMailHelper  | 
                                                        |
| 81 | + * @param FederatedFileSharingFactory $federatedFileSharingFactory  | 
                                                        |
| 82 | + * @param ISecureRandom $secureRandom  | 
                                                        |
| 83 | + */  | 
                                                        |
| 84 | + public function __construct(string $appName,  | 
                                                        |
| 85 | + IRequest $request,  | 
                                                        |
| 86 | + IUserManager $userManager,  | 
                                                        |
| 87 | + IConfig $config,  | 
                                                        |
| 88 | + IAppManager $appManager,  | 
                                                        |
| 89 | + IGroupManager $groupManager,  | 
                                                        |
| 90 | + IUserSession $userSession,  | 
                                                        |
| 91 | + AccountManager $accountManager,  | 
                                                        |
| 92 | + ILogger $logger,  | 
                                                        |
| 93 | + IFactory $l10nFactory,  | 
                                                        |
| 94 | + NewUserMailHelper $newUserMailHelper,  | 
                                                        |
| 95 | + FederatedFileSharingFactory $federatedFileSharingFactory,  | 
                                                        |
| 96 | +                                ISecureRandom $secureRandom) { | 
                                                        |
| 97 | + parent::__construct($appName,  | 
                                                        |
| 98 | + $request,  | 
                                                        |
| 99 | + $userManager,  | 
                                                        |
| 100 | + $config,  | 
                                                        |
| 101 | + $groupManager,  | 
                                                        |
| 102 | + $userSession,  | 
                                                        |
| 103 | + $accountManager);  | 
                                                        |
| 104 | +  | 
                                                        |
| 105 | + $this->appManager = $appManager;  | 
                                                        |
| 106 | + $this->logger = $logger;  | 
                                                        |
| 107 | + $this->l10nFactory = $l10nFactory;  | 
                                                        |
| 108 | + $this->newUserMailHelper = $newUserMailHelper;  | 
                                                        |
| 109 | + $this->federatedFileSharingFactory = $federatedFileSharingFactory;  | 
                                                        |
| 110 | + $this->secureRandom = $secureRandom;  | 
                                                        |
| 111 | + }  | 
                                                        |
| 112 | +  | 
                                                        |
| 113 | + /**  | 
                                                        |
| 114 | + * @NoAdminRequired  | 
                                                        |
| 115 | + *  | 
                                                        |
| 116 | + * returns a list of users  | 
                                                        |
| 117 | + *  | 
                                                        |
| 118 | + * @param string $search  | 
                                                        |
| 119 | + * @param int $limit  | 
                                                        |
| 120 | + * @param int $offset  | 
                                                        |
| 121 | + * @return DataResponse  | 
                                                        |
| 122 | + */  | 
                                                        |
| 123 | +    public function getUsers(string $search = '', $limit = null, $offset = 0): DataResponse { | 
                                                        |
| 124 | + $user = $this->userSession->getUser();  | 
                                                        |
| 125 | + $users = [];  | 
                                                        |
| 126 | +  | 
                                                        |
| 127 | + // Admin? Or SubAdmin?  | 
                                                        |
| 128 | + $uid = $user->getUID();  | 
                                                        |
| 129 | + $subAdminManager = $this->groupManager->getSubAdmin();  | 
                                                        |
| 130 | +        if ($this->groupManager->isAdmin($uid)){ | 
                                                        |
| 131 | + $users = $this->userManager->search($search, $limit, $offset);  | 
                                                        |
| 132 | +        } else if ($subAdminManager->isSubAdmin($user)) { | 
                                                        |
| 133 | + $subAdminOfGroups = $subAdminManager->getSubAdminsGroups($user);  | 
                                                        |
| 134 | +            foreach ($subAdminOfGroups as $key => $group) { | 
                                                        |
| 135 | + $subAdminOfGroups[$key] = $group->getGID();  | 
                                                        |
| 136 | + }  | 
                                                        |
| 137 | +  | 
                                                        |
| 138 | + $users = [];  | 
                                                        |
| 139 | +            foreach ($subAdminOfGroups as $group) { | 
                                                        |
| 140 | + $users = array_merge($users, $this->groupManager->displayNamesInGroup($group, $search, $limit, $offset));  | 
                                                        |
| 141 | + }  | 
                                                        |
| 142 | + }  | 
                                                        |
| 143 | +  | 
                                                        |
| 144 | + $users = array_keys($users);  | 
                                                        |
| 145 | +  | 
                                                        |
| 146 | + return new DataResponse([  | 
                                                        |
| 147 | + 'users' => $users  | 
                                                        |
| 148 | + ]);  | 
                                                        |
| 149 | + }  | 
                                                        |
| 150 | +  | 
                                                        |
| 151 | + /**  | 
                                                        |
| 152 | + * @NoAdminRequired  | 
                                                        |
| 153 | + *  | 
                                                        |
| 154 | + * returns a list of users and their data  | 
                                                        |
| 155 | + */  | 
                                                        |
| 156 | +    public function getUsersDetails(string $search = '', $limit = null, $offset = 0): DataResponse { | 
                                                        |
| 157 | + $currentUser = $this->userSession->getUser();  | 
                                                        |
| 158 | + $users = [];  | 
                                                        |
| 159 | +  | 
                                                        |
| 160 | + // Admin? Or SubAdmin?  | 
                                                        |
| 161 | + $uid = $currentUser->getUID();  | 
                                                        |
| 162 | + $subAdminManager = $this->groupManager->getSubAdmin();  | 
                                                        |
| 163 | +        if ($this->groupManager->isAdmin($uid)){ | 
                                                        |
| 164 | + $users = $this->userManager->search($search, $limit, $offset);  | 
                                                        |
| 165 | + $users = array_keys($users);  | 
                                                        |
| 166 | +        } else if ($subAdminManager->isSubAdmin($currentUser)) { | 
                                                        |
| 167 | + $subAdminOfGroups = $subAdminManager->getSubAdminsGroups($currentUser);  | 
                                                        |
| 168 | +            foreach ($subAdminOfGroups as $key => $group) { | 
                                                        |
| 169 | + $subAdminOfGroups[$key] = $group->getGID();  | 
                                                        |
| 170 | + }  | 
                                                        |
| 171 | +  | 
                                                        |
| 172 | + $users = [];  | 
                                                        |
| 173 | +            foreach ($subAdminOfGroups as $group) { | 
                                                        |
| 174 | + $users[] = array_keys($this->groupManager->displayNamesInGroup($group, $search, $limit, $offset));  | 
                                                        |
| 175 | + }  | 
                                                        |
| 176 | + $users = array_merge(...$users);  | 
                                                        |
| 177 | + }  | 
                                                        |
| 178 | +  | 
                                                        |
| 179 | + $usersDetails = [];  | 
                                                        |
| 180 | +        foreach ($users as $userId) { | 
                                                        |
| 181 | + $userId = (string) $userId;  | 
                                                        |
| 182 | + $userData = $this->getUserData($userId);  | 
                                                        |
| 183 | + // Do not insert empty entry  | 
                                                        |
| 184 | +            if (!empty($userData)) { | 
                                                        |
| 185 | + $usersDetails[$userId] = $userData;  | 
                                                        |
| 186 | +            } else { | 
                                                        |
| 187 | + // Logged user does not have permissions to see this user  | 
                                                        |
| 188 | + // only showing its id  | 
                                                        |
| 189 | + $usersDetails[$userId] = ['id' => $userId];  | 
                                                        |
| 190 | + }  | 
                                                        |
| 191 | + }  | 
                                                        |
| 192 | +  | 
                                                        |
| 193 | + return new DataResponse([  | 
                                                        |
| 194 | + 'users' => $usersDetails  | 
                                                        |
| 195 | + ]);  | 
                                                        |
| 196 | + }  | 
                                                        |
| 197 | +  | 
                                                        |
| 198 | + /**  | 
                                                        |
| 199 | + * @throws OCSException  | 
                                                        |
| 200 | + */  | 
                                                        |
| 201 | +    private function createNewUserId(): string { | 
                                                        |
| 202 | + $attempts = 0;  | 
                                                        |
| 203 | +        do { | 
                                                        |
| 204 | + $uidCandidate = $this->secureRandom->generate(10, ISecureRandom::CHAR_HUMAN_READABLE);  | 
                                                        |
| 205 | +            if (!$this->userManager->userExists($uidCandidate)) { | 
                                                        |
| 206 | + return $uidCandidate;  | 
                                                        |
| 207 | + }  | 
                                                        |
| 208 | + $attempts++;  | 
                                                        |
| 209 | + } while ($attempts < 10);  | 
                                                        |
| 210 | +        throw new OCSException('Could not create non-existing user id', 111); | 
                                                        |
| 211 | + }  | 
                                                        |
| 212 | +  | 
                                                        |
| 213 | + /**  | 
                                                        |
| 214 | + * @PasswordConfirmationRequired  | 
                                                        |
| 215 | + * @NoAdminRequired  | 
                                                        |
| 216 | + *  | 
                                                        |
| 217 | + * @param string $userid  | 
                                                        |
| 218 | + * @param string $password  | 
                                                        |
| 219 | + * @param string $displayName  | 
                                                        |
| 220 | + * @param string $email  | 
                                                        |
| 221 | + * @param array $groups  | 
                                                        |
| 222 | + * @param array $subadmin  | 
                                                        |
| 223 | + * @param string $quota  | 
                                                        |
| 224 | + * @param string $language  | 
                                                        |
| 225 | + * @return DataResponse  | 
                                                        |
| 226 | + * @throws OCSException  | 
                                                        |
| 227 | + */  | 
                                                        |
| 228 | + public function addUser(string $userid,  | 
                                                        |
| 229 | + string $password = '',  | 
                                                        |
| 230 | + string $displayName = '',  | 
                                                        |
| 231 | + string $email = '',  | 
                                                        |
| 232 | + array $groups = [],  | 
                                                        |
| 233 | + array $subadmin = [],  | 
                                                        |
| 234 | + string $quota = '',  | 
                                                        |
| 235 | +                            string $language = ''): DataResponse { | 
                                                        |
| 236 | + $user = $this->userSession->getUser();  | 
                                                        |
| 237 | + $isAdmin = $this->groupManager->isAdmin($user->getUID());  | 
                                                        |
| 238 | + $subAdminManager = $this->groupManager->getSubAdmin();  | 
                                                        |
| 239 | +  | 
                                                        |
| 240 | +        if(empty($userid) && $this->config->getAppValue('core', 'newUser.generateUserID', 'no') === 'yes') { | 
                                                        |
| 241 | + $userid = $this->createNewUserId();  | 
                                                        |
| 242 | + }  | 
                                                        |
| 243 | +  | 
                                                        |
| 244 | +        if ($this->userManager->userExists($userid)) { | 
                                                        |
| 245 | +            $this->logger->error('Failed addUser attempt: User already exists.', ['app' => 'ocs_api']); | 
                                                        |
| 246 | +            throw new OCSException('User already exists', 102); | 
                                                        |
| 247 | + }  | 
                                                        |
| 248 | +  | 
                                                        |
| 249 | +        if ($groups !== []) { | 
                                                        |
| 250 | +            foreach ($groups as $group) { | 
                                                        |
| 251 | +                if (!$this->groupManager->groupExists($group)) { | 
                                                        |
| 252 | +                    throw new OCSException('group '.$group.' does not exist', 104); | 
                                                        |
| 253 | + }  | 
                                                        |
| 254 | +                if (!$isAdmin && !$subAdminManager->isSubAdminOfGroup($user, $this->groupManager->get($group))) { | 
                                                        |
| 255 | +                    throw new OCSException('insufficient privileges for group '. $group, 105); | 
                                                        |
| 256 | + }  | 
                                                        |
| 257 | + }  | 
                                                        |
| 258 | +        } else { | 
                                                        |
| 259 | +            if (!$isAdmin) { | 
                                                        |
| 260 | +                throw new OCSException('no group specified (required for subadmins)', 106); | 
                                                        |
| 261 | + }  | 
                                                        |
| 262 | + }  | 
                                                        |
| 263 | +  | 
                                                        |
| 264 | + $subadminGroups = [];  | 
                                                        |
| 265 | +        if ($subadmin !== []) { | 
                                                        |
| 266 | +            foreach ($subadmin as $groupid) { | 
                                                        |
| 267 | + $group = $this->groupManager->get($groupid);  | 
                                                        |
| 268 | + // Check if group exists  | 
                                                        |
| 269 | +                if ($group === null) { | 
                                                        |
| 270 | +                    throw new OCSException('Subadmin group does not exist',  102); | 
                                                        |
| 271 | + }  | 
                                                        |
| 272 | + // Check if trying to make subadmin of admin group  | 
                                                        |
| 273 | +                if ($group->getGID() === 'admin') { | 
                                                        |
| 274 | +                    throw new OCSException('Cannot create subadmins for admin group', 103); | 
                                                        |
| 275 | + }  | 
                                                        |
| 276 | + // Check if has permission to promote subadmins  | 
                                                        |
| 277 | +                if (!$subAdminManager->isSubAdminOfGroup($user, $group) && !$isAdmin) { | 
                                                        |
| 278 | +                    throw new OCSForbiddenException('No permissions to promote subadmins'); | 
                                                        |
| 279 | + }  | 
                                                        |
| 280 | + $subadminGroups[] = $group;  | 
                                                        |
| 281 | + }  | 
                                                        |
| 282 | + }  | 
                                                        |
| 283 | +  | 
                                                        |
| 284 | + $generatePasswordResetToken = false;  | 
                                                        |
| 285 | +        if ($password === '') { | 
                                                        |
| 286 | +            if ($email === '') { | 
                                                        |
| 287 | +                throw new OCSException('To send a password link to the user an email address is required.', 108); | 
                                                        |
| 288 | + }  | 
                                                        |
| 289 | +  | 
                                                        |
| 290 | + $password = $this->secureRandom->generate(10);  | 
                                                        |
| 291 | + // Make sure we pass the password_policy  | 
                                                        |
| 292 | +            $password .= $this->secureRandom->generate(2, '$!.,;:-~+*[]{}()'); | 
                                                        |
| 293 | + $generatePasswordResetToken = true;  | 
                                                        |
| 294 | + }  | 
                                                        |
| 295 | +  | 
                                                        |
| 296 | +        if ($email === '' && $this->config->getAppValue('core', 'newUser.requireEmail', 'no') === 'yes') { | 
                                                        |
| 297 | +            throw new OCSException('Required email address was not provided', 110); | 
                                                        |
| 298 | + }  | 
                                                        |
| 299 | +  | 
                                                        |
| 300 | +        try { | 
                                                        |
| 301 | + $newUser = $this->userManager->createUser($userid, $password);  | 
                                                        |
| 302 | +            $this->logger->info('Successful addUser call with userid: ' . $userid, ['app' => 'ocs_api']); | 
                                                        |
| 303 | +  | 
                                                        |
| 304 | +            foreach ($groups as $group) { | 
                                                        |
| 305 | + $this->groupManager->get($group)->addUser($newUser);  | 
                                                        |
| 306 | +                $this->logger->info('Added userid ' . $userid . ' to group ' . $group, ['app' => 'ocs_api']); | 
                                                        |
| 307 | + }  | 
                                                        |
| 308 | +            foreach ($subadminGroups as $group) { | 
                                                        |
| 309 | + $subAdminManager->createSubAdmin($newUser, $group);  | 
                                                        |
| 310 | + }  | 
                                                        |
| 311 | +  | 
                                                        |
| 312 | +            if ($displayName !== '') { | 
                                                        |
| 313 | + $this->editUser($userid, 'display', $displayName);  | 
                                                        |
| 314 | + }  | 
                                                        |
| 315 | +  | 
                                                        |
| 316 | +            if ($quota !== '') { | 
                                                        |
| 317 | + $this->editUser($userid, 'quota', $quota);  | 
                                                        |
| 318 | + }  | 
                                                        |
| 319 | +  | 
                                                        |
| 320 | +            if ($language !== '') { | 
                                                        |
| 321 | + $this->editUser($userid, 'language', $language);  | 
                                                        |
| 322 | + }  | 
                                                        |
| 323 | +  | 
                                                        |
| 324 | + // Send new user mail only if a mail is set  | 
                                                        |
| 325 | +            if ($email !== '') { | 
                                                        |
| 326 | + $newUser->setEMailAddress($email);  | 
                                                        |
| 327 | +                try { | 
                                                        |
| 328 | + $emailTemplate = $this->newUserMailHelper->generateTemplate($newUser, $generatePasswordResetToken);  | 
                                                        |
| 329 | + $this->newUserMailHelper->sendMail($newUser, $emailTemplate);  | 
                                                        |
| 330 | +                } catch (\Exception $e) { | 
                                                        |
| 331 | + $this->logger->logException($e, [  | 
                                                        |
| 332 | + 'message' => "Can't send new user mail to $email",  | 
                                                        |
| 333 | + 'level' => ILogger::ERROR,  | 
                                                        |
| 334 | + 'app' => 'ocs_api',  | 
                                                        |
| 335 | + ]);  | 
                                                        |
| 336 | +                    throw new OCSException('Unable to send the invitation mail', 109); | 
                                                        |
| 337 | + }  | 
                                                        |
| 338 | + }  | 
                                                        |
| 339 | +  | 
                                                        |
| 340 | + return new DataResponse(['id' => $userid]);  | 
                                                        |
| 341 | +  | 
                                                        |
| 342 | +        } catch (HintException $e) { | 
                                                        |
| 343 | + $this->logger->logException($e, [  | 
                                                        |
| 344 | + 'message' => 'Failed addUser attempt with hint exception.',  | 
                                                        |
| 345 | + 'level' => ILogger::WARN,  | 
                                                        |
| 346 | + 'app' => 'ocs_api',  | 
                                                        |
| 347 | + ]);  | 
                                                        |
| 348 | + throw new OCSException($e->getHint(), 107);  | 
                                                        |
| 349 | +        } catch (OCSException $e) { | 
                                                        |
| 350 | + $this->logger->logException($e, [  | 
                                                        |
| 351 | + 'message' => 'Failed addUser attempt with ocs exeption.',  | 
                                                        |
| 352 | + 'level' => ILogger::ERROR,  | 
                                                        |
| 353 | + 'app' => 'ocs_api',  | 
                                                        |
| 354 | + ]);  | 
                                                        |
| 355 | + throw $e;  | 
                                                        |
| 356 | +        } catch (\Exception $e) { | 
                                                        |
| 357 | + $this->logger->logException($e, [  | 
                                                        |
| 358 | + 'message' => 'Failed addUser attempt with exception.',  | 
                                                        |
| 359 | + 'level' => ILogger::ERROR,  | 
                                                        |
| 360 | + 'app' => 'ocs_api',  | 
                                                        |
| 361 | + ]);  | 
                                                        |
| 362 | +            throw new OCSException('Bad request', 101); | 
                                                        |
| 363 | + }  | 
                                                        |
| 364 | + }  | 
                                                        |
| 365 | +  | 
                                                        |
| 366 | + /**  | 
                                                        |
| 367 | + * @NoAdminRequired  | 
                                                        |
| 368 | + * @NoSubAdminRequired  | 
                                                        |
| 369 | + *  | 
                                                        |
| 370 | + * gets user info  | 
                                                        |
| 371 | + *  | 
                                                        |
| 372 | + * @param string $userId  | 
                                                        |
| 373 | + * @return DataResponse  | 
                                                        |
| 374 | + * @throws OCSException  | 
                                                        |
| 375 | + */  | 
                                                        |
| 376 | +    public function getUser(string $userId): DataResponse { | 
                                                        |
| 377 | + $data = $this->getUserData($userId);  | 
                                                        |
| 378 | + // getUserData returns empty array if not enough permissions  | 
                                                        |
| 379 | +        if (empty($data)) { | 
                                                        |
| 380 | +            throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); | 
                                                        |
| 381 | + }  | 
                                                        |
| 382 | + return new DataResponse($data);  | 
                                                        |
| 383 | + }  | 
                                                        |
| 384 | +  | 
                                                        |
| 385 | + /**  | 
                                                        |
| 386 | + * @NoAdminRequired  | 
                                                        |
| 387 | + * @NoSubAdminRequired  | 
                                                        |
| 388 | + *  | 
                                                        |
| 389 | + * gets user info from the currently logged in user  | 
                                                        |
| 390 | + *  | 
                                                        |
| 391 | + * @return DataResponse  | 
                                                        |
| 392 | + * @throws OCSException  | 
                                                        |
| 393 | + */  | 
                                                        |
| 394 | +    public function getCurrentUser(): DataResponse { | 
                                                        |
| 395 | + $user = $this->userSession->getUser();  | 
                                                        |
| 396 | +        if ($user) { | 
                                                        |
| 397 | + $data = $this->getUserData($user->getUID());  | 
                                                        |
| 398 | + // rename "displayname" to "display-name" only for this call to keep  | 
                                                        |
| 399 | + // the API stable.  | 
                                                        |
| 400 | + $data['display-name'] = $data['displayname'];  | 
                                                        |
| 401 | + unset($data['displayname']);  | 
                                                        |
| 402 | + return new DataResponse($data);  | 
                                                        |
| 403 | +  | 
                                                        |
| 404 | + }  | 
                                                        |
| 405 | +  | 
                                                        |
| 406 | +        throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); | 
                                                        |
| 407 | + }  | 
                                                        |
| 408 | +  | 
                                                        |
| 409 | + /**  | 
                                                        |
| 410 | + * @NoAdminRequired  | 
                                                        |
| 411 | + * @NoSubAdminRequired  | 
                                                        |
| 412 | + */  | 
                                                        |
| 413 | +    public function getEditableFields(): DataResponse { | 
                                                        |
| 414 | + $permittedFields = [];  | 
                                                        |
| 415 | +  | 
                                                        |
| 416 | + // Editing self (display, email)  | 
                                                        |
| 417 | +        if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) { | 
                                                        |
| 418 | + $permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME;  | 
                                                        |
| 419 | + $permittedFields[] = AccountManager::PROPERTY_EMAIL;  | 
                                                        |
| 420 | + }  | 
                                                        |
| 421 | +  | 
                                                        |
| 422 | +        if ($this->appManager->isEnabledForUser('federatedfilesharing')) { | 
                                                        |
| 423 | + $federatedFileSharing = $this->federatedFileSharingFactory->get();  | 
                                                        |
| 424 | + $shareProvider = $federatedFileSharing->getFederatedShareProvider();  | 
                                                        |
| 425 | +            if ($shareProvider->isLookupServerUploadEnabled()) { | 
                                                        |
| 426 | + $permittedFields[] = AccountManager::PROPERTY_PHONE;  | 
                                                        |
| 427 | + $permittedFields[] = AccountManager::PROPERTY_ADDRESS;  | 
                                                        |
| 428 | + $permittedFields[] = AccountManager::PROPERTY_WEBSITE;  | 
                                                        |
| 429 | + $permittedFields[] = AccountManager::PROPERTY_TWITTER;  | 
                                                        |
| 430 | + }  | 
                                                        |
| 431 | + }  | 
                                                        |
| 432 | +  | 
                                                        |
| 433 | + return new DataResponse($permittedFields);  | 
                                                        |
| 434 | + }  | 
                                                        |
| 435 | +  | 
                                                        |
| 436 | + /**  | 
                                                        |
| 437 | + * @NoAdminRequired  | 
                                                        |
| 438 | + * @NoSubAdminRequired  | 
                                                        |
| 439 | + * @PasswordConfirmationRequired  | 
                                                        |
| 440 | + *  | 
                                                        |
| 441 | + * edit users  | 
                                                        |
| 442 | + *  | 
                                                        |
| 443 | + * @param string $userId  | 
                                                        |
| 444 | + * @param string $key  | 
                                                        |
| 445 | + * @param string $value  | 
                                                        |
| 446 | + * @return DataResponse  | 
                                                        |
| 447 | + * @throws OCSException  | 
                                                        |
| 448 | + */  | 
                                                        |
| 449 | +    public function editUser(string $userId, string $key, string $value): DataResponse { | 
                                                        |
| 450 | + $currentLoggedInUser = $this->userSession->getUser();  | 
                                                        |
| 451 | +  | 
                                                        |
| 452 | + $targetUser = $this->userManager->get($userId);  | 
                                                        |
| 453 | +        if ($targetUser === null) { | 
                                                        |
| 454 | +            throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); | 
                                                        |
| 455 | + }  | 
                                                        |
| 456 | +  | 
                                                        |
| 457 | + $permittedFields = [];  | 
                                                        |
| 458 | +        if ($targetUser->getUID() === $currentLoggedInUser->getUID()) { | 
                                                        |
| 459 | + // Editing self (display, email)  | 
                                                        |
| 460 | +            if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) { | 
                                                        |
| 461 | + $permittedFields[] = 'display';  | 
                                                        |
| 462 | + $permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME;  | 
                                                        |
| 463 | + $permittedFields[] = AccountManager::PROPERTY_EMAIL;  | 
                                                        |
| 464 | + }  | 
                                                        |
| 465 | +  | 
                                                        |
| 466 | + $permittedFields[] = 'password';  | 
                                                        |
| 467 | +            if ($this->config->getSystemValue('force_language', false) === false || | 
                                                        |
| 468 | +                $this->groupManager->isAdmin($currentLoggedInUser->getUID())) { | 
                                                        |
| 469 | + $permittedFields[] = 'language';  | 
                                                        |
| 470 | + }  | 
                                                        |
| 471 | +  | 
                                                        |
| 472 | +            if ($this->config->getSystemValue('force_locale', false) === false || | 
                                                        |
| 473 | +                $this->groupManager->isAdmin($currentLoggedInUser->getUID())) { | 
                                                        |
| 474 | + $permittedFields[] = 'locale';  | 
                                                        |
| 475 | + }  | 
                                                        |
| 476 | +  | 
                                                        |
| 477 | +            if ($this->appManager->isEnabledForUser('federatedfilesharing')) { | 
                                                        |
| 478 | + $federatedFileSharing = new \OCA\FederatedFileSharing\AppInfo\Application();  | 
                                                        |
| 479 | + $shareProvider = $federatedFileSharing->getFederatedShareProvider();  | 
                                                        |
| 480 | +                if ($shareProvider->isLookupServerUploadEnabled()) { | 
                                                        |
| 481 | + $permittedFields[] = AccountManager::PROPERTY_PHONE;  | 
                                                        |
| 482 | + $permittedFields[] = AccountManager::PROPERTY_ADDRESS;  | 
                                                        |
| 483 | + $permittedFields[] = AccountManager::PROPERTY_WEBSITE;  | 
                                                        |
| 484 | + $permittedFields[] = AccountManager::PROPERTY_TWITTER;  | 
                                                        |
| 485 | + }  | 
                                                        |
| 486 | + }  | 
                                                        |
| 487 | +  | 
                                                        |
| 488 | + // If admin they can edit their own quota  | 
                                                        |
| 489 | +            if ($this->groupManager->isAdmin($currentLoggedInUser->getUID())) { | 
                                                        |
| 490 | + $permittedFields[] = 'quota';  | 
                                                        |
| 491 | + }  | 
                                                        |
| 492 | +        } else { | 
                                                        |
| 493 | + // Check if admin / subadmin  | 
                                                        |
| 494 | + $subAdminManager = $this->groupManager->getSubAdmin();  | 
                                                        |
| 495 | + if ($subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)  | 
                                                        |
| 496 | +            || $this->groupManager->isAdmin($currentLoggedInUser->getUID())) { | 
                                                        |
| 497 | + // They have permissions over the user  | 
                                                        |
| 498 | + $permittedFields[] = 'display';  | 
                                                        |
| 499 | + $permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME;  | 
                                                        |
| 500 | + $permittedFields[] = AccountManager::PROPERTY_EMAIL;  | 
                                                        |
| 501 | + $permittedFields[] = 'password';  | 
                                                        |
| 502 | + $permittedFields[] = 'language';  | 
                                                        |
| 503 | + $permittedFields[] = 'locale';  | 
                                                        |
| 504 | + $permittedFields[] = AccountManager::PROPERTY_PHONE;  | 
                                                        |
| 505 | + $permittedFields[] = AccountManager::PROPERTY_ADDRESS;  | 
                                                        |
| 506 | + $permittedFields[] = AccountManager::PROPERTY_WEBSITE;  | 
                                                        |
| 507 | + $permittedFields[] = AccountManager::PROPERTY_TWITTER;  | 
                                                        |
| 508 | + $permittedFields[] = 'quota';  | 
                                                        |
| 509 | +            } else { | 
                                                        |
| 510 | + // No rights  | 
                                                        |
| 511 | +                throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); | 
                                                        |
| 512 | + }  | 
                                                        |
| 513 | + }  | 
                                                        |
| 514 | + // Check if permitted to edit this field  | 
                                                        |
| 515 | +        if (!in_array($key, $permittedFields)) { | 
                                                        |
| 516 | +            throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); | 
                                                        |
| 517 | + }  | 
                                                        |
| 518 | + // Process the edit  | 
                                                        |
| 519 | +        switch($key) { | 
                                                        |
| 520 | + case 'display':  | 
                                                        |
| 521 | + case AccountManager::PROPERTY_DISPLAYNAME:  | 
                                                        |
| 522 | + $targetUser->setDisplayName($value);  | 
                                                        |
| 523 | + break;  | 
                                                        |
| 524 | + case 'quota':  | 
                                                        |
| 525 | + $quota = $value;  | 
                                                        |
| 526 | +                if ($quota !== 'none' && $quota !== 'default') { | 
                                                        |
| 527 | +                    if (is_numeric($quota)) { | 
                                                        |
| 528 | + $quota = (float) $quota;  | 
                                                        |
| 529 | +                    } else { | 
                                                        |
| 530 | + $quota = \OCP\Util::computerFileSize($quota);  | 
                                                        |
| 531 | + }  | 
                                                        |
| 532 | +                    if ($quota === false) { | 
                                                        |
| 533 | +                        throw new OCSException('Invalid quota value '.$value, 103); | 
                                                        |
| 534 | + }  | 
                                                        |
| 535 | +                    if ($quota === -1) { | 
                                                        |
| 536 | + $quota = 'none';  | 
                                                        |
| 537 | +                    } else { | 
                                                        |
| 538 | + $quota = \OCP\Util::humanFileSize($quota);  | 
                                                        |
| 539 | + }  | 
                                                        |
| 540 | + }  | 
                                                        |
| 541 | + $targetUser->setQuota($quota);  | 
                                                        |
| 542 | + break;  | 
                                                        |
| 543 | + case 'password':  | 
                                                        |
| 544 | +                try { | 
                                                        |
| 545 | +                    if (!$targetUser->canChangePassword()) { | 
                                                        |
| 546 | +                        throw new OCSException('Setting the password is not supported by the users backend', 103); | 
                                                        |
| 547 | + }  | 
                                                        |
| 548 | + $targetUser->setPassword($value);  | 
                                                        |
| 549 | +                } catch (HintException $e) { // password policy error | 
                                                        |
| 550 | + throw new OCSException($e->getMessage(), 103);  | 
                                                        |
| 551 | + }  | 
                                                        |
| 552 | + break;  | 
                                                        |
| 553 | + case 'language':  | 
                                                        |
| 554 | + $languagesCodes = $this->l10nFactory->findAvailableLanguages();  | 
                                                        |
| 555 | +                if (!in_array($value, $languagesCodes, true) && $value !== 'en') { | 
                                                        |
| 556 | +                    throw new OCSException('Invalid language', 102); | 
                                                        |
| 557 | + }  | 
                                                        |
| 558 | + $this->config->setUserValue($targetUser->getUID(), 'core', 'lang', $value);  | 
                                                        |
| 559 | + break;  | 
                                                        |
| 560 | + case 'locale':  | 
                                                        |
| 561 | +                if (!$this->l10nFactory->localeExists($value)) { | 
                                                        |
| 562 | +                    throw new OCSException('Invalid locale', 102); | 
                                                        |
| 563 | + }  | 
                                                        |
| 564 | + $this->config->setUserValue($targetUser->getUID(), 'core', 'locale', $value);  | 
                                                        |
| 565 | + break;  | 
                                                        |
| 566 | + case AccountManager::PROPERTY_EMAIL:  | 
                                                        |
| 567 | +                if (filter_var($value, FILTER_VALIDATE_EMAIL) || $value === '') { | 
                                                        |
| 568 | + $targetUser->setEMailAddress($value);  | 
                                                        |
| 569 | +                } else { | 
                                                        |
| 570 | +                    throw new OCSException('', 102); | 
                                                        |
| 571 | + }  | 
                                                        |
| 572 | + break;  | 
                                                        |
| 573 | + case AccountManager::PROPERTY_PHONE:  | 
                                                        |
| 574 | + case AccountManager::PROPERTY_ADDRESS:  | 
                                                        |
| 575 | + case AccountManager::PROPERTY_WEBSITE:  | 
                                                        |
| 576 | + case AccountManager::PROPERTY_TWITTER:  | 
                                                        |
| 577 | + $userAccount = $this->accountManager->getUser($targetUser);  | 
                                                        |
| 578 | +                if ($userAccount[$key]['value'] !== $value) { | 
                                                        |
| 579 | + $userAccount[$key]['value'] = $value;  | 
                                                        |
| 580 | + $this->accountManager->updateUser($targetUser, $userAccount);  | 
                                                        |
| 581 | + }  | 
                                                        |
| 582 | + break;  | 
                                                        |
| 583 | + default:  | 
                                                        |
| 584 | +                throw new OCSException('', 103); | 
                                                        |
| 585 | + }  | 
                                                        |
| 586 | + return new DataResponse();  | 
                                                        |
| 587 | + }  | 
                                                        |
| 588 | +  | 
                                                        |
| 589 | + /**  | 
                                                        |
| 590 | + * @PasswordConfirmationRequired  | 
                                                        |
| 591 | + * @NoAdminRequired  | 
                                                        |
| 592 | + *  | 
                                                        |
| 593 | + * @param string $userId  | 
                                                        |
| 594 | + * @return DataResponse  | 
                                                        |
| 595 | + * @throws OCSException  | 
                                                        |
| 596 | + */  | 
                                                        |
| 597 | +    public function deleteUser(string $userId): DataResponse { | 
                                                        |
| 598 | + $currentLoggedInUser = $this->userSession->getUser();  | 
                                                        |
| 599 | +  | 
                                                        |
| 600 | + $targetUser = $this->userManager->get($userId);  | 
                                                        |
| 601 | +  | 
                                                        |
| 602 | +        if ($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) { | 
                                                        |
| 603 | +            throw new OCSException('', 101); | 
                                                        |
| 604 | + }  | 
                                                        |
| 605 | +  | 
                                                        |
| 606 | + // If not permitted  | 
                                                        |
| 607 | + $subAdminManager = $this->groupManager->getSubAdmin();  | 
                                                        |
| 608 | +        if (!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) { | 
                                                        |
| 609 | +            throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); | 
                                                        |
| 610 | + }  | 
                                                        |
| 611 | +  | 
                                                        |
| 612 | + // Go ahead with the delete  | 
                                                        |
| 613 | +        if ($targetUser->delete()) { | 
                                                        |
| 614 | + return new DataResponse();  | 
                                                        |
| 615 | +        } else { | 
                                                        |
| 616 | +            throw new OCSException('', 101); | 
                                                        |
| 617 | + }  | 
                                                        |
| 618 | + }  | 
                                                        |
| 619 | +  | 
                                                        |
| 620 | + /**  | 
                                                        |
| 621 | + * @PasswordConfirmationRequired  | 
                                                        |
| 622 | + * @NoAdminRequired  | 
                                                        |
| 623 | + *  | 
                                                        |
| 624 | + * @param string $userId  | 
                                                        |
| 625 | + * @return DataResponse  | 
                                                        |
| 626 | + * @throws OCSException  | 
                                                        |
| 627 | + * @throws OCSForbiddenException  | 
                                                        |
| 628 | + */  | 
                                                        |
| 629 | +    public function disableUser(string $userId): DataResponse { | 
                                                        |
| 630 | + return $this->setEnabled($userId, false);  | 
                                                        |
| 631 | + }  | 
                                                        |
| 632 | +  | 
                                                        |
| 633 | + /**  | 
                                                        |
| 634 | + * @PasswordConfirmationRequired  | 
                                                        |
| 635 | + * @NoAdminRequired  | 
                                                        |
| 636 | + *  | 
                                                        |
| 637 | + * @param string $userId  | 
                                                        |
| 638 | + * @return DataResponse  | 
                                                        |
| 639 | + * @throws OCSException  | 
                                                        |
| 640 | + * @throws OCSForbiddenException  | 
                                                        |
| 641 | + */  | 
                                                        |
| 642 | +    public function enableUser(string $userId): DataResponse { | 
                                                        |
| 643 | + return $this->setEnabled($userId, true);  | 
                                                        |
| 644 | + }  | 
                                                        |
| 645 | +  | 
                                                        |
| 646 | + /**  | 
                                                        |
| 647 | + * @param string $userId  | 
                                                        |
| 648 | + * @param bool $value  | 
                                                        |
| 649 | + * @return DataResponse  | 
                                                        |
| 650 | + * @throws OCSException  | 
                                                        |
| 651 | + */  | 
                                                        |
| 652 | +    private function setEnabled(string $userId, bool $value): DataResponse { | 
                                                        |
| 653 | + $currentLoggedInUser = $this->userSession->getUser();  | 
                                                        |
| 654 | +  | 
                                                        |
| 655 | + $targetUser = $this->userManager->get($userId);  | 
                                                        |
| 656 | +        if ($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) { | 
                                                        |
| 657 | +            throw new OCSException('', 101); | 
                                                        |
| 658 | + }  | 
                                                        |
| 659 | +  | 
                                                        |
| 660 | + // If not permitted  | 
                                                        |
| 661 | + $subAdminManager = $this->groupManager->getSubAdmin();  | 
                                                        |
| 662 | +        if (!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) { | 
                                                        |
| 663 | +            throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); | 
                                                        |
| 664 | + }  | 
                                                        |
| 665 | +  | 
                                                        |
| 666 | + // enable/disable the user now  | 
                                                        |
| 667 | + $targetUser->setEnabled($value);  | 
                                                        |
| 668 | + return new DataResponse();  | 
                                                        |
| 669 | + }  | 
                                                        |
| 670 | +  | 
                                                        |
| 671 | + /**  | 
                                                        |
| 672 | + * @NoAdminRequired  | 
                                                        |
| 673 | + * @NoSubAdminRequired  | 
                                                        |
| 674 | + *  | 
                                                        |
| 675 | + * @param string $userId  | 
                                                        |
| 676 | + * @return DataResponse  | 
                                                        |
| 677 | + * @throws OCSException  | 
                                                        |
| 678 | + */  | 
                                                        |
| 679 | +    public function getUsersGroups(string $userId): DataResponse { | 
                                                        |
| 680 | + $loggedInUser = $this->userSession->getUser();  | 
                                                        |
| 681 | +  | 
                                                        |
| 682 | + $targetUser = $this->userManager->get($userId);  | 
                                                        |
| 683 | +        if ($targetUser === null) { | 
                                                        |
| 684 | +            throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND); | 
                                                        |
| 685 | + }  | 
                                                        |
| 686 | +  | 
                                                        |
| 687 | +        if ($targetUser->getUID() === $loggedInUser->getUID() || $this->groupManager->isAdmin($loggedInUser->getUID())) { | 
                                                        |
| 688 | + // Self lookup or admin lookup  | 
                                                        |
| 689 | + return new DataResponse([  | 
                                                        |
| 690 | + 'groups' => $this->groupManager->getUserGroupIds($targetUser)  | 
                                                        |
| 691 | + ]);  | 
                                                        |
| 692 | +        } else { | 
                                                        |
| 693 | + $subAdminManager = $this->groupManager->getSubAdmin();  | 
                                                        |
| 694 | +  | 
                                                        |
| 695 | + // Looking up someone else  | 
                                                        |
| 696 | +            if ($subAdminManager->isUserAccessible($loggedInUser, $targetUser)) { | 
                                                        |
| 697 | + // Return the group that the method caller is subadmin of for the user in question  | 
                                                        |
| 698 | + /** @var IGroup[] $getSubAdminsGroups */  | 
                                                        |
| 699 | + $getSubAdminsGroups = $subAdminManager->getSubAdminsGroups($loggedInUser);  | 
                                                        |
| 700 | +                foreach ($getSubAdminsGroups as $key => $group) { | 
                                                        |
| 701 | + $getSubAdminsGroups[$key] = $group->getGID();  | 
                                                        |
| 702 | + }  | 
                                                        |
| 703 | + $groups = array_intersect(  | 
                                                        |
| 704 | + $getSubAdminsGroups,  | 
                                                        |
| 705 | + $this->groupManager->getUserGroupIds($targetUser)  | 
                                                        |
| 706 | + );  | 
                                                        |
| 707 | + return new DataResponse(['groups' => $groups]);  | 
                                                        |
| 708 | +            } else { | 
                                                        |
| 709 | + // Not permitted  | 
                                                        |
| 710 | +                throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); | 
                                                        |
| 711 | + }  | 
                                                        |
| 712 | + }  | 
                                                        |
| 713 | +  | 
                                                        |
| 714 | + }  | 
                                                        |
| 715 | +  | 
                                                        |
| 716 | + /**  | 
                                                        |
| 717 | + * @PasswordConfirmationRequired  | 
                                                        |
| 718 | + * @NoAdminRequired  | 
                                                        |
| 719 | + *  | 
                                                        |
| 720 | + * @param string $userId  | 
                                                        |
| 721 | + * @param string $groupid  | 
                                                        |
| 722 | + * @return DataResponse  | 
                                                        |
| 723 | + * @throws OCSException  | 
                                                        |
| 724 | + */  | 
                                                        |
| 725 | +    public function addToGroup(string $userId, string $groupid = ''): DataResponse { | 
                                                        |
| 726 | +        if ($groupid === '') { | 
                                                        |
| 727 | +            throw new OCSException('', 101); | 
                                                        |
| 728 | + }  | 
                                                        |
| 729 | +  | 
                                                        |
| 730 | + $group = $this->groupManager->get($groupid);  | 
                                                        |
| 731 | + $targetUser = $this->userManager->get($userId);  | 
                                                        |
| 732 | +        if ($group === null) { | 
                                                        |
| 733 | +            throw new OCSException('', 102); | 
                                                        |
| 734 | + }  | 
                                                        |
| 735 | +        if ($targetUser === null) { | 
                                                        |
| 736 | +            throw new OCSException('', 103); | 
                                                        |
| 737 | + }  | 
                                                        |
| 738 | +  | 
                                                        |
| 739 | + // If they're not an admin, check they are a subadmin of the group in question  | 
                                                        |
| 740 | + $loggedInUser = $this->userSession->getUser();  | 
                                                        |
| 741 | + $subAdminManager = $this->groupManager->getSubAdmin();  | 
                                                        |
| 742 | +        if (!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminOfGroup($loggedInUser, $group)) { | 
                                                        |
| 743 | +            throw new OCSException('', 104); | 
                                                        |
| 744 | + }  | 
                                                        |
| 745 | +  | 
                                                        |
| 746 | + // Add user to group  | 
                                                        |
| 747 | + $group->addUser($targetUser);  | 
                                                        |
| 748 | + return new DataResponse();  | 
                                                        |
| 749 | + }  | 
                                                        |
| 750 | +  | 
                                                        |
| 751 | + /**  | 
                                                        |
| 752 | + * @PasswordConfirmationRequired  | 
                                                        |
| 753 | + * @NoAdminRequired  | 
                                                        |
| 754 | + *  | 
                                                        |
| 755 | + * @param string $userId  | 
                                                        |
| 756 | + * @param string $groupid  | 
                                                        |
| 757 | + * @return DataResponse  | 
                                                        |
| 758 | + * @throws OCSException  | 
                                                        |
| 759 | + */  | 
                                                        |
| 760 | +    public function removeFromGroup(string $userId, string $groupid): DataResponse { | 
                                                        |
| 761 | + $loggedInUser = $this->userSession->getUser();  | 
                                                        |
| 762 | +  | 
                                                        |
| 763 | +        if ($groupid === null || trim($groupid) === '') { | 
                                                        |
| 764 | +            throw new OCSException('', 101); | 
                                                        |
| 765 | + }  | 
                                                        |
| 766 | +  | 
                                                        |
| 767 | + $group = $this->groupManager->get($groupid);  | 
                                                        |
| 768 | +        if ($group === null) { | 
                                                        |
| 769 | +            throw new OCSException('', 102); | 
                                                        |
| 770 | + }  | 
                                                        |
| 771 | +  | 
                                                        |
| 772 | + $targetUser = $this->userManager->get($userId);  | 
                                                        |
| 773 | +        if ($targetUser === null) { | 
                                                        |
| 774 | +            throw new OCSException('', 103); | 
                                                        |
| 775 | + }  | 
                                                        |
| 776 | +  | 
                                                        |
| 777 | + // If they're not an admin, check they are a subadmin of the group in question  | 
                                                        |
| 778 | + $subAdminManager = $this->groupManager->getSubAdmin();  | 
                                                        |
| 779 | +        if (!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminOfGroup($loggedInUser, $group)) { | 
                                                        |
| 780 | +            throw new OCSException('', 104); | 
                                                        |
| 781 | + }  | 
                                                        |
| 782 | +  | 
                                                        |
| 783 | + // Check they aren't removing themselves from 'admin' or their 'subadmin; group  | 
                                                        |
| 784 | +        if ($targetUser->getUID() === $loggedInUser->getUID()) { | 
                                                        |
| 785 | +            if ($this->groupManager->isAdmin($loggedInUser->getUID())) { | 
                                                        |
| 786 | +                if ($group->getGID() === 'admin') { | 
                                                        |
| 787 | +                    throw new OCSException('Cannot remove yourself from the admin group', 105); | 
                                                        |
| 788 | + }  | 
                                                        |
| 789 | +            } else { | 
                                                        |
| 790 | + // Not an admin, so the user must be a subadmin of this group, but that is not allowed.  | 
                                                        |
| 791 | +                throw new OCSException('Cannot remove yourself from this group as you are a SubAdmin', 105); | 
                                                        |
| 792 | + }  | 
                                                        |
| 793 | +  | 
                                                        |
| 794 | +        } else if (!$this->groupManager->isAdmin($loggedInUser->getUID())) { | 
                                                        |
| 795 | + /** @var IGroup[] $subAdminGroups */  | 
                                                        |
| 796 | + $subAdminGroups = $subAdminManager->getSubAdminsGroups($loggedInUser);  | 
                                                        |
| 797 | +            $subAdminGroups = array_map(function (IGroup $subAdminGroup) { | 
                                                        |
| 798 | + return $subAdminGroup->getGID();  | 
                                                        |
| 799 | + }, $subAdminGroups);  | 
                                                        |
| 800 | + $userGroups = $this->groupManager->getUserGroupIds($targetUser);  | 
                                                        |
| 801 | + $userSubAdminGroups = array_intersect($subAdminGroups, $userGroups);  | 
                                                        |
| 802 | +  | 
                                                        |
| 803 | +            if (count($userSubAdminGroups) <= 1) { | 
                                                        |
| 804 | + // Subadmin must not be able to remove a user from all their subadmin groups.  | 
                                                        |
| 805 | +                throw new OCSException('Not viable to remove user from the last group you are SubAdmin of', 105); | 
                                                        |
| 806 | + }  | 
                                                        |
| 807 | + }  | 
                                                        |
| 808 | +  | 
                                                        |
| 809 | + // Remove user from group  | 
                                                        |
| 810 | + $group->removeUser($targetUser);  | 
                                                        |
| 811 | + return new DataResponse();  | 
                                                        |
| 812 | + }  | 
                                                        |
| 813 | +  | 
                                                        |
| 814 | + /**  | 
                                                        |
| 815 | + * Creates a subadmin  | 
                                                        |
| 816 | + *  | 
                                                        |
| 817 | + * @PasswordConfirmationRequired  | 
                                                        |
| 818 | + *  | 
                                                        |
| 819 | + * @param string $userId  | 
                                                        |
| 820 | + * @param string $groupid  | 
                                                        |
| 821 | + * @return DataResponse  | 
                                                        |
| 822 | + * @throws OCSException  | 
                                                        |
| 823 | + */  | 
                                                        |
| 824 | +    public function addSubAdmin(string $userId, string $groupid): DataResponse { | 
                                                        |
| 825 | + $group = $this->groupManager->get($groupid);  | 
                                                        |
| 826 | + $user = $this->userManager->get($userId);  | 
                                                        |
| 827 | +  | 
                                                        |
| 828 | + // Check if the user exists  | 
                                                        |
| 829 | +        if ($user === null) { | 
                                                        |
| 830 | +            throw new OCSException('User does not exist', 101); | 
                                                        |
| 831 | + }  | 
                                                        |
| 832 | + // Check if group exists  | 
                                                        |
| 833 | +        if ($group === null) { | 
                                                        |
| 834 | +            throw new OCSException('Group does not exist',  102); | 
                                                        |
| 835 | + }  | 
                                                        |
| 836 | + // Check if trying to make subadmin of admin group  | 
                                                        |
| 837 | +        if ($group->getGID() === 'admin') { | 
                                                        |
| 838 | +            throw new OCSException('Cannot create subadmins for admin group', 103); | 
                                                        |
| 839 | + }  | 
                                                        |
| 840 | +  | 
                                                        |
| 841 | + $subAdminManager = $this->groupManager->getSubAdmin();  | 
                                                        |
| 842 | +  | 
                                                        |
| 843 | + // We cannot be subadmin twice  | 
                                                        |
| 844 | +        if ($subAdminManager->isSubAdminOfGroup($user, $group)) { | 
                                                        |
| 845 | + return new DataResponse();  | 
                                                        |
| 846 | + }  | 
                                                        |
| 847 | + // Go  | 
                                                        |
| 848 | + $subAdminManager->createSubAdmin($user, $group);  | 
                                                        |
| 849 | + return new DataResponse();  | 
                                                        |
| 850 | + }  | 
                                                        |
| 851 | +  | 
                                                        |
| 852 | + /**  | 
                                                        |
| 853 | + * Removes a subadmin from a group  | 
                                                        |
| 854 | + *  | 
                                                        |
| 855 | + * @PasswordConfirmationRequired  | 
                                                        |
| 856 | + *  | 
                                                        |
| 857 | + * @param string $userId  | 
                                                        |
| 858 | + * @param string $groupid  | 
                                                        |
| 859 | + * @return DataResponse  | 
                                                        |
| 860 | + * @throws OCSException  | 
                                                        |
| 861 | + */  | 
                                                        |
| 862 | +    public function removeSubAdmin(string $userId, string $groupid): DataResponse { | 
                                                        |
| 863 | + $group = $this->groupManager->get($groupid);  | 
                                                        |
| 864 | + $user = $this->userManager->get($userId);  | 
                                                        |
| 865 | + $subAdminManager = $this->groupManager->getSubAdmin();  | 
                                                        |
| 866 | +  | 
                                                        |
| 867 | + // Check if the user exists  | 
                                                        |
| 868 | +        if ($user === null) { | 
                                                        |
| 869 | +            throw new OCSException('User does not exist', 101); | 
                                                        |
| 870 | + }  | 
                                                        |
| 871 | + // Check if the group exists  | 
                                                        |
| 872 | +        if ($group === null) { | 
                                                        |
| 873 | +            throw new OCSException('Group does not exist', 101); | 
                                                        |
| 874 | + }  | 
                                                        |
| 875 | + // Check if they are a subadmin of this said group  | 
                                                        |
| 876 | +        if (!$subAdminManager->isSubAdminOfGroup($user, $group)) { | 
                                                        |
| 877 | +            throw new OCSException('User is not a subadmin of this group', 102); | 
                                                        |
| 878 | + }  | 
                                                        |
| 879 | +  | 
                                                        |
| 880 | + // Go  | 
                                                        |
| 881 | + $subAdminManager->deleteSubAdmin($user, $group);  | 
                                                        |
| 882 | + return new DataResponse();  | 
                                                        |
| 883 | + }  | 
                                                        |
| 884 | +  | 
                                                        |
| 885 | + /**  | 
                                                        |
| 886 | + * Get the groups a user is a subadmin of  | 
                                                        |
| 887 | + *  | 
                                                        |
| 888 | + * @param string $userId  | 
                                                        |
| 889 | + * @return DataResponse  | 
                                                        |
| 890 | + * @throws OCSException  | 
                                                        |
| 891 | + */  | 
                                                        |
| 892 | +    public function getUserSubAdminGroups(string $userId): DataResponse { | 
                                                        |
| 893 | + $groups = $this->getUserSubAdminGroupsData($userId);  | 
                                                        |
| 894 | + return new DataResponse($groups);  | 
                                                        |
| 895 | + }  | 
                                                        |
| 896 | +  | 
                                                        |
| 897 | + /**  | 
                                                        |
| 898 | + * @NoAdminRequired  | 
                                                        |
| 899 | + * @PasswordConfirmationRequired  | 
                                                        |
| 900 | + *  | 
                                                        |
| 901 | + * resend welcome message  | 
                                                        |
| 902 | + *  | 
                                                        |
| 903 | + * @param string $userId  | 
                                                        |
| 904 | + * @return DataResponse  | 
                                                        |
| 905 | + * @throws OCSException  | 
                                                        |
| 906 | + */  | 
                                                        |
| 907 | +    public function resendWelcomeMessage(string $userId): DataResponse { | 
                                                        |
| 908 | + $currentLoggedInUser = $this->userSession->getUser();  | 
                                                        |
| 909 | +  | 
                                                        |
| 910 | + $targetUser = $this->userManager->get($userId);  | 
                                                        |
| 911 | +        if ($targetUser === null) { | 
                                                        |
| 912 | +            throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND); | 
                                                        |
| 913 | + }  | 
                                                        |
| 914 | +  | 
                                                        |
| 915 | + // Check if admin / subadmin  | 
                                                        |
| 916 | + $subAdminManager = $this->groupManager->getSubAdmin();  | 
                                                        |
| 917 | + if (!$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)  | 
                                                        |
| 918 | +            && !$this->groupManager->isAdmin($currentLoggedInUser->getUID())) { | 
                                                        |
| 919 | + // No rights  | 
                                                        |
| 920 | +            throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); | 
                                                        |
| 921 | + }  | 
                                                        |
| 922 | +  | 
                                                        |
| 923 | + $email = $targetUser->getEMailAddress();  | 
                                                        |
| 924 | +        if ($email === '' || $email === null) { | 
                                                        |
| 925 | +            throw new OCSException('Email address not available', 101); | 
                                                        |
| 926 | + }  | 
                                                        |
| 927 | +  | 
                                                        |
| 928 | +        try { | 
                                                        |
| 929 | + $emailTemplate = $this->newUserMailHelper->generateTemplate($targetUser, false);  | 
                                                        |
| 930 | + $this->newUserMailHelper->sendMail($targetUser, $emailTemplate);  | 
                                                        |
| 931 | +        } catch(\Exception $e) { | 
                                                        |
| 932 | + $this->logger->logException($e, [  | 
                                                        |
| 933 | + 'message' => "Can't send new user mail to $email",  | 
                                                        |
| 934 | + 'level' => ILogger::ERROR,  | 
                                                        |
| 935 | + 'app' => 'settings',  | 
                                                        |
| 936 | + ]);  | 
                                                        |
| 937 | +            throw new OCSException('Sending email failed', 102); | 
                                                        |
| 938 | + }  | 
                                                        |
| 939 | +  | 
                                                        |
| 940 | + return new DataResponse();  | 
                                                        |
| 941 | + }  | 
                                                        |
| 942 | 942 | }  | 
                                                        
@@ -127,7 +127,7 @@ discard block  | 
                                                    ||
| 127 | 127 | // Admin? Or SubAdmin?  | 
                                                        
| 128 | 128 | $uid = $user->getUID();  | 
                                                        
| 129 | 129 | $subAdminManager = $this->groupManager->getSubAdmin();  | 
                                                        
| 130 | -		if ($this->groupManager->isAdmin($uid)){ | 
                                                        |
| 130 | +		if ($this->groupManager->isAdmin($uid)) { | 
                                                        |
| 131 | 131 | $users = $this->userManager->search($search, $limit, $offset);  | 
                                                        
| 132 | 132 |  		} else if ($subAdminManager->isSubAdmin($user)) { | 
                                                        
| 133 | 133 | $subAdminOfGroups = $subAdminManager->getSubAdminsGroups($user);  | 
                                                        
@@ -160,7 +160,7 @@ discard block  | 
                                                    ||
| 160 | 160 | // Admin? Or SubAdmin?  | 
                                                        
| 161 | 161 | $uid = $currentUser->getUID();  | 
                                                        
| 162 | 162 | $subAdminManager = $this->groupManager->getSubAdmin();  | 
                                                        
| 163 | -		if ($this->groupManager->isAdmin($uid)){ | 
                                                        |
| 163 | +		if ($this->groupManager->isAdmin($uid)) { | 
                                                        |
| 164 | 164 | $users = $this->userManager->search($search, $limit, $offset);  | 
                                                        
| 165 | 165 | $users = array_keys($users);  | 
                                                        
| 166 | 166 |  		} else if ($subAdminManager->isSubAdmin($currentUser)) { | 
                                                        
@@ -237,7 +237,7 @@ discard block  | 
                                                    ||
| 237 | 237 | $isAdmin = $this->groupManager->isAdmin($user->getUID());  | 
                                                        
| 238 | 238 | $subAdminManager = $this->groupManager->getSubAdmin();  | 
                                                        
| 239 | 239 | |
| 240 | -		if(empty($userid) && $this->config->getAppValue('core', 'newUser.generateUserID', 'no') === 'yes') { | 
                                                        |
| 240 | +		if (empty($userid) && $this->config->getAppValue('core', 'newUser.generateUserID', 'no') === 'yes') { | 
                                                        |
| 241 | 241 | $userid = $this->createNewUserId();  | 
                                                        
| 242 | 242 | }  | 
                                                        
| 243 | 243 | |
@@ -252,7 +252,7 @@ discard block  | 
                                                    ||
| 252 | 252 |  					throw new OCSException('group '.$group.' does not exist', 104); | 
                                                        
| 253 | 253 | }  | 
                                                        
| 254 | 254 |  				if (!$isAdmin && !$subAdminManager->isSubAdminOfGroup($user, $this->groupManager->get($group))) { | 
                                                        
| 255 | -					throw new OCSException('insufficient privileges for group '. $group, 105); | 
                                                        |
| 255 | +					throw new OCSException('insufficient privileges for group '.$group, 105); | 
                                                        |
| 256 | 256 | }  | 
                                                        
| 257 | 257 | }  | 
                                                        
| 258 | 258 |  		} else { | 
                                                        
@@ -267,7 +267,7 @@ discard block  | 
                                                    ||
| 267 | 267 | $group = $this->groupManager->get($groupid);  | 
                                                        
| 268 | 268 | // Check if group exists  | 
                                                        
| 269 | 269 |  				if ($group === null) { | 
                                                        
| 270 | -					throw new OCSException('Subadmin group does not exist',  102); | 
                                                        |
| 270 | +					throw new OCSException('Subadmin group does not exist', 102); | 
                                                        |
| 271 | 271 | }  | 
                                                        
| 272 | 272 | // Check if trying to make subadmin of admin group  | 
                                                        
| 273 | 273 |  				if ($group->getGID() === 'admin') { | 
                                                        
@@ -299,11 +299,11 @@ discard block  | 
                                                    ||
| 299 | 299 | |
| 300 | 300 |  		try { | 
                                                        
| 301 | 301 | $newUser = $this->userManager->createUser($userid, $password);  | 
                                                        
| 302 | -			$this->logger->info('Successful addUser call with userid: ' . $userid, ['app' => 'ocs_api']); | 
                                                        |
| 302 | +			$this->logger->info('Successful addUser call with userid: '.$userid, ['app' => 'ocs_api']); | 
                                                        |
| 303 | 303 | |
| 304 | 304 |  			foreach ($groups as $group) { | 
                                                        
| 305 | 305 | $this->groupManager->get($group)->addUser($newUser);  | 
                                                        
| 306 | -				$this->logger->info('Added userid ' . $userid . ' to group ' . $group, ['app' => 'ocs_api']); | 
                                                        |
| 306 | +				$this->logger->info('Added userid '.$userid.' to group '.$group, ['app' => 'ocs_api']); | 
                                                        |
| 307 | 307 | }  | 
                                                        
| 308 | 308 |  			foreach ($subadminGroups as $group) { | 
                                                        
| 309 | 309 | $subAdminManager->createSubAdmin($newUser, $group);  | 
                                                        
@@ -394,7 +394,7 @@ discard block  | 
                                                    ||
| 394 | 394 |  	public function getCurrentUser(): DataResponse { | 
                                                        
| 395 | 395 | $user = $this->userSession->getUser();  | 
                                                        
| 396 | 396 |  		if ($user) { | 
                                                        
| 397 | - $data = $this->getUserData($user->getUID());  | 
                                                        |
| 397 | + $data = $this->getUserData($user->getUID());  | 
                                                        |
| 398 | 398 | // rename "displayname" to "display-name" only for this call to keep  | 
                                                        
| 399 | 399 | // the API stable.  | 
                                                        
| 400 | 400 | $data['display-name'] = $data['displayname'];  | 
                                                        
@@ -516,7 +516,7 @@ discard block  | 
                                                    ||
| 516 | 516 |  			throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); | 
                                                        
| 517 | 517 | }  | 
                                                        
| 518 | 518 | // Process the edit  | 
                                                        
| 519 | -		switch($key) { | 
                                                        |
| 519 | +		switch ($key) { | 
                                                        |
| 520 | 520 | case 'display':  | 
                                                        
| 521 | 521 | case AccountManager::PROPERTY_DISPLAYNAME:  | 
                                                        
| 522 | 522 | $targetUser->setDisplayName($value);  | 
                                                        
@@ -794,7 +794,7 @@ discard block  | 
                                                    ||
| 794 | 794 |  		} else if (!$this->groupManager->isAdmin($loggedInUser->getUID())) { | 
                                                        
| 795 | 795 | /** @var IGroup[] $subAdminGroups */  | 
                                                        
| 796 | 796 | $subAdminGroups = $subAdminManager->getSubAdminsGroups($loggedInUser);  | 
                                                        
| 797 | -			$subAdminGroups = array_map(function (IGroup $subAdminGroup) { | 
                                                        |
| 797 | +			$subAdminGroups = array_map(function(IGroup $subAdminGroup) { | 
                                                        |
| 798 | 798 | return $subAdminGroup->getGID();  | 
                                                        
| 799 | 799 | }, $subAdminGroups);  | 
                                                        
| 800 | 800 | $userGroups = $this->groupManager->getUserGroupIds($targetUser);  | 
                                                        
@@ -831,7 +831,7 @@ discard block  | 
                                                    ||
| 831 | 831 | }  | 
                                                        
| 832 | 832 | // Check if group exists  | 
                                                        
| 833 | 833 |  		if ($group === null) { | 
                                                        
| 834 | -			throw new OCSException('Group does not exist',  102); | 
                                                        |
| 834 | +			throw new OCSException('Group does not exist', 102); | 
                                                        |
| 835 | 835 | }  | 
                                                        
| 836 | 836 | // Check if trying to make subadmin of admin group  | 
                                                        
| 837 | 837 |  		if ($group->getGID() === 'admin') { | 
                                                        
@@ -928,7 +928,7 @@ discard block  | 
                                                    ||
| 928 | 928 |  		try { | 
                                                        
| 929 | 929 | $emailTemplate = $this->newUserMailHelper->generateTemplate($targetUser, false);  | 
                                                        
| 930 | 930 | $this->newUserMailHelper->sendMail($targetUser, $emailTemplate);  | 
                                                        
| 931 | -		} catch(\Exception $e) { | 
                                                        |
| 931 | +		} catch (\Exception $e) { | 
                                                        |
| 932 | 932 | $this->logger->logException($e, [  | 
                                                        
| 933 | 933 | 'message' => "Can't send new user mail to $email",  | 
                                                        
| 934 | 934 | 'level' => ILogger::ERROR,  |