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>
|
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;
|
2018-02-22 16:16:49 +03:00
|
|
|
use OC\HintException;
|
2017-04-07 15:51:05 +03:00
|
|
|
use OC\Settings\Mailer\NewUserMailHelper;
|
2017-04-24 11:18:14 +03:00
|
|
|
use OC_Helper;
|
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;
|
|
|
|
use OCP\AppFramework\OCSController;
|
2015-05-19 13:38:03 +03:00
|
|
|
use OCP\Files\NotFoundException;
|
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;
|
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;
|
2014-12-22 16:54:50 +03:00
|
|
|
|
2016-08-11 20:49:45 +03:00
|
|
|
class UsersController extends OCSController {
|
2014-12-22 16:54:50 +03:00
|
|
|
|
2016-04-07 18:22:21 +03:00
|
|
|
/** @var IUserManager */
|
2015-07-25 13:46:39 +03:00
|
|
|
private $userManager;
|
2016-04-07 18:22:21 +03:00
|
|
|
/** @var IConfig */
|
2015-07-25 13:46:39 +03:00
|
|
|
private $config;
|
2017-06-02 11:09:42 +03:00
|
|
|
/** @var IAppManager */
|
|
|
|
private $appManager;
|
2016-05-11 20:38:49 +03:00
|
|
|
/** @var IGroupManager|\OC\Group\Manager */ // FIXME Requires a method that is not on the interface
|
2015-07-25 13:46:39 +03:00
|
|
|
private $groupManager;
|
2016-04-07 18:22:21 +03:00
|
|
|
/** @var IUserSession */
|
2015-07-25 16:01:31 +03:00
|
|
|
private $userSession;
|
2017-01-24 16:07:52 +03:00
|
|
|
/** @var AccountManager */
|
|
|
|
private $accountManager;
|
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;
|
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
|
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,
|
|
|
|
FederatedFileSharingFactory $federatedFileSharingFactory) {
|
2016-08-11 20:49:45 +03:00
|
|
|
parent::__construct($appName, $request);
|
|
|
|
|
2015-07-25 13:46:39 +03:00
|
|
|
$this->userManager = $userManager;
|
|
|
|
$this->config = $config;
|
2017-06-02 11:09:42 +03:00
|
|
|
$this->appManager = $appManager;
|
2015-07-25 13:46:39 +03:00
|
|
|
$this->groupManager = $groupManager;
|
2015-07-25 16:01:31 +03:00
|
|
|
$this->userSession = $userSession;
|
2017-01-24 16:07:52 +03:00
|
|
|
$this->accountManager = $accountManager;
|
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;
|
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();
|
|
|
|
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-03-16 11:18:53 +03:00
|
|
|
$user = $this->userSession->getUser();
|
|
|
|
$users = [];
|
|
|
|
|
|
|
|
// Admin? Or SubAdmin?
|
|
|
|
$uid = $user->getUID();
|
|
|
|
$subAdminManager = $this->groupManager->getSubAdmin();
|
|
|
|
if($this->groupManager->isAdmin($uid)){
|
|
|
|
$users = $this->userManager->search($search, $limit, $offset);
|
|
|
|
} 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);
|
2018-03-16 11:18:53 +03:00
|
|
|
$usersDetails = [];
|
|
|
|
foreach ($users as $key => $userId) {
|
2018-03-16 13:44:20 +03:00
|
|
|
$userData = $this->getUserData($userId);
|
|
|
|
// Do not insert empty entry
|
|
|
|
if(!empty($userData)) {
|
|
|
|
$usersDetails[$userId] = $userData;
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
* @param array $groups
|
|
|
|
* @return DataResponse
|
|
|
|
* @throws OCSException
|
2015-09-08 15:02:30 +03:00
|
|
|
*/
|
2018-02-28 00:03:40 +03:00
|
|
|
public function addUser(string $userid, string $password, array $groups = []): 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
|
|
|
|
2016-08-11 20:49:45 +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-02-28 00:03:40 +03:00
|
|
|
if($groups !== []) {
|
2015-10-30 11:30:00 +03:00
|
|
|
foreach ($groups as $group) {
|
2016-08-11 20:49:45 +03:00
|
|
|
if(!$this->groupManager->groupExists($group)) {
|
|
|
|
throw new OCSException('group '.$group.' does not exist', 104);
|
2015-08-27 19:29:28 +03:00
|
|
|
}
|
2015-10-30 11:30:00 +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 {
|
2015-08-27 19:29:28 +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
|
|
|
|
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-02-28 00:03:40 +03:00
|
|
|
|
2016-08-11 20:49:45 +03:00
|
|
|
return new DataResponse();
|
2018-02-22 16:16:49 +03:00
|
|
|
} catch (HintException $e ) {
|
|
|
|
$this->logger->logException($e, [
|
|
|
|
'message' => 'Failed addUser attempt with hint exception.',
|
|
|
|
'level' => \OCP\Util::WARN,
|
|
|
|
'app' => 'ocs_api',
|
|
|
|
]);
|
|
|
|
throw new OCSException($e->getHint(), 107);
|
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.',
|
|
|
|
'level' => \OCP\Util::ERROR,
|
|
|
|
'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
|
|
|
|
if(empty($data)) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* creates a array with all user data
|
|
|
|
*
|
|
|
|
* @param $userId
|
|
|
|
* @return array
|
|
|
|
* @throws OCSException
|
|
|
|
*/
|
2018-02-27 17:47:59 +03:00
|
|
|
protected function getUserData(string $userId): array {
|
2015-10-27 16:09:45 +03:00
|
|
|
$currentLoggedInUser = $this->userSession->getUser();
|
2015-08-11 16:37:06 +03:00
|
|
|
|
2015-10-08 22:47:30 +03:00
|
|
|
$data = [];
|
|
|
|
|
2015-10-27 16:09:45 +03:00
|
|
|
// Check if the target user exists
|
|
|
|
$targetUserObject = $this->userManager->get($userId);
|
|
|
|
if($targetUserObject === null) {
|
2016-08-11 20:49:45 +03:00
|
|
|
throw new OCSException('The requested user could not be found', \OCP\API::RESPOND_NOT_FOUND);
|
2015-10-27 16:09:45 +03:00
|
|
|
}
|
|
|
|
|
2018-03-16 13:44:20 +03:00
|
|
|
// Should be at least Admin Or SubAdmin!
|
|
|
|
if( $this->groupManager->isAdmin($currentLoggedInUser->getUID())
|
2015-10-27 16:09:45 +03:00
|
|
|
|| $this->groupManager->getSubAdmin()->isUserAccessible($currentLoggedInUser, $targetUserObject)) {
|
2018-03-16 13:44:20 +03:00
|
|
|
$data['enabled'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'enabled', 'true');
|
2014-12-22 16:54:50 +03:00
|
|
|
} else {
|
|
|
|
// Check they are looking up themselves
|
2017-04-24 10:43:44 +03:00
|
|
|
if($currentLoggedInUser->getUID() !== $targetUserObject->getUID()) {
|
2018-03-16 11:38:09 +03:00
|
|
|
return $data;
|
2014-12-22 16:54:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-16 11:18:53 +03:00
|
|
|
// Get groups data
|
2017-01-24 16:07:52 +03:00
|
|
|
$userAccount = $this->accountManager->getUser($targetUserObject);
|
2017-02-09 18:12:57 +03:00
|
|
|
$groups = $this->groupManager->getUserGroups($targetUserObject);
|
|
|
|
$gids = [];
|
|
|
|
foreach ($groups as $group) {
|
|
|
|
$gids[] = $group->getDisplayName();
|
|
|
|
}
|
2017-01-24 16:07:52 +03:00
|
|
|
|
2014-12-22 16:54:50 +03:00
|
|
|
// Find the data
|
2017-01-24 17:45:55 +03:00
|
|
|
$data['id'] = $targetUserObject->getUID();
|
2018-03-16 11:38:09 +03:00
|
|
|
$data['storageLocation'] = $targetUserObject->getHome();
|
|
|
|
$data['lastLogin'] = $targetUserObject->getLastLogin() * 1000;
|
|
|
|
$data['backend'] = $targetUserObject->getBackendClassName();
|
2018-03-16 17:37:05 +03:00
|
|
|
$data['subadmin'] = $this->getUserSubAdminGroupsData($targetUserObject->getUID());
|
2017-04-24 10:43:44 +03:00
|
|
|
$data['quota'] = $this->fillStorageInfo($targetUserObject->getUID());
|
2017-04-05 15:51:55 +03:00
|
|
|
$data[AccountManager::PROPERTY_EMAIL] = $targetUserObject->getEMailAddress();
|
|
|
|
$data[AccountManager::PROPERTY_DISPLAYNAME] = $targetUserObject->getDisplayName();
|
|
|
|
$data[AccountManager::PROPERTY_PHONE] = $userAccount[AccountManager::PROPERTY_PHONE]['value'];
|
|
|
|
$data[AccountManager::PROPERTY_ADDRESS] = $userAccount[AccountManager::PROPERTY_ADDRESS]['value'];
|
|
|
|
$data[AccountManager::PROPERTY_WEBSITE] = $userAccount[AccountManager::PROPERTY_WEBSITE]['value'];
|
|
|
|
$data[AccountManager::PROPERTY_TWITTER] = $userAccount[AccountManager::PROPERTY_TWITTER]['value'];
|
2017-02-09 18:12:57 +03:00
|
|
|
$data['groups'] = $gids;
|
2017-06-21 12:22:05 +03:00
|
|
|
$data['language'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'lang');
|
2014-12-22 16:54:50 +03:00
|
|
|
|
2017-01-26 13:31:08 +03:00
|
|
|
return $data;
|
2017-01-24 17:45:55 +03:00
|
|
|
}
|
|
|
|
|
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);
|
2015-10-27 16:09:45 +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 = [];
|
2017-04-24 10:43:44 +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-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
|
2015-10-27 16:09:45 +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();
|
|
|
|
if($subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)
|
|
|
|
|| $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-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
|
2016-08-11 20:49:45 +03:00
|
|
|
if(!in_array($key, $permittedFields)) {
|
|
|
|
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;
|
2017-01-18 13:43:52 +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
|
|
|
}
|
2015-07-25 15:49:20 +03:00
|
|
|
if($quota === 0) {
|
2014-12-22 16:54:50 +03:00
|
|
|
$quota = 'default';
|
2015-10-27 16:09:45 +03:00
|
|
|
}else 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':
|
2016-08-11 20:49:45 +03:00
|
|
|
$targetUser->setPassword($value);
|
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-04-05 15:51:55 +03:00
|
|
|
case AccountManager::PROPERTY_EMAIL:
|
2018-03-17 14:18:22 +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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
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();
|
|
|
|
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
|
2015-10-27 16:09:45 +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);
|
2016-04-07 18:22:21 +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();
|
|
|
|
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);
|
2015-10-27 16:09:45 +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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2015-10-27 16:09:45 +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 {
|
2016-08-11 20:49:45 +03:00
|
|
|
if($groupid === '') {
|
|
|
|
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);
|
2015-10-27 16:09:45 +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
|
|
|
}
|
2015-10-27 16:09:45 +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
|
|
|
|
2017-09-18 11:56:13 +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);
|
2015-11-05 19:01:47 +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);
|
2015-10-27 16:09:45 +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.
|
|
|
|
throw new OCSException('Cannot remove user from this group as this is the only remaining group you are a SubAdmin of', 105);
|
|
|
|
}
|
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
|
2015-10-27 16:09:45 +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
|
2015-10-27 16:09:45 +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
|
2017-04-24 11:15:03 +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
|
2015-10-27 16:09:45 +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
|
2015-10-27 16:09:45 +03:00
|
|
|
if($subAdminManager->createSubAdmin($user, $group)) {
|
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('Unknown error occurred', 103);
|
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
|
2015-10-27 16:09:45 +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
|
|
|
|
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
|
2017-04-24 11:15:03 +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
|
2015-10-27 16:09:45 +03:00
|
|
|
if($subAdminManager->deleteSubAdmin($user, $group)) {
|
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('Unknown error occurred', 103);
|
2014-12-22 16:54:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-09-08 15:02:30 +03:00
|
|
|
* Get the groups a user is a subadmin of
|
|
|
|
*
|
2016-08-11 20:49:45 +03:00
|
|
|
* @param string $userId
|
2018-03-16 11:18:53 +03:00
|
|
|
* @return array
|
2016-08-11 20:49:45 +03:00
|
|
|
* @throws OCSException
|
2014-12-22 16:54:50 +03:00
|
|
|
*/
|
2018-03-16 11:18:53 +03:00
|
|
|
protected function getUserSubAdminGroupsData(string $userId): array {
|
2016-08-11 20:49:45 +03:00
|
|
|
$user = $this->userManager->get($userId);
|
2014-12-22 16:54:50 +03:00
|
|
|
// Check if the user exists
|
2015-10-27 16:09:45 +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
|
|
|
|
2014-12-22 16:54:50 +03:00
|
|
|
// Get the subadmin groups
|
2018-03-08 19:09:29 +03:00
|
|
|
$subAdminGroups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($user);
|
|
|
|
$groups = [];
|
|
|
|
foreach ($subAdminGroups as $key => $group) {
|
|
|
|
$groups[] = $group->getGID();
|
2015-10-27 16:09:45 +03:00
|
|
|
}
|
|
|
|
|
2018-03-16 11:18:53 +03:00
|
|
|
return $groups;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
|
|
|
/**
|
2015-09-08 15:02:30 +03:00
|
|
|
* @param string $userId
|
2015-10-27 16:09:45 +03:00
|
|
|
* @return array
|
2015-05-19 13:38:03 +03:00
|
|
|
* @throws \OCP\Files\NotFoundException
|
|
|
|
*/
|
2018-02-27 17:47:59 +03:00
|
|
|
protected function fillStorageInfo(string $userId): array {
|
2015-05-19 13:38:03 +03:00
|
|
|
try {
|
|
|
|
\OC_Util::tearDownFS();
|
|
|
|
\OC_Util::setupFS($userId);
|
|
|
|
$storage = OC_Helper::getStorageInfo('/');
|
2015-10-08 22:47:30 +03:00
|
|
|
$data = [
|
2015-05-19 13:38:03 +03:00
|
|
|
'free' => $storage['free'],
|
|
|
|
'used' => $storage['used'],
|
|
|
|
'total' => $storage['total'],
|
|
|
|
'relative' => $storage['relative'],
|
2016-08-16 12:21:10 +03:00
|
|
|
'quota' => $storage['quota'],
|
2015-05-19 13:38:03 +03:00
|
|
|
];
|
|
|
|
} catch (NotFoundException $ex) {
|
2015-10-08 22:47:30 +03:00
|
|
|
$data = [];
|
2015-05-19 13:38:03 +03:00
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
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);
|
|
|
|
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();
|
|
|
|
if(!$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)
|
|
|
|
&& !$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);
|
|
|
|
}
|
|
|
|
$username = $targetUser->getUID();
|
|
|
|
$lang = $this->config->getUserValue($username, 'core', 'lang', 'en');
|
|
|
|
if (!$this->l10nFactory->languageExists('settings', $lang)) {
|
|
|
|
$lang = 'en';
|
|
|
|
}
|
|
|
|
|
|
|
|
$l10n = $this->l10nFactory->get('settings', $lang);
|
|
|
|
|
|
|
|
try {
|
2017-04-07 15:51:05 +03:00
|
|
|
$this->newUserMailHelper->setL10N($l10n);
|
|
|
|
$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",
|
|
|
|
'level' => \OCP\Util::ERROR,
|
|
|
|
'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
|
|
|
}
|