nextcloud/apps/provisioning_api/lib/Controller/UsersController.php

722 lines
20 KiB
PHP
Raw Normal View History

<?php
/**
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>
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>
*
2015-03-26 13:44:34 +03:00
* @license AGPL-3.0
*
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.
*
2015-03-26 13:44:34 +03:00
* This program is distributed in the hope that it will be useful,
* 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.
*
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/>
*
*/
2016-08-11 20:49:45 +03:00
namespace OCA\Provisioning_API\Controller;
use OC\Accounts\AccountManager;
use \OC_Helper;
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;
use OCP\Files\NotFoundException;
use OCP\IConfig;
use OCP\IGroup;
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;
use OCP\IUserManager;
use OCP\IUserSession;
2016-08-11 20:49:45 +03:00
class UsersController extends OCSController {
/** @var IUserManager */
private $userManager;
/** @var IConfig */
private $config;
/** @var IGroupManager|\OC\Group\Manager */ // FIXME Requires a method that is not on the interface
private $groupManager;
/** @var IUserSession */
private $userSession;
/** @var AccountManager */
private $accountManager;
2015-10-27 16:09:45 +03:00
/** @var ILogger */
private $logger;
/**
2016-08-11 20:49:45 +03:00
* @param string $appName
* @param IRequest $request
* @param IUserManager $userManager
* @param IConfig $config
* @param IGroupManager $groupManager
* @param IUserSession $userSession
* @param AccountManager $accountManager
2015-10-27 16:09:45 +03:00
* @param ILogger $logger
*/
2016-08-11 20:49:45 +03:00
public function __construct($appName,
IRequest $request,
IUserManager $userManager,
IConfig $config,
IGroupManager $groupManager,
IUserSession $userSession,
AccountManager $accountManager,
2015-10-27 16:09:45 +03:00
ILogger $logger) {
2016-08-11 20:49:45 +03:00
parent::__construct($appName, $request);
$this->userManager = $userManager;
$this->config = $config;
$this->groupManager = $groupManager;
$this->userSession = $userSession;
$this->accountManager = $accountManager;
2015-10-27 16:09:45 +03:00
$this->logger = $logger;
}
/**
2016-08-11 20:49:45 +03:00
* @NoAdminRequired
*
* returns a list of users
*
2016-08-11 20:49:45 +03:00
* @param string $search
* @param int $limit
* @param int $offset
* @return DataResponse
*/
2016-08-11 20:49:45 +03:00
public function getUsers($search = '', $limit = null, $offset = null) {
$user = $this->userSession->getUser();
$users = [];
// Admin? Or SubAdmin?
2015-10-27 16:09:45 +03:00
$uid = $user->getUID();
$subAdminManager = $this->groupManager->getSubAdmin();
if($this->groupManager->isAdmin($uid)){
$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();
}
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);
}
2016-08-11 20:49:45 +03:00
$users = array_keys($users);
2016-08-11 20:49:45 +03:00
return new DataResponse([
'users' => $users
]);
}
/**
* @PasswordConfirmationRequired
2016-08-11 20:49:45 +03:00
* @NoAdminRequired
*
* @param string $userid
* @param string $password
* @param array $groups
* @return DataResponse
* @throws OCSException
*/
2016-08-11 20:49:45 +03:00
public function addUser($userid, $password, $groups = null) {
$user = $this->userSession->getUser();
$isAdmin = $this->groupManager->isAdmin($user->getUID());
2015-10-30 11:30:00 +03:00
$subAdminManager = $this->groupManager->getSubAdmin();
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);
}
if(is_array($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-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);
}
}
} else {
if(!$isAdmin) {
2016-08-11 20:49:45 +03:00
throw new OCSException('no group specified (required for subadmins)', 106);
}
}
try {
2016-08-11 20:49:45 +03:00
$newUser = $this->userManager->createUser($userid, $password);
$this->logger->info('Successful addUser call with userid: '.$userid, ['app' => 'ocs_api']);
if (is_array($groups)) {
foreach ($groups as $group) {
2015-10-30 11:30:00 +03:00
$this->groupManager->get($group)->addUser($newUser);
2016-08-11 20:49:45 +03:00
$this->logger->info('Added userid '.$userid.' to group '.$group, ['app' => 'ocs_api']);
}
}
2016-08-11 20:49:45 +03:00
return new DataResponse();
} catch (\Exception $e) {
$this->logger->error('Failed addUser attempt with exception: '.$e->getMessage(), ['app' => 'ocs_api']);
2016-08-11 20:49:45 +03:00
throw new OCSException('Bad request', 101);
}
}
/**
2016-08-11 20:49:45 +03:00
* @NoAdminRequired
* @NoSubAdminRequired
*
* gets user info
*
2016-08-11 20:49:45 +03:00
* @param string $userId
* @return DataResponse
* @throws OCSException
*/
2016-08-11 20:49:45 +03:00
public function getUser($userId) {
$data = $this->getUserData($userId);
return new DataResponse($data);
}
/**
* @NoAdminRequired
* @NoSubAdminRequired
*
* gets user info from the currently logged in user
*
* @return DataResponse
* @throws OCSException
*/
public function getCurrentUser() {
$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
*/
protected function getUserData($userId) {
2015-10-27 16:09:45 +03:00
$currentLoggedInUser = $this->userSession->getUser();
$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
}
// Admin? Or SubAdmin?
2015-10-27 16:09:45 +03:00
if($this->groupManager->isAdmin($currentLoggedInUser->getUID())
|| $this->groupManager->getSubAdmin()->isUserAccessible($currentLoggedInUser, $targetUserObject)) {
$data['enabled'] = $this->config->getUserValue($userId, 'core', 'enabled', 'true');
} else {
// Check they are looking up themselves
2015-10-27 16:09:45 +03:00
if($currentLoggedInUser->getUID() !== $userId) {
2016-08-11 20:49:45 +03:00
throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
}
}
$userAccount = $this->accountManager->getUser($targetUserObject);
$groups = $this->groupManager->getUserGroups($targetUserObject);
$gids = [];
foreach ($groups as $group) {
$gids[] = $group->getDisplayName();
}
// Find the data
$data['id'] = $targetUserObject->getUID();
2015-10-27 16:09:45 +03:00
$data['quota'] = $this->fillStorageInfo($userId);
$data['email'] = $targetUserObject->getEMailAddress();
2015-10-27 16:09:45 +03:00
$data['displayname'] = $targetUserObject->getDisplayName();
$data['phone'] = $userAccount[\OC\Accounts\AccountManager::PROPERTY_PHONE]['value'];
$data['address'] = $userAccount[\OC\Accounts\AccountManager::PROPERTY_ADDRESS]['value'];
$data['webpage'] = $userAccount[\OC\Accounts\AccountManager::PROPERTY_WEBSITE]['value'];
$data['twitter'] = $userAccount[\OC\Accounts\AccountManager::PROPERTY_TWITTER]['value'];
$data['groups'] = $gids;
return $data;
}
2016-08-11 20:49:45 +03:00
/**
* @NoAdminRequired
* @NoSubAdminRequired
* @PasswordConfirmationRequired
2016-08-11 20:49:45 +03:00
*
* edit users
*
2016-08-11 20:49:45 +03:00
* @param string $userId
* @param string $key
* @param string $value
* @return DataResponse
* @throws OCSException
* @throws OCSForbiddenException
*/
2016-08-11 20:49:45 +03:00
public function editUser($userId, $key, $value) {
2015-10-27 16:09:45 +03:00
$currentLoggedInUser = $this->userSession->getUser();
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
}
$permittedFields = [];
2016-08-11 20:49:45 +03:00
if($userId === $currentLoggedInUser->getUID()) {
// Editing self (display, email)
$permittedFields[] = 'display';
$permittedFields[] = 'email';
$permittedFields[] = 'password';
// If admin they can edit their own quota
2015-10-27 16:09:45 +03:00
if($this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
$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())) {
// They have permissions over the user
$permittedFields[] = 'display';
$permittedFields[] = 'quota';
$permittedFields[] = 'password';
$permittedFields[] = 'email';
} else {
// No rights
2016-08-11 20:49:45 +03:00
throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
}
}
// 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);
}
// Process the edit
2016-08-11 20:49:45 +03:00
switch($key) {
case 'display':
2016-08-11 20:49:45 +03:00
$targetUser->setDisplayName($value);
break;
case 'quota':
2016-08-11 20:49:45 +03:00
$quota = $value;
if($quota !== 'none' && $quota !== 'default') {
if (is_numeric($quota)) {
$quota = (float) $quota;
} else {
$quota = \OCP\Util::computerFileSize($quota);
}
if ($quota === false) {
2016-08-11 20:49:45 +03:00
throw new OCSException('Invalid quota value '.$value, 103);
}
if($quota === 0) {
$quota = 'default';
2015-10-27 16:09:45 +03:00
}else if($quota === -1) {
$quota = 'none';
} else {
$quota = \OCP\Util::humanFileSize($quota);
}
}
$targetUser->setQuota($quota);
break;
case 'password':
2016-08-11 20:49:45 +03:00
$targetUser->setPassword($value);
break;
case 'email':
2016-08-11 20:49:45 +03:00
if(filter_var($value, FILTER_VALIDATE_EMAIL)) {
$targetUser->setEMailAddress($value);
} else {
2016-08-11 20:49:45 +03:00
throw new OCSException('', 102);
}
break;
default:
2016-08-11 20:49:45 +03:00
throw new OCSException('', 103);
}
2016-08-11 20:49:45 +03:00
return new DataResponse();
}
/**
* @PasswordConfirmationRequired
2016-08-11 20:49:45 +03:00
* @NoAdminRequired
*
* @param string $userId
* @return DataResponse
* @throws OCSException
* @throws OCSForbiddenException
*/
2016-08-11 20:49:45 +03:00
public function deleteUser($userId) {
2015-10-27 16:09:45 +03:00
$currentLoggedInUser = $this->userSession->getUser();
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);
}
2015-10-27 16:09:45 +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);
}
2015-10-27 16:09:45 +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();
} else {
2016-08-11 20:49:45 +03:00
throw new OCSException('', 101);
}
}
/**
* @PasswordConfirmationRequired
2016-08-11 20:49:45 +03:00
* @NoAdminRequired
*
* @param string $userId
* @return DataResponse
* @throws OCSException
* @throws OCSForbiddenException
*/
2016-08-11 20:49:45 +03:00
public function disableUser($userId) {
return $this->setEnabled($userId, false);
}
/**
* @PasswordConfirmationRequired
2016-08-11 20:49:45 +03:00
* @NoAdminRequired
*
* @param string $userId
* @return DataResponse
* @throws OCSException
* @throws OCSForbiddenException
*/
2016-08-11 20:49:45 +03:00
public function enableUser($userId) {
return $this->setEnabled($userId, true);
}
/**
2016-08-11 20:49:45 +03:00
* @param string $userId
* @param bool $value
2016-08-11 20:49:45 +03:00
* @return DataResponse
* @throws OCSException
* @throws OCSForbiddenException
*/
2016-08-11 20:49:45 +03:00
private function setEnabled($userId, $value) {
$currentLoggedInUser = $this->userSession->getUser();
2016-08-11 20:49:45 +03:00
$targetUser = $this->userManager->get($userId);
if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) {
2016-08-11 20:49:45 +03:00
throw new OCSException('', 101);
}
// 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);
}
// enable/disable the user now
$targetUser->setEnabled($value);
2016-08-11 20:49:45 +03:00
return new DataResponse();
}
/**
2016-08-11 20:49:45 +03:00
* @NoAdminRequired
* @NoSubAdminRequired
*
* @param string $userId
* @return DataResponse
* @throws OCSException
*/
2016-08-11 20:49:45 +03:00
public function getUsersGroups($userId) {
2015-10-27 16:09:45 +03:00
$loggedInUser = $this->userSession->getUser();
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())) {
// 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)
]);
} else {
2015-10-27 16:09:45 +03:00
$subAdminManager = $this->groupManager->getSubAdmin();
// Looking up someone else
2015-10-27 16:09:45 +03:00
if($subAdminManager->isUserAccessible($loggedInUser, $targetUser)) {
// Return the group that the method caller is subadmin of for the user in question
/** @var IGroup[] $getSubAdminsGroups */
2015-10-27 16:09:45 +03:00
$getSubAdminsGroups = $subAdminManager->getSubAdminsGroups($loggedInUser);
foreach ($getSubAdminsGroups as $key => $group) {
$getSubAdminsGroups[$key] = $group->getGID();
}
$groups = array_intersect(
2015-10-27 16:09:45 +03:00
$getSubAdminsGroups,
$this->groupManager->getUserGroupIds($targetUser)
);
2016-08-11 20:49:45 +03:00
return new DataResponse(['groups' => $groups]);
} else {
// Not permitted
2016-08-11 20:49:45 +03:00
throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
}
}
}
/**
* @PasswordConfirmationRequired
* @NoAdminRequired
*
2016-08-11 20:49:45 +03:00
* @param string $userId
* @param string $groupid
* @return DataResponse
* @throws OCSException
*/
2016-08-11 20:49:45 +03:00
public function addToGroup($userId, $groupid = '') {
if($groupid === '') {
throw new OCSException('', 101);
}
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);
}
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
// 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);
}
// 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();
}
/**
* @PasswordConfirmationRequired
2016-08-11 20:49:45 +03:00
* @NoAdminRequired
*
* @param string $userId
2016-08-11 20:49:45 +03:00
* @param string $groupid
* @return DataResponse
* @throws OCSException
*/
2016-08-11 20:49:45 +03:00
public function removeFromGroup($userId, $groupid) {
2015-10-27 16:09:45 +03:00
$loggedInUser = $this->userSession->getUser();
2016-08-11 20:49:45 +03:00
if($groupid === null) {
throw new OCSException('', 101);
}
2015-10-27 16:09:45 +03:00
2016-08-11 20:49:45 +03:00
$group = $this->groupManager->get($groupid);
if($group === null) {
2016-08-11 20:49:45 +03:00
throw new OCSException('', 102);
}
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
}
// 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();
if (!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminOfGroup($loggedInUser, $group)) {
2016-08-11 20:49:45 +03:00
throw new OCSException('', 104);
}
// Check they aren't removing themselves from 'admin' or their 'subadmin; group
if ($userId === $loggedInUser->getUID()) {
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);
}
} else {
// 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);
}
} 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);
}
}
2015-10-27 16:09:45 +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();
}
/**
* Creates a subadmin
*
* @PasswordConfirmationRequired
*
2016-08-11 20:49:45 +03:00
* @param string $userId
* @param string $groupid
* @return DataResponse
* @throws OCSException
*/
2016-08-11 20:49:45 +03:00
public function addSubAdmin($userId, $groupid) {
$group = $this->groupManager->get($groupid);
$user = $this->userManager->get($userId);
2015-10-27 16:09:45 +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);
}
// Check if group exists
2015-10-27 16:09:45 +03:00
if($group === null) {
2016-08-11 20:49:45 +03:00
throw new OCSException('Group:'.$groupid.' does not exist', 102);
}
// Check if trying to make subadmin of admin group
2016-08-11 20:49:45 +03:00
if(strtolower($groupid) === 'admin') {
throw new OCSException('Cannot create subadmins for admin group', 103);
}
2015-10-27 16:09:45 +03:00
$subAdminManager = $this->groupManager->getSubAdmin();
// 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();
}
// 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();
} else {
2016-08-11 20:49:45 +03:00
throw new OCSException('Unknown error occurred', 103);
}
}
/**
* Removes a subadmin from a group
*
* @PasswordConfirmationRequired
*
2016-08-11 20:49:45 +03:00
* @param string $userId
* @param string $groupid
* @return DataResponse
* @throws OCSException
*/
2016-08-11 20:49:45 +03:00
public function removeSubAdmin($userId, $groupid) {
$group = $this->groupManager->get($groupid);
$user = $this->userManager->get($userId);
2015-10-27 16:09:45 +03:00
$subAdminManager = $this->groupManager->getSubAdmin();
// 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);
}
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
}
// Check if they are a subadmin of this said group
2015-10-27 16:09:45 +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);
}
2015-10-27 16:09:45 +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();
} else {
2016-08-11 20:49:45 +03:00
throw new OCSException('Unknown error occurred', 103);
}
}
/**
* Get the groups a user is a subadmin of
*
2016-08-11 20:49:45 +03:00
* @param string $userId
* @return DataResponse
* @throws OCSException
*/
2016-08-11 20:49:45 +03:00
public function getUserSubAdminGroups($userId) {
$user = $this->userManager->get($userId);
// 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);
}
2015-10-27 16:09:45 +03:00
// Get the subadmin groups
2015-10-27 16:09:45 +03:00
$groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($user);
foreach ($groups as $key => $group) {
$groups[$key] = $group->getGID();
}
if(!$groups) {
2016-08-11 20:49:45 +03:00
throw new OCSException('Unknown error occurred', 102);
} else {
2016-08-11 20:49:45 +03:00
return new DataResponse($groups);
}
}
/**
* @param string $userId
2015-10-27 16:09:45 +03:00
* @return array
* @throws \OCP\Files\NotFoundException
*/
2015-10-27 16:09:45 +03:00
protected function fillStorageInfo($userId) {
try {
\OC_Util::tearDownFS();
\OC_Util::setupFS($userId);
$storage = OC_Helper::getStorageInfo('/');
$data = [
'free' => $storage['free'],
'used' => $storage['used'],
'total' => $storage['total'],
'relative' => $storage['relative'],
'quota' => $storage['quota'],
];
} catch (NotFoundException $ex) {
$data = [];
}
return $data;
}
}