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