2014-12-22 16:54:50 +03:00
|
|
|
<?php
|
2019-12-03 21:57:53 +03:00
|
|
|
|
2018-02-27 17:47:59 +03:00
|
|
|
declare(strict_types=1);
|
2019-12-03 21:57:53 +03:00
|
|
|
|
2014-12-22 16:54:50 +03:00
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Bjoern Schiessle <bjoern@schiessle.org>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
|
|
|
* @author Daniel Calviño Sánchez <danxuliu@gmail.com>
|
|
|
|
* @author Daniel Kesselberg <mail@danielkesselberg.de>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
|
|
|
|
* @author Julius Härtl <jus@bitgrid.net>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-10-26 15:54:55 +03:00
|
|
|
* @author michag86 <micha_g@arcor.de>
|
2020-08-24 15:54:25 +03:00
|
|
|
* @author Mikael Hammarin <mikael@try2.se>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2020-12-16 16:54:15 +03:00
|
|
|
* @author Sujith Haridasan <sujith.h@gmail.com>
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Thomas Citharel <nextcloud@tcit.fr>
|
2015-06-25 12:43:55 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Tom Needham <tom@owncloud.com>
|
2014-12-22 16:54:50 +03:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* @license AGPL-3.0
|
2014-12-22 16:54:50 +03:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* 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.
|
2014-12-22 16:54:50 +03:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2014-12-22 16:54:50 +03:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-03-26 13:44:34 +03:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
2014-12-22 16:54:50 +03:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2014-12-22 16:54:50 +03:00
|
|
|
*
|
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2016-08-11 20:49:45 +03:00
|
|
|
namespace OCA\Provisioning_API\Controller;
|
2014-12-22 16:54:50 +03:00
|
|
|
|
2020-12-02 16:11:47 +03:00
|
|
|
use libphonenumber\NumberParseException;
|
|
|
|
use libphonenumber\PhoneNumber;
|
|
|
|
use libphonenumber\PhoneNumberFormat;
|
|
|
|
use libphonenumber\PhoneNumberUtil;
|
2017-01-24 16:07:52 +03:00
|
|
|
use OC\Accounts\AccountManager;
|
2019-07-03 11:10:56 +03:00
|
|
|
use OC\Authentication\Token\RemoteWipe;
|
2018-02-22 16:16:49 +03:00
|
|
|
use OC\HintException;
|
2021-03-09 23:22:59 +03:00
|
|
|
use OC\KnownUser\KnownUserService;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCA\Settings\Mailer\NewUserMailHelper;
|
2020-12-01 16:33:22 +03:00
|
|
|
use OCP\Accounts\IAccountManager;
|
2017-06-02 11:09:42 +03:00
|
|
|
use OCP\App\IAppManager;
|
2020-12-03 22:44:29 +03:00
|
|
|
use OCP\AppFramework\Http;
|
2016-08-11 20:49:45 +03:00
|
|
|
use OCP\AppFramework\Http\DataResponse;
|
|
|
|
use OCP\AppFramework\OCS\OCSException;
|
|
|
|
use OCP\AppFramework\OCS\OCSForbiddenException;
|
2021-03-03 17:10:42 +03:00
|
|
|
use OCP\AppFramework\OCSController;
|
2016-04-07 18:22:21 +03:00
|
|
|
use OCP\IConfig;
|
2017-01-18 13:43:52 +03:00
|
|
|
use OCP\IGroup;
|
2016-04-07 18:22:21 +03:00
|
|
|
use OCP\IGroupManager;
|
2016-08-11 20:49:45 +03:00
|
|
|
use OCP\IRequest;
|
2020-12-02 16:11:47 +03:00
|
|
|
use OCP\IURLGenerator;
|
2019-07-03 11:10:56 +03:00
|
|
|
use OCP\IUser;
|
2016-04-07 18:22:21 +03:00
|
|
|
use OCP\IUserManager;
|
|
|
|
use OCP\IUserSession;
|
2017-02-24 09:01:13 +03:00
|
|
|
use OCP\L10N\IFactory;
|
2018-03-16 17:30:48 +03:00
|
|
|
use OCP\Security\ISecureRandom;
|
2020-08-09 17:36:19 +03:00
|
|
|
use OCP\Security\Events\GenerateSecurePasswordEvent;
|
|
|
|
use OCP\EventDispatcher\IEventDispatcher;
|
2021-04-08 14:28:13 +03:00
|
|
|
use OCP\User\Backend\ISetDisplayNameBackend;
|
2021-03-02 23:46:51 +03:00
|
|
|
use Psr\Log\LoggerInterface;
|
2014-12-22 16:54:50 +03:00
|
|
|
|
2018-03-27 12:05:36 +03:00
|
|
|
class UsersController extends AUserData {
|
2014-12-22 16:54:50 +03:00
|
|
|
|
2017-06-02 11:09:42 +03:00
|
|
|
/** @var IAppManager */
|
|
|
|
private $appManager;
|
2020-12-02 16:11:47 +03:00
|
|
|
/** @var IURLGenerator */
|
|
|
|
protected $urlGenerator;
|
2021-03-02 23:46:51 +03:00
|
|
|
/** @var LoggerInterface */
|
2015-10-27 16:09:45 +03:00
|
|
|
private $logger;
|
2017-02-24 09:01:13 +03:00
|
|
|
/** @var IFactory */
|
2020-06-23 09:45:01 +03:00
|
|
|
protected $l10nFactory;
|
2017-04-07 15:51:05 +03:00
|
|
|
/** @var NewUserMailHelper */
|
|
|
|
private $newUserMailHelper;
|
2018-03-16 17:30:48 +03:00
|
|
|
/** @var ISecureRandom */
|
|
|
|
private $secureRandom;
|
2019-07-03 11:10:56 +03:00
|
|
|
/** @var RemoteWipe */
|
|
|
|
private $remoteWipe;
|
2021-03-09 23:22:59 +03:00
|
|
|
/** @var KnownUserService */
|
|
|
|
private $knownUserService;
|
2020-08-09 17:36:19 +03:00
|
|
|
/** @var IEventDispatcher */
|
|
|
|
private $eventDispatcher;
|
2015-07-25 16:01:31 +03:00
|
|
|
|
2018-02-27 17:47:59 +03:00
|
|
|
public function __construct(string $appName,
|
2016-08-11 20:49:45 +03:00
|
|
|
IRequest $request,
|
|
|
|
IUserManager $userManager,
|
2016-04-07 18:22:21 +03:00
|
|
|
IConfig $config,
|
2017-06-02 11:09:42 +03:00
|
|
|
IAppManager $appManager,
|
2016-04-07 18:22:21 +03:00
|
|
|
IGroupManager $groupManager,
|
|
|
|
IUserSession $userSession,
|
2017-01-24 16:07:52 +03:00
|
|
|
AccountManager $accountManager,
|
2020-12-02 16:11:47 +03:00
|
|
|
IURLGenerator $urlGenerator,
|
2021-03-02 23:46:51 +03:00
|
|
|
LoggerInterface $logger,
|
2017-04-07 15:51:05 +03:00
|
|
|
IFactory $l10nFactory,
|
2018-01-26 14:47:59 +03:00
|
|
|
NewUserMailHelper $newUserMailHelper,
|
2019-07-03 11:10:56 +03:00
|
|
|
ISecureRandom $secureRandom,
|
2020-08-09 17:36:19 +03:00
|
|
|
RemoteWipe $remoteWipe,
|
2021-03-09 23:22:59 +03:00
|
|
|
KnownUserService $knownUserService,
|
2020-08-09 17:36:19 +03:00
|
|
|
IEventDispatcher $eventDispatcher) {
|
2018-03-27 12:05:36 +03:00
|
|
|
parent::__construct($appName,
|
|
|
|
$request,
|
|
|
|
$userManager,
|
|
|
|
$config,
|
|
|
|
$groupManager,
|
|
|
|
$userSession,
|
2020-06-23 09:45:01 +03:00
|
|
|
$accountManager,
|
|
|
|
$l10nFactory);
|
2016-08-11 20:49:45 +03:00
|
|
|
|
2017-06-02 11:09:42 +03:00
|
|
|
$this->appManager = $appManager;
|
2020-12-02 16:11:47 +03:00
|
|
|
$this->urlGenerator = $urlGenerator;
|
2015-10-27 16:09:45 +03:00
|
|
|
$this->logger = $logger;
|
2017-02-24 09:01:13 +03:00
|
|
|
$this->l10nFactory = $l10nFactory;
|
2017-04-07 15:51:05 +03:00
|
|
|
$this->newUserMailHelper = $newUserMailHelper;
|
2018-03-16 17:30:48 +03:00
|
|
|
$this->secureRandom = $secureRandom;
|
2019-07-03 11:10:56 +03:00
|
|
|
$this->remoteWipe = $remoteWipe;
|
2021-03-09 23:22:59 +03:00
|
|
|
$this->knownUserService = $knownUserService;
|
2020-08-09 17:36:19 +03:00
|
|
|
$this->eventDispatcher = $eventDispatcher;
|
2015-07-25 13:46:39 +03:00
|
|
|
}
|
|
|
|
|
2014-12-22 16:54:50 +03:00
|
|
|
/**
|
2016-08-11 20:49:45 +03:00
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
2014-12-22 16:54:50 +03:00
|
|
|
* returns a list of users
|
2015-09-08 15:02:30 +03:00
|
|
|
*
|
2016-08-11 20:49:45 +03:00
|
|
|
* @param string $search
|
|
|
|
* @param int $limit
|
|
|
|
* @param int $offset
|
|
|
|
* @return DataResponse
|
2014-12-22 16:54:50 +03:00
|
|
|
*/
|
2019-11-28 11:33:26 +03:00
|
|
|
public function getUsers(string $search = '', int $limit = null, int $offset = 0): DataResponse {
|
2015-08-18 18:43:16 +03:00
|
|
|
$user = $this->userSession->getUser();
|
2016-12-05 13:55:21 +03:00
|
|
|
$users = [];
|
2015-08-18 18:43:16 +03:00
|
|
|
|
|
|
|
// Admin? Or SubAdmin?
|
2015-10-27 16:09:45 +03:00
|
|
|
$uid = $user->getUID();
|
|
|
|
$subAdminManager = $this->groupManager->getSubAdmin();
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($this->groupManager->isAdmin($uid)) {
|
2015-08-18 18:43:16 +03:00
|
|
|
$users = $this->userManager->search($search, $limit, $offset);
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($subAdminManager->isSubAdmin($user)) {
|
2015-10-27 16:09:45 +03:00
|
|
|
$subAdminOfGroups = $subAdminManager->getSubAdminsGroups($user);
|
|
|
|
foreach ($subAdminOfGroups as $key => $group) {
|
|
|
|
$subAdminOfGroups[$key] = $group->getGID();
|
|
|
|
}
|
2015-08-18 18:43:16 +03:00
|
|
|
|
|
|
|
$users = [];
|
|
|
|
foreach ($subAdminOfGroups as $group) {
|
2018-03-16 13:44:20 +03:00
|
|
|
$users = array_merge($users, $this->groupManager->displayNamesInGroup($group, $search, $limit, $offset));
|
2015-08-18 18:43:16 +03:00
|
|
|
}
|
|
|
|
}
|
2016-08-11 20:49:45 +03:00
|
|
|
|
2015-07-25 13:46:39 +03:00
|
|
|
$users = array_keys($users);
|
|
|
|
|
2016-08-11 20:49:45 +03:00
|
|
|
return new DataResponse([
|
2015-07-25 13:46:39 +03:00
|
|
|
'users' => $users
|
|
|
|
]);
|
2014-12-22 16:54:50 +03:00
|
|
|
}
|
|
|
|
|
2018-03-16 11:18:53 +03:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* returns a list of users and their data
|
|
|
|
*/
|
2019-11-28 11:33:26 +03:00
|
|
|
public function getUsersDetails(string $search = '', int $limit = null, int $offset = 0): DataResponse {
|
2018-09-12 10:40:27 +03:00
|
|
|
$currentUser = $this->userSession->getUser();
|
2018-03-16 11:18:53 +03:00
|
|
|
$users = [];
|
|
|
|
|
|
|
|
// Admin? Or SubAdmin?
|
2018-09-12 10:40:27 +03:00
|
|
|
$uid = $currentUser->getUID();
|
2018-03-16 11:18:53 +03:00
|
|
|
$subAdminManager = $this->groupManager->getSubAdmin();
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($this->groupManager->isAdmin($uid)) {
|
2018-03-16 11:18:53 +03:00
|
|
|
$users = $this->userManager->search($search, $limit, $offset);
|
2018-09-12 10:40:27 +03:00
|
|
|
$users = array_keys($users);
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($subAdminManager->isSubAdmin($currentUser)) {
|
2018-09-12 10:40:27 +03:00
|
|
|
$subAdminOfGroups = $subAdminManager->getSubAdminsGroups($currentUser);
|
2018-03-16 11:18:53 +03:00
|
|
|
foreach ($subAdminOfGroups as $key => $group) {
|
|
|
|
$subAdminOfGroups[$key] = $group->getGID();
|
2015-08-18 18:43:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$users = [];
|
|
|
|
foreach ($subAdminOfGroups as $group) {
|
2018-09-12 10:40:27 +03:00
|
|
|
$users[] = array_keys($this->groupManager->displayNamesInGroup($group, $search, $limit, $offset));
|
2015-08-18 18:43:16 +03:00
|
|
|
}
|
2018-09-12 10:40:27 +03:00
|
|
|
$users = array_merge(...$users);
|
2015-08-18 18:43:16 +03:00
|
|
|
}
|
2016-08-11 20:49:45 +03:00
|
|
|
|
2018-03-16 11:18:53 +03:00
|
|
|
$usersDetails = [];
|
2018-09-12 10:40:27 +03:00
|
|
|
foreach ($users as $userId) {
|
|
|
|
$userId = (string) $userId;
|
2018-03-16 13:44:20 +03:00
|
|
|
$userData = $this->getUserData($userId);
|
|
|
|
// Do not insert empty entry
|
2018-04-06 16:07:04 +03:00
|
|
|
if (!empty($userData)) {
|
2018-03-16 13:44:20 +03:00
|
|
|
$usersDetails[$userId] = $userData;
|
2018-05-23 14:01:40 +03:00
|
|
|
} else {
|
|
|
|
// Logged user does not have permissions to see this user
|
|
|
|
// only showing its id
|
|
|
|
$usersDetails[$userId] = ['id' => $userId];
|
2018-03-16 13:44:20 +03:00
|
|
|
}
|
2018-03-16 11:18:53 +03:00
|
|
|
}
|
2015-07-25 13:46:39 +03:00
|
|
|
|
2016-08-11 20:49:45 +03:00
|
|
|
return new DataResponse([
|
2018-03-16 11:18:53 +03:00
|
|
|
'users' => $usersDetails
|
2015-07-25 13:46:39 +03:00
|
|
|
]);
|
2014-12-22 16:54:50 +03:00
|
|
|
}
|
|
|
|
|
2020-12-02 16:11:47 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoSubAdminRequired
|
|
|
|
*
|
|
|
|
* @param string $location
|
|
|
|
* @param array $search
|
|
|
|
* @return DataResponse
|
|
|
|
*/
|
|
|
|
public function searchByPhoneNumbers(string $location, array $search): DataResponse {
|
|
|
|
$phoneUtil = PhoneNumberUtil::getInstance();
|
|
|
|
|
2020-12-03 22:44:29 +03:00
|
|
|
if ($phoneUtil->getCountryCodeForRegion($location) === 0) {
|
|
|
|
// Not a valid region code
|
|
|
|
return new DataResponse([], Http::STATUS_BAD_REQUEST);
|
|
|
|
}
|
|
|
|
|
2021-03-09 23:22:59 +03:00
|
|
|
/** @var IUser $user */
|
|
|
|
$user = $this->userSession->getUser();
|
|
|
|
$knownTo = $user->getUID();
|
2021-03-31 09:45:32 +03:00
|
|
|
$defaultPhoneRegion = $this->config->getSystemValueString('default_phone_region');
|
2021-03-09 23:22:59 +03:00
|
|
|
|
2020-12-02 16:11:47 +03:00
|
|
|
$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) {
|
|
|
|
}
|
2021-03-31 09:45:32 +03:00
|
|
|
|
|
|
|
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) {
|
|
|
|
}
|
|
|
|
}
|
2020-12-02 16:11:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$phoneNumbers = array_keys($normalizedNumberToKey);
|
|
|
|
|
|
|
|
if (empty($phoneNumbers)) {
|
|
|
|
return new DataResponse();
|
|
|
|
}
|
|
|
|
|
2021-03-12 11:04:13 +03:00
|
|
|
// Cleanup all previous entries and only allow new matches
|
|
|
|
$this->knownUserService->deleteKnownTo($knownTo);
|
|
|
|
|
2020-12-02 16:11:47 +03:00
|
|
|
$userMatches = $this->accountManager->searchUsers(IAccountManager::PROPERTY_PHONE, $phoneNumbers);
|
|
|
|
|
|
|
|
if (empty($userMatches)) {
|
|
|
|
return new DataResponse();
|
|
|
|
}
|
|
|
|
|
2020-12-03 22:44:29 +03:00
|
|
|
$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://'));
|
|
|
|
}
|
2020-12-02 16:11:47 +03:00
|
|
|
|
|
|
|
$matches = [];
|
|
|
|
foreach ($userMatches as $phone => $userId) {
|
2020-12-03 22:44:29 +03:00
|
|
|
// 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;
|
2021-03-09 23:22:59 +03:00
|
|
|
$this->knownUserService->storeIsKnownToUser($knownTo, $userId);
|
2020-12-02 16:11:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return new DataResponse($matches);
|
|
|
|
}
|
|
|
|
|
2019-06-14 18:07:40 +03:00
|
|
|
/**
|
|
|
|
* @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);
|
|
|
|
}
|
|
|
|
|
2015-09-08 15:02:30 +03:00
|
|
|
/**
|
2016-12-05 13:55:21 +03:00
|
|
|
* @PasswordConfirmationRequired
|
2016-08-11 20:49:45 +03:00
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* @param string $userid
|
|
|
|
* @param string $password
|
2018-04-19 01:12:06 +03:00
|
|
|
* @param string $displayName
|
2018-03-16 17:30:48 +03:00
|
|
|
* @param string $email
|
2016-08-11 20:49:45 +03:00
|
|
|
* @param array $groups
|
2019-01-10 12:49:09 +03:00
|
|
|
* @param array $subadmin
|
2018-04-06 16:07:04 +03:00
|
|
|
* @param string $quota
|
2018-04-12 13:37:26 +03:00
|
|
|
* @param string $language
|
2016-08-11 20:49:45 +03:00
|
|
|
* @return DataResponse
|
|
|
|
* @throws OCSException
|
2015-09-08 15:02:30 +03:00
|
|
|
*/
|
2018-04-06 16:07:04 +03:00
|
|
|
public function addUser(string $userid,
|
|
|
|
string $password = '',
|
2018-04-19 01:12:06 +03:00
|
|
|
string $displayName = '',
|
2018-04-06 16:07:04 +03:00
|
|
|
string $email = '',
|
|
|
|
array $groups = [],
|
|
|
|
array $subadmin = [],
|
2018-04-10 13:08:10 +03:00
|
|
|
string $quota = '',
|
2018-04-12 13:37:26 +03:00
|
|
|
string $language = ''): DataResponse {
|
2015-08-27 19:29:28 +03:00
|
|
|
$user = $this->userSession->getUser();
|
|
|
|
$isAdmin = $this->groupManager->isAdmin($user->getUID());
|
2015-10-30 11:30:00 +03:00
|
|
|
$subAdminManager = $this->groupManager->getSubAdmin();
|
2015-08-27 19:29:28 +03:00
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
if (empty($userid) && $this->config->getAppValue('core', 'newUser.generateUserID', 'no') === 'yes') {
|
2019-06-14 18:07:40 +03:00
|
|
|
$userid = $this->createNewUserId();
|
|
|
|
}
|
|
|
|
|
2018-04-06 16:07:04 +03:00
|
|
|
if ($this->userManager->userExists($userid)) {
|
2015-10-27 16:09:45 +03:00
|
|
|
$this->logger->error('Failed addUser attempt: User already exists.', ['app' => 'ocs_api']);
|
2016-08-11 20:49:45 +03:00
|
|
|
throw new OCSException('User already exists', 102);
|
2015-08-27 19:29:28 +03:00
|
|
|
}
|
|
|
|
|
2018-04-06 16:07:04 +03:00
|
|
|
if ($groups !== []) {
|
2015-10-30 11:30:00 +03:00
|
|
|
foreach ($groups as $group) {
|
2018-04-06 16:07:04 +03:00
|
|
|
if (!$this->groupManager->groupExists($group)) {
|
2016-08-11 20:49:45 +03:00
|
|
|
throw new OCSException('group '.$group.' does not exist', 104);
|
2015-08-27 19:29:28 +03:00
|
|
|
}
|
2018-04-06 16:07:04 +03:00
|
|
|
if (!$isAdmin && !$subAdminManager->isSubAdminOfGroup($user, $this->groupManager->get($group))) {
|
2016-08-11 20:49:45 +03:00
|
|
|
throw new OCSException('insufficient privileges for group '. $group, 105);
|
2015-08-27 19:29:28 +03:00
|
|
|
}
|
|
|
|
}
|
2014-12-22 16:54:50 +03:00
|
|
|
} else {
|
2018-04-06 16:07:04 +03:00
|
|
|
if (!$isAdmin) {
|
2016-08-11 20:49:45 +03:00
|
|
|
throw new OCSException('no group specified (required for subadmins)', 106);
|
2015-08-27 19:29:28 +03:00
|
|
|
}
|
|
|
|
}
|
2017-01-24 16:07:52 +03:00
|
|
|
|
2018-04-06 16:07:04 +03:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-16 17:30:48 +03:00
|
|
|
$generatePasswordResetToken = false;
|
|
|
|
if ($password === '') {
|
|
|
|
if ($email === '') {
|
|
|
|
throw new OCSException('To send a password link to the user an email address is required.', 108);
|
|
|
|
}
|
|
|
|
|
2020-08-09 17:36:19 +03:00
|
|
|
$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);
|
|
|
|
}
|
2018-03-16 17:30:48 +03:00
|
|
|
$generatePasswordResetToken = true;
|
|
|
|
}
|
|
|
|
|
2019-06-21 11:18:44 +03:00
|
|
|
if ($email === '' && $this->config->getAppValue('core', 'newUser.requireEmail', 'no') === 'yes') {
|
2019-06-14 18:07:40 +03:00
|
|
|
throw new OCSException('Required email address was not provided', 110);
|
|
|
|
}
|
|
|
|
|
2015-08-27 19:29:28 +03:00
|
|
|
try {
|
2016-08-11 20:49:45 +03:00
|
|
|
$newUser = $this->userManager->createUser($userid, $password);
|
2018-02-22 16:16:49 +03:00
|
|
|
$this->logger->info('Successful addUser call with userid: ' . $userid, ['app' => 'ocs_api']);
|
2015-08-27 19:29:28 +03:00
|
|
|
|
2018-02-28 00:03:40 +03:00
|
|
|
foreach ($groups as $group) {
|
|
|
|
$this->groupManager->get($group)->addUser($newUser);
|
|
|
|
$this->logger->info('Added userid ' . $userid . ' to group ' . $group, ['app' => 'ocs_api']);
|
2014-12-22 16:54:50 +03:00
|
|
|
}
|
2018-04-06 16:07:04 +03:00
|
|
|
foreach ($subadminGroups as $group) {
|
|
|
|
$subAdminManager->createSubAdmin($newUser, $group);
|
|
|
|
}
|
|
|
|
|
2018-04-19 01:12:06 +03:00
|
|
|
if ($displayName !== '') {
|
|
|
|
$this->editUser($userid, 'display', $displayName);
|
|
|
|
}
|
|
|
|
|
2018-04-06 16:07:04 +03:00
|
|
|
if ($quota !== '') {
|
|
|
|
$this->editUser($userid, 'quota', $quota);
|
|
|
|
}
|
2018-02-28 00:03:40 +03:00
|
|
|
|
2018-04-10 13:08:10 +03:00
|
|
|
if ($language !== '') {
|
|
|
|
$this->editUser($userid, 'language', $language);
|
|
|
|
}
|
|
|
|
|
2018-03-16 17:30:48 +03:00
|
|
|
// Send new user mail only if a mail is set
|
2020-10-05 13:39:00 +03:00
|
|
|
if ($email !== '') {
|
2018-03-16 17:30:48 +03:00
|
|
|
$newUser->setEMailAddress($email);
|
2020-10-05 13:39:00 +03:00
|
|
|
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
|
2021-03-02 23:46:51 +03:00
|
|
|
$this->logger->error("Unable to send the invitation mail to $email",
|
|
|
|
[
|
|
|
|
'app' => 'ocs_api',
|
|
|
|
'exception' => $e,
|
|
|
|
]
|
|
|
|
);
|
2020-10-05 13:39:00 +03:00
|
|
|
}
|
2018-03-16 17:30:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-21 17:38:25 +03:00
|
|
|
return new DataResponse(['id' => $userid]);
|
2019-06-20 19:04:52 +03:00
|
|
|
} catch (HintException $e) {
|
2021-03-02 23:46:51 +03:00
|
|
|
$this->logger->warning('Failed addUser attempt with hint exception.',
|
|
|
|
[
|
|
|
|
'app' => 'ocs_api',
|
|
|
|
'exception' => $e,
|
|
|
|
]
|
|
|
|
);
|
2018-02-22 16:16:49 +03:00
|
|
|
throw new OCSException($e->getHint(), 107);
|
2019-06-20 19:04:52 +03:00
|
|
|
} catch (OCSException $e) {
|
2021-03-02 23:46:51 +03:00
|
|
|
$this->logger->warning('Failed addUser attempt with ocs exeption.',
|
|
|
|
[
|
|
|
|
'app' => 'ocs_api',
|
|
|
|
'exception' => $e,
|
|
|
|
]
|
|
|
|
);
|
2019-06-20 19:04:52 +03:00
|
|
|
throw $e;
|
2021-02-11 19:05:51 +03:00
|
|
|
} catch (\InvalidArgumentException $e) {
|
2021-03-05 11:14:01 +03:00
|
|
|
$this->logger->error('Failed addUser attempt with invalid argument exeption.',
|
|
|
|
[
|
|
|
|
'app' => 'ocs_api',
|
|
|
|
'exception' => $e,
|
|
|
|
]
|
|
|
|
);
|
2021-02-11 19:05:51 +03:00
|
|
|
throw new OCSException($e->getMessage(), 101);
|
2015-08-27 19:29:28 +03:00
|
|
|
} catch (\Exception $e) {
|
2021-03-02 23:46:51 +03:00
|
|
|
$this->logger->error('Failed addUser attempt with exception.',
|
|
|
|
[
|
|
|
|
'app' => 'ocs_api',
|
|
|
|
'exception' => $e
|
|
|
|
]
|
|
|
|
);
|
2016-08-11 20:49:45 +03:00
|
|
|
throw new OCSException('Bad request', 101);
|
2014-12-22 16:54:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-11 20:49:45 +03:00
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoSubAdminRequired
|
|
|
|
*
|
2014-12-22 16:54:50 +03:00
|
|
|
* gets user info
|
2015-09-08 15:02:30 +03:00
|
|
|
*
|
2016-08-11 20:49:45 +03:00
|
|
|
* @param string $userId
|
|
|
|
* @return DataResponse
|
|
|
|
* @throws OCSException
|
2014-12-22 16:54:50 +03:00
|
|
|
*/
|
2018-02-27 17:47:59 +03:00
|
|
|
public function getUser(string $userId): DataResponse {
|
2021-03-24 14:32:06 +03:00
|
|
|
$includeScopes = false;
|
|
|
|
$currentUser = $this->userSession->getUser();
|
|
|
|
if ($currentUser && $currentUser->getUID() === $userId) {
|
|
|
|
$includeScopes = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = $this->getUserData($userId, $includeScopes);
|
2018-03-16 13:44:20 +03:00
|
|
|
// getUserData returns empty array if not enough permissions
|
2018-04-06 16:07:04 +03:00
|
|
|
if (empty($data)) {
|
2021-03-03 17:10:42 +03:00
|
|
|
throw new OCSException('', OCSController::RESPOND_UNAUTHORISED);
|
2018-03-16 13:44:20 +03:00
|
|
|
}
|
2017-01-26 13:31:08 +03:00
|
|
|
return new DataResponse($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoSubAdminRequired
|
|
|
|
*
|
|
|
|
* gets user info from the currently logged in user
|
|
|
|
*
|
|
|
|
* @return DataResponse
|
|
|
|
* @throws OCSException
|
|
|
|
*/
|
2018-02-27 17:47:59 +03:00
|
|
|
public function getCurrentUser(): DataResponse {
|
2017-01-26 13:31:08 +03:00
|
|
|
$user = $this->userSession->getUser();
|
|
|
|
if ($user) {
|
2021-03-24 14:32:06 +03:00
|
|
|
$data = $this->getUserData($user->getUID(), true);
|
2017-01-26 13:31:08 +03:00
|
|
|
// rename "displayname" to "display-name" only for this call to keep
|
|
|
|
// the API stable.
|
|
|
|
$data['display-name'] = $data['displayname'];
|
|
|
|
unset($data['displayname']);
|
|
|
|
return new DataResponse($data);
|
|
|
|
}
|
|
|
|
|
2021-03-03 17:10:42 +03:00
|
|
|
throw new OCSException('', OCSController::RESPOND_UNAUTHORISED);
|
2017-01-26 13:31:08 +03:00
|
|
|
}
|
|
|
|
|
2018-01-22 23:47:25 +03:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoSubAdminRequired
|
2021-04-08 14:28:13 +03:00
|
|
|
*
|
|
|
|
* @return DataResponse
|
|
|
|
* @throws OCSException
|
2018-01-22 23:47:25 +03:00
|
|
|
*/
|
2021-04-08 14:28:13 +03:00
|
|
|
public function getEditableFields(?string $userId = null): DataResponse {
|
|
|
|
$currentLoggedInUser = $this->userSession->getUser();
|
|
|
|
if (!$currentLoggedInUser instanceof IUser) {
|
|
|
|
throw new OCSException('', OCSController::RESPOND_NOT_FOUND);
|
|
|
|
}
|
|
|
|
|
2018-01-22 23:47:25 +03:00
|
|
|
$permittedFields = [];
|
|
|
|
|
2021-04-08 14:28:13 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-01-22 23:47:25 +03:00
|
|
|
// Editing self (display, email)
|
|
|
|
if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) {
|
2021-04-08 14:28:13 +03:00
|
|
|
if ($targetUser->getBackend() instanceof ISetDisplayNameBackend) {
|
|
|
|
$permittedFields[] = IAccountManager::PROPERTY_DISPLAYNAME;
|
|
|
|
}
|
2020-12-01 16:33:22 +03:00
|
|
|
$permittedFields[] = IAccountManager::PROPERTY_EMAIL;
|
2018-01-22 23:47:25 +03:00
|
|
|
}
|
|
|
|
|
2021-03-23 18:59:05 +03:00
|
|
|
$permittedFields[] = IAccountManager::PROPERTY_PHONE;
|
|
|
|
$permittedFields[] = IAccountManager::PROPERTY_ADDRESS;
|
|
|
|
$permittedFields[] = IAccountManager::PROPERTY_WEBSITE;
|
|
|
|
$permittedFields[] = IAccountManager::PROPERTY_TWITTER;
|
2018-01-22 23:47:25 +03:00
|
|
|
|
|
|
|
return new DataResponse($permittedFields);
|
|
|
|
}
|
|
|
|
|
2016-08-11 20:49:45 +03:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoSubAdminRequired
|
2016-12-05 13:55:21 +03:00
|
|
|
* @PasswordConfirmationRequired
|
2016-08-11 20:49:45 +03:00
|
|
|
*
|
2014-12-22 16:54:50 +03:00
|
|
|
* edit users
|
2015-09-08 15:02:30 +03:00
|
|
|
*
|
2016-08-11 20:49:45 +03:00
|
|
|
* @param string $userId
|
|
|
|
* @param string $key
|
|
|
|
* @param string $value
|
|
|
|
* @return DataResponse
|
|
|
|
* @throws OCSException
|
2014-12-22 16:54:50 +03:00
|
|
|
*/
|
2018-02-27 17:47:59 +03:00
|
|
|
public function editUser(string $userId, string $key, string $value): DataResponse {
|
2015-10-27 16:09:45 +03:00
|
|
|
$currentLoggedInUser = $this->userSession->getUser();
|
2015-08-11 16:37:06 +03:00
|
|
|
|
2016-08-11 20:49:45 +03:00
|
|
|
$targetUser = $this->userManager->get($userId);
|
2018-04-06 16:07:04 +03:00
|
|
|
if ($targetUser === null) {
|
2021-03-03 17:10:42 +03:00
|
|
|
throw new OCSException('', OCSController::RESPOND_UNAUTHORISED);
|
2015-10-27 16:09:45 +03:00
|
|
|
}
|
|
|
|
|
2016-07-12 09:38:56 +03:00
|
|
|
$permittedFields = [];
|
2018-04-06 16:07:04 +03:00
|
|
|
if ($targetUser->getUID() === $currentLoggedInUser->getUID()) {
|
2014-12-22 16:54:50 +03:00
|
|
|
// Editing self (display, email)
|
2017-06-02 11:09:42 +03:00
|
|
|
if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) {
|
2021-04-08 14:28:13 +03:00
|
|
|
if ($targetUser->getBackend() instanceof ISetDisplayNameBackend) {
|
|
|
|
$permittedFields[] = 'display';
|
|
|
|
$permittedFields[] = IAccountManager::PROPERTY_DISPLAYNAME;
|
|
|
|
}
|
2020-12-01 16:33:22 +03:00
|
|
|
$permittedFields[] = IAccountManager::PROPERTY_EMAIL;
|
2017-06-02 11:09:42 +03:00
|
|
|
}
|
|
|
|
|
2021-03-24 14:32:06 +03:00
|
|
|
$permittedFields[] = IAccountManager::PROPERTY_DISPLAYNAME . self::SCOPE_SUFFIX;
|
|
|
|
$permittedFields[] = IAccountManager::PROPERTY_EMAIL . self::SCOPE_SUFFIX;
|
|
|
|
|
2014-12-22 16:54:50 +03:00
|
|
|
$permittedFields[] = 'password';
|
2017-06-21 12:22:05 +03:00
|
|
|
if ($this->config->getSystemValue('force_language', false) === false ||
|
|
|
|
$this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
|
|
|
|
$permittedFields[] = 'language';
|
2017-07-13 11:34:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->config->getSystemValue('force_locale', false) === false ||
|
|
|
|
$this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
|
2017-07-05 18:36:43 +03:00
|
|
|
$permittedFields[] = 'locale';
|
2017-06-21 12:22:05 +03:00
|
|
|
}
|
2017-06-02 11:09:42 +03:00
|
|
|
|
2021-03-23 18:59:05 +03:00
|
|
|
$permittedFields[] = IAccountManager::PROPERTY_PHONE;
|
|
|
|
$permittedFields[] = IAccountManager::PROPERTY_ADDRESS;
|
|
|
|
$permittedFields[] = IAccountManager::PROPERTY_WEBSITE;
|
|
|
|
$permittedFields[] = IAccountManager::PROPERTY_TWITTER;
|
2021-03-24 14:32:06 +03:00
|
|
|
$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;
|
2017-06-02 11:09:42 +03:00
|
|
|
|
2021-03-24 17:32:51 +03:00
|
|
|
$permittedFields[] = IAccountManager::PROPERTY_AVATAR . self::SCOPE_SUFFIX;
|
|
|
|
|
2014-12-22 16:54:50 +03:00
|
|
|
// If admin they can edit their own quota
|
2018-04-06 16:07:04 +03:00
|
|
|
if ($this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
|
2014-12-22 16:54:50 +03:00
|
|
|
$permittedFields[] = 'quota';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Check if admin / subadmin
|
2015-10-27 16:09:45 +03:00
|
|
|
$subAdminManager = $this->groupManager->getSubAdmin();
|
2019-10-28 17:48:46 +03:00
|
|
|
if ($this->groupManager->isAdmin($currentLoggedInUser->getUID())
|
|
|
|
|| $subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) {
|
2014-12-22 16:54:50 +03:00
|
|
|
// They have permissions over the user
|
2021-04-08 14:28:13 +03:00
|
|
|
if ($targetUser->getBackend() instanceof ISetDisplayNameBackend) {
|
|
|
|
$permittedFields[] = 'display';
|
|
|
|
$permittedFields[] = IAccountManager::PROPERTY_DISPLAYNAME;
|
|
|
|
}
|
2020-12-01 16:33:22 +03:00
|
|
|
$permittedFields[] = IAccountManager::PROPERTY_EMAIL;
|
2014-12-22 16:54:50 +03:00
|
|
|
$permittedFields[] = 'password';
|
2017-06-21 12:22:05 +03:00
|
|
|
$permittedFields[] = 'language';
|
2017-07-05 18:36:43 +03:00
|
|
|
$permittedFields[] = 'locale';
|
2020-12-01 16:33:22 +03:00
|
|
|
$permittedFields[] = IAccountManager::PROPERTY_PHONE;
|
|
|
|
$permittedFields[] = IAccountManager::PROPERTY_ADDRESS;
|
|
|
|
$permittedFields[] = IAccountManager::PROPERTY_WEBSITE;
|
|
|
|
$permittedFields[] = IAccountManager::PROPERTY_TWITTER;
|
2017-04-05 15:51:55 +03:00
|
|
|
$permittedFields[] = 'quota';
|
2014-12-22 16:54:50 +03:00
|
|
|
} else {
|
|
|
|
// No rights
|
2021-03-03 17:10:42 +03:00
|
|
|
throw new OCSException('', OCSController::RESPOND_UNAUTHORISED);
|
2014-12-22 16:54:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check if permitted to edit this field
|
2018-04-06 16:07:04 +03:00
|
|
|
if (!in_array($key, $permittedFields)) {
|
2021-03-03 17:10:42 +03:00
|
|
|
throw new OCSException('', OCSController::RESPOND_UNAUTHORISED);
|
2014-12-22 16:54:50 +03:00
|
|
|
}
|
|
|
|
// Process the edit
|
2020-04-10 15:19:56 +03:00
|
|
|
switch ($key) {
|
2014-12-22 16:54:50 +03:00
|
|
|
case 'display':
|
2020-12-01 16:33:22 +03:00
|
|
|
case IAccountManager::PROPERTY_DISPLAYNAME:
|
2016-08-11 20:49:45 +03:00
|
|
|
$targetUser->setDisplayName($value);
|
2014-12-22 16:54:50 +03:00
|
|
|
break;
|
|
|
|
case 'quota':
|
2016-08-11 20:49:45 +03:00
|
|
|
$quota = $value;
|
2018-04-06 16:07:04 +03:00
|
|
|
if ($quota !== 'none' && $quota !== 'default') {
|
2015-05-07 18:56:13 +03:00
|
|
|
if (is_numeric($quota)) {
|
2017-01-18 13:43:52 +03:00
|
|
|
$quota = (float) $quota;
|
2015-05-07 18:56:13 +03:00
|
|
|
} else {
|
2015-08-02 11:27:36 +03:00
|
|
|
$quota = \OCP\Util::computerFileSize($quota);
|
2015-05-07 18:56:13 +03:00
|
|
|
}
|
|
|
|
if ($quota === false) {
|
2016-08-11 20:49:45 +03:00
|
|
|
throw new OCSException('Invalid quota value '.$value, 103);
|
2015-05-07 18:56:13 +03:00
|
|
|
}
|
2018-05-26 11:51:24 +03:00
|
|
|
if ($quota === -1) {
|
2014-12-22 16:54:50 +03:00
|
|
|
$quota = 'none';
|
|
|
|
} else {
|
2015-08-02 11:27:36 +03:00
|
|
|
$quota = \OCP\Util::humanFileSize($quota);
|
2014-12-22 16:54:50 +03:00
|
|
|
}
|
|
|
|
}
|
2016-02-09 19:16:43 +03:00
|
|
|
$targetUser->setQuota($quota);
|
2014-12-22 16:54:50 +03:00
|
|
|
break;
|
|
|
|
case 'password':
|
2019-01-07 18:28:06 +03:00
|
|
|
try {
|
2019-03-01 12:11:37 +03:00
|
|
|
if (!$targetUser->canChangePassword()) {
|
|
|
|
throw new OCSException('Setting the password is not supported by the users backend', 103);
|
|
|
|
}
|
2019-01-07 18:28:06 +03:00
|
|
|
$targetUser->setPassword($value);
|
|
|
|
} catch (HintException $e) { // password policy error
|
|
|
|
throw new OCSException($e->getMessage(), 103);
|
|
|
|
}
|
2014-12-22 16:54:50 +03:00
|
|
|
break;
|
2017-06-21 12:22:05 +03:00
|
|
|
case '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;
|
2017-07-05 18:36:43 +03:00
|
|
|
case 'locale':
|
2017-07-07 12:17:54 +03:00
|
|
|
if (!$this->l10nFactory->localeExists($value)) {
|
|
|
|
throw new OCSException('Invalid locale', 102);
|
|
|
|
}
|
2017-07-05 18:36:43 +03:00
|
|
|
$this->config->setUserValue($targetUser->getUID(), 'core', 'locale', $value);
|
|
|
|
break;
|
2020-12-01 16:33:22 +03:00
|
|
|
case IAccountManager::PROPERTY_EMAIL:
|
2018-04-06 16:07:04 +03:00
|
|
|
if (filter_var($value, FILTER_VALIDATE_EMAIL) || $value === '') {
|
2016-08-11 20:49:45 +03:00
|
|
|
$targetUser->setEMailAddress($value);
|
2014-12-22 16:54:50 +03:00
|
|
|
} else {
|
2016-08-11 20:49:45 +03:00
|
|
|
throw new OCSException('', 102);
|
2014-12-22 16:54:50 +03:00
|
|
|
}
|
|
|
|
break;
|
2020-12-01 16:33:22 +03:00
|
|
|
case IAccountManager::PROPERTY_PHONE:
|
|
|
|
case IAccountManager::PROPERTY_ADDRESS:
|
|
|
|
case IAccountManager::PROPERTY_WEBSITE:
|
|
|
|
case IAccountManager::PROPERTY_TWITTER:
|
2017-04-05 15:51:55 +03:00
|
|
|
$userAccount = $this->accountManager->getUser($targetUser);
|
|
|
|
if ($userAccount[$key]['value'] !== $value) {
|
|
|
|
$userAccount[$key]['value'] = $value;
|
2020-12-02 18:03:08 +03:00
|
|
|
try {
|
2020-12-03 14:16:39 +03:00
|
|
|
$this->accountManager->updateUser($targetUser, $userAccount, true);
|
2021-03-09 22:32:50 +03:00
|
|
|
|
|
|
|
if ($key === IAccountManager::PROPERTY_PHONE) {
|
2021-03-10 22:30:29 +03:00
|
|
|
$this->knownUserService->deleteByContactUserId($targetUser->getUID());
|
2021-03-09 22:32:50 +03:00
|
|
|
}
|
2020-12-02 18:03:08 +03:00
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
throw new OCSException('Invalid ' . $e->getMessage(), 102);
|
|
|
|
}
|
2017-04-05 15:51:55 +03:00
|
|
|
}
|
|
|
|
break;
|
2021-03-24 14:32:06 +03:00
|
|
|
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:
|
2021-03-24 17:32:51 +03:00
|
|
|
case IAccountManager::PROPERTY_AVATAR . self::SCOPE_SUFFIX:
|
2021-03-24 14:32:06 +03:00
|
|
|
$propertyName = substr($key, 0, strlen($key) - strlen(self::SCOPE_SUFFIX));
|
|
|
|
$userAccount = $this->accountManager->getUser($targetUser);
|
|
|
|
if ($userAccount[$propertyName]['scope'] !== $value) {
|
|
|
|
$userAccount[$propertyName]['scope'] = $value;
|
|
|
|
try {
|
|
|
|
$this->accountManager->updateUser($targetUser, $userAccount, true);
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
throw new OCSException('Invalid ' . $e->getMessage(), 102);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2014-12-22 16:54:50 +03:00
|
|
|
default:
|
2016-08-11 20:49:45 +03:00
|
|
|
throw new OCSException('', 103);
|
2014-12-22 16:54:50 +03:00
|
|
|
}
|
2016-08-11 20:49:45 +03:00
|
|
|
return new DataResponse();
|
2014-12-22 16:54:50 +03:00
|
|
|
}
|
|
|
|
|
2019-07-03 11:10:56 +03:00
|
|
|
/**
|
|
|
|
* @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 || $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)) {
|
2021-03-03 17:10:42 +03:00
|
|
|
throw new OCSException('', OCSController::RESPOND_UNAUTHORISED);
|
2019-07-03 11:10:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->remoteWipe->markAllTokensForWipe($targetUser);
|
|
|
|
|
|
|
|
return new DataResponse();
|
|
|
|
}
|
|
|
|
|
2015-09-08 15:02:30 +03:00
|
|
|
/**
|
2016-12-05 13:55:21 +03:00
|
|
|
* @PasswordConfirmationRequired
|
2016-08-11 20:49:45 +03:00
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* @param string $userId
|
|
|
|
* @return DataResponse
|
|
|
|
* @throws OCSException
|
2015-09-08 15:02:30 +03:00
|
|
|
*/
|
2018-02-27 17:47:59 +03:00
|
|
|
public function deleteUser(string $userId): DataResponse {
|
2015-10-27 16:09:45 +03:00
|
|
|
$currentLoggedInUser = $this->userSession->getUser();
|
2015-08-11 16:37:06 +03:00
|
|
|
|
2016-08-11 20:49:45 +03:00
|
|
|
$targetUser = $this->userManager->get($userId);
|
2015-10-27 16:09:45 +03:00
|
|
|
|
2018-04-06 16:07:04 +03:00
|
|
|
if ($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) {
|
2016-08-11 20:49:45 +03:00
|
|
|
throw new OCSException('', 101);
|
2014-12-22 16:54:50 +03:00
|
|
|
}
|
2015-10-27 16:09:45 +03:00
|
|
|
|
2014-12-22 16:54:50 +03:00
|
|
|
// If not permitted
|
2015-10-27 16:09:45 +03:00
|
|
|
$subAdminManager = $this->groupManager->getSubAdmin();
|
2018-04-06 16:07:04 +03:00
|
|
|
if (!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) {
|
2021-03-03 17:10:42 +03:00
|
|
|
throw new OCSException('', OCSController::RESPOND_UNAUTHORISED);
|
2014-12-22 16:54:50 +03:00
|
|
|
}
|
2015-10-27 16:09:45 +03:00
|
|
|
|
2014-12-22 16:54:50 +03:00
|
|
|
// Go ahead with the delete
|
2018-04-06 16:07:04 +03:00
|
|
|
if ($targetUser->delete()) {
|
2016-08-11 20:49:45 +03:00
|
|
|
return new DataResponse();
|
2014-12-22 16:54:50 +03:00
|
|
|
} else {
|
2016-08-11 20:49:45 +03:00
|
|
|
throw new OCSException('', 101);
|
2014-12-22 16:54:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-07 18:22:21 +03:00
|
|
|
/**
|
2016-12-05 13:55:21 +03:00
|
|
|
* @PasswordConfirmationRequired
|
2016-08-11 20:49:45 +03:00
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* @param string $userId
|
|
|
|
* @return DataResponse
|
2016-12-05 13:55:21 +03:00
|
|
|
* @throws OCSException
|
|
|
|
* @throws OCSForbiddenException
|
2016-04-07 18:22:21 +03:00
|
|
|
*/
|
2018-02-27 17:47:59 +03:00
|
|
|
public function disableUser(string $userId): DataResponse {
|
2016-08-11 20:49:45 +03:00
|
|
|
return $this->setEnabled($userId, false);
|
2016-04-07 18:22:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-05 13:55:21 +03:00
|
|
|
* @PasswordConfirmationRequired
|
2016-08-11 20:49:45 +03:00
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* @param string $userId
|
|
|
|
* @return DataResponse
|
2016-12-05 13:55:21 +03:00
|
|
|
* @throws OCSException
|
|
|
|
* @throws OCSForbiddenException
|
2016-04-07 18:22:21 +03:00
|
|
|
*/
|
2018-02-27 17:47:59 +03:00
|
|
|
public function enableUser(string $userId): DataResponse {
|
2016-08-11 20:49:45 +03:00
|
|
|
return $this->setEnabled($userId, true);
|
2016-04-07 18:22:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-11 20:49:45 +03:00
|
|
|
* @param string $userId
|
2016-04-07 18:22:21 +03:00
|
|
|
* @param bool $value
|
2016-08-11 20:49:45 +03:00
|
|
|
* @return DataResponse
|
|
|
|
* @throws OCSException
|
2016-04-07 18:22:21 +03:00
|
|
|
*/
|
2018-02-27 17:47:59 +03:00
|
|
|
private function setEnabled(string $userId, bool $value): DataResponse {
|
2016-04-07 18:22:21 +03:00
|
|
|
$currentLoggedInUser = $this->userSession->getUser();
|
|
|
|
|
2016-08-11 20:49:45 +03:00
|
|
|
$targetUser = $this->userManager->get($userId);
|
2018-04-06 16:07:04 +03:00
|
|
|
if ($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) {
|
2016-08-11 20:49:45 +03:00
|
|
|
throw new OCSException('', 101);
|
2016-04-07 18:22:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If not permitted
|
|
|
|
$subAdminManager = $this->groupManager->getSubAdmin();
|
2018-04-06 16:07:04 +03:00
|
|
|
if (!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) {
|
2021-03-03 17:10:42 +03:00
|
|
|
throw new OCSException('', OCSController::RESPOND_UNAUTHORISED);
|
2016-04-07 18:22:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// enable/disable the user now
|
|
|
|
$targetUser->setEnabled($value);
|
2016-08-11 20:49:45 +03:00
|
|
|
return new DataResponse();
|
2016-04-07 18:22:21 +03:00
|
|
|
}
|
|
|
|
|
2015-09-08 15:02:30 +03:00
|
|
|
/**
|
2016-08-11 20:49:45 +03:00
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoSubAdminRequired
|
|
|
|
*
|
|
|
|
* @param string $userId
|
|
|
|
* @return DataResponse
|
2016-12-05 13:55:21 +03:00
|
|
|
* @throws OCSException
|
2015-09-08 15:02:30 +03:00
|
|
|
*/
|
2018-02-27 17:47:59 +03:00
|
|
|
public function getUsersGroups(string $userId): DataResponse {
|
2015-10-27 16:09:45 +03:00
|
|
|
$loggedInUser = $this->userSession->getUser();
|
2015-08-11 16:37:06 +03:00
|
|
|
|
2016-08-11 20:49:45 +03:00
|
|
|
$targetUser = $this->userManager->get($userId);
|
2018-04-06 16:07:04 +03:00
|
|
|
if ($targetUser === null) {
|
2021-03-03 17:10:42 +03:00
|
|
|
throw new OCSException('', OCSController::RESPOND_NOT_FOUND);
|
2015-10-27 16:09:45 +03:00
|
|
|
}
|
|
|
|
|
2018-04-06 16:07:04 +03:00
|
|
|
if ($targetUser->getUID() === $loggedInUser->getUID() || $this->groupManager->isAdmin($loggedInUser->getUID())) {
|
2014-12-22 16:54:50 +03:00
|
|
|
// Self lookup or admin lookup
|
2016-08-11 20:49:45 +03:00
|
|
|
return new DataResponse([
|
2015-10-27 16:09:45 +03:00
|
|
|
'groups' => $this->groupManager->getUserGroupIds($targetUser)
|
2015-07-25 13:46:39 +03:00
|
|
|
]);
|
2014-12-22 16:54:50 +03:00
|
|
|
} else {
|
2015-10-27 16:09:45 +03:00
|
|
|
$subAdminManager = $this->groupManager->getSubAdmin();
|
|
|
|
|
2014-12-22 16:54:50 +03:00
|
|
|
// Looking up someone else
|
2018-04-06 16:07:04 +03:00
|
|
|
if ($subAdminManager->isUserAccessible($loggedInUser, $targetUser)) {
|
2014-12-22 16:54:50 +03:00
|
|
|
// Return the group that the method caller is subadmin of for the user in question
|
2017-01-18 13:43:52 +03:00
|
|
|
/** @var IGroup[] $getSubAdminsGroups */
|
2015-10-27 16:09:45 +03:00
|
|
|
$getSubAdminsGroups = $subAdminManager->getSubAdminsGroups($loggedInUser);
|
|
|
|
foreach ($getSubAdminsGroups as $key => $group) {
|
|
|
|
$getSubAdminsGroups[$key] = $group->getGID();
|
|
|
|
}
|
2015-07-25 13:46:39 +03:00
|
|
|
$groups = array_intersect(
|
2015-10-27 16:09:45 +03:00
|
|
|
$getSubAdminsGroups,
|
|
|
|
$this->groupManager->getUserGroupIds($targetUser)
|
2015-07-25 13:46:39 +03:00
|
|
|
);
|
2016-08-11 20:49:45 +03:00
|
|
|
return new DataResponse(['groups' => $groups]);
|
2014-12-22 16:54:50 +03:00
|
|
|
} else {
|
|
|
|
// Not permitted
|
2021-03-03 17:10:42 +03:00
|
|
|
throw new OCSException('', OCSController::RESPOND_UNAUTHORISED);
|
2014-12-22 16:54:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-08 15:02:30 +03:00
|
|
|
/**
|
2016-12-05 13:55:21 +03:00
|
|
|
* @PasswordConfirmationRequired
|
2017-01-18 16:34:38 +03:00
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
2016-08-11 20:49:45 +03:00
|
|
|
* @param string $userId
|
|
|
|
* @param string $groupid
|
|
|
|
* @return DataResponse
|
|
|
|
* @throws OCSException
|
2015-09-08 15:02:30 +03:00
|
|
|
*/
|
2018-02-27 17:47:59 +03:00
|
|
|
public function addToGroup(string $userId, string $groupid = ''): DataResponse {
|
2018-04-06 16:07:04 +03:00
|
|
|
if ($groupid === '') {
|
2016-08-11 20:49:45 +03:00
|
|
|
throw new OCSException('', 101);
|
2015-08-11 16:37:06 +03:00
|
|
|
}
|
|
|
|
|
2016-08-11 20:49:45 +03:00
|
|
|
$group = $this->groupManager->get($groupid);
|
|
|
|
$targetUser = $this->userManager->get($userId);
|
2018-04-06 16:07:04 +03:00
|
|
|
if ($group === null) {
|
2016-08-11 20:49:45 +03:00
|
|
|
throw new OCSException('', 102);
|
2014-12-22 16:54:50 +03:00
|
|
|
}
|
2018-04-06 16:07:04 +03:00
|
|
|
if ($targetUser === null) {
|
2016-08-11 20:49:45 +03:00
|
|
|
throw new OCSException('', 103);
|
2014-12-22 16:54:50 +03:00
|
|
|
}
|
2015-10-27 16:09:45 +03:00
|
|
|
|
2017-01-18 16:34:38 +03:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2014-12-22 16:54:50 +03:00
|
|
|
// Add user to group
|
2015-10-27 16:09:45 +03:00
|
|
|
$group->addUser($targetUser);
|
2016-08-11 20:49:45 +03:00
|
|
|
return new DataResponse();
|
2014-12-22 16:54:50 +03:00
|
|
|
}
|
|
|
|
|
2015-09-08 15:02:30 +03:00
|
|
|
/**
|
2016-12-05 13:55:21 +03:00
|
|
|
* @PasswordConfirmationRequired
|
2016-08-11 20:49:45 +03:00
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
2016-12-05 13:55:21 +03:00
|
|
|
* @param string $userId
|
2016-08-11 20:49:45 +03:00
|
|
|
* @param string $groupid
|
|
|
|
* @return DataResponse
|
|
|
|
* @throws OCSException
|
2015-09-08 15:02:30 +03:00
|
|
|
*/
|
2018-02-27 17:47:59 +03:00
|
|
|
public function removeFromGroup(string $userId, string $groupid): DataResponse {
|
2015-10-27 16:09:45 +03:00
|
|
|
$loggedInUser = $this->userSession->getUser();
|
2015-08-11 16:37:06 +03:00
|
|
|
|
2018-04-06 16:07:04 +03:00
|
|
|
if ($groupid === null || trim($groupid) === '') {
|
2016-08-11 20:49:45 +03:00
|
|
|
throw new OCSException('', 101);
|
2014-12-22 16:54:50 +03:00
|
|
|
}
|
2015-10-27 16:09:45 +03:00
|
|
|
|
2016-08-11 20:49:45 +03:00
|
|
|
$group = $this->groupManager->get($groupid);
|
2018-04-06 16:07:04 +03:00
|
|
|
if ($group === null) {
|
2016-08-11 20:49:45 +03:00
|
|
|
throw new OCSException('', 102);
|
2015-11-05 19:01:47 +03:00
|
|
|
}
|
|
|
|
|
2016-08-11 20:49:45 +03:00
|
|
|
$targetUser = $this->userManager->get($userId);
|
2018-04-06 16:07:04 +03:00
|
|
|
if ($targetUser === null) {
|
2016-08-11 20:49:45 +03:00
|
|
|
throw new OCSException('', 103);
|
2015-10-27 16:09:45 +03:00
|
|
|
}
|
|
|
|
|
2014-12-22 16:54:50 +03:00
|
|
|
// If they're not an admin, check they are a subadmin of the group in question
|
2015-10-27 16:09:45 +03:00
|
|
|
$subAdminManager = $this->groupManager->getSubAdmin();
|
2017-01-18 13:43:52 +03:00
|
|
|
if (!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminOfGroup($loggedInUser, $group)) {
|
2016-08-11 20:49:45 +03:00
|
|
|
throw new OCSException('', 104);
|
2014-12-22 16:54:50 +03:00
|
|
|
}
|
2017-01-18 13:43:52 +03:00
|
|
|
|
2014-12-22 16:54:50 +03:00
|
|
|
// Check they aren't removing themselves from 'admin' or their 'subadmin; group
|
2017-04-24 10:43:44 +03:00
|
|
|
if ($targetUser->getUID() === $loggedInUser->getUID()) {
|
2017-01-18 13:43:52 +03:00
|
|
|
if ($this->groupManager->isAdmin($loggedInUser->getUID())) {
|
|
|
|
if ($group->getGID() === 'admin') {
|
2016-08-11 20:49:45 +03:00
|
|
|
throw new OCSException('Cannot remove yourself from the admin group', 105);
|
2014-12-22 16:54:50 +03:00
|
|
|
}
|
|
|
|
} else {
|
2017-01-18 13:43:52 +03:00
|
|
|
// 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);
|
2014-12-22 16:54:50 +03:00
|
|
|
}
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif (!$this->groupManager->isAdmin($loggedInUser->getUID())) {
|
2017-01-18 13:56:24 +03:00
|
|
|
/** @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.
|
2019-05-16 15:17:41 +03:00
|
|
|
throw new OCSException('Not viable to remove user from the last group you are SubAdmin of', 105);
|
2017-01-18 13:56:24 +03:00
|
|
|
}
|
2014-12-22 16:54:50 +03:00
|
|
|
}
|
2015-10-27 16:09:45 +03:00
|
|
|
|
2014-12-22 16:54:50 +03:00
|
|
|
// Remove user from group
|
2015-10-27 16:09:45 +03:00
|
|
|
$group->removeUser($targetUser);
|
2016-08-11 20:49:45 +03:00
|
|
|
return new DataResponse();
|
2014-12-22 16:54:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a subadmin
|
2015-09-08 15:02:30 +03:00
|
|
|
*
|
2016-12-05 13:55:21 +03:00
|
|
|
* @PasswordConfirmationRequired
|
|
|
|
*
|
2016-08-11 20:49:45 +03:00
|
|
|
* @param string $userId
|
|
|
|
* @param string $groupid
|
|
|
|
* @return DataResponse
|
|
|
|
* @throws OCSException
|
2014-12-22 16:54:50 +03:00
|
|
|
*/
|
2018-02-27 17:47:59 +03:00
|
|
|
public function addSubAdmin(string $userId, string $groupid): DataResponse {
|
2016-08-11 20:49:45 +03:00
|
|
|
$group = $this->groupManager->get($groupid);
|
|
|
|
$user = $this->userManager->get($userId);
|
2015-10-27 16:09:45 +03:00
|
|
|
|
2014-12-22 16:54:50 +03:00
|
|
|
// Check if the user exists
|
2018-04-06 16:07:04 +03:00
|
|
|
if ($user === null) {
|
2016-08-11 20:49:45 +03:00
|
|
|
throw new OCSException('User does not exist', 101);
|
2014-12-22 16:54:50 +03:00
|
|
|
}
|
|
|
|
// Check if group exists
|
2018-04-06 16:07:04 +03:00
|
|
|
if ($group === null) {
|
2017-04-24 11:15:03 +03:00
|
|
|
throw new OCSException('Group does not exist', 102);
|
2014-12-22 16:54:50 +03:00
|
|
|
}
|
|
|
|
// Check if trying to make subadmin of admin group
|
2018-04-06 16:07:04 +03:00
|
|
|
if ($group->getGID() === 'admin') {
|
2016-08-11 20:49:45 +03:00
|
|
|
throw new OCSException('Cannot create subadmins for admin group', 103);
|
2014-12-22 16:54:50 +03:00
|
|
|
}
|
2015-10-27 16:09:45 +03:00
|
|
|
|
|
|
|
$subAdminManager = $this->groupManager->getSubAdmin();
|
|
|
|
|
2015-04-13 11:32:18 +03:00
|
|
|
// We cannot be subadmin twice
|
2018-03-20 21:06:08 +03:00
|
|
|
if ($subAdminManager->isSubAdminOfGroup($user, $group)) {
|
2016-08-11 20:49:45 +03:00
|
|
|
return new DataResponse();
|
2015-04-13 11:32:18 +03:00
|
|
|
}
|
2014-12-22 16:54:50 +03:00
|
|
|
// Go
|
2019-01-29 12:38:05 +03:00
|
|
|
$subAdminManager->createSubAdmin($user, $group);
|
|
|
|
return new DataResponse();
|
2014-12-22 16:54:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a subadmin from a group
|
2015-09-08 15:02:30 +03:00
|
|
|
*
|
2016-12-05 13:55:21 +03:00
|
|
|
* @PasswordConfirmationRequired
|
|
|
|
*
|
2016-08-11 20:49:45 +03:00
|
|
|
* @param string $userId
|
|
|
|
* @param string $groupid
|
|
|
|
* @return DataResponse
|
|
|
|
* @throws OCSException
|
2014-12-22 16:54:50 +03:00
|
|
|
*/
|
2018-02-27 17:47:59 +03:00
|
|
|
public function removeSubAdmin(string $userId, string $groupid): DataResponse {
|
2016-08-11 20:49:45 +03:00
|
|
|
$group = $this->groupManager->get($groupid);
|
|
|
|
$user = $this->userManager->get($userId);
|
2015-10-27 16:09:45 +03:00
|
|
|
$subAdminManager = $this->groupManager->getSubAdmin();
|
|
|
|
|
2014-12-22 16:54:50 +03:00
|
|
|
// Check if the user exists
|
2018-04-06 16:07:04 +03:00
|
|
|
if ($user === null) {
|
2016-08-11 20:49:45 +03:00
|
|
|
throw new OCSException('User does not exist', 101);
|
2014-12-22 16:54:50 +03:00
|
|
|
}
|
2015-10-27 16:09:45 +03:00
|
|
|
// Check if the group exists
|
2018-04-06 16:07:04 +03:00
|
|
|
if ($group === null) {
|
2016-08-11 20:49:45 +03:00
|
|
|
throw new OCSException('Group does not exist', 101);
|
2015-10-27 16:09:45 +03:00
|
|
|
}
|
2014-12-22 16:54:50 +03:00
|
|
|
// Check if they are a subadmin of this said group
|
2018-04-06 16:07:04 +03:00
|
|
|
if (!$subAdminManager->isSubAdminOfGroup($user, $group)) {
|
2016-08-11 20:49:45 +03:00
|
|
|
throw new OCSException('User is not a subadmin of this group', 102);
|
2014-12-22 16:54:50 +03:00
|
|
|
}
|
2015-10-27 16:09:45 +03:00
|
|
|
|
2014-12-22 16:54:50 +03:00
|
|
|
// Go
|
2019-01-29 12:38:05 +03:00
|
|
|
$subAdminManager->deleteSubAdmin($user, $group);
|
|
|
|
return new DataResponse();
|
2014-12-22 16:54:50 +03:00
|
|
|
}
|
|
|
|
|
2018-03-16 11:18:53 +03:00
|
|
|
/**
|
|
|
|
* 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);
|
2018-03-16 17:37:05 +03:00
|
|
|
return new DataResponse($groups);
|
2014-12-22 16:54:50 +03:00
|
|
|
}
|
2015-05-19 13:38:03 +03:00
|
|
|
|
2017-02-24 09:01:13 +03:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @PasswordConfirmationRequired
|
|
|
|
*
|
|
|
|
* resend welcome message
|
|
|
|
*
|
|
|
|
* @param string $userId
|
|
|
|
* @return DataResponse
|
|
|
|
* @throws OCSException
|
|
|
|
*/
|
2018-02-27 17:47:59 +03:00
|
|
|
public function resendWelcomeMessage(string $userId): DataResponse {
|
2017-02-24 09:01:13 +03:00
|
|
|
$currentLoggedInUser = $this->userSession->getUser();
|
|
|
|
|
|
|
|
$targetUser = $this->userManager->get($userId);
|
2018-04-06 16:07:04 +03:00
|
|
|
if ($targetUser === null) {
|
2021-03-03 17:10:42 +03:00
|
|
|
throw new OCSException('', OCSController::RESPOND_NOT_FOUND);
|
2017-02-24 09:01:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if admin / subadmin
|
|
|
|
$subAdminManager = $this->groupManager->getSubAdmin();
|
2018-04-06 16:07:04 +03:00
|
|
|
if (!$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)
|
2017-02-24 09:01:13 +03:00
|
|
|
&& !$this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
|
|
|
|
// No rights
|
2021-03-03 17:10:42 +03:00
|
|
|
throw new OCSException('', OCSController::RESPOND_UNAUTHORISED);
|
2017-02-24 09:01:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$email = $targetUser->getEMailAddress();
|
|
|
|
if ($email === '' || $email === null) {
|
|
|
|
throw new OCSException('Email address not available', 101);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2017-04-07 15:51:05 +03:00
|
|
|
$emailTemplate = $this->newUserMailHelper->generateTemplate($targetUser, false);
|
|
|
|
$this->newUserMailHelper->sendMail($targetUser, $emailTemplate);
|
2020-04-10 15:19:56 +03:00
|
|
|
} catch (\Exception $e) {
|
2021-03-02 23:46:51 +03:00
|
|
|
$this->logger->error("Can't send new user mail to $email",
|
|
|
|
[
|
|
|
|
'app' => 'settings',
|
|
|
|
'exception' => $e,
|
|
|
|
]
|
|
|
|
);
|
2017-02-24 09:01:13 +03:00
|
|
|
throw new OCSException('Sending email failed', 102);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new DataResponse();
|
|
|
|
}
|
2014-12-22 16:54:50 +03:00
|
|
|
}
|