2014-12-22 13:54:50 +00:00
|
|
|
<?php
|
2018-02-27 14:47:59 +00:00
|
|
|
declare(strict_types=1);
|
2014-12-22 13:54:50 +00:00
|
|
|
/**
|
2016-07-21 14:49:16 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author Bjoern Schiessle <bjoern@schiessle.org>
|
2016-07-21 14:49:16 +00:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-10-26 12:54:55 +00:00
|
|
|
* @author michag86 <micha_g@arcor.de>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 14:49:16 +00:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2015-06-25 09:43:55 +00:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Tom Needham <tom@owncloud.com>
|
2018-03-26 17:30:40 +00:00
|
|
|
* @author John Molakvoæ <skjnldsv@protonmail.com>
|
2017-07-05 15:36:43 +00:00
|
|
|
* @author Thomas Citharel <tcit@tcit.fr>
|
2014-12-22 13:54:50 +00:00
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* @license AGPL-3.0
|
2014-12-22 13:54:50 +00:00
|
|
|
*
|
2015-03-26 10:44:34 +00: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 13:54:50 +00:00
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2014-12-22 13:54:50 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-03-26 10:44:34 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
2014-12-22 13:54:50 +00:00
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* 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/>
|
2014-12-22 13:54:50 +00:00
|
|
|
*
|
|
|
|
*/
|
2015-02-26 10:37:37 +00:00
|
|
|
|
2016-08-11 17:49:45 +00:00
|
|
|
namespace OCA\Provisioning_API\Controller;
|
2014-12-22 13:54:50 +00:00
|
|
|
|
2017-01-24 13:07:52 +00:00
|
|
|
use OC\Accounts\AccountManager;
|
2018-02-22 13:16:49 +00:00
|
|
|
use OC\HintException;
|
2017-04-07 12:51:05 +00:00
|
|
|
use OC\Settings\Mailer\NewUserMailHelper;
|
2018-01-26 11:47:59 +00:00
|
|
|
use OCA\Provisioning_API\FederatedFileSharingFactory;
|
2017-06-02 08:09:42 +00:00
|
|
|
use OCP\App\IAppManager;
|
2016-08-11 17:49:45 +00:00
|
|
|
use OCP\AppFramework\Http\DataResponse;
|
|
|
|
use OCP\AppFramework\OCS\OCSException;
|
|
|
|
use OCP\AppFramework\OCS\OCSForbiddenException;
|
2016-04-07 15:22:21 +00:00
|
|
|
use OCP\IConfig;
|
2017-01-18 10:43:52 +00:00
|
|
|
use OCP\IGroup;
|
2016-04-07 15:22:21 +00:00
|
|
|
use OCP\IGroupManager;
|
2015-10-27 13:09:45 +00:00
|
|
|
use OCP\ILogger;
|
2016-08-11 17:49:45 +00:00
|
|
|
use OCP\IRequest;
|
2016-04-07 15:22:21 +00:00
|
|
|
use OCP\IUserManager;
|
|
|
|
use OCP\IUserSession;
|
2017-02-24 06:01:13 +00:00
|
|
|
use OCP\L10N\IFactory;
|
2018-03-16 14:30:48 +00:00
|
|
|
use OCP\Security\ISecureRandom;
|
2014-12-22 13:54:50 +00:00
|
|
|
|
2018-03-27 09:05:36 +00:00
|
|
|
class UsersController extends AUserData {
|
2014-12-22 13:54:50 +00:00
|
|
|
|
2017-06-02 08:09:42 +00:00
|
|
|
/** @var IAppManager */
|
|
|
|
private $appManager;
|
2015-10-27 13:09:45 +00:00
|
|
|
/** @var ILogger */
|
|
|
|
private $logger;
|
2017-02-24 06:01:13 +00:00
|
|
|
/** @var IFactory */
|
|
|
|
private $l10nFactory;
|
2017-04-07 12:51:05 +00:00
|
|
|
/** @var NewUserMailHelper */
|
|
|
|
private $newUserMailHelper;
|
2018-01-26 11:47:59 +00:00
|
|
|
/** @var FederatedFileSharingFactory */
|
|
|
|
private $federatedFileSharingFactory;
|
2018-03-16 14:30:48 +00:00
|
|
|
/** @var ISecureRandom */
|
|
|
|
private $secureRandom;
|
2015-07-25 13:01:31 +00:00
|
|
|
|
2015-07-25 10:46:39 +00:00
|
|
|
/**
|
2016-08-11 17:49:45 +00:00
|
|
|
* @param string $appName
|
|
|
|
* @param IRequest $request
|
2016-04-07 15:22:21 +00:00
|
|
|
* @param IUserManager $userManager
|
|
|
|
* @param IConfig $config
|
2017-06-02 08:09:42 +00:00
|
|
|
* @param IAppManager $appManager
|
2016-04-07 15:22:21 +00:00
|
|
|
* @param IGroupManager $groupManager
|
|
|
|
* @param IUserSession $userSession
|
2017-01-24 13:07:52 +00:00
|
|
|
* @param AccountManager $accountManager
|
2015-10-27 13:09:45 +00:00
|
|
|
* @param ILogger $logger
|
2017-02-24 06:01:13 +00:00
|
|
|
* @param IFactory $l10nFactory
|
2017-04-07 12:51:05 +00:00
|
|
|
* @param NewUserMailHelper $newUserMailHelper
|
2018-01-26 11:47:59 +00:00
|
|
|
* @param FederatedFileSharingFactory $federatedFileSharingFactory
|
2018-03-16 14:30:48 +00:00
|
|
|
* @param ISecureRandom $secureRandom
|
2015-07-25 10:46:39 +00:00
|
|
|
*/
|
2018-02-27 14:47:59 +00:00
|
|
|
public function __construct(string $appName,
|
2016-08-11 17:49:45 +00:00
|
|
|
IRequest $request,
|
|
|
|
IUserManager $userManager,
|
2016-04-07 15:22:21 +00:00
|
|
|
IConfig $config,
|
2017-06-02 08:09:42 +00:00
|
|
|
IAppManager $appManager,
|
2016-04-07 15:22:21 +00:00
|
|
|
IGroupManager $groupManager,
|
|
|
|
IUserSession $userSession,
|
2017-01-24 13:07:52 +00:00
|
|
|
AccountManager $accountManager,
|
2017-02-24 06:01:13 +00:00
|
|
|
ILogger $logger,
|
2017-04-07 12:51:05 +00:00
|
|
|
IFactory $l10nFactory,
|
2018-01-26 11:47:59 +00:00
|
|
|
NewUserMailHelper $newUserMailHelper,
|
2018-03-16 14:30:48 +00:00
|
|
|
FederatedFileSharingFactory $federatedFileSharingFactory,
|
|
|
|
ISecureRandom $secureRandom) {
|
2018-03-27 09:05:36 +00:00
|
|
|
parent::__construct($appName,
|
|
|
|
$request,
|
|
|
|
$userManager,
|
|
|
|
$config,
|
|
|
|
$groupManager,
|
|
|
|
$userSession,
|
|
|
|
$accountManager);
|
2016-08-11 17:49:45 +00:00
|
|
|
|
2017-06-02 08:09:42 +00:00
|
|
|
$this->appManager = $appManager;
|
2015-10-27 13:09:45 +00:00
|
|
|
$this->logger = $logger;
|
2017-02-24 06:01:13 +00:00
|
|
|
$this->l10nFactory = $l10nFactory;
|
2017-04-07 12:51:05 +00:00
|
|
|
$this->newUserMailHelper = $newUserMailHelper;
|
2018-01-26 11:47:59 +00:00
|
|
|
$this->federatedFileSharingFactory = $federatedFileSharingFactory;
|
2018-03-16 14:30:48 +00:00
|
|
|
$this->secureRandom = $secureRandom;
|
2015-07-25 10:46:39 +00:00
|
|
|
}
|
|
|
|
|
2014-12-22 13:54:50 +00:00
|
|
|
/**
|
2016-08-11 17:49:45 +00:00
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
2014-12-22 13:54:50 +00:00
|
|
|
* returns a list of users
|
2015-09-08 12:02:30 +00:00
|
|
|
*
|
2016-08-11 17:49:45 +00:00
|
|
|
* @param string $search
|
|
|
|
* @param int $limit
|
|
|
|
* @param int $offset
|
|
|
|
* @return DataResponse
|
2014-12-22 13:54:50 +00:00
|
|
|
*/
|
2018-03-16 10:44:20 +00:00
|
|
|
public function getUsers(string $search = '', $limit = null, $offset = 0): DataResponse {
|
2015-08-18 15:43:16 +00:00
|
|
|
$user = $this->userSession->getUser();
|
2016-12-05 10:55:21 +00:00
|
|
|
$users = [];
|
2015-08-18 15:43:16 +00:00
|
|
|
|
|
|
|
// Admin? Or SubAdmin?
|
2015-10-27 13:09:45 +00:00
|
|
|
$uid = $user->getUID();
|
|
|
|
$subAdminManager = $this->groupManager->getSubAdmin();
|
2018-04-06 13:07:04 +00:00
|
|
|
if ($this->groupManager->isAdmin($uid)){
|
2015-08-18 15:43:16 +00:00
|
|
|
$users = $this->userManager->search($search, $limit, $offset);
|
2015-10-27 13:09:45 +00:00
|
|
|
} else if ($subAdminManager->isSubAdmin($user)) {
|
|
|
|
$subAdminOfGroups = $subAdminManager->getSubAdminsGroups($user);
|
|
|
|
foreach ($subAdminOfGroups as $key => $group) {
|
|
|
|
$subAdminOfGroups[$key] = $group->getGID();
|
|
|
|
}
|
2015-08-18 15:43:16 +00:00
|
|
|
|
|
|
|
$users = [];
|
|
|
|
foreach ($subAdminOfGroups as $group) {
|
2018-03-16 10:44:20 +00:00
|
|
|
$users = array_merge($users, $this->groupManager->displayNamesInGroup($group, $search, $limit, $offset));
|
2015-08-18 15:43:16 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-11 17:49:45 +00:00
|
|
|
|
2015-07-25 10:46:39 +00:00
|
|
|
$users = array_keys($users);
|
|
|
|
|
2016-08-11 17:49:45 +00:00
|
|
|
return new DataResponse([
|
2015-07-25 10:46:39 +00:00
|
|
|
'users' => $users
|
|
|
|
]);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
|
|
|
|
2018-03-16 08:18:53 +00:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* returns a list of users and their data
|
|
|
|
*/
|
2018-03-16 10:44:20 +00:00
|
|
|
public function getUsersDetails(string $search = '', $limit = null, $offset = 0): DataResponse {
|
2018-09-12 07:40:27 +00:00
|
|
|
$currentUser = $this->userSession->getUser();
|
2018-03-16 08:18:53 +00:00
|
|
|
$users = [];
|
|
|
|
|
|
|
|
// Admin? Or SubAdmin?
|
2018-09-12 07:40:27 +00:00
|
|
|
$uid = $currentUser->getUID();
|
2018-03-16 08:18:53 +00:00
|
|
|
$subAdminManager = $this->groupManager->getSubAdmin();
|
2018-04-06 13:07:04 +00:00
|
|
|
if ($this->groupManager->isAdmin($uid)){
|
2018-03-16 08:18:53 +00:00
|
|
|
$users = $this->userManager->search($search, $limit, $offset);
|
2018-09-12 07:40:27 +00:00
|
|
|
$users = array_keys($users);
|
|
|
|
} else if ($subAdminManager->isSubAdmin($currentUser)) {
|
|
|
|
$subAdminOfGroups = $subAdminManager->getSubAdminsGroups($currentUser);
|
2018-03-16 08:18:53 +00:00
|
|
|
foreach ($subAdminOfGroups as $key => $group) {
|
|
|
|
$subAdminOfGroups[$key] = $group->getGID();
|
2015-08-18 15:43:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$users = [];
|
|
|
|
foreach ($subAdminOfGroups as $group) {
|
2018-09-12 07:40:27 +00:00
|
|
|
$users[] = array_keys($this->groupManager->displayNamesInGroup($group, $search, $limit, $offset));
|
2015-08-18 15:43:16 +00:00
|
|
|
}
|
2018-09-12 07:40:27 +00:00
|
|
|
$users = array_merge(...$users);
|
2015-08-18 15:43:16 +00:00
|
|
|
}
|
2016-08-11 17:49:45 +00:00
|
|
|
|
2018-03-16 08:18:53 +00:00
|
|
|
$usersDetails = [];
|
2018-09-12 07:40:27 +00:00
|
|
|
foreach ($users as $userId) {
|
|
|
|
$userId = (string) $userId;
|
2018-03-16 10:44:20 +00:00
|
|
|
$userData = $this->getUserData($userId);
|
|
|
|
// Do not insert empty entry
|
2018-04-06 13:07:04 +00:00
|
|
|
if (!empty($userData)) {
|
2018-03-16 10:44:20 +00:00
|
|
|
$usersDetails[$userId] = $userData;
|
2018-05-23 11:01:40 +00:00
|
|
|
} else {
|
|
|
|
// Logged user does not have permissions to see this user
|
|
|
|
// only showing its id
|
|
|
|
$usersDetails[$userId] = ['id' => $userId];
|
2018-03-16 10:44:20 +00:00
|
|
|
}
|
2018-03-16 08:18:53 +00:00
|
|
|
}
|
2015-07-25 10:46:39 +00:00
|
|
|
|
2016-08-11 17:49:45 +00:00
|
|
|
return new DataResponse([
|
2018-03-16 08:18:53 +00:00
|
|
|
'users' => $usersDetails
|
2015-07-25 10:46:39 +00:00
|
|
|
]);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
|
|
|
|
2019-06-14 15:07:40 +00: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 12:02:30 +00:00
|
|
|
/**
|
2016-12-05 10:55:21 +00:00
|
|
|
* @PasswordConfirmationRequired
|
2016-08-11 17:49:45 +00:00
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* @param string $userid
|
|
|
|
* @param string $password
|
2018-04-18 22:12:06 +00:00
|
|
|
* @param string $displayName
|
2018-03-16 14:30:48 +00:00
|
|
|
* @param string $email
|
2016-08-11 17:49:45 +00:00
|
|
|
* @param array $groups
|
2019-01-10 09:49:09 +00:00
|
|
|
* @param array $subadmin
|
2018-04-06 13:07:04 +00:00
|
|
|
* @param string $quota
|
2018-04-12 10:37:26 +00:00
|
|
|
* @param string $language
|
2016-08-11 17:49:45 +00:00
|
|
|
* @return DataResponse
|
|
|
|
* @throws OCSException
|
2015-09-08 12:02:30 +00:00
|
|
|
*/
|
2018-04-06 13:07:04 +00:00
|
|
|
public function addUser(string $userid,
|
|
|
|
string $password = '',
|
2018-04-18 22:12:06 +00:00
|
|
|
string $displayName = '',
|
2018-04-06 13:07:04 +00:00
|
|
|
string $email = '',
|
|
|
|
array $groups = [],
|
|
|
|
array $subadmin = [],
|
2018-04-10 10:08:10 +00:00
|
|
|
string $quota = '',
|
2018-04-12 10:37:26 +00:00
|
|
|
string $language = ''): DataResponse {
|
2015-08-27 16:29:28 +00:00
|
|
|
$user = $this->userSession->getUser();
|
|
|
|
$isAdmin = $this->groupManager->isAdmin($user->getUID());
|
2015-10-30 08:30:00 +00:00
|
|
|
$subAdminManager = $this->groupManager->getSubAdmin();
|
2015-08-27 16:29:28 +00:00
|
|
|
|
2019-06-21 08:18:44 +00:00
|
|
|
if(empty($userid) && $this->config->getAppValue('core', 'newUser.generateUserID', 'no') === 'yes') {
|
2019-06-14 15:07:40 +00:00
|
|
|
$userid = $this->createNewUserId();
|
|
|
|
}
|
|
|
|
|
2018-04-06 13:07:04 +00:00
|
|
|
if ($this->userManager->userExists($userid)) {
|
2015-10-27 13:09:45 +00:00
|
|
|
$this->logger->error('Failed addUser attempt: User already exists.', ['app' => 'ocs_api']);
|
2016-08-11 17:49:45 +00:00
|
|
|
throw new OCSException('User already exists', 102);
|
2015-08-27 16:29:28 +00:00
|
|
|
}
|
|
|
|
|
2018-04-06 13:07:04 +00:00
|
|
|
if ($groups !== []) {
|
2015-10-30 08:30:00 +00:00
|
|
|
foreach ($groups as $group) {
|
2018-04-06 13:07:04 +00:00
|
|
|
if (!$this->groupManager->groupExists($group)) {
|
2016-08-11 17:49:45 +00:00
|
|
|
throw new OCSException('group '.$group.' does not exist', 104);
|
2015-08-27 16:29:28 +00:00
|
|
|
}
|
2018-04-06 13:07:04 +00:00
|
|
|
if (!$isAdmin && !$subAdminManager->isSubAdminOfGroup($user, $this->groupManager->get($group))) {
|
2016-08-11 17:49:45 +00:00
|
|
|
throw new OCSException('insufficient privileges for group '. $group, 105);
|
2015-08-27 16:29:28 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-22 13:54:50 +00:00
|
|
|
} else {
|
2018-04-06 13:07:04 +00:00
|
|
|
if (!$isAdmin) {
|
2016-08-11 17:49:45 +00:00
|
|
|
throw new OCSException('no group specified (required for subadmins)', 106);
|
2015-08-27 16:29:28 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-24 13:07:52 +00:00
|
|
|
|
2018-04-06 13:07:04 +00: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 14:30:48 +00:00
|
|
|
$generatePasswordResetToken = false;
|
|
|
|
if ($password === '') {
|
|
|
|
if ($email === '') {
|
|
|
|
throw new OCSException('To send a password link to the user an email address is required.', 108);
|
|
|
|
}
|
|
|
|
|
|
|
|
$password = $this->secureRandom->generate(10);
|
|
|
|
// Make sure we pass the password_policy
|
|
|
|
$password .= $this->secureRandom->generate(2, '$!.,;:-~+*[]{}()');
|
|
|
|
$generatePasswordResetToken = true;
|
|
|
|
}
|
|
|
|
|
2019-06-21 08:18:44 +00:00
|
|
|
if ($email === '' && $this->config->getAppValue('core', 'newUser.requireEmail', 'no') === 'yes') {
|
2019-06-14 15:07:40 +00:00
|
|
|
throw new OCSException('Required email address was not provided', 110);
|
|
|
|
}
|
|
|
|
|
2015-08-27 16:29:28 +00:00
|
|
|
try {
|
2016-08-11 17:49:45 +00:00
|
|
|
$newUser = $this->userManager->createUser($userid, $password);
|
2018-02-22 13:16:49 +00:00
|
|
|
$this->logger->info('Successful addUser call with userid: ' . $userid, ['app' => 'ocs_api']);
|
2015-08-27 16:29:28 +00:00
|
|
|
|
2018-02-27 21:03:40 +00: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 13:54:50 +00:00
|
|
|
}
|
2018-04-06 13:07:04 +00:00
|
|
|
foreach ($subadminGroups as $group) {
|
|
|
|
$subAdminManager->createSubAdmin($newUser, $group);
|
|
|
|
}
|
|
|
|
|
2018-04-18 22:12:06 +00:00
|
|
|
if ($displayName !== '') {
|
|
|
|
$this->editUser($userid, 'display', $displayName);
|
|
|
|
}
|
|
|
|
|
2018-04-06 13:07:04 +00:00
|
|
|
if ($quota !== '') {
|
|
|
|
$this->editUser($userid, 'quota', $quota);
|
|
|
|
}
|
2018-02-27 21:03:40 +00:00
|
|
|
|
2018-04-10 10:08:10 +00:00
|
|
|
if ($language !== '') {
|
|
|
|
$this->editUser($userid, 'language', $language);
|
|
|
|
}
|
|
|
|
|
2018-03-16 14:30:48 +00:00
|
|
|
// Send new user mail only if a mail is set
|
|
|
|
if ($email !== '') {
|
|
|
|
$newUser->setEMailAddress($email);
|
|
|
|
try {
|
|
|
|
$emailTemplate = $this->newUserMailHelper->generateTemplate($newUser, $generatePasswordResetToken);
|
|
|
|
$this->newUserMailHelper->sendMail($newUser, $emailTemplate);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->logger->logException($e, [
|
|
|
|
'message' => "Can't send new user mail to $email",
|
2018-04-25 13:22:28 +00:00
|
|
|
'level' => ILogger::ERROR,
|
2018-03-16 14:30:48 +00:00
|
|
|
'app' => 'ocs_api',
|
|
|
|
]);
|
|
|
|
throw new OCSException('Unable to send the invitation mail', 109);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-21 14:38:25 +00:00
|
|
|
return new DataResponse(['id' => $userid]);
|
2018-03-16 14:30:48 +00:00
|
|
|
|
2019-06-20 16:04:52 +00:00
|
|
|
} catch (HintException $e) {
|
2018-02-22 13:16:49 +00:00
|
|
|
$this->logger->logException($e, [
|
|
|
|
'message' => 'Failed addUser attempt with hint exception.',
|
2018-04-25 13:22:28 +00:00
|
|
|
'level' => ILogger::WARN,
|
2018-02-22 13:16:49 +00:00
|
|
|
'app' => 'ocs_api',
|
|
|
|
]);
|
|
|
|
throw new OCSException($e->getHint(), 107);
|
2019-06-20 16:04:52 +00:00
|
|
|
} catch (OCSException $e) {
|
|
|
|
$this->logger->logException($e, [
|
|
|
|
'message' => 'Failed addUser attempt with ocs exeption.',
|
|
|
|
'level' => ILogger::ERROR,
|
|
|
|
'app' => 'ocs_api',
|
|
|
|
]);
|
|
|
|
throw $e;
|
2015-08-27 16:29:28 +00:00
|
|
|
} catch (\Exception $e) {
|
2018-01-17 14:21:56 +00:00
|
|
|
$this->logger->logException($e, [
|
|
|
|
'message' => 'Failed addUser attempt with exception.',
|
2018-04-25 13:22:28 +00:00
|
|
|
'level' => ILogger::ERROR,
|
2018-01-17 14:21:56 +00:00
|
|
|
'app' => 'ocs_api',
|
|
|
|
]);
|
2016-08-11 17:49:45 +00:00
|
|
|
throw new OCSException('Bad request', 101);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-11 17:49:45 +00:00
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoSubAdminRequired
|
|
|
|
*
|
2014-12-22 13:54:50 +00:00
|
|
|
* gets user info
|
2015-09-08 12:02:30 +00:00
|
|
|
*
|
2016-08-11 17:49:45 +00:00
|
|
|
* @param string $userId
|
|
|
|
* @return DataResponse
|
|
|
|
* @throws OCSException
|
2014-12-22 13:54:50 +00:00
|
|
|
*/
|
2018-02-27 14:47:59 +00:00
|
|
|
public function getUser(string $userId): DataResponse {
|
2017-01-26 10:31:08 +00:00
|
|
|
$data = $this->getUserData($userId);
|
2018-03-16 10:44:20 +00:00
|
|
|
// getUserData returns empty array if not enough permissions
|
2018-04-06 13:07:04 +00:00
|
|
|
if (empty($data)) {
|
2018-03-16 10:44:20 +00:00
|
|
|
throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
|
|
|
|
}
|
2017-01-26 10:31:08 +00:00
|
|
|
return new DataResponse($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoSubAdminRequired
|
|
|
|
*
|
|
|
|
* gets user info from the currently logged in user
|
|
|
|
*
|
|
|
|
* @return DataResponse
|
|
|
|
* @throws OCSException
|
|
|
|
*/
|
2018-02-27 14:47:59 +00:00
|
|
|
public function getCurrentUser(): DataResponse {
|
2017-01-26 10:31:08 +00:00
|
|
|
$user = $this->userSession->getUser();
|
|
|
|
if ($user) {
|
|
|
|
$data = $this->getUserData($user->getUID());
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
|
|
|
|
}
|
|
|
|
|
2018-01-22 20:47:25 +00:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoSubAdminRequired
|
|
|
|
*/
|
2018-02-27 14:47:59 +00:00
|
|
|
public function getEditableFields(): DataResponse {
|
2018-01-22 20:47:25 +00:00
|
|
|
$permittedFields = [];
|
|
|
|
|
|
|
|
// Editing self (display, email)
|
|
|
|
if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) {
|
|
|
|
$permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME;
|
|
|
|
$permittedFields[] = AccountManager::PROPERTY_EMAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->appManager->isEnabledForUser('federatedfilesharing')) {
|
2018-01-26 11:47:59 +00:00
|
|
|
$federatedFileSharing = $this->federatedFileSharingFactory->get();
|
2018-01-22 20:47:25 +00:00
|
|
|
$shareProvider = $federatedFileSharing->getFederatedShareProvider();
|
|
|
|
if ($shareProvider->isLookupServerUploadEnabled()) {
|
|
|
|
$permittedFields[] = AccountManager::PROPERTY_PHONE;
|
|
|
|
$permittedFields[] = AccountManager::PROPERTY_ADDRESS;
|
|
|
|
$permittedFields[] = AccountManager::PROPERTY_WEBSITE;
|
|
|
|
$permittedFields[] = AccountManager::PROPERTY_TWITTER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new DataResponse($permittedFields);
|
|
|
|
}
|
|
|
|
|
2016-08-11 17:49:45 +00:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoSubAdminRequired
|
2016-12-05 10:55:21 +00:00
|
|
|
* @PasswordConfirmationRequired
|
2016-08-11 17:49:45 +00:00
|
|
|
*
|
2014-12-22 13:54:50 +00:00
|
|
|
* edit users
|
2015-09-08 12:02:30 +00:00
|
|
|
*
|
2016-08-11 17:49:45 +00:00
|
|
|
* @param string $userId
|
|
|
|
* @param string $key
|
|
|
|
* @param string $value
|
|
|
|
* @return DataResponse
|
|
|
|
* @throws OCSException
|
2014-12-22 13:54:50 +00:00
|
|
|
*/
|
2018-02-27 14:47:59 +00:00
|
|
|
public function editUser(string $userId, string $key, string $value): DataResponse {
|
2015-10-27 13:09:45 +00:00
|
|
|
$currentLoggedInUser = $this->userSession->getUser();
|
2015-08-11 13:37:06 +00:00
|
|
|
|
2016-08-11 17:49:45 +00:00
|
|
|
$targetUser = $this->userManager->get($userId);
|
2018-04-06 13:07:04 +00:00
|
|
|
if ($targetUser === null) {
|
2016-08-11 17:49:45 +00:00
|
|
|
throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
|
2015-10-27 13:09:45 +00:00
|
|
|
}
|
|
|
|
|
2016-07-12 06:38:56 +00:00
|
|
|
$permittedFields = [];
|
2018-04-06 13:07:04 +00:00
|
|
|
if ($targetUser->getUID() === $currentLoggedInUser->getUID()) {
|
2014-12-22 13:54:50 +00:00
|
|
|
// Editing self (display, email)
|
2017-06-02 08:09:42 +00:00
|
|
|
if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) {
|
|
|
|
$permittedFields[] = 'display';
|
|
|
|
$permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME;
|
|
|
|
$permittedFields[] = AccountManager::PROPERTY_EMAIL;
|
|
|
|
}
|
|
|
|
|
2014-12-22 13:54:50 +00:00
|
|
|
$permittedFields[] = 'password';
|
2017-06-21 09:22:05 +00:00
|
|
|
if ($this->config->getSystemValue('force_language', false) === false ||
|
|
|
|
$this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
|
|
|
|
$permittedFields[] = 'language';
|
2017-07-13 08:34:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->config->getSystemValue('force_locale', false) === false ||
|
|
|
|
$this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
|
2017-07-05 15:36:43 +00:00
|
|
|
$permittedFields[] = 'locale';
|
2017-06-21 09:22:05 +00:00
|
|
|
}
|
2017-06-02 08:09:42 +00:00
|
|
|
|
|
|
|
if ($this->appManager->isEnabledForUser('federatedfilesharing')) {
|
|
|
|
$federatedFileSharing = new \OCA\FederatedFileSharing\AppInfo\Application();
|
|
|
|
$shareProvider = $federatedFileSharing->getFederatedShareProvider();
|
|
|
|
if ($shareProvider->isLookupServerUploadEnabled()) {
|
|
|
|
$permittedFields[] = AccountManager::PROPERTY_PHONE;
|
|
|
|
$permittedFields[] = AccountManager::PROPERTY_ADDRESS;
|
|
|
|
$permittedFields[] = AccountManager::PROPERTY_WEBSITE;
|
|
|
|
$permittedFields[] = AccountManager::PROPERTY_TWITTER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-22 13:54:50 +00:00
|
|
|
// If admin they can edit their own quota
|
2018-04-06 13:07:04 +00:00
|
|
|
if ($this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
|
2014-12-22 13:54:50 +00:00
|
|
|
$permittedFields[] = 'quota';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Check if admin / subadmin
|
2015-10-27 13:09:45 +00:00
|
|
|
$subAdminManager = $this->groupManager->getSubAdmin();
|
2018-04-06 13:07:04 +00:00
|
|
|
if ($subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)
|
2015-10-27 13:09:45 +00:00
|
|
|
|| $this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
|
2014-12-22 13:54:50 +00:00
|
|
|
// They have permissions over the user
|
|
|
|
$permittedFields[] = 'display';
|
2017-04-05 12:51:55 +00:00
|
|
|
$permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME;
|
|
|
|
$permittedFields[] = AccountManager::PROPERTY_EMAIL;
|
2014-12-22 13:54:50 +00:00
|
|
|
$permittedFields[] = 'password';
|
2017-06-21 09:22:05 +00:00
|
|
|
$permittedFields[] = 'language';
|
2017-07-05 15:36:43 +00:00
|
|
|
$permittedFields[] = 'locale';
|
2017-04-05 12:51:55 +00:00
|
|
|
$permittedFields[] = AccountManager::PROPERTY_PHONE;
|
|
|
|
$permittedFields[] = AccountManager::PROPERTY_ADDRESS;
|
|
|
|
$permittedFields[] = AccountManager::PROPERTY_WEBSITE;
|
|
|
|
$permittedFields[] = AccountManager::PROPERTY_TWITTER;
|
|
|
|
$permittedFields[] = 'quota';
|
2014-12-22 13:54:50 +00:00
|
|
|
} else {
|
|
|
|
// No rights
|
2016-08-11 17:49:45 +00:00
|
|
|
throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check if permitted to edit this field
|
2018-04-06 13:07:04 +00:00
|
|
|
if (!in_array($key, $permittedFields)) {
|
2016-08-11 17:49:45 +00:00
|
|
|
throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
|
|
|
// Process the edit
|
2016-08-11 17:49:45 +00:00
|
|
|
switch($key) {
|
2014-12-22 13:54:50 +00:00
|
|
|
case 'display':
|
2017-04-05 12:51:55 +00:00
|
|
|
case AccountManager::PROPERTY_DISPLAYNAME:
|
2016-08-11 17:49:45 +00:00
|
|
|
$targetUser->setDisplayName($value);
|
2014-12-22 13:54:50 +00:00
|
|
|
break;
|
|
|
|
case 'quota':
|
2016-08-11 17:49:45 +00:00
|
|
|
$quota = $value;
|
2018-04-06 13:07:04 +00:00
|
|
|
if ($quota !== 'none' && $quota !== 'default') {
|
2015-05-07 15:56:13 +00:00
|
|
|
if (is_numeric($quota)) {
|
2017-01-18 10:43:52 +00:00
|
|
|
$quota = (float) $quota;
|
2015-05-07 15:56:13 +00:00
|
|
|
} else {
|
2015-08-02 08:27:36 +00:00
|
|
|
$quota = \OCP\Util::computerFileSize($quota);
|
2015-05-07 15:56:13 +00:00
|
|
|
}
|
|
|
|
if ($quota === false) {
|
2016-08-11 17:49:45 +00:00
|
|
|
throw new OCSException('Invalid quota value '.$value, 103);
|
2015-05-07 15:56:13 +00:00
|
|
|
}
|
2018-05-26 08:51:24 +00:00
|
|
|
if ($quota === -1) {
|
2014-12-22 13:54:50 +00:00
|
|
|
$quota = 'none';
|
|
|
|
} else {
|
2015-08-02 08:27:36 +00:00
|
|
|
$quota = \OCP\Util::humanFileSize($quota);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-09 16:16:43 +00:00
|
|
|
$targetUser->setQuota($quota);
|
2014-12-22 13:54:50 +00:00
|
|
|
break;
|
|
|
|
case 'password':
|
2019-01-07 15:28:06 +00:00
|
|
|
try {
|
2019-03-01 09:11:37 +00:00
|
|
|
if (!$targetUser->canChangePassword()) {
|
|
|
|
throw new OCSException('Setting the password is not supported by the users backend', 103);
|
|
|
|
}
|
2019-01-07 15:28:06 +00:00
|
|
|
$targetUser->setPassword($value);
|
|
|
|
} catch (HintException $e) { // password policy error
|
|
|
|
throw new OCSException($e->getMessage(), 103);
|
|
|
|
}
|
2014-12-22 13:54:50 +00:00
|
|
|
break;
|
2017-06-21 09:22:05 +00: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 15:36:43 +00:00
|
|
|
case 'locale':
|
2017-07-07 09:17:54 +00:00
|
|
|
if (!$this->l10nFactory->localeExists($value)) {
|
|
|
|
throw new OCSException('Invalid locale', 102);
|
|
|
|
}
|
2017-07-05 15:36:43 +00:00
|
|
|
$this->config->setUserValue($targetUser->getUID(), 'core', 'locale', $value);
|
|
|
|
break;
|
2017-04-05 12:51:55 +00:00
|
|
|
case AccountManager::PROPERTY_EMAIL:
|
2018-04-06 13:07:04 +00:00
|
|
|
if (filter_var($value, FILTER_VALIDATE_EMAIL) || $value === '') {
|
2016-08-11 17:49:45 +00:00
|
|
|
$targetUser->setEMailAddress($value);
|
2014-12-22 13:54:50 +00:00
|
|
|
} else {
|
2016-08-11 17:49:45 +00:00
|
|
|
throw new OCSException('', 102);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
|
|
|
break;
|
2017-04-05 12:51:55 +00:00
|
|
|
case AccountManager::PROPERTY_PHONE:
|
|
|
|
case AccountManager::PROPERTY_ADDRESS:
|
|
|
|
case AccountManager::PROPERTY_WEBSITE:
|
|
|
|
case AccountManager::PROPERTY_TWITTER:
|
|
|
|
$userAccount = $this->accountManager->getUser($targetUser);
|
|
|
|
if ($userAccount[$key]['value'] !== $value) {
|
|
|
|
$userAccount[$key]['value'] = $value;
|
|
|
|
$this->accountManager->updateUser($targetUser, $userAccount);
|
|
|
|
}
|
|
|
|
break;
|
2014-12-22 13:54:50 +00:00
|
|
|
default:
|
2016-08-11 17:49:45 +00:00
|
|
|
throw new OCSException('', 103);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
2016-08-11 17:49:45 +00:00
|
|
|
return new DataResponse();
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
|
|
|
|
2015-09-08 12:02:30 +00:00
|
|
|
/**
|
2016-12-05 10:55:21 +00:00
|
|
|
* @PasswordConfirmationRequired
|
2016-08-11 17:49:45 +00:00
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* @param string $userId
|
|
|
|
* @return DataResponse
|
|
|
|
* @throws OCSException
|
2015-09-08 12:02:30 +00:00
|
|
|
*/
|
2018-02-27 14:47:59 +00:00
|
|
|
public function deleteUser(string $userId): DataResponse {
|
2015-10-27 13:09:45 +00:00
|
|
|
$currentLoggedInUser = $this->userSession->getUser();
|
2015-08-11 13:37:06 +00:00
|
|
|
|
2016-08-11 17:49:45 +00:00
|
|
|
$targetUser = $this->userManager->get($userId);
|
2015-10-27 13:09:45 +00:00
|
|
|
|
2018-04-06 13:07:04 +00:00
|
|
|
if ($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) {
|
2016-08-11 17:49:45 +00:00
|
|
|
throw new OCSException('', 101);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
2015-10-27 13:09:45 +00:00
|
|
|
|
2014-12-22 13:54:50 +00:00
|
|
|
// If not permitted
|
2015-10-27 13:09:45 +00:00
|
|
|
$subAdminManager = $this->groupManager->getSubAdmin();
|
2018-04-06 13:07:04 +00:00
|
|
|
if (!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) {
|
2016-08-11 17:49:45 +00:00
|
|
|
throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
2015-10-27 13:09:45 +00:00
|
|
|
|
2014-12-22 13:54:50 +00:00
|
|
|
// Go ahead with the delete
|
2018-04-06 13:07:04 +00:00
|
|
|
if ($targetUser->delete()) {
|
2016-08-11 17:49:45 +00:00
|
|
|
return new DataResponse();
|
2014-12-22 13:54:50 +00:00
|
|
|
} else {
|
2016-08-11 17:49:45 +00:00
|
|
|
throw new OCSException('', 101);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-07 15:22:21 +00:00
|
|
|
/**
|
2016-12-05 10:55:21 +00:00
|
|
|
* @PasswordConfirmationRequired
|
2016-08-11 17:49:45 +00:00
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* @param string $userId
|
|
|
|
* @return DataResponse
|
2016-12-05 10:55:21 +00:00
|
|
|
* @throws OCSException
|
|
|
|
* @throws OCSForbiddenException
|
2016-04-07 15:22:21 +00:00
|
|
|
*/
|
2018-02-27 14:47:59 +00:00
|
|
|
public function disableUser(string $userId): DataResponse {
|
2016-08-11 17:49:45 +00:00
|
|
|
return $this->setEnabled($userId, false);
|
2016-04-07 15:22:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-05 10:55:21 +00:00
|
|
|
* @PasswordConfirmationRequired
|
2016-08-11 17:49:45 +00:00
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* @param string $userId
|
|
|
|
* @return DataResponse
|
2016-12-05 10:55:21 +00:00
|
|
|
* @throws OCSException
|
|
|
|
* @throws OCSForbiddenException
|
2016-04-07 15:22:21 +00:00
|
|
|
*/
|
2018-02-27 14:47:59 +00:00
|
|
|
public function enableUser(string $userId): DataResponse {
|
2016-08-11 17:49:45 +00:00
|
|
|
return $this->setEnabled($userId, true);
|
2016-04-07 15:22:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-11 17:49:45 +00:00
|
|
|
* @param string $userId
|
2016-04-07 15:22:21 +00:00
|
|
|
* @param bool $value
|
2016-08-11 17:49:45 +00:00
|
|
|
* @return DataResponse
|
|
|
|
* @throws OCSException
|
2016-04-07 15:22:21 +00:00
|
|
|
*/
|
2018-02-27 14:47:59 +00:00
|
|
|
private function setEnabled(string $userId, bool $value): DataResponse {
|
2016-04-07 15:22:21 +00:00
|
|
|
$currentLoggedInUser = $this->userSession->getUser();
|
|
|
|
|
2016-08-11 17:49:45 +00:00
|
|
|
$targetUser = $this->userManager->get($userId);
|
2018-04-06 13:07:04 +00:00
|
|
|
if ($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) {
|
2016-08-11 17:49:45 +00:00
|
|
|
throw new OCSException('', 101);
|
2016-04-07 15:22:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If not permitted
|
|
|
|
$subAdminManager = $this->groupManager->getSubAdmin();
|
2018-04-06 13:07:04 +00:00
|
|
|
if (!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) {
|
2016-08-11 17:49:45 +00:00
|
|
|
throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
|
2016-04-07 15:22:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// enable/disable the user now
|
|
|
|
$targetUser->setEnabled($value);
|
2016-08-11 17:49:45 +00:00
|
|
|
return new DataResponse();
|
2016-04-07 15:22:21 +00:00
|
|
|
}
|
|
|
|
|
2015-09-08 12:02:30 +00:00
|
|
|
/**
|
2016-08-11 17:49:45 +00:00
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoSubAdminRequired
|
|
|
|
*
|
|
|
|
* @param string $userId
|
|
|
|
* @return DataResponse
|
2016-12-05 10:55:21 +00:00
|
|
|
* @throws OCSException
|
2015-09-08 12:02:30 +00:00
|
|
|
*/
|
2018-02-27 14:47:59 +00:00
|
|
|
public function getUsersGroups(string $userId): DataResponse {
|
2015-10-27 13:09:45 +00:00
|
|
|
$loggedInUser = $this->userSession->getUser();
|
2015-08-11 13:37:06 +00:00
|
|
|
|
2016-08-11 17:49:45 +00:00
|
|
|
$targetUser = $this->userManager->get($userId);
|
2018-04-06 13:07:04 +00:00
|
|
|
if ($targetUser === null) {
|
2016-08-11 17:49:45 +00:00
|
|
|
throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND);
|
2015-10-27 13:09:45 +00:00
|
|
|
}
|
|
|
|
|
2018-04-06 13:07:04 +00:00
|
|
|
if ($targetUser->getUID() === $loggedInUser->getUID() || $this->groupManager->isAdmin($loggedInUser->getUID())) {
|
2014-12-22 13:54:50 +00:00
|
|
|
// Self lookup or admin lookup
|
2016-08-11 17:49:45 +00:00
|
|
|
return new DataResponse([
|
2015-10-27 13:09:45 +00:00
|
|
|
'groups' => $this->groupManager->getUserGroupIds($targetUser)
|
2015-07-25 10:46:39 +00:00
|
|
|
]);
|
2014-12-22 13:54:50 +00:00
|
|
|
} else {
|
2015-10-27 13:09:45 +00:00
|
|
|
$subAdminManager = $this->groupManager->getSubAdmin();
|
|
|
|
|
2014-12-22 13:54:50 +00:00
|
|
|
// Looking up someone else
|
2018-04-06 13:07:04 +00:00
|
|
|
if ($subAdminManager->isUserAccessible($loggedInUser, $targetUser)) {
|
2014-12-22 13:54:50 +00:00
|
|
|
// Return the group that the method caller is subadmin of for the user in question
|
2017-01-18 10:43:52 +00:00
|
|
|
/** @var IGroup[] $getSubAdminsGroups */
|
2015-10-27 13:09:45 +00:00
|
|
|
$getSubAdminsGroups = $subAdminManager->getSubAdminsGroups($loggedInUser);
|
|
|
|
foreach ($getSubAdminsGroups as $key => $group) {
|
|
|
|
$getSubAdminsGroups[$key] = $group->getGID();
|
|
|
|
}
|
2015-07-25 10:46:39 +00:00
|
|
|
$groups = array_intersect(
|
2015-10-27 13:09:45 +00:00
|
|
|
$getSubAdminsGroups,
|
|
|
|
$this->groupManager->getUserGroupIds($targetUser)
|
2015-07-25 10:46:39 +00:00
|
|
|
);
|
2016-08-11 17:49:45 +00:00
|
|
|
return new DataResponse(['groups' => $groups]);
|
2014-12-22 13:54:50 +00:00
|
|
|
} else {
|
|
|
|
// Not permitted
|
2016-08-11 17:49:45 +00:00
|
|
|
throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-24 13:07:52 +00:00
|
|
|
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
|
|
|
|
2015-09-08 12:02:30 +00:00
|
|
|
/**
|
2016-12-05 10:55:21 +00:00
|
|
|
* @PasswordConfirmationRequired
|
2017-01-18 13:34:38 +00:00
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
2016-08-11 17:49:45 +00:00
|
|
|
* @param string $userId
|
|
|
|
* @param string $groupid
|
|
|
|
* @return DataResponse
|
|
|
|
* @throws OCSException
|
2015-09-08 12:02:30 +00:00
|
|
|
*/
|
2018-02-27 14:47:59 +00:00
|
|
|
public function addToGroup(string $userId, string $groupid = ''): DataResponse {
|
2018-04-06 13:07:04 +00:00
|
|
|
if ($groupid === '') {
|
2016-08-11 17:49:45 +00:00
|
|
|
throw new OCSException('', 101);
|
2015-08-11 13:37:06 +00:00
|
|
|
}
|
|
|
|
|
2016-08-11 17:49:45 +00:00
|
|
|
$group = $this->groupManager->get($groupid);
|
|
|
|
$targetUser = $this->userManager->get($userId);
|
2018-04-06 13:07:04 +00:00
|
|
|
if ($group === null) {
|
2016-08-11 17:49:45 +00:00
|
|
|
throw new OCSException('', 102);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
2018-04-06 13:07:04 +00:00
|
|
|
if ($targetUser === null) {
|
2016-08-11 17:49:45 +00:00
|
|
|
throw new OCSException('', 103);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
2015-10-27 13:09:45 +00:00
|
|
|
|
2017-01-18 13:34:38 +00: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 13:54:50 +00:00
|
|
|
// Add user to group
|
2015-10-27 13:09:45 +00:00
|
|
|
$group->addUser($targetUser);
|
2016-08-11 17:49:45 +00:00
|
|
|
return new DataResponse();
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
|
|
|
|
2015-09-08 12:02:30 +00:00
|
|
|
/**
|
2016-12-05 10:55:21 +00:00
|
|
|
* @PasswordConfirmationRequired
|
2016-08-11 17:49:45 +00:00
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
2016-12-05 10:55:21 +00:00
|
|
|
* @param string $userId
|
2016-08-11 17:49:45 +00:00
|
|
|
* @param string $groupid
|
|
|
|
* @return DataResponse
|
|
|
|
* @throws OCSException
|
2015-09-08 12:02:30 +00:00
|
|
|
*/
|
2018-02-27 14:47:59 +00:00
|
|
|
public function removeFromGroup(string $userId, string $groupid): DataResponse {
|
2015-10-27 13:09:45 +00:00
|
|
|
$loggedInUser = $this->userSession->getUser();
|
2015-08-11 13:37:06 +00:00
|
|
|
|
2018-04-06 13:07:04 +00:00
|
|
|
if ($groupid === null || trim($groupid) === '') {
|
2016-08-11 17:49:45 +00:00
|
|
|
throw new OCSException('', 101);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
2015-10-27 13:09:45 +00:00
|
|
|
|
2016-08-11 17:49:45 +00:00
|
|
|
$group = $this->groupManager->get($groupid);
|
2018-04-06 13:07:04 +00:00
|
|
|
if ($group === null) {
|
2016-08-11 17:49:45 +00:00
|
|
|
throw new OCSException('', 102);
|
2015-11-05 16:01:47 +00:00
|
|
|
}
|
|
|
|
|
2016-08-11 17:49:45 +00:00
|
|
|
$targetUser = $this->userManager->get($userId);
|
2018-04-06 13:07:04 +00:00
|
|
|
if ($targetUser === null) {
|
2016-08-11 17:49:45 +00:00
|
|
|
throw new OCSException('', 103);
|
2015-10-27 13:09:45 +00:00
|
|
|
}
|
|
|
|
|
2014-12-22 13:54:50 +00:00
|
|
|
// If they're not an admin, check they are a subadmin of the group in question
|
2015-10-27 13:09:45 +00:00
|
|
|
$subAdminManager = $this->groupManager->getSubAdmin();
|
2017-01-18 10:43:52 +00:00
|
|
|
if (!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminOfGroup($loggedInUser, $group)) {
|
2016-08-11 17:49:45 +00:00
|
|
|
throw new OCSException('', 104);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
2017-01-18 10:43:52 +00:00
|
|
|
|
2014-12-22 13:54:50 +00:00
|
|
|
// Check they aren't removing themselves from 'admin' or their 'subadmin; group
|
2017-04-24 07:43:44 +00:00
|
|
|
if ($targetUser->getUID() === $loggedInUser->getUID()) {
|
2017-01-18 10:43:52 +00:00
|
|
|
if ($this->groupManager->isAdmin($loggedInUser->getUID())) {
|
|
|
|
if ($group->getGID() === 'admin') {
|
2016-08-11 17:49:45 +00:00
|
|
|
throw new OCSException('Cannot remove yourself from the admin group', 105);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-01-18 10:43:52 +00: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 13:54:50 +00:00
|
|
|
}
|
2017-01-18 10:56:24 +00:00
|
|
|
|
|
|
|
} else if (!$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.
|
2019-05-16 12:17:41 +00:00
|
|
|
throw new OCSException('Not viable to remove user from the last group you are SubAdmin of', 105);
|
2017-01-18 10:56:24 +00:00
|
|
|
}
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
2015-10-27 13:09:45 +00:00
|
|
|
|
2014-12-22 13:54:50 +00:00
|
|
|
// Remove user from group
|
2015-10-27 13:09:45 +00:00
|
|
|
$group->removeUser($targetUser);
|
2016-08-11 17:49:45 +00:00
|
|
|
return new DataResponse();
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a subadmin
|
2015-09-08 12:02:30 +00:00
|
|
|
*
|
2016-12-05 10:55:21 +00:00
|
|
|
* @PasswordConfirmationRequired
|
|
|
|
*
|
2016-08-11 17:49:45 +00:00
|
|
|
* @param string $userId
|
|
|
|
* @param string $groupid
|
|
|
|
* @return DataResponse
|
|
|
|
* @throws OCSException
|
2014-12-22 13:54:50 +00:00
|
|
|
*/
|
2018-02-27 14:47:59 +00:00
|
|
|
public function addSubAdmin(string $userId, string $groupid): DataResponse {
|
2016-08-11 17:49:45 +00:00
|
|
|
$group = $this->groupManager->get($groupid);
|
|
|
|
$user = $this->userManager->get($userId);
|
2015-10-27 13:09:45 +00:00
|
|
|
|
2014-12-22 13:54:50 +00:00
|
|
|
// Check if the user exists
|
2018-04-06 13:07:04 +00:00
|
|
|
if ($user === null) {
|
2016-08-11 17:49:45 +00:00
|
|
|
throw new OCSException('User does not exist', 101);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
|
|
|
// Check if group exists
|
2018-04-06 13:07:04 +00:00
|
|
|
if ($group === null) {
|
2017-04-24 08:15:03 +00:00
|
|
|
throw new OCSException('Group does not exist', 102);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
|
|
|
// Check if trying to make subadmin of admin group
|
2018-04-06 13:07:04 +00:00
|
|
|
if ($group->getGID() === 'admin') {
|
2016-08-11 17:49:45 +00:00
|
|
|
throw new OCSException('Cannot create subadmins for admin group', 103);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
2015-10-27 13:09:45 +00:00
|
|
|
|
|
|
|
$subAdminManager = $this->groupManager->getSubAdmin();
|
|
|
|
|
2015-04-13 08:32:18 +00:00
|
|
|
// We cannot be subadmin twice
|
2018-03-20 18:06:08 +00:00
|
|
|
if ($subAdminManager->isSubAdminOfGroup($user, $group)) {
|
2016-08-11 17:49:45 +00:00
|
|
|
return new DataResponse();
|
2015-04-13 08:32:18 +00:00
|
|
|
}
|
2014-12-22 13:54:50 +00:00
|
|
|
// Go
|
2019-01-29 09:38:05 +00:00
|
|
|
$subAdminManager->createSubAdmin($user, $group);
|
|
|
|
return new DataResponse();
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a subadmin from a group
|
2015-09-08 12:02:30 +00:00
|
|
|
*
|
2016-12-05 10:55:21 +00:00
|
|
|
* @PasswordConfirmationRequired
|
|
|
|
*
|
2016-08-11 17:49:45 +00:00
|
|
|
* @param string $userId
|
|
|
|
* @param string $groupid
|
|
|
|
* @return DataResponse
|
|
|
|
* @throws OCSException
|
2014-12-22 13:54:50 +00:00
|
|
|
*/
|
2018-02-27 14:47:59 +00:00
|
|
|
public function removeSubAdmin(string $userId, string $groupid): DataResponse {
|
2016-08-11 17:49:45 +00:00
|
|
|
$group = $this->groupManager->get($groupid);
|
|
|
|
$user = $this->userManager->get($userId);
|
2015-10-27 13:09:45 +00:00
|
|
|
$subAdminManager = $this->groupManager->getSubAdmin();
|
|
|
|
|
2014-12-22 13:54:50 +00:00
|
|
|
// Check if the user exists
|
2018-04-06 13:07:04 +00:00
|
|
|
if ($user === null) {
|
2016-08-11 17:49:45 +00:00
|
|
|
throw new OCSException('User does not exist', 101);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
2015-10-27 13:09:45 +00:00
|
|
|
// Check if the group exists
|
2018-04-06 13:07:04 +00:00
|
|
|
if ($group === null) {
|
2016-08-11 17:49:45 +00:00
|
|
|
throw new OCSException('Group does not exist', 101);
|
2015-10-27 13:09:45 +00:00
|
|
|
}
|
2014-12-22 13:54:50 +00:00
|
|
|
// Check if they are a subadmin of this said group
|
2018-04-06 13:07:04 +00:00
|
|
|
if (!$subAdminManager->isSubAdminOfGroup($user, $group)) {
|
2016-08-11 17:49:45 +00:00
|
|
|
throw new OCSException('User is not a subadmin of this group', 102);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
2015-10-27 13:09:45 +00:00
|
|
|
|
2014-12-22 13:54:50 +00:00
|
|
|
// Go
|
2019-01-29 09:38:05 +00:00
|
|
|
$subAdminManager->deleteSubAdmin($user, $group);
|
|
|
|
return new DataResponse();
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
|
|
|
|
2018-03-16 08:18:53 +00: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 14:37:05 +00:00
|
|
|
return new DataResponse($groups);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
2015-05-19 10:38:03 +00:00
|
|
|
|
2017-02-24 06:01:13 +00:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @PasswordConfirmationRequired
|
|
|
|
*
|
|
|
|
* resend welcome message
|
|
|
|
*
|
|
|
|
* @param string $userId
|
|
|
|
* @return DataResponse
|
|
|
|
* @throws OCSException
|
|
|
|
*/
|
2018-02-27 14:47:59 +00:00
|
|
|
public function resendWelcomeMessage(string $userId): DataResponse {
|
2017-02-24 06:01:13 +00:00
|
|
|
$currentLoggedInUser = $this->userSession->getUser();
|
|
|
|
|
|
|
|
$targetUser = $this->userManager->get($userId);
|
2018-04-06 13:07:04 +00:00
|
|
|
if ($targetUser === null) {
|
2017-04-21 16:30:00 +00:00
|
|
|
throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND);
|
2017-02-24 06:01:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if admin / subadmin
|
|
|
|
$subAdminManager = $this->groupManager->getSubAdmin();
|
2018-04-06 13:07:04 +00:00
|
|
|
if (!$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)
|
2017-02-24 06:01:13 +00:00
|
|
|
&& !$this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
|
|
|
|
// No rights
|
|
|
|
throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
|
|
|
|
}
|
|
|
|
|
|
|
|
$email = $targetUser->getEMailAddress();
|
|
|
|
if ($email === '' || $email === null) {
|
|
|
|
throw new OCSException('Email address not available', 101);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2017-04-07 12:51:05 +00:00
|
|
|
$emailTemplate = $this->newUserMailHelper->generateTemplate($targetUser, false);
|
|
|
|
$this->newUserMailHelper->sendMail($targetUser, $emailTemplate);
|
2017-02-24 06:01:13 +00:00
|
|
|
} catch(\Exception $e) {
|
2018-01-17 14:21:56 +00:00
|
|
|
$this->logger->logException($e, [
|
|
|
|
'message' => "Can't send new user mail to $email",
|
2018-04-25 13:22:28 +00:00
|
|
|
'level' => ILogger::ERROR,
|
2018-01-17 14:21:56 +00:00
|
|
|
'app' => 'settings',
|
|
|
|
]);
|
2017-02-24 06:01:13 +00:00
|
|
|
throw new OCSException('Sending email failed', 102);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new DataResponse();
|
|
|
|
}
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|