123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440 |
- <?php
- declare(strict_types=1);
- /**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
- * @author Bjoern Schiessle <bjoern@schiessle.org>
- * @author Christoph Wurst <christoph@winzerhof-wurst.at>
- * @author Daniel Calviño Sánchez <danxuliu@gmail.com>
- * @author Daniel Kesselberg <mail@danielkesselberg.de>
- * @author Joas Schilling <coding@schilljs.com>
- * @author John Molakvoæ <skjnldsv@protonmail.com>
- * @author Julius Härtl <jus@bitgrid.net>
- * @author Lukas Reschke <lukas@statuscode.ch>
- * @author michag86 <micha_g@arcor.de>
- * @author Mikael Hammarin <mikael@try2.se>
- * @author Morris Jobke <hey@morrisjobke.de>
- * @author Robin Appelman <robin@icewind.nl>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- * @author Sujith Haridasan <sujith.h@gmail.com>
- * @author Thomas Citharel <nextcloud@tcit.fr>
- * @author Thomas Müller <thomas.mueller@tmit.eu>
- * @author Tom Needham <tom@owncloud.com>
- * @author Vincent Petry <vincent@nextcloud.com>
- * @author Kate Döen <kate.doeen@nextcloud.com>
- *
- * @license AGPL-3.0
- *
- * This code is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License, version 3,
- * as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License, version 3,
- * along with this program. If not, see <http://www.gnu.org/licenses/>
- *
- */
- namespace OCA\Provisioning_API\Controller;
- use InvalidArgumentException;
- use libphonenumber\NumberParseException;
- use libphonenumber\PhoneNumber;
- use libphonenumber\PhoneNumberFormat;
- use libphonenumber\PhoneNumberUtil;
- use OC\Authentication\Token\RemoteWipe;
- use OC\KnownUser\KnownUserService;
- use OC\User\Backend;
- use OCA\Settings\Mailer\NewUserMailHelper;
- use OCP\Accounts\IAccountManager;
- use OCP\Accounts\IAccountProperty;
- use OCP\Accounts\PropertyDoesNotExistException;
- use OCP\AppFramework\Http;
- use OCP\AppFramework\Http\DataResponse;
- use OCP\AppFramework\OCS\OCSException;
- use OCP\AppFramework\OCS\OCSForbiddenException;
- use OCP\AppFramework\OCSController;
- use OCP\EventDispatcher\IEventDispatcher;
- use OCP\HintException;
- use OCP\IConfig;
- use OCP\IGroup;
- use OCP\IGroupManager;
- use OCP\IRequest;
- use OCP\IURLGenerator;
- use OCP\IUser;
- use OCP\IUserManager;
- use OCP\IUserSession;
- use OCP\L10N\IFactory;
- use OCP\Security\Events\GenerateSecurePasswordEvent;
- use OCP\Security\ISecureRandom;
- use OCP\User\Backend\ISetDisplayNameBackend;
- use Psr\Log\LoggerInterface;
- class UsersController extends AUserData {
- /** @var IURLGenerator */
- protected $urlGenerator;
- /** @var LoggerInterface */
- private $logger;
- /** @var IFactory */
- protected $l10nFactory;
- /** @var NewUserMailHelper */
- private $newUserMailHelper;
- /** @var ISecureRandom */
- private $secureRandom;
- /** @var RemoteWipe */
- private $remoteWipe;
- /** @var KnownUserService */
- private $knownUserService;
- /** @var IEventDispatcher */
- private $eventDispatcher;
- public function __construct(
- string $appName,
- IRequest $request,
- IUserManager $userManager,
- IConfig $config,
- IGroupManager $groupManager,
- IUserSession $userSession,
- IAccountManager $accountManager,
- IURLGenerator $urlGenerator,
- LoggerInterface $logger,
- IFactory $l10nFactory,
- NewUserMailHelper $newUserMailHelper,
- ISecureRandom $secureRandom,
- RemoteWipe $remoteWipe,
- KnownUserService $knownUserService,
- IEventDispatcher $eventDispatcher
- ) {
- parent::__construct(
- $appName,
- $request,
- $userManager,
- $config,
- $groupManager,
- $userSession,
- $accountManager,
- $l10nFactory
- );
- $this->urlGenerator = $urlGenerator;
- $this->logger = $logger;
- $this->l10nFactory = $l10nFactory;
- $this->newUserMailHelper = $newUserMailHelper;
- $this->secureRandom = $secureRandom;
- $this->remoteWipe = $remoteWipe;
- $this->knownUserService = $knownUserService;
- $this->eventDispatcher = $eventDispatcher;
- }
- /**
- * @NoAdminRequired
- *
- * returns a list of users
- *
- * @param string $search
- * @param int $limit
- * @param int $offset
- * @return DataResponse
- */
- public function getUsers(string $search = '', int $limit = null, int $offset = 0): DataResponse {
- $user = $this->userSession->getUser();
- $users = [];
- // Admin? Or SubAdmin?
- $uid = $user->getUID();
- $subAdminManager = $this->groupManager->getSubAdmin();
- if ($this->groupManager->isAdmin($uid)) {
- $users = $this->userManager->search($search, $limit, $offset);
- } elseif ($subAdminManager->isSubAdmin($user)) {
- $subAdminOfGroups = $subAdminManager->getSubAdminsGroups($user);
- foreach ($subAdminOfGroups as $key => $group) {
- $subAdminOfGroups[$key] = $group->getGID();
- }
- $users = [];
- foreach ($subAdminOfGroups as $group) {
- $users = array_merge($users, $this->groupManager->displayNamesInGroup($group, $search, $limit, $offset));
- }
- }
- $users = array_keys($users);
- return new DataResponse([
- 'users' => $users
- ]);
- }
- /**
- * @NoAdminRequired
- *
- * returns a list of users and their data
- */
- public function getUsersDetails(string $search = '', int $limit = null, int $offset = 0): DataResponse {
- $currentUser = $this->userSession->getUser();
- $users = [];
- // Admin? Or SubAdmin?
- $uid = $currentUser->getUID();
- $subAdminManager = $this->groupManager->getSubAdmin();
- if ($this->groupManager->isAdmin($uid)) {
- $users = $this->userManager->search($search, $limit, $offset);
- $users = array_keys($users);
- } elseif ($subAdminManager->isSubAdmin($currentUser)) {
- $subAdminOfGroups = $subAdminManager->getSubAdminsGroups($currentUser);
- foreach ($subAdminOfGroups as $key => $group) {
- $subAdminOfGroups[$key] = $group->getGID();
- }
- $users = [];
- foreach ($subAdminOfGroups as $group) {
- $users[] = array_keys($this->groupManager->displayNamesInGroup($group, $search, $limit, $offset));
- }
- $users = array_merge(...$users);
- }
- $usersDetails = [];
- foreach ($users as $userId) {
- $userId = (string) $userId;
- $userData = $this->getUserData($userId);
- // Do not insert empty entry
- if (!empty($userData)) {
- $usersDetails[$userId] = $userData;
- } else {
- // Logged user does not have permissions to see this user
- // only showing its id
- $usersDetails[$userId] = ['id' => $userId];
- }
- }
- return new DataResponse([
- 'users' => $usersDetails
- ]);
- }
- /**
- * @NoAdminRequired
- * @NoSubAdminRequired
- *
- * @param string $location
- * @param array $search
- * @return DataResponse
- */
- public function searchByPhoneNumbers(string $location, array $search): DataResponse {
- $phoneUtil = PhoneNumberUtil::getInstance();
- if ($phoneUtil->getCountryCodeForRegion($location) === 0) {
- // Not a valid region code
- return new DataResponse([], Http::STATUS_BAD_REQUEST);
- }
- /** @var IUser $user */
- $user = $this->userSession->getUser();
- $knownTo = $user->getUID();
- $defaultPhoneRegion = $this->config->getSystemValueString('default_phone_region');
- $normalizedNumberToKey = [];
- foreach ($search as $key => $phoneNumbers) {
- foreach ($phoneNumbers as $phone) {
- try {
- $phoneNumber = $phoneUtil->parse($phone, $location);
- if ($phoneNumber instanceof PhoneNumber && $phoneUtil->isValidNumber($phoneNumber)) {
- $normalizedNumber = $phoneUtil->format($phoneNumber, PhoneNumberFormat::E164);
- $normalizedNumberToKey[$normalizedNumber] = (string) $key;
- }
- } catch (NumberParseException $e) {
- }
- if ($defaultPhoneRegion !== '' && $defaultPhoneRegion !== $location && strpos($phone, '0') === 0) {
- // If the number has a leading zero (no country code),
- // we also check the default phone region of the instance,
- // when it's different to the user's given region.
- try {
- $phoneNumber = $phoneUtil->parse($phone, $defaultPhoneRegion);
- if ($phoneNumber instanceof PhoneNumber && $phoneUtil->isValidNumber($phoneNumber)) {
- $normalizedNumber = $phoneUtil->format($phoneNumber, PhoneNumberFormat::E164);
- $normalizedNumberToKey[$normalizedNumber] = (string) $key;
- }
- } catch (NumberParseException $e) {
- }
- }
- }
- }
- $phoneNumbers = array_keys($normalizedNumberToKey);
- if (empty($phoneNumbers)) {
- return new DataResponse();
- }
- // Cleanup all previous entries and only allow new matches
- $this->knownUserService->deleteKnownTo($knownTo);
- $userMatches = $this->accountManager->searchUsers(IAccountManager::PROPERTY_PHONE, $phoneNumbers);
- if (empty($userMatches)) {
- return new DataResponse();
- }
- $cloudUrl = rtrim($this->urlGenerator->getAbsoluteURL('/'), '/');
- if (strpos($cloudUrl, 'http://') === 0) {
- $cloudUrl = substr($cloudUrl, strlen('http://'));
- } elseif (strpos($cloudUrl, 'https://') === 0) {
- $cloudUrl = substr($cloudUrl, strlen('https://'));
- }
- $matches = [];
- foreach ($userMatches as $phone => $userId) {
- // Not using the ICloudIdManager as that would run a search for each contact to find the display name in the address book
- $matches[$normalizedNumberToKey[$phone]] = $userId . '@' . $cloudUrl;
- $this->knownUserService->storeIsKnownToUser($knownTo, $userId);
- }
- return new DataResponse($matches);
- }
- /**
- * @throws OCSException
- */
- private function createNewUserId(): string {
- $attempts = 0;
- do {
- $uidCandidate = $this->secureRandom->generate(10, ISecureRandom::CHAR_HUMAN_READABLE);
- if (!$this->userManager->userExists($uidCandidate)) {
- return $uidCandidate;
- }
- $attempts++;
- } while ($attempts < 10);
- throw new OCSException('Could not create non-existing user id', 111);
- }
- /**
- * @PasswordConfirmationRequired
- * @NoAdminRequired
- *
- * @param string $userid
- * @param string $password
- * @param string $displayName
- * @param string $email
- * @param array $groups
- * @param array $subadmin
- * @param string $quota
- * @param string $language
- * @return DataResponse
- * @throws OCSException
- */
- public function addUser(
- string $userid,
- string $password = '',
- string $displayName = '',
- string $email = '',
- array $groups = [],
- array $subadmin = [],
- string $quota = '',
- string $language = '',
- ?string $manager = null,
- ): DataResponse {
- $user = $this->userSession->getUser();
- $isAdmin = $this->groupManager->isAdmin($user->getUID());
- $subAdminManager = $this->groupManager->getSubAdmin();
- if (empty($userid) && $this->config->getAppValue('core', 'newUser.generateUserID', 'no') === 'yes') {
- $userid = $this->createNewUserId();
- }
- if ($this->userManager->userExists($userid)) {
- $this->logger->error('Failed addUser attempt: User already exists.', ['app' => 'ocs_api']);
- throw new OCSException($this->l10nFactory->get('provisioning_api')->t('User already exists'), 102);
- }
- if ($groups !== []) {
- foreach ($groups as $group) {
- if (!$this->groupManager->groupExists($group)) {
- throw new OCSException('group ' . $group . ' does not exist', 104);
- }
- if (!$isAdmin && !$subAdminManager->isSubAdminOfGroup($user, $this->groupManager->get($group))) {
- throw new OCSException('insufficient privileges for group ' . $group, 105);
- }
- }
- } else {
- if (!$isAdmin) {
- throw new OCSException('no group specified (required for subadmins)', 106);
- }
- }
- $subadminGroups = [];
- if ($subadmin !== []) {
- foreach ($subadmin as $groupid) {
- $group = $this->groupManager->get($groupid);
- // Check if group exists
- if ($group === null) {
- throw new OCSException('Subadmin group does not exist', 102);
- }
- // Check if trying to make subadmin of admin group
- if ($group->getGID() === 'admin') {
- throw new OCSException('Cannot create subadmins for admin group', 103);
- }
- // Check if has permission to promote subadmins
- if (!$subAdminManager->isSubAdminOfGroup($user, $group) && !$isAdmin) {
- throw new OCSForbiddenException('No permissions to promote subadmins');
- }
- $subadminGroups[] = $group;
- }
- }
- $generatePasswordResetToken = false;
- if (strlen($password) > IUserManager::MAX_PASSWORD_LENGTH) {
- throw new OCSException('Invalid password value', 101);
- }
- if ($password === '') {
- if ($email === '') {
- throw new OCSException('To send a password link to the user an email address is required.', 108);
- }
- $passwordEvent = new GenerateSecurePasswordEvent();
- $this->eventDispatcher->dispatchTyped($passwordEvent);
- $password = $passwordEvent->getPassword();
- if ($password === null) {
- // Fallback: ensure to pass password_policy in any case
- $password = $this->secureRandom->generate(10)
- . $this->secureRandom->generate(1, ISecureRandom::CHAR_UPPER)
- . $this->secureRandom->generate(1, ISecureRandom::CHAR_LOWER)
- . $this->secureRandom->generate(1, ISecureRandom::CHAR_DIGITS)
- . $this->secureRandom->generate(1, ISecureRandom::CHAR_SYMBOLS);
- }
- $generatePasswordResetToken = true;
- }
- if ($email === '' && $this->config->getAppValue('core', 'newUser.requireEmail', 'no') === 'yes') {
- throw new OCSException('Required email address was not provided', 110);
- }
- try {
- $newUser = $this->userManager->createUser($userid, $password);
- $this->logger->info('Successful addUser call with userid: ' . $userid, ['app' => 'ocs_api']);
- foreach ($groups as $group) {
- $this->groupManager->get($group)->addUser($newUser);
- $this->logger->info('Added userid ' . $userid . ' to group ' . $group, ['app' => 'ocs_api']);
- }
- foreach ($subadminGroups as $group) {
- $subAdminManager->createSubAdmin($newUser, $group);
- }
- if ($displayName !== '') {
- try {
- $this->editUser($userid, self::USER_FIELD_DISPLAYNAME, $displayName);
- } catch (OCSException $e) {
- if ($newUser instanceof IUser) {
- $newUser->delete();
- }
- throw $e;
- }
- }
- if ($quota !== '') {
- $this->editUser($userid, self::USER_FIELD_QUOTA, $quota);
- }
- if ($language !== '') {
- $this->editUser($userid, self::USER_FIELD_LANGUAGE, $language);
- }
- /**
- * null -> nothing sent
- * '' -> unset manager
- * else -> set manager
- */
- if ($manager !== null) {
- $this->editUser($userid, self::USER_FIELD_MANAGER, $manager);
- }
- // Send new user mail only if a mail is set
- if ($email !== '') {
- $newUser->setEMailAddress($email);
- if ($this->config->getAppValue('core', 'newUser.sendEmail', 'yes') === 'yes') {
- try {
- $emailTemplate = $this->newUserMailHelper->generateTemplate($newUser, $generatePasswordResetToken);
- $this->newUserMailHelper->sendMail($newUser, $emailTemplate);
- } catch (\Exception $e) {
- // Mail could be failing hard or just be plain not configured
- // Logging error as it is the hardest of the two
- $this->logger->error(
- "Unable to send the invitation mail to $email",
- [
- 'app' => 'ocs_api',
- 'exception' => $e,
- ]
- );
- }
- }
- }
- return new DataResponse(['id' => $userid]);
- } catch (HintException $e) {
- $this->logger->warning(
- 'Failed addUser attempt with hint exception.',
- [
- 'app' => 'ocs_api',
- 'exception' => $e,
- ]
- );
- throw new OCSException($e->getHint(), 107);
- } catch (OCSException $e) {
- $this->logger->warning(
- 'Failed addUser attempt with ocs exception.',
- [
- 'app' => 'ocs_api',
- 'exception' => $e,
- ]
- );
- throw $e;
- } catch (InvalidArgumentException $e) {
- $this->logger->error(
- 'Failed addUser attempt with invalid argument exception.',
- [
- 'app' => 'ocs_api',
- 'exception' => $e,
- ]
- );
- throw new OCSException($e->getMessage(), 101);
- } catch (\Exception $e) {
- $this->logger->error(
- 'Failed addUser attempt with exception.',
- [
- 'app' => 'ocs_api',
- 'exception' => $e
- ]
- );
- throw new OCSException('Bad request', 101);
- }
- }
- /**
- * @NoAdminRequired
- * @NoSubAdminRequired
- *
- * gets user info
- *
- * @param string $userId
- * @return DataResponse
- * @throws OCSException
- */
- public function getUser(string $userId): DataResponse {
- $includeScopes = false;
- $currentUser = $this->userSession->getUser();
- if ($currentUser && $currentUser->getUID() === $userId) {
- $includeScopes = true;
- }
- $data = $this->getUserData($userId, $includeScopes);
- // getUserData returns empty array if not enough permissions
- if (empty($data)) {
- throw new OCSException('', OCSController::RESPOND_NOT_FOUND);
- }
- return new DataResponse($data);
- }
- /**
- * @NoAdminRequired
- * @NoSubAdminRequired
- *
- * gets user info from the currently logged in user
- *
- * @return DataResponse
- * @throws OCSException
- */
- public function getCurrentUser(): DataResponse {
- $user = $this->userSession->getUser();
- if ($user) {
- $data = $this->getUserData($user->getUID(), true);
- return new DataResponse($data);
- }
- throw new OCSException('', OCSController::RESPOND_UNAUTHORISED);
- }
- /**
- * @NoAdminRequired
- * @NoSubAdminRequired
- *
- * @return DataResponse
- * @throws OCSException
- */
- public function getEditableFields(): DataResponse {
- $currentLoggedInUser = $this->userSession->getUser();
- if (!$currentLoggedInUser instanceof IUser) {
- throw new OCSException('', OCSController::RESPOND_NOT_FOUND);
- }
- return $this->getEditableFieldsForUser($currentLoggedInUser->getUID());
- }
- /**
- * @NoAdminRequired
- * @NoSubAdminRequired
- *
- * @param string $userId
- * @return DataResponse
- * @throws OCSException
- */
- public function getEditableFieldsForUser(string $userId): DataResponse {
- $currentLoggedInUser = $this->userSession->getUser();
- if (!$currentLoggedInUser instanceof IUser) {
- throw new OCSException('', OCSController::RESPOND_NOT_FOUND);
- }
- $permittedFields = [];
- if ($userId !== $currentLoggedInUser->getUID()) {
- $targetUser = $this->userManager->get($userId);
- if (!$targetUser instanceof IUser) {
- throw new OCSException('', OCSController::RESPOND_NOT_FOUND);
- }
- $subAdminManager = $this->groupManager->getSubAdmin();
- if (
- !$this->groupManager->isAdmin($currentLoggedInUser->getUID())
- && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)
- ) {
- throw new OCSException('', OCSController::RESPOND_NOT_FOUND);
- }
- } else {
- $targetUser = $currentLoggedInUser;
- }
- // Editing self (display, email)
- if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) {
- if (
- $targetUser->getBackend() instanceof ISetDisplayNameBackend
- || $targetUser->getBackend()->implementsActions(Backend::SET_DISPLAYNAME)
- ) {
- $permittedFields[] = IAccountManager::PROPERTY_DISPLAYNAME;
- }
- $permittedFields[] = IAccountManager::PROPERTY_EMAIL;
- }
- $permittedFields[] = IAccountManager::COLLECTION_EMAIL;
- $permittedFields[] = IAccountManager::PROPERTY_PHONE;
- $permittedFields[] = IAccountManager::PROPERTY_ADDRESS;
- $permittedFields[] = IAccountManager::PROPERTY_WEBSITE;
- $permittedFields[] = IAccountManager::PROPERTY_TWITTER;
- $permittedFields[] = IAccountManager::PROPERTY_FEDIVERSE;
- $permittedFields[] = IAccountManager::PROPERTY_ORGANISATION;
- $permittedFields[] = IAccountManager::PROPERTY_ROLE;
- $permittedFields[] = IAccountManager::PROPERTY_HEADLINE;
- $permittedFields[] = IAccountManager::PROPERTY_BIOGRAPHY;
- $permittedFields[] = IAccountManager::PROPERTY_PROFILE_ENABLED;
- return new DataResponse($permittedFields);
- }
- /**
- * @NoAdminRequired
- * @NoSubAdminRequired
- * @PasswordConfirmationRequired
- * @UserRateThrottle(limit=5, period=60)
- *
- * @throws OCSException
- */
- public function editUserMultiValue(
- string $userId,
- string $collectionName,
- string $key,
- string $value
- ): DataResponse {
- $currentLoggedInUser = $this->userSession->getUser();
- if ($currentLoggedInUser === null) {
- throw new OCSException('', OCSController::RESPOND_UNAUTHORISED);
- }
- $targetUser = $this->userManager->get($userId);
- if ($targetUser === null) {
- throw new OCSException('', OCSController::RESPOND_NOT_FOUND);
- }
- $subAdminManager = $this->groupManager->getSubAdmin();
- $isAdminOrSubadmin = $this->groupManager->isAdmin($currentLoggedInUser->getUID())
- || $subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser);
- $permittedFields = [];
- if ($targetUser->getUID() === $currentLoggedInUser->getUID()) {
- // Editing self (display, email)
- $permittedFields[] = IAccountManager::COLLECTION_EMAIL;
- $permittedFields[] = IAccountManager::COLLECTION_EMAIL . self::SCOPE_SUFFIX;
- } else {
- // Check if admin / subadmin
- if ($isAdminOrSubadmin) {
- // They have permissions over the user
- $permittedFields[] = IAccountManager::COLLECTION_EMAIL;
- } else {
- // No rights
- throw new OCSException('', OCSController::RESPOND_NOT_FOUND);
- }
- }
- // Check if permitted to edit this field
- if (!in_array($collectionName, $permittedFields)) {
- throw new OCSException('', 103);
- }
- switch ($collectionName) {
- case IAccountManager::COLLECTION_EMAIL:
- $userAccount = $this->accountManager->getAccount($targetUser);
- $mailCollection = $userAccount->getPropertyCollection(IAccountManager::COLLECTION_EMAIL);
- $mailCollection->removePropertyByValue($key);
- if ($value !== '') {
- $mailCollection->addPropertyWithDefaults($value);
- $property = $mailCollection->getPropertyByValue($key);
- if ($isAdminOrSubadmin && $property) {
- // admin set mails are auto-verified
- $property->setLocallyVerified(IAccountManager::VERIFIED);
- }
- }
- $this->accountManager->updateAccount($userAccount);
- break;
- case IAccountManager::COLLECTION_EMAIL . self::SCOPE_SUFFIX:
- $userAccount = $this->accountManager->getAccount($targetUser);
- $mailCollection = $userAccount->getPropertyCollection(IAccountManager::COLLECTION_EMAIL);
- $targetProperty = null;
- foreach ($mailCollection->getProperties() as $property) {
- if ($property->getValue() === $key) {
- $targetProperty = $property;
- break;
- }
- }
- if ($targetProperty instanceof IAccountProperty) {
- try {
- $targetProperty->setScope($value);
- $this->accountManager->updateAccount($userAccount);
- } catch (InvalidArgumentException $e) {
- throw new OCSException('', 102);
- }
- } else {
- throw new OCSException('', 102);
- }
- break;
- default:
- throw new OCSException('', 103);
- }
- return new DataResponse();
- }
- /**
- * @NoAdminRequired
- * @NoSubAdminRequired
- * @PasswordConfirmationRequired
- * @UserRateThrottle(limit=50, period=600)
- *
- * edit users
- *
- * @param string $userId
- * @param string $key
- * @param string $value
- * @return DataResponse
- * @throws OCSException
- */
- public function editUser(string $userId, string $key, string $value): DataResponse {
- $currentLoggedInUser = $this->userSession->getUser();
- $targetUser = $this->userManager->get($userId);
- if ($targetUser === null) {
- throw new OCSException('', OCSController::RESPOND_NOT_FOUND);
- }
- $permittedFields = [];
- if ($targetUser->getUID() === $currentLoggedInUser->getUID()) {
- // Editing self (display, email)
- if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) {
- if (
- $targetUser->getBackend() instanceof ISetDisplayNameBackend
- || $targetUser->getBackend()->implementsActions(Backend::SET_DISPLAYNAME)
- ) {
- $permittedFields[] = self::USER_FIELD_DISPLAYNAME;
- $permittedFields[] = IAccountManager::PROPERTY_DISPLAYNAME;
- }
- $permittedFields[] = IAccountManager::PROPERTY_EMAIL;
- }
- $permittedFields[] = IAccountManager::PROPERTY_DISPLAYNAME . self::SCOPE_SUFFIX;
- $permittedFields[] = IAccountManager::PROPERTY_EMAIL . self::SCOPE_SUFFIX;
- $permittedFields[] = IAccountManager::COLLECTION_EMAIL;
- $permittedFields[] = self::USER_FIELD_PASSWORD;
- $permittedFields[] = self::USER_FIELD_NOTIFICATION_EMAIL;
- if (
- $this->config->getSystemValue('force_language', false) === false ||
- $this->groupManager->isAdmin($currentLoggedInUser->getUID())
- ) {
- $permittedFields[] = self::USER_FIELD_LANGUAGE;
- }
- if (
- $this->config->getSystemValue('force_locale', false) === false ||
- $this->groupManager->isAdmin($currentLoggedInUser->getUID())
- ) {
- $permittedFields[] = self::USER_FIELD_LOCALE;
- }
- $permittedFields[] = IAccountManager::PROPERTY_PHONE;
- $permittedFields[] = IAccountManager::PROPERTY_ADDRESS;
- $permittedFields[] = IAccountManager::PROPERTY_WEBSITE;
- $permittedFields[] = IAccountManager::PROPERTY_TWITTER;
- $permittedFields[] = IAccountManager::PROPERTY_FEDIVERSE;
- $permittedFields[] = IAccountManager::PROPERTY_ORGANISATION;
- $permittedFields[] = IAccountManager::PROPERTY_ROLE;
- $permittedFields[] = IAccountManager::PROPERTY_HEADLINE;
- $permittedFields[] = IAccountManager::PROPERTY_BIOGRAPHY;
- $permittedFields[] = IAccountManager::PROPERTY_PROFILE_ENABLED;
- $permittedFields[] = IAccountManager::PROPERTY_PHONE . self::SCOPE_SUFFIX;
- $permittedFields[] = IAccountManager::PROPERTY_ADDRESS . self::SCOPE_SUFFIX;
- $permittedFields[] = IAccountManager::PROPERTY_WEBSITE . self::SCOPE_SUFFIX;
- $permittedFields[] = IAccountManager::PROPERTY_TWITTER . self::SCOPE_SUFFIX;
- $permittedFields[] = IAccountManager::PROPERTY_FEDIVERSE . self::SCOPE_SUFFIX;
- $permittedFields[] = IAccountManager::PROPERTY_ORGANISATION . self::SCOPE_SUFFIX;
- $permittedFields[] = IAccountManager::PROPERTY_ROLE . self::SCOPE_SUFFIX;
- $permittedFields[] = IAccountManager::PROPERTY_HEADLINE . self::SCOPE_SUFFIX;
- $permittedFields[] = IAccountManager::PROPERTY_BIOGRAPHY . self::SCOPE_SUFFIX;
- $permittedFields[] = IAccountManager::PROPERTY_PROFILE_ENABLED . self::SCOPE_SUFFIX;
- $permittedFields[] = IAccountManager::PROPERTY_AVATAR . self::SCOPE_SUFFIX;
- // If admin they can edit their own quota and manager
- if ($this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
- $permittedFields[] = self::USER_FIELD_QUOTA;
- $permittedFields[] = self::USER_FIELD_MANAGER;
- }
- } else {
- // Check if admin / subadmin
- $subAdminManager = $this->groupManager->getSubAdmin();
- if (
- $this->groupManager->isAdmin($currentLoggedInUser->getUID())
- || $subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)
- ) {
- // They have permissions over the user
- if (
- $targetUser->getBackend() instanceof ISetDisplayNameBackend
- || $targetUser->getBackend()->implementsActions(Backend::SET_DISPLAYNAME)
- ) {
- $permittedFields[] = self::USER_FIELD_DISPLAYNAME;
- $permittedFields[] = IAccountManager::PROPERTY_DISPLAYNAME;
- }
- $permittedFields[] = IAccountManager::PROPERTY_EMAIL;
- $permittedFields[] = IAccountManager::COLLECTION_EMAIL;
- $permittedFields[] = self::USER_FIELD_PASSWORD;
- $permittedFields[] = self::USER_FIELD_LANGUAGE;
- $permittedFields[] = self::USER_FIELD_LOCALE;
- $permittedFields[] = IAccountManager::PROPERTY_PHONE;
- $permittedFields[] = IAccountManager::PROPERTY_ADDRESS;
- $permittedFields[] = IAccountManager::PROPERTY_WEBSITE;
- $permittedFields[] = IAccountManager::PROPERTY_TWITTER;
- $permittedFields[] = IAccountManager::PROPERTY_FEDIVERSE;
- $permittedFields[] = IAccountManager::PROPERTY_ORGANISATION;
- $permittedFields[] = IAccountManager::PROPERTY_ROLE;
- $permittedFields[] = IAccountManager::PROPERTY_HEADLINE;
- $permittedFields[] = IAccountManager::PROPERTY_BIOGRAPHY;
- $permittedFields[] = IAccountManager::PROPERTY_PROFILE_ENABLED;
- $permittedFields[] = self::USER_FIELD_QUOTA;
- $permittedFields[] = self::USER_FIELD_NOTIFICATION_EMAIL;
- $permittedFields[] = self::USER_FIELD_MANAGER;
- } else {
- // No rights
- throw new OCSException('', OCSController::RESPOND_NOT_FOUND);
- }
- }
- // Check if permitted to edit this field
- if (!in_array($key, $permittedFields)) {
- throw new OCSException('', 103);
- }
- // Process the edit
- switch ($key) {
- case self::USER_FIELD_DISPLAYNAME:
- case IAccountManager::PROPERTY_DISPLAYNAME:
- try {
- $targetUser->setDisplayName($value);
- } catch (InvalidArgumentException $e) {
- throw new OCSException($e->getMessage(), 101);
- }
- break;
- case self::USER_FIELD_QUOTA:
- $quota = $value;
- if ($quota !== 'none' && $quota !== 'default') {
- if (is_numeric($quota)) {
- $quota = (float) $quota;
- } else {
- $quota = \OCP\Util::computerFileSize($quota);
- }
- if ($quota === false) {
- throw new OCSException('Invalid quota value ' . $value, 102);
- }
- if ($quota === -1) {
- $quota = 'none';
- } else {
- $maxQuota = (int) $this->config->getAppValue('files', 'max_quota', '-1');
- if ($maxQuota !== -1 && $quota > $maxQuota) {
- throw new OCSException('Invalid quota value. ' . $value . ' is exceeding the maximum quota', 102);
- }
- $quota = \OCP\Util::humanFileSize($quota);
- }
- }
- // no else block because quota can be set to 'none' in previous if
- if ($quota === 'none') {
- $allowUnlimitedQuota = $this->config->getAppValue('files', 'allow_unlimited_quota', '1') === '1';
- if (!$allowUnlimitedQuota) {
- throw new OCSException('Unlimited quota is forbidden on this instance', 102);
- }
- }
- $targetUser->setQuota($quota);
- break;
- case self::USER_FIELD_MANAGER:
- $targetUser->setManagerUids([$value]);
- break;
- case self::USER_FIELD_PASSWORD:
- try {
- if (strlen($value) > IUserManager::MAX_PASSWORD_LENGTH) {
- throw new OCSException('Invalid password value', 102);
- }
- if (!$targetUser->canChangePassword()) {
- throw new OCSException('Setting the password is not supported by the users backend', 103);
- }
- $targetUser->setPassword($value);
- } catch (HintException $e) { // password policy error
- throw new OCSException($e->getMessage(), 103);
- }
- break;
- case self::USER_FIELD_LANGUAGE:
- $languagesCodes = $this->l10nFactory->findAvailableLanguages();
- if (!in_array($value, $languagesCodes, true) && $value !== 'en') {
- throw new OCSException('Invalid language', 102);
- }
- $this->config->setUserValue($targetUser->getUID(), 'core', 'lang', $value);
- break;
- case self::USER_FIELD_LOCALE:
- if (!$this->l10nFactory->localeExists($value)) {
- throw new OCSException('Invalid locale', 102);
- }
- $this->config->setUserValue($targetUser->getUID(), 'core', 'locale', $value);
- break;
- case self::USER_FIELD_NOTIFICATION_EMAIL:
- $success = false;
- if ($value === '' || filter_var($value, FILTER_VALIDATE_EMAIL)) {
- try {
- $targetUser->setPrimaryEMailAddress($value);
- $success = true;
- } catch (InvalidArgumentException $e) {
- $this->logger->info(
- 'Cannot set primary email, because provided address is not verified',
- [
- 'app' => 'provisioning_api',
- 'exception' => $e,
- ]
- );
- }
- }
- if (!$success) {
- throw new OCSException('', 102);
- }
- break;
- case IAccountManager::PROPERTY_EMAIL:
- if (filter_var($value, FILTER_VALIDATE_EMAIL) || $value === '') {
- $targetUser->setEMailAddress($value);
- } else {
- throw new OCSException('', 102);
- }
- break;
- case IAccountManager::COLLECTION_EMAIL:
- if (filter_var($value, FILTER_VALIDATE_EMAIL) && $value !== $targetUser->getSystemEMailAddress()) {
- $userAccount = $this->accountManager->getAccount($targetUser);
- $mailCollection = $userAccount->getPropertyCollection(IAccountManager::COLLECTION_EMAIL);
- if ($mailCollection->getPropertyByValue($value)) {
- throw new OCSException('', 102);
- }
- $mailCollection->addPropertyWithDefaults($value);
- $this->accountManager->updateAccount($userAccount);
- } else {
- throw new OCSException('', 102);
- }
- break;
- case IAccountManager::PROPERTY_PHONE:
- case IAccountManager::PROPERTY_ADDRESS:
- case IAccountManager::PROPERTY_WEBSITE:
- case IAccountManager::PROPERTY_TWITTER:
- case IAccountManager::PROPERTY_FEDIVERSE:
- case IAccountManager::PROPERTY_ORGANISATION:
- case IAccountManager::PROPERTY_ROLE:
- case IAccountManager::PROPERTY_HEADLINE:
- case IAccountManager::PROPERTY_BIOGRAPHY:
- $userAccount = $this->accountManager->getAccount($targetUser);
- try {
- $userProperty = $userAccount->getProperty($key);
- if ($userProperty->getValue() !== $value) {
- try {
- $userProperty->setValue($value);
- if ($userProperty->getName() === IAccountManager::PROPERTY_PHONE) {
- $this->knownUserService->deleteByContactUserId($targetUser->getUID());
- }
- } catch (InvalidArgumentException $e) {
- throw new OCSException('Invalid ' . $e->getMessage(), 102);
- }
- }
- } catch (PropertyDoesNotExistException $e) {
- $userAccount->setProperty($key, $value, IAccountManager::SCOPE_PRIVATE, IAccountManager::NOT_VERIFIED);
- }
- try {
- $this->accountManager->updateAccount($userAccount);
- } catch (InvalidArgumentException $e) {
- throw new OCSException('Invalid ' . $e->getMessage(), 102);
- }
- break;
- case IAccountManager::PROPERTY_PROFILE_ENABLED:
- $userAccount = $this->accountManager->getAccount($targetUser);
- try {
- $userProperty = $userAccount->getProperty($key);
- if ($userProperty->getValue() !== $value) {
- $userProperty->setValue($value);
- }
- } catch (PropertyDoesNotExistException $e) {
- $userAccount->setProperty($key, $value, IAccountManager::SCOPE_LOCAL, IAccountManager::NOT_VERIFIED);
- }
- $this->accountManager->updateAccount($userAccount);
- break;
- case IAccountManager::PROPERTY_DISPLAYNAME . self::SCOPE_SUFFIX:
- case IAccountManager::PROPERTY_EMAIL . self::SCOPE_SUFFIX:
- case IAccountManager::PROPERTY_PHONE . self::SCOPE_SUFFIX:
- case IAccountManager::PROPERTY_ADDRESS . self::SCOPE_SUFFIX:
- case IAccountManager::PROPERTY_WEBSITE . self::SCOPE_SUFFIX:
- case IAccountManager::PROPERTY_TWITTER . self::SCOPE_SUFFIX:
- case IAccountManager::PROPERTY_FEDIVERSE . self::SCOPE_SUFFIX:
- case IAccountManager::PROPERTY_ORGANISATION . self::SCOPE_SUFFIX:
- case IAccountManager::PROPERTY_ROLE . self::SCOPE_SUFFIX:
- case IAccountManager::PROPERTY_HEADLINE . self::SCOPE_SUFFIX:
- case IAccountManager::PROPERTY_BIOGRAPHY . self::SCOPE_SUFFIX:
- case IAccountManager::PROPERTY_PROFILE_ENABLED . self::SCOPE_SUFFIX:
- case IAccountManager::PROPERTY_AVATAR . self::SCOPE_SUFFIX:
- $propertyName = substr($key, 0, strlen($key) - strlen(self::SCOPE_SUFFIX));
- $userAccount = $this->accountManager->getAccount($targetUser);
- $userProperty = $userAccount->getProperty($propertyName);
- if ($userProperty->getScope() !== $value) {
- try {
- $userProperty->setScope($value);
- $this->accountManager->updateAccount($userAccount);
- } catch (InvalidArgumentException $e) {
- throw new OCSException('Invalid ' . $e->getMessage(), 102);
- }
- }
- break;
- default:
- throw new OCSException('', 103);
- }
- return new DataResponse();
- }
- /**
- * @PasswordConfirmationRequired
- * @NoAdminRequired
- *
- * @param string $userId
- *
- * @return DataResponse
- *
- * @throws OCSException
- */
- public function wipeUserDevices(string $userId): DataResponse {
- /** @var IUser $currentLoggedInUser */
- $currentLoggedInUser = $this->userSession->getUser();
- $targetUser = $this->userManager->get($userId);
- if ($targetUser === null) {
- throw new OCSException('', OCSController::RESPOND_NOT_FOUND);
- }
- if ($targetUser->getUID() === $currentLoggedInUser->getUID()) {
- throw new OCSException('', 101);
- }
- // If not permitted
- $subAdminManager = $this->groupManager->getSubAdmin();
- if (!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) {
- throw new OCSException('', OCSController::RESPOND_NOT_FOUND);
- }
- $this->remoteWipe->markAllTokensForWipe($targetUser);
- return new DataResponse();
- }
- /**
- * @PasswordConfirmationRequired
- * @NoAdminRequired
- *
- * @param string $userId
- * @return DataResponse
- * @throws OCSException
- */
- public function deleteUser(string $userId): DataResponse {
- $currentLoggedInUser = $this->userSession->getUser();
- $targetUser = $this->userManager->get($userId);
- if ($targetUser === null) {
- throw new OCSException('', OCSController::RESPOND_NOT_FOUND);
- }
- if ($targetUser->getUID() === $currentLoggedInUser->getUID()) {
- throw new OCSException('', 101);
- }
- // If not permitted
- $subAdminManager = $this->groupManager->getSubAdmin();
- if (!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) {
- throw new OCSException('', OCSController::RESPOND_NOT_FOUND);
- }
- // Go ahead with the delete
- if ($targetUser->delete()) {
- return new DataResponse();
- } else {
- throw new OCSException('', 101);
- }
- }
- /**
- * @PasswordConfirmationRequired
- * @NoAdminRequired
- *
- * @param string $userId
- * @return DataResponse
- * @throws OCSException
- * @throws OCSForbiddenException
- */
- public function disableUser(string $userId): DataResponse {
- return $this->setEnabled($userId, false);
- }
- /**
- * @PasswordConfirmationRequired
- * @NoAdminRequired
- *
- * @param string $userId
- * @return DataResponse
- * @throws OCSException
- * @throws OCSForbiddenException
- */
- public function enableUser(string $userId): DataResponse {
- return $this->setEnabled($userId, true);
- }
- /**
- * @param string $userId
- * @param bool $value
- * @return DataResponse
- * @throws OCSException
- */
- private function setEnabled(string $userId, bool $value): DataResponse {
- $currentLoggedInUser = $this->userSession->getUser();
- $targetUser = $this->userManager->get($userId);
- if ($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) {
- throw new OCSException('', 101);
- }
- // If not permitted
- $subAdminManager = $this->groupManager->getSubAdmin();
- if (!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) {
- throw new OCSException('', OCSController::RESPOND_NOT_FOUND);
- }
- // enable/disable the user now
- $targetUser->setEnabled($value);
- return new DataResponse();
- }
- /**
- * @NoAdminRequired
- * @NoSubAdminRequired
- *
- * @param string $userId
- * @return DataResponse
- * @throws OCSException
- */
- public function getUsersGroups(string $userId): DataResponse {
- $loggedInUser = $this->userSession->getUser();
- $targetUser = $this->userManager->get($userId);
- if ($targetUser === null) {
- throw new OCSException('', OCSController::RESPOND_NOT_FOUND);
- }
- if ($targetUser->getUID() === $loggedInUser->getUID() || $this->groupManager->isAdmin($loggedInUser->getUID())) {
- // Self lookup or admin lookup
- return new DataResponse([
- 'groups' => $this->groupManager->getUserGroupIds($targetUser)
- ]);
- } else {
- $subAdminManager = $this->groupManager->getSubAdmin();
- // Looking up someone else
- if ($subAdminManager->isUserAccessible($loggedInUser, $targetUser)) {
- // Return the group that the method caller is subadmin of for the user in question
- /** @var IGroup[] $getSubAdminsGroups */
- $getSubAdminsGroups = $subAdminManager->getSubAdminsGroups($loggedInUser);
- foreach ($getSubAdminsGroups as $key => $group) {
- $getSubAdminsGroups[$key] = $group->getGID();
- }
- $groups = array_intersect(
- $getSubAdminsGroups,
- $this->groupManager->getUserGroupIds($targetUser)
- );
- return new DataResponse(['groups' => $groups]);
- } else {
- // Not permitted
- throw new OCSException('', OCSController::RESPOND_NOT_FOUND);
- }
- }
- }
- /**
- * @PasswordConfirmationRequired
- * @NoAdminRequired
- *
- * @param string $userId
- * @param string $groupid
- * @return DataResponse
- * @throws OCSException
- */
- public function addToGroup(string $userId, string $groupid = ''): DataResponse {
- if ($groupid === '') {
- throw new OCSException('', 101);
- }
- $group = $this->groupManager->get($groupid);
- $targetUser = $this->userManager->get($userId);
- if ($group === null) {
- throw new OCSException('', 102);
- }
- if ($targetUser === null) {
- throw new OCSException('', 103);
- }
- // If they're not an admin, check they are a subadmin of the group in question
- $loggedInUser = $this->userSession->getUser();
- $subAdminManager = $this->groupManager->getSubAdmin();
- if (!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminOfGroup($loggedInUser, $group)) {
- throw new OCSException('', 104);
- }
- // Add user to group
- $group->addUser($targetUser);
- return new DataResponse();
- }
- /**
- * @PasswordConfirmationRequired
- * @NoAdminRequired
- *
- * @param string $userId
- * @param string $groupid
- * @return DataResponse
- * @throws OCSException
- */
- public function removeFromGroup(string $userId, string $groupid): DataResponse {
- $loggedInUser = $this->userSession->getUser();
- if ($groupid === null || trim($groupid) === '') {
- throw new OCSException('', 101);
- }
- $group = $this->groupManager->get($groupid);
- if ($group === null) {
- throw new OCSException('', 102);
- }
- $targetUser = $this->userManager->get($userId);
- if ($targetUser === null) {
- throw new OCSException('', 103);
- }
- // If they're not an admin, check they are a subadmin of the group in question
- $subAdminManager = $this->groupManager->getSubAdmin();
- if (!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminOfGroup($loggedInUser, $group)) {
- throw new OCSException('', 104);
- }
- // Check they aren't removing themselves from 'admin' or their 'subadmin; group
- if ($targetUser->getUID() === $loggedInUser->getUID()) {
- if ($this->groupManager->isAdmin($loggedInUser->getUID())) {
- if ($group->getGID() === 'admin') {
- throw new OCSException('Cannot remove yourself from the admin group', 105);
- }
- } else {
- // Not an admin, so the user must be a subadmin of this group, but that is not allowed.
- throw new OCSException('Cannot remove yourself from this group as you are a SubAdmin', 105);
- }
- } elseif (!$this->groupManager->isAdmin($loggedInUser->getUID())) {
- /** @var IGroup[] $subAdminGroups */
- $subAdminGroups = $subAdminManager->getSubAdminsGroups($loggedInUser);
- $subAdminGroups = array_map(function (IGroup $subAdminGroup) {
- return $subAdminGroup->getGID();
- }, $subAdminGroups);
- $userGroups = $this->groupManager->getUserGroupIds($targetUser);
- $userSubAdminGroups = array_intersect($subAdminGroups, $userGroups);
- if (count($userSubAdminGroups) <= 1) {
- // Subadmin must not be able to remove a user from all their subadmin groups.
- throw new OCSException('Not viable to remove user from the last group you are SubAdmin of', 105);
- }
- }
- // Remove user from group
- $group->removeUser($targetUser);
- return new DataResponse();
- }
- /**
- * Creates a subadmin
- *
- * @PasswordConfirmationRequired
- *
- * @param string $userId
- * @param string $groupid
- * @return DataResponse
- * @throws OCSException
- */
- public function addSubAdmin(string $userId, string $groupid): DataResponse {
- $group = $this->groupManager->get($groupid);
- $user = $this->userManager->get($userId);
- // Check if the user exists
- if ($user === null) {
- throw new OCSException('User does not exist', 101);
- }
- // Check if group exists
- if ($group === null) {
- throw new OCSException('Group does not exist', 102);
- }
- // Check if trying to make subadmin of admin group
- if ($group->getGID() === 'admin') {
- throw new OCSException('Cannot create subadmins for admin group', 103);
- }
- $subAdminManager = $this->groupManager->getSubAdmin();
- // We cannot be subadmin twice
- if ($subAdminManager->isSubAdminOfGroup($user, $group)) {
- return new DataResponse();
- }
- // Go
- $subAdminManager->createSubAdmin($user, $group);
- return new DataResponse();
- }
- /**
- * Removes a subadmin from a group
- *
- * @PasswordConfirmationRequired
- *
- * @param string $userId
- * @param string $groupid
- * @return DataResponse
- * @throws OCSException
- */
- public function removeSubAdmin(string $userId, string $groupid): DataResponse {
- $group = $this->groupManager->get($groupid);
- $user = $this->userManager->get($userId);
- $subAdminManager = $this->groupManager->getSubAdmin();
- // Check if the user exists
- if ($user === null) {
- throw new OCSException('User does not exist', 101);
- }
- // Check if the group exists
- if ($group === null) {
- throw new OCSException('Group does not exist', 101);
- }
- // Check if they are a subadmin of this said group
- if (!$subAdminManager->isSubAdminOfGroup($user, $group)) {
- throw new OCSException('User is not a subadmin of this group', 102);
- }
- // Go
- $subAdminManager->deleteSubAdmin($user, $group);
- return new DataResponse();
- }
- /**
- * Get the groups a user is a subadmin of
- *
- * @param string $userId
- * @return DataResponse
- * @throws OCSException
- */
- public function getUserSubAdminGroups(string $userId): DataResponse {
- $groups = $this->getUserSubAdminGroupsData($userId);
- return new DataResponse($groups);
- }
- /**
- * @NoAdminRequired
- * @PasswordConfirmationRequired
- *
- * resend welcome message
- *
- * @param string $userId
- * @return DataResponse
- * @throws OCSException
- */
- public function resendWelcomeMessage(string $userId): DataResponse {
- $currentLoggedInUser = $this->userSession->getUser();
- $targetUser = $this->userManager->get($userId);
- if ($targetUser === null) {
- throw new OCSException('', OCSController::RESPOND_NOT_FOUND);
- }
- // Check if admin / subadmin
- $subAdminManager = $this->groupManager->getSubAdmin();
- if (
- !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)
- && !$this->groupManager->isAdmin($currentLoggedInUser->getUID())
- ) {
- // No rights
- throw new OCSException('', OCSController::RESPOND_NOT_FOUND);
- }
- $email = $targetUser->getEMailAddress();
- if ($email === '' || $email === null) {
- throw new OCSException('Email address not available', 101);
- }
- try {
- $emailTemplate = $this->newUserMailHelper->generateTemplate($targetUser, false);
- $this->newUserMailHelper->sendMail($targetUser, $emailTemplate);
- } catch (\Exception $e) {
- $this->logger->error(
- "Can't send new user mail to $email",
- [
- 'app' => 'settings',
- 'exception' => $e,
- ]
- );
- throw new OCSException('Sending email failed', 102);
- }
- return new DataResponse();
- }
- }
|