2014-12-22 16:54:50 +03:00
|
|
|
<?php
|
2018-02-27 17:47:59 +03:00
|
|
|
declare(strict_types=1);
|
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>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
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>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
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>
|
2018-03-26 20:30:40 +03:00
|
|
|
* @author John Molakvoæ <skjnldsv@protonmail.com>
|
2017-07-05 18:36:43 +03:00
|
|
|
* @author Thomas Citharel <tcit@tcit.fr>
|
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,
|
|
|
|
* 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
|
|
|
|
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;
|
2017-04-07 15:51:05 +03:00
|
|
|
use OC\Settings\Mailer\NewUserMailHelper;
|
2018-01-26 14:47:59 +03:00
|
|
|
use OCA\Provisioning_API\FederatedFileSharingFactory;
|
2017-06-02 11:09:42 +03:00
|
|
|
use OCP\App\IAppManager;
|
2016-08-11 20:49:45 +03:00
|
|
|
use OCP\AppFramework\Http\DataResponse;
|
|
|
|
use OCP\AppFramework\OCS\OCSException;
|
|
|
|
use OCP\AppFramework\OCS\OCSForbiddenException;
|
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;
|
2015-10-27 16:09:45 +03:00
|
|
|
use OCP\ILogger;
|
2016-08-11 20:49:45 +03:00
|
|
|
use OCP\IRequest;
|
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;
|
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;
|
2015-10-27 16:09:45 +03:00
|
|
|
/** @var ILogger */
|
|
|
|
private $logger;
|
2017-02-24 09:01:13 +03:00
|
|
|
/** @var IFactory */
|
|
|
|
private $l10nFactory;
|
2017-04-07 15:51:05 +03:00
|
|
|
/** @var NewUserMailHelper */
|
|
|
|
private $newUserMailHelper;
|
2018-01-26 14:47:59 +03:00
|
|
|
/** @var FederatedFileSharingFactory */
|
|
|
|
private $federatedFileSharingFactory;
|
2018-03-16 17:30:48 +03:00
|
|
|
/** @var ISecureRandom */
|
|
|
|
private $secureRandom;
|
2019-07-03 11:10:56 +03:00
|
|
|
/** @var RemoteWipe */
|
|
|
|
private $remoteWipe;
|
2015-07-25 16:01:31 +03:00
|
|
|
|
2015-07-25 13:46:39 +03:00
|
|
|
/**
|
2016-08-11 20:49:45 +03:00
|
|
|
* @param string $appName
|
|
|
|
* @param IRequest $request
|
2016-04-07 18:22:21 +03:00
|
|
|
* @param IUserManager $userManager
|
|
|
|
* @param IConfig $config
|
2017-06-02 11:09:42 +03:00
|
|
|
* @param IAppManager $appManager
|
2016-04-07 18:22:21 +03:00
|
|
|
* @param IGroupManager $groupManager
|
|
|
|
* @param IUserSession $userSession
|
2017-01-24 16:07:52 +03:00
|
|
|
* @param AccountManager $accountManager
|
2015-10-27 16:09:45 +03:00
|
|
|
* @param ILogger $logger
|
2017-02-24 09:01:13 +03:00
|
|
|
* @param IFactory $l10nFactory
|
2017-04-07 15:51:05 +03:00
|
|
|
* @param NewUserMailHelper $newUserMailHelper
|
2018-01-26 14:47:59 +03:00
|
|
|
* @param FederatedFileSharingFactory $federatedFileSharingFactory
|
2018-03-16 17:30:48 +03:00
|
|
|
* @param ISecureRandom $secureRandom
|
2015-07-25 13:46:39 +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,
|
2017-02-24 09:01:13 +03:00
|
|
|
ILogger $logger,
|
2017-04-07 15:51:05 +03:00
|
|
|
IFactory $l10nFactory,
|
2018-01-26 14:47:59 +03:00
|
|
|
NewUserMailHelper $newUserMailHelper,
|
2018-03-16 17:30:48 +03:00
|
|
|
FederatedFileSharingFactory $federatedFileSharingFactory,
|
2019-07-03 11:10:56 +03:00
|
|
|
ISecureRandom $secureRandom,
|
|
|
|
RemoteWipe $remoteWipe) {
|
2018-03-27 12:05:36 +03:00
|
|
|
parent::__construct($appName,
|
|
|
|
$request,
|
|
|
|
$userManager,
|
|
|
|
$config,
|
|
|
|
$groupManager,
|
|
|
|
$userSession,
|
|
|
|
$accountManager);
|
2016-08-11 20:49:45 +03:00
|
|
|
|
2017-06-02 11:09:42 +03:00
|
|
|
$this->appManager = $appManager;
|
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-01-26 14:47:59 +03:00
|
|
|
$this->federatedFileSharingFactory = $federatedFileSharingFactory;
|
2018-03-16 17:30:48 +03:00
|
|
|
$this->secureRandom = $secureRandom;
|
2019-07-03 11:10:56 +03:00
|
|
|
$this->remoteWipe = $remoteWipe;
|
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
|
|
|
*/
|
2018-03-16 13:44:20 +03:00
|
|
|
public function getUsers(string $search = '', $limit = null, $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();
|
2018-04-06 16:07:04 +03:00
|
|
|
if ($this->groupManager->isAdmin($uid)){
|
2015-08-18 18:43:16 +03:00
|
|
|
$users = $this->userManager->search($search, $limit, $offset);
|
2015-10-27 16:09:45 +03:00
|
|
|
} else if ($subAdminManager->isSubAdmin($user)) {
|
|
|
|
$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
|
|
|
|
*/
|
2018-03-16 13:44:20 +03:00
|
|
|
public function getUsersDetails(string $search = '', $limit = null, $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();
|
2018-04-06 16:07:04 +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);
|
|
|
|
} else if ($subAdminManager->isSubAdmin($currentUser)) {
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-06-21 11:18:44 +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);
|
|
|
|
}
|
|
|
|
|
|
|
|
$password = $this->secureRandom->generate(10);
|
|
|
|
// Make sure we pass the password_policy
|
|
|
|
$password .= $this->secureRandom->generate(2, '$!.,;:-~+*[]{}()');
|
|
|
|
$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
|
|
|
|
if ($email !== '') {
|
|
|
|
$newUser->setEMailAddress($email);
|
|
|
|
try {
|
|
|
|
$emailTemplate = $this->newUserMailHelper->generateTemplate($newUser, $generatePasswordResetToken);
|
|
|
|
$this->newUserMailHelper->sendMail($newUser, $emailTemplate);
|
|
|
|
} catch (\Exception $e) {
|
2019-07-02 09:04:56 +03:00
|
|
|
// Mail could be failing hard or just be plain not configured
|
|
|
|
// Logging error as it is the hardest of the two
|
2018-03-16 17:30:48 +03:00
|
|
|
$this->logger->logException($e, [
|
2019-07-02 09:04:56 +03:00
|
|
|
'message' => "Unable to send the invitation mail to $email",
|
2018-04-25 16:22:28 +03:00
|
|
|
'level' => ILogger::ERROR,
|
2018-03-16 17:30:48 +03:00
|
|
|
'app' => 'ocs_api',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-21 17:38:25 +03:00
|
|
|
return new DataResponse(['id' => $userid]);
|
2018-03-16 17:30:48 +03:00
|
|
|
|
2019-06-20 19:04:52 +03:00
|
|
|
} catch (HintException $e) {
|
2018-02-22 16:16:49 +03:00
|
|
|
$this->logger->logException($e, [
|
|
|
|
'message' => 'Failed addUser attempt with hint exception.',
|
2018-04-25 16:22:28 +03:00
|
|
|
'level' => ILogger::WARN,
|
2018-02-22 16:16:49 +03:00
|
|
|
'app' => 'ocs_api',
|
|
|
|
]);
|
|
|
|
throw new OCSException($e->getHint(), 107);
|
2019-06-20 19:04:52 +03: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 19:29:28 +03:00
|
|
|
} catch (\Exception $e) {
|
2018-01-17 17:21:56 +03:00
|
|
|
$this->logger->logException($e, [
|
|
|
|
'message' => 'Failed addUser attempt with exception.',
|
2018-04-25 16:22:28 +03:00
|
|
|
'level' => ILogger::ERROR,
|
2018-01-17 17:21:56 +03:00
|
|
|
'app' => 'ocs_api',
|
|
|
|
]);
|
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 {
|
2017-01-26 13:31:08 +03:00
|
|
|
$data = $this->getUserData($userId);
|
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)) {
|
2018-03-16 13:44:20 +03:00
|
|
|
throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
|
|
|
|
}
|
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) {
|
|
|
|
$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 23:47:25 +03:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoSubAdminRequired
|
|
|
|
*/
|
2018-02-27 17:47:59 +03:00
|
|
|
public function getEditableFields(): DataResponse {
|
2018-01-22 23:47:25 +03: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 14:47:59 +03:00
|
|
|
$federatedFileSharing = $this->federatedFileSharingFactory->get();
|
2018-01-22 23:47:25 +03: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 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) {
|
2016-08-11 20:49:45 +03:00
|
|
|
throw new OCSException('', \OCP\API::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) {
|
|
|
|
$permittedFields[] = 'display';
|
|
|
|
$permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME;
|
|
|
|
$permittedFields[] = AccountManager::PROPERTY_EMAIL;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
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 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();
|
2018-04-06 16:07:04 +03:00
|
|
|
if ($subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)
|
2015-10-27 16:09:45 +03:00
|
|
|
|| $this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
|
2014-12-22 16:54:50 +03:00
|
|
|
// They have permissions over the user
|
|
|
|
$permittedFields[] = 'display';
|
2017-04-05 15:51:55 +03:00
|
|
|
$permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME;
|
|
|
|
$permittedFields[] = AccountManager::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';
|
2017-04-05 15:51:55 +03:00
|
|
|
$permittedFields[] = AccountManager::PROPERTY_PHONE;
|
|
|
|
$permittedFields[] = AccountManager::PROPERTY_ADDRESS;
|
|
|
|
$permittedFields[] = AccountManager::PROPERTY_WEBSITE;
|
|
|
|
$permittedFields[] = AccountManager::PROPERTY_TWITTER;
|
|
|
|
$permittedFields[] = 'quota';
|
2014-12-22 16:54:50 +03:00
|
|
|
} else {
|
|
|
|
// No rights
|
2016-08-11 20:49:45 +03:00
|
|
|
throw new OCSException('', \OCP\API::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)) {
|
2016-08-11 20:49:45 +03:00
|
|
|
throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
|
2014-12-22 16:54:50 +03:00
|
|
|
}
|
|
|
|
// Process the edit
|
2016-08-11 20:49:45 +03:00
|
|
|
switch($key) {
|
2014-12-22 16:54:50 +03:00
|
|
|
case 'display':
|
2017-04-05 15:51:55 +03:00
|
|
|
case AccountManager::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;
|
2017-04-05 15:51:55 +03:00
|
|
|
case AccountManager::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;
|
2017-04-05 15:51:55 +03: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 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)) {
|
|
|
|
throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
|
|
|
|
}
|
|
|
|
|
|
|
|
$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)) {
|
2016-08-11 20:49:45 +03:00
|
|
|
throw new OCSException('', \OCP\API::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)) {
|
2016-08-11 20:49:45 +03:00
|
|
|
throw new OCSException('', \OCP\API::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) {
|
2016-08-11 20:49:45 +03:00
|
|
|
throw new OCSException('', \OCP\API::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
|
2016-08-11 20:49:45 +03:00
|
|
|
throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
|
2014-12-22 16:54:50 +03:00
|
|
|
}
|
|
|
|
}
|
2017-01-24 16:07:52 +03:00
|
|
|
|
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
|
|
|
}
|
2017-01-18 13:56:24 +03: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 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) {
|
2017-04-21 19:30:00 +03:00
|
|
|
throw new OCSException('', \OCP\API::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
|
|
|
|
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 15:51:05 +03:00
|
|
|
$emailTemplate = $this->newUserMailHelper->generateTemplate($targetUser, false);
|
|
|
|
$this->newUserMailHelper->sendMail($targetUser, $emailTemplate);
|
2017-02-24 09:01:13 +03:00
|
|
|
} catch(\Exception $e) {
|
2018-01-17 17:21:56 +03:00
|
|
|
$this->logger->logException($e, [
|
|
|
|
'message' => "Can't send new user mail to $email",
|
2018-04-25 16:22:28 +03:00
|
|
|
'level' => ILogger::ERROR,
|
2018-01-17 17:21:56 +03:00
|
|
|
'app' => 'settings',
|
|
|
|
]);
|
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
|
|
|
}
|