2014-12-22 13:54:50 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2015-06-25 09:43:55 +00:00
|
|
|
* @author Joas Schilling <nickvergessen@owncloud.com>
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-10-26 12:54:55 +00:00
|
|
|
* @author michag86 <micha_g@arcor.de>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2015-10-26 12:54:55 +00:00
|
|
|
* @author Roeland Jago Douma <rullzer@owncloud.com>
|
2015-06-25 09:43:55 +00:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Tom Needham <tom@owncloud.com>
|
2014-12-22 13:54:50 +00:00
|
|
|
*
|
2016-01-12 14:02:16 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
2015-03-26 10:44:34 +00:00
|
|
|
* @license AGPL-3.0
|
2014-12-22 13:54:50 +00:00
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
2014-12-22 13:54:50 +00:00
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2014-12-22 13:54:50 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-03-26 10:44:34 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
2014-12-22 13:54:50 +00:00
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2014-12-22 13:54:50 +00:00
|
|
|
*
|
|
|
|
*/
|
2015-02-26 10:37:37 +00:00
|
|
|
|
2014-12-22 13:54:50 +00:00
|
|
|
namespace OCA\Provisioning_API;
|
|
|
|
|
|
|
|
use \OC_Helper;
|
2015-05-19 10:38:03 +00:00
|
|
|
use OCP\Files\NotFoundException;
|
2016-04-07 15:22:21 +00:00
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\IGroupManager;
|
2015-10-27 13:09:45 +00:00
|
|
|
use OCP\ILogger;
|
2016-04-07 15:22:21 +00:00
|
|
|
use OCP\IUserManager;
|
|
|
|
use OCP\IUserSession;
|
2014-12-22 13:54:50 +00:00
|
|
|
|
|
|
|
class Users {
|
|
|
|
|
2016-04-07 15:22:21 +00:00
|
|
|
/** @var IUserManager */
|
2015-07-25 10:46:39 +00:00
|
|
|
private $userManager;
|
2016-04-07 15:22:21 +00:00
|
|
|
/** @var IConfig */
|
2015-07-25 10:46:39 +00:00
|
|
|
private $config;
|
2016-05-11 17:38:49 +00:00
|
|
|
/** @var IGroupManager|\OC\Group\Manager */ // FIXME Requires a method that is not on the interface
|
2015-07-25 10:46:39 +00:00
|
|
|
private $groupManager;
|
2016-04-07 15:22:21 +00:00
|
|
|
/** @var IUserSession */
|
2015-07-25 13:01:31 +00:00
|
|
|
private $userSession;
|
2015-10-27 13:09:45 +00:00
|
|
|
/** @var ILogger */
|
|
|
|
private $logger;
|
2015-07-25 13:01:31 +00:00
|
|
|
|
2015-07-25 10:46:39 +00:00
|
|
|
/**
|
2016-04-07 15:22:21 +00:00
|
|
|
* @param IUserManager $userManager
|
|
|
|
* @param IConfig $config
|
|
|
|
* @param IGroupManager $groupManager
|
|
|
|
* @param IUserSession $userSession
|
2015-10-27 13:09:45 +00:00
|
|
|
* @param ILogger $logger
|
2015-07-25 10:46:39 +00:00
|
|
|
*/
|
2016-04-07 15:22:21 +00:00
|
|
|
public function __construct(IUserManager $userManager,
|
|
|
|
IConfig $config,
|
|
|
|
IGroupManager $groupManager,
|
|
|
|
IUserSession $userSession,
|
2015-10-27 13:09:45 +00:00
|
|
|
ILogger $logger) {
|
2015-07-25 10:46:39 +00:00
|
|
|
$this->userManager = $userManager;
|
|
|
|
$this->config = $config;
|
|
|
|
$this->groupManager = $groupManager;
|
2015-07-25 13:01:31 +00:00
|
|
|
$this->userSession = $userSession;
|
2015-10-27 13:09:45 +00:00
|
|
|
$this->logger = $logger;
|
2015-07-25 10:46:39 +00:00
|
|
|
}
|
|
|
|
|
2014-12-22 13:54:50 +00:00
|
|
|
/**
|
|
|
|
* returns a list of users
|
2015-09-08 12:02:30 +00:00
|
|
|
*
|
2016-07-12 06:38:56 +00:00
|
|
|
* @return \OC\OCS\Result
|
2014-12-22 13:54:50 +00:00
|
|
|
*/
|
2015-09-08 12:02:30 +00:00
|
|
|
public function getUsers() {
|
2014-12-22 13:54:50 +00:00
|
|
|
$search = !empty($_GET['search']) ? $_GET['search'] : '';
|
|
|
|
$limit = !empty($_GET['limit']) ? $_GET['limit'] : null;
|
|
|
|
$offset = !empty($_GET['offset']) ? $_GET['offset'] : null;
|
2015-07-25 10:46:39 +00:00
|
|
|
|
2015-08-18 15:43:16 +00:00
|
|
|
// Check if user is logged in
|
|
|
|
$user = $this->userSession->getUser();
|
|
|
|
if ($user === null) {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, \OCP\API::RESPOND_UNAUTHORISED);
|
2015-08-18 15:43:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Admin? Or SubAdmin?
|
2015-10-27 13:09:45 +00:00
|
|
|
$uid = $user->getUID();
|
|
|
|
$subAdminManager = $this->groupManager->getSubAdmin();
|
|
|
|
if($this->groupManager->isAdmin($uid)){
|
2015-08-18 15:43:16 +00:00
|
|
|
$users = $this->userManager->search($search, $limit, $offset);
|
2015-10-27 13:09:45 +00:00
|
|
|
} else if ($subAdminManager->isSubAdmin($user)) {
|
|
|
|
$subAdminOfGroups = $subAdminManager->getSubAdminsGroups($user);
|
|
|
|
foreach ($subAdminOfGroups as $key => $group) {
|
|
|
|
$subAdminOfGroups[$key] = $group->getGID();
|
|
|
|
}
|
2015-08-18 15:43:16 +00:00
|
|
|
|
|
|
|
if($offset === null) {
|
|
|
|
$offset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
$users = [];
|
|
|
|
foreach ($subAdminOfGroups as $group) {
|
|
|
|
$users = array_merge($users, $this->groupManager->displayNamesInGroup($group, $search));
|
|
|
|
}
|
|
|
|
|
|
|
|
$users = array_slice($users, $offset, $limit);
|
|
|
|
} else {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, \OCP\API::RESPOND_UNAUTHORISED);
|
2015-08-18 15:43:16 +00:00
|
|
|
}
|
2015-07-25 10:46:39 +00:00
|
|
|
$users = array_keys($users);
|
|
|
|
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result([
|
2015-07-25 10:46:39 +00:00
|
|
|
'users' => $users
|
|
|
|
]);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
|
|
|
|
2015-09-08 12:02:30 +00:00
|
|
|
/**
|
2016-07-12 06:38:56 +00:00
|
|
|
* @return \OC\OCS\Result
|
2015-09-08 12:02:30 +00:00
|
|
|
*/
|
|
|
|
public function addUser() {
|
2014-12-22 13:54:50 +00:00
|
|
|
$userId = isset($_POST['userid']) ? $_POST['userid'] : null;
|
|
|
|
$password = isset($_POST['password']) ? $_POST['password'] : null;
|
2015-08-27 16:29:28 +00:00
|
|
|
$groups = isset($_POST['groups']) ? $_POST['groups'] : null;
|
|
|
|
$user = $this->userSession->getUser();
|
|
|
|
$isAdmin = $this->groupManager->isAdmin($user->getUID());
|
2015-10-30 08:30:00 +00:00
|
|
|
$subAdminManager = $this->groupManager->getSubAdmin();
|
2015-08-27 16:29:28 +00:00
|
|
|
|
2015-10-30 08:30:00 +00:00
|
|
|
if (!$isAdmin && !$subAdminManager->isSubAdmin($user)) {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, \OCP\API::RESPOND_UNAUTHORISED);
|
2015-08-27 16:29:28 +00:00
|
|
|
}
|
|
|
|
|
2015-07-25 10:46:39 +00:00
|
|
|
if($this->userManager->userExists($userId)) {
|
2015-10-27 13:09:45 +00:00
|
|
|
$this->logger->error('Failed addUser attempt: User already exists.', ['app' => 'ocs_api']);
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 102, 'User already exists');
|
2015-08-27 16:29:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(is_array($groups)) {
|
2015-10-30 08:30:00 +00:00
|
|
|
foreach ($groups as $group) {
|
2015-08-27 16:29:28 +00:00
|
|
|
if(!$this->groupManager->groupExists($group)){
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 104, 'group '.$group.' does not exist');
|
2015-08-27 16:29:28 +00:00
|
|
|
}
|
2015-10-30 08:30:00 +00:00
|
|
|
if(!$isAdmin && !$subAdminManager->isSubAdminofGroup($user, $this->groupManager->get($group))) {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 105, 'insufficient privileges for group '. $group);
|
2015-08-27 16:29:28 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-22 13:54:50 +00:00
|
|
|
} else {
|
2015-08-27 16:29:28 +00:00
|
|
|
if(!$isAdmin) {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 106, 'no group specified (required for subadmins)');
|
2015-08-27 16:29:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2015-10-30 08:30:00 +00:00
|
|
|
$newUser = $this->userManager->createUser($userId, $password);
|
|
|
|
$this->logger->info('Successful addUser call with userid: '.$userId, ['app' => 'ocs_api']);
|
2015-08-27 16:29:28 +00:00
|
|
|
|
|
|
|
if (is_array($groups)) {
|
|
|
|
foreach ($groups as $group) {
|
2015-10-30 08:30:00 +00:00
|
|
|
$this->groupManager->get($group)->addUser($newUser);
|
|
|
|
$this->logger->info('Added userid '.$userId.' to group '.$group, ['app' => 'ocs_api']);
|
2015-08-27 16:29:28 +00:00
|
|
|
}
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 100);
|
2015-08-27 16:29:28 +00:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->logger->error('Failed addUser attempt with exception: '.$e->getMessage(), ['app' => 'ocs_api']);
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 101, 'Bad request');
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gets user info
|
2015-09-08 12:02:30 +00:00
|
|
|
*
|
|
|
|
* @param array $parameters
|
2016-07-12 06:38:56 +00:00
|
|
|
* @return \OC\OCS\Result
|
2014-12-22 13:54:50 +00:00
|
|
|
*/
|
2015-10-27 13:09:45 +00:00
|
|
|
public function getUser($parameters) {
|
2014-12-22 13:54:50 +00:00
|
|
|
$userId = $parameters['userid'];
|
2015-08-11 13:37:06 +00:00
|
|
|
|
|
|
|
// Check if user is logged in
|
2015-10-27 13:09:45 +00:00
|
|
|
$currentLoggedInUser = $this->userSession->getUser();
|
|
|
|
if ($currentLoggedInUser === null) {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, \OCP\API::RESPOND_UNAUTHORISED);
|
2015-08-11 13:37:06 +00:00
|
|
|
}
|
|
|
|
|
2015-10-08 19:47:30 +00:00
|
|
|
$data = [];
|
|
|
|
|
2015-10-27 13:09:45 +00:00
|
|
|
// Check if the target user exists
|
|
|
|
$targetUserObject = $this->userManager->get($userId);
|
|
|
|
if($targetUserObject === null) {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, \OCP\API::RESPOND_NOT_FOUND, 'The requested user could not be found');
|
2015-10-27 13:09:45 +00:00
|
|
|
}
|
|
|
|
|
2014-12-22 13:54:50 +00:00
|
|
|
// Admin? Or SubAdmin?
|
2015-10-27 13:09:45 +00:00
|
|
|
if($this->groupManager->isAdmin($currentLoggedInUser->getUID())
|
|
|
|
|| $this->groupManager->getSubAdmin()->isUserAccessible($currentLoggedInUser, $targetUserObject)) {
|
2015-10-08 19:47:30 +00:00
|
|
|
$data['enabled'] = $this->config->getUserValue($userId, 'core', 'enabled', 'true');
|
2014-12-22 13:54:50 +00:00
|
|
|
} else {
|
|
|
|
// Check they are looking up themselves
|
2015-10-27 13:09:45 +00:00
|
|
|
if($currentLoggedInUser->getUID() !== $userId) {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, \OCP\API::RESPOND_UNAUTHORISED);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the data
|
2015-10-27 13:09:45 +00:00
|
|
|
$data['quota'] = $this->fillStorageInfo($userId);
|
2015-12-01 11:05:40 +00:00
|
|
|
$data['email'] = $targetUserObject->getEMailAddress();
|
2015-10-27 13:09:45 +00:00
|
|
|
$data['displayname'] = $targetUserObject->getDisplayName();
|
2014-12-22 13:54:50 +00:00
|
|
|
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result($data);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* edit users
|
2015-09-08 12:02:30 +00:00
|
|
|
*
|
|
|
|
* @param array $parameters
|
2016-07-12 06:38:56 +00:00
|
|
|
* @return \OC\OCS\Result
|
2014-12-22 13:54:50 +00:00
|
|
|
*/
|
2015-09-08 12:02:30 +00:00
|
|
|
public function editUser($parameters) {
|
2015-10-27 13:09:45 +00:00
|
|
|
/** @var string $targetUserId */
|
|
|
|
$targetUserId = $parameters['userid'];
|
2015-08-11 13:37:06 +00:00
|
|
|
|
|
|
|
// Check if user is logged in
|
2015-10-27 13:09:45 +00:00
|
|
|
$currentLoggedInUser = $this->userSession->getUser();
|
|
|
|
if ($currentLoggedInUser === null) {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, \OCP\API::RESPOND_UNAUTHORISED);
|
2015-08-11 13:37:06 +00:00
|
|
|
}
|
|
|
|
|
2015-10-27 13:09:45 +00:00
|
|
|
$targetUser = $this->userManager->get($targetUserId);
|
|
|
|
if($targetUser === null) {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 997);
|
2015-10-27 13:09:45 +00:00
|
|
|
}
|
|
|
|
|
2016-07-12 06:38:56 +00:00
|
|
|
$permittedFields = [];
|
2015-10-27 13:09:45 +00:00
|
|
|
if($targetUserId === $currentLoggedInUser->getUID()) {
|
2014-12-22 13:54:50 +00:00
|
|
|
// Editing self (display, email)
|
|
|
|
$permittedFields[] = 'display';
|
|
|
|
$permittedFields[] = 'email';
|
|
|
|
$permittedFields[] = 'password';
|
|
|
|
// If admin they can edit their own quota
|
2015-10-27 13:09:45 +00:00
|
|
|
if($this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
|
2014-12-22 13:54:50 +00:00
|
|
|
$permittedFields[] = 'quota';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Check if admin / subadmin
|
2015-10-27 13:09:45 +00:00
|
|
|
$subAdminManager = $this->groupManager->getSubAdmin();
|
|
|
|
if($subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)
|
|
|
|
|| $this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
|
2014-12-22 13:54:50 +00:00
|
|
|
// They have permissions over the user
|
|
|
|
$permittedFields[] = 'display';
|
|
|
|
$permittedFields[] = 'quota';
|
|
|
|
$permittedFields[] = 'password';
|
|
|
|
$permittedFields[] = 'email';
|
|
|
|
} else {
|
|
|
|
// No rights
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 997);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check if permitted to edit this field
|
|
|
|
if(!in_array($parameters['_put']['key'], $permittedFields)) {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 997);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
|
|
|
// Process the edit
|
2015-10-27 13:09:45 +00:00
|
|
|
switch($parameters['_put']['key']) {
|
2014-12-22 13:54:50 +00:00
|
|
|
case 'display':
|
2015-10-27 13:09:45 +00:00
|
|
|
$targetUser->setDisplayName($parameters['_put']['value']);
|
2014-12-22 13:54:50 +00:00
|
|
|
break;
|
|
|
|
case 'quota':
|
|
|
|
$quota = $parameters['_put']['value'];
|
|
|
|
if($quota !== 'none' and $quota !== 'default') {
|
2015-05-07 15:56:13 +00:00
|
|
|
if (is_numeric($quota)) {
|
|
|
|
$quota = floatval($quota);
|
|
|
|
} else {
|
2015-08-02 08:27:36 +00:00
|
|
|
$quota = \OCP\Util::computerFileSize($quota);
|
2015-05-07 15:56:13 +00:00
|
|
|
}
|
|
|
|
if ($quota === false) {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 103, "Invalid quota value {$parameters['_put']['value']}");
|
2015-05-07 15:56:13 +00:00
|
|
|
}
|
2015-07-25 12:49:20 +00:00
|
|
|
if($quota === 0) {
|
2014-12-22 13:54:50 +00:00
|
|
|
$quota = 'default';
|
2015-10-27 13:09:45 +00:00
|
|
|
}else if($quota === -1) {
|
2014-12-22 13:54:50 +00:00
|
|
|
$quota = 'none';
|
|
|
|
} else {
|
2015-08-02 08:27:36 +00:00
|
|
|
$quota = \OCP\Util::humanFileSize($quota);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-09 16:16:43 +00:00
|
|
|
$targetUser->setQuota($quota);
|
2014-12-22 13:54:50 +00:00
|
|
|
break;
|
|
|
|
case 'password':
|
2015-10-27 13:09:45 +00:00
|
|
|
$targetUser->setPassword($parameters['_put']['value']);
|
2014-12-22 13:54:50 +00:00
|
|
|
break;
|
|
|
|
case 'email':
|
|
|
|
if(filter_var($parameters['_put']['value'], FILTER_VALIDATE_EMAIL)) {
|
2016-01-18 19:27:43 +00:00
|
|
|
$targetUser->setEMailAddress($parameters['_put']['value']);
|
2014-12-22 13:54:50 +00:00
|
|
|
} else {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 102);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 103);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 100);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
|
|
|
|
2015-09-08 12:02:30 +00:00
|
|
|
/**
|
|
|
|
* @param array $parameters
|
2016-07-12 06:38:56 +00:00
|
|
|
* @return \OC\OCS\Result
|
2015-09-08 12:02:30 +00:00
|
|
|
*/
|
|
|
|
public function deleteUser($parameters) {
|
2015-08-11 13:37:06 +00:00
|
|
|
// Check if user is logged in
|
2015-10-27 13:09:45 +00:00
|
|
|
$currentLoggedInUser = $this->userSession->getUser();
|
|
|
|
if ($currentLoggedInUser === null) {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, \OCP\API::RESPOND_UNAUTHORISED);
|
2015-08-11 13:37:06 +00:00
|
|
|
}
|
|
|
|
|
2015-10-27 13:09:45 +00:00
|
|
|
$targetUser = $this->userManager->get($parameters['userid']);
|
|
|
|
|
|
|
|
if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 101);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
2015-10-27 13:09:45 +00:00
|
|
|
|
2014-12-22 13:54:50 +00:00
|
|
|
// If not permitted
|
2015-10-27 13:09:45 +00:00
|
|
|
$subAdminManager = $this->groupManager->getSubAdmin();
|
|
|
|
if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 997);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
2015-10-27 13:09:45 +00:00
|
|
|
|
2014-12-22 13:54:50 +00:00
|
|
|
// Go ahead with the delete
|
2015-10-27 13:09:45 +00:00
|
|
|
if($targetUser->delete()) {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 100);
|
2014-12-22 13:54:50 +00:00
|
|
|
} else {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 101);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-07 15:22:21 +00:00
|
|
|
/**
|
|
|
|
* @param array $parameters
|
2016-07-12 06:38:56 +00:00
|
|
|
* @return \OC\OCS\Result
|
2016-04-07 15:22:21 +00:00
|
|
|
*/
|
|
|
|
public function disableUser($parameters) {
|
|
|
|
return $this->setEnabled($parameters, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $parameters
|
2016-07-12 06:38:56 +00:00
|
|
|
* @return \OC\OCS\Result
|
2016-04-07 15:22:21 +00:00
|
|
|
*/
|
|
|
|
public function enableUser($parameters) {
|
|
|
|
return $this->setEnabled($parameters, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $parameters
|
|
|
|
* @param bool $value
|
2016-07-12 06:38:56 +00:00
|
|
|
* @return \OC\OCS\Result
|
2016-04-07 15:22:21 +00:00
|
|
|
*/
|
|
|
|
private function setEnabled($parameters, $value) {
|
|
|
|
// Check if user is logged in
|
|
|
|
$currentLoggedInUser = $this->userSession->getUser();
|
|
|
|
if ($currentLoggedInUser === null) {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, \OCP\API::RESPOND_UNAUTHORISED);
|
2016-04-07 15:22:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$targetUser = $this->userManager->get($parameters['userid']);
|
|
|
|
if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 101);
|
2016-04-07 15:22:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If not permitted
|
|
|
|
$subAdminManager = $this->groupManager->getSubAdmin();
|
|
|
|
if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 997);
|
2016-04-07 15:22:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// enable/disable the user now
|
|
|
|
$targetUser->setEnabled($value);
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 100);
|
2016-04-07 15:22:21 +00:00
|
|
|
}
|
|
|
|
|
2015-09-08 12:02:30 +00:00
|
|
|
/**
|
|
|
|
* @param array $parameters
|
2016-07-12 06:38:56 +00:00
|
|
|
* @return \OC\OCS\Result
|
2015-09-08 12:02:30 +00:00
|
|
|
*/
|
2015-08-11 13:37:06 +00:00
|
|
|
public function getUsersGroups($parameters) {
|
|
|
|
// Check if user is logged in
|
2015-10-27 13:09:45 +00:00
|
|
|
$loggedInUser = $this->userSession->getUser();
|
|
|
|
if ($loggedInUser === null) {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, \OCP\API::RESPOND_UNAUTHORISED);
|
2015-08-11 13:37:06 +00:00
|
|
|
}
|
|
|
|
|
2015-10-27 13:09:45 +00:00
|
|
|
$targetUser = $this->userManager->get($parameters['userid']);
|
|
|
|
if($targetUser === null) {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, \OCP\API::RESPOND_NOT_FOUND);
|
2015-10-27 13:09:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if($targetUser->getUID() === $loggedInUser->getUID() || $this->groupManager->isAdmin($loggedInUser->getUID())) {
|
2014-12-22 13:54:50 +00:00
|
|
|
// Self lookup or admin lookup
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result([
|
2015-10-27 13:09:45 +00:00
|
|
|
'groups' => $this->groupManager->getUserGroupIds($targetUser)
|
2015-07-25 10:46:39 +00:00
|
|
|
]);
|
2014-12-22 13:54:50 +00:00
|
|
|
} else {
|
2015-10-27 13:09:45 +00:00
|
|
|
$subAdminManager = $this->groupManager->getSubAdmin();
|
|
|
|
|
2014-12-22 13:54:50 +00:00
|
|
|
// Looking up someone else
|
2015-10-27 13:09:45 +00:00
|
|
|
if($subAdminManager->isUserAccessible($loggedInUser, $targetUser)) {
|
2014-12-22 13:54:50 +00:00
|
|
|
// Return the group that the method caller is subadmin of for the user in question
|
2015-10-27 13:09:45 +00:00
|
|
|
$getSubAdminsGroups = $subAdminManager->getSubAdminsGroups($loggedInUser);
|
|
|
|
foreach ($getSubAdminsGroups as $key => $group) {
|
|
|
|
$getSubAdminsGroups[$key] = $group->getGID();
|
|
|
|
}
|
2015-07-25 10:46:39 +00:00
|
|
|
$groups = array_intersect(
|
2015-10-27 13:09:45 +00:00
|
|
|
$getSubAdminsGroups,
|
|
|
|
$this->groupManager->getUserGroupIds($targetUser)
|
2015-07-25 10:46:39 +00:00
|
|
|
);
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(array('groups' => $groups));
|
2014-12-22 13:54:50 +00:00
|
|
|
} else {
|
|
|
|
// Not permitted
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 997);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-09-08 12:02:30 +00:00
|
|
|
/**
|
|
|
|
* @param array $parameters
|
2016-07-12 06:38:56 +00:00
|
|
|
* @return \OC\OCS\Result
|
2015-09-08 12:02:30 +00:00
|
|
|
*/
|
|
|
|
public function addToGroup($parameters) {
|
2015-08-11 13:37:06 +00:00
|
|
|
// Check if user is logged in
|
|
|
|
$user = $this->userSession->getUser();
|
|
|
|
if ($user === null) {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, \OCP\API::RESPOND_UNAUTHORISED);
|
2015-08-11 13:37:06 +00:00
|
|
|
}
|
|
|
|
|
2014-12-22 13:54:50 +00:00
|
|
|
// Check they're an admin
|
2015-10-27 13:09:45 +00:00
|
|
|
if(!$this->groupManager->isAdmin($user->getUID())) {
|
2014-12-22 13:54:50 +00:00
|
|
|
// This user doesn't have rights to add a user to this group
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, \OCP\API::RESPOND_UNAUTHORISED);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
2015-10-27 13:09:45 +00:00
|
|
|
|
|
|
|
$groupId = !empty($_POST['groupid']) ? $_POST['groupid'] : null;
|
|
|
|
if($groupId === null) {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 101);
|
2015-10-27 13:09:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$group = $this->groupManager->get($groupId);
|
|
|
|
$targetUser = $this->userManager->get($parameters['userid']);
|
|
|
|
if($group === null) {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 102);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
2015-10-27 13:09:45 +00:00
|
|
|
if($targetUser === null) {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 103);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
2015-10-27 13:09:45 +00:00
|
|
|
|
2014-12-22 13:54:50 +00:00
|
|
|
// Add user to group
|
2015-10-27 13:09:45 +00:00
|
|
|
$group->addUser($targetUser);
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 100);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
|
|
|
|
2015-09-08 12:02:30 +00:00
|
|
|
/**
|
|
|
|
* @param array $parameters
|
2016-07-12 06:38:56 +00:00
|
|
|
* @return \OC\OCS\Result
|
2015-09-08 12:02:30 +00:00
|
|
|
*/
|
2015-08-11 13:37:06 +00:00
|
|
|
public function removeFromGroup($parameters) {
|
|
|
|
// Check if user is logged in
|
2015-10-27 13:09:45 +00:00
|
|
|
$loggedInUser = $this->userSession->getUser();
|
|
|
|
if ($loggedInUser === null) {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, \OCP\API::RESPOND_UNAUTHORISED);
|
2015-08-11 13:37:06 +00:00
|
|
|
}
|
|
|
|
|
2015-11-05 16:01:47 +00:00
|
|
|
$group = !empty($parameters['_delete']['groupid']) ? $parameters['_delete']['groupid'] : null;
|
2015-10-27 13:09:45 +00:00
|
|
|
if($group === null) {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 101);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
2015-10-27 13:09:45 +00:00
|
|
|
|
2015-11-05 16:01:47 +00:00
|
|
|
$group = $this->groupManager->get($group);
|
|
|
|
if($group === null) {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 102);
|
2015-11-05 16:01:47 +00:00
|
|
|
}
|
|
|
|
|
2015-10-27 13:09:45 +00:00
|
|
|
$targetUser = $this->userManager->get($parameters['userid']);
|
|
|
|
if($targetUser === null) {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 103);
|
2015-10-27 13:09:45 +00:00
|
|
|
}
|
|
|
|
|
2014-12-22 13:54:50 +00:00
|
|
|
// If they're not an admin, check they are a subadmin of the group in question
|
2015-10-27 13:09:45 +00:00
|
|
|
$subAdminManager = $this->groupManager->getSubAdmin();
|
|
|
|
if(!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminofGroup($loggedInUser, $group)) {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 104);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
|
|
|
// Check they aren't removing themselves from 'admin' or their 'subadmin; group
|
2015-10-27 13:09:45 +00:00
|
|
|
if($parameters['userid'] === $loggedInUser->getUID()) {
|
|
|
|
if($this->groupManager->isAdmin($loggedInUser->getUID())) {
|
|
|
|
if($group->getGID() === 'admin') {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 105, 'Cannot remove yourself from the admin group');
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Not an admin, check they are not removing themself from their subadmin group
|
2015-10-27 13:09:45 +00:00
|
|
|
$subAdminGroups = $subAdminManager->getSubAdminsGroups($loggedInUser);
|
|
|
|
foreach ($subAdminGroups as $key => $group) {
|
|
|
|
$subAdminGroups[$key] = $group->getGID();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(in_array($group->getGID(), $subAdminGroups, true)) {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 105, 'Cannot remove yourself from this group as you are a SubAdmin');
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-27 13:09:45 +00:00
|
|
|
|
2014-12-22 13:54:50 +00:00
|
|
|
// Remove user from group
|
2015-10-27 13:09:45 +00:00
|
|
|
$group->removeUser($targetUser);
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 100);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a subadmin
|
2015-09-08 12:02:30 +00:00
|
|
|
*
|
|
|
|
* @param array $parameters
|
2016-07-12 06:38:56 +00:00
|
|
|
* @return \OC\OCS\Result
|
2014-12-22 13:54:50 +00:00
|
|
|
*/
|
2015-07-25 10:46:39 +00:00
|
|
|
public function addSubAdmin($parameters) {
|
2015-10-27 13:09:45 +00:00
|
|
|
$group = $this->groupManager->get($_POST['groupid']);
|
|
|
|
$user = $this->userManager->get($parameters['userid']);
|
|
|
|
|
2014-12-22 13:54:50 +00:00
|
|
|
// Check if the user exists
|
2015-10-27 13:09:45 +00:00
|
|
|
if($user === null) {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 101, 'User does not exist');
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
|
|
|
// Check if group exists
|
2015-10-27 13:09:45 +00:00
|
|
|
if($group === null) {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 102, 'Group:'.$_POST['groupid'].' does not exist');
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
|
|
|
// Check if trying to make subadmin of admin group
|
2015-10-27 13:09:45 +00:00
|
|
|
if(strtolower($_POST['groupid']) === 'admin') {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 103, 'Cannot create subadmins for admin group');
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
2015-10-27 13:09:45 +00:00
|
|
|
|
|
|
|
$subAdminManager = $this->groupManager->getSubAdmin();
|
|
|
|
|
2015-04-13 08:32:18 +00:00
|
|
|
// We cannot be subadmin twice
|
2015-10-27 13:09:45 +00:00
|
|
|
if ($subAdminManager->isSubAdminofGroup($user, $group)) {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 100);
|
2015-04-13 08:32:18 +00:00
|
|
|
}
|
2014-12-22 13:54:50 +00:00
|
|
|
// Go
|
2015-10-27 13:09:45 +00:00
|
|
|
if($subAdminManager->createSubAdmin($user, $group)) {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 100);
|
2014-12-22 13:54:50 +00:00
|
|
|
} else {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 103, 'Unknown error occurred');
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a subadmin from a group
|
2015-09-08 12:02:30 +00:00
|
|
|
*
|
|
|
|
* @param array $parameters
|
2016-07-12 06:38:56 +00:00
|
|
|
* @return \OC\OCS\Result
|
2014-12-22 13:54:50 +00:00
|
|
|
*/
|
2015-07-25 10:46:39 +00:00
|
|
|
public function removeSubAdmin($parameters) {
|
2015-10-27 13:09:45 +00:00
|
|
|
$group = $this->groupManager->get($parameters['_delete']['groupid']);
|
|
|
|
$user = $this->userManager->get($parameters['userid']);
|
|
|
|
$subAdminManager = $this->groupManager->getSubAdmin();
|
|
|
|
|
2014-12-22 13:54:50 +00:00
|
|
|
// Check if the user exists
|
2015-10-27 13:09:45 +00:00
|
|
|
if($user === null) {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 101, 'User does not exist');
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
2015-10-27 13:09:45 +00:00
|
|
|
// Check if the group exists
|
|
|
|
if($group === null) {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 101, 'Group does not exist');
|
2015-10-27 13:09:45 +00:00
|
|
|
}
|
2014-12-22 13:54:50 +00:00
|
|
|
// Check if they are a subadmin of this said group
|
2015-10-27 13:09:45 +00:00
|
|
|
if(!$subAdminManager->isSubAdminofGroup($user, $group)) {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 102, 'User is not a subadmin of this group');
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
2015-10-27 13:09:45 +00:00
|
|
|
|
2014-12-22 13:54:50 +00:00
|
|
|
// Go
|
2015-10-27 13:09:45 +00:00
|
|
|
if($subAdminManager->deleteSubAdmin($user, $group)) {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 100);
|
2014-12-22 13:54:50 +00:00
|
|
|
} else {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 103, 'Unknown error occurred');
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-09-08 12:02:30 +00:00
|
|
|
* Get the groups a user is a subadmin of
|
|
|
|
*
|
|
|
|
* @param array $parameters
|
2016-07-12 06:38:56 +00:00
|
|
|
* @return \OC\OCS\Result
|
2014-12-22 13:54:50 +00:00
|
|
|
*/
|
2015-07-25 10:46:39 +00:00
|
|
|
public function getUserSubAdminGroups($parameters) {
|
2015-10-27 13:09:45 +00:00
|
|
|
$user = $this->userManager->get($parameters['userid']);
|
2014-12-22 13:54:50 +00:00
|
|
|
// Check if the user exists
|
2015-10-27 13:09:45 +00:00
|
|
|
if($user === null) {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 101, 'User does not exist');
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
2015-10-27 13:09:45 +00:00
|
|
|
|
2014-12-22 13:54:50 +00:00
|
|
|
// Get the subadmin groups
|
2015-10-27 13:09:45 +00:00
|
|
|
$groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($user);
|
|
|
|
foreach ($groups as $key => $group) {
|
|
|
|
$groups[$key] = $group->getGID();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!$groups) {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result(null, 102, 'Unknown error occurred');
|
2014-12-22 13:54:50 +00:00
|
|
|
} else {
|
2016-07-12 06:38:56 +00:00
|
|
|
return new \OC\OCS\Result($groups);
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-19 10:38:03 +00:00
|
|
|
|
|
|
|
/**
|
2015-09-08 12:02:30 +00:00
|
|
|
* @param string $userId
|
2015-10-27 13:09:45 +00:00
|
|
|
* @return array
|
2015-05-19 10:38:03 +00:00
|
|
|
* @throws \OCP\Files\NotFoundException
|
|
|
|
*/
|
2015-10-27 13:09:45 +00:00
|
|
|
protected function fillStorageInfo($userId) {
|
2015-05-19 10:38:03 +00:00
|
|
|
try {
|
|
|
|
\OC_Util::tearDownFS();
|
|
|
|
\OC_Util::setupFS($userId);
|
|
|
|
$storage = OC_Helper::getStorageInfo('/');
|
2015-10-08 19:47:30 +00:00
|
|
|
$data = [
|
2015-05-19 10:38:03 +00:00
|
|
|
'free' => $storage['free'],
|
|
|
|
'used' => $storage['used'],
|
|
|
|
'total' => $storage['total'],
|
|
|
|
'relative' => $storage['relative'],
|
|
|
|
];
|
|
|
|
} catch (NotFoundException $ex) {
|
2015-10-08 19:47:30 +00:00
|
|
|
$data = [];
|
2015-05-19 10:38:03 +00:00
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
2014-12-22 13:54:50 +00:00
|
|
|
}
|