2018-03-26 20:30:40 +03:00
|
|
|
<?php
|
2019-12-03 21:57:53 +03:00
|
|
|
|
2018-03-26 20:30:40 +03:00
|
|
|
declare(strict_types=1);
|
2019-12-03 21:57:53 +03:00
|
|
|
|
2018-03-26 20:30:40 +03:00
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2018 John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
|
|
|
|
*
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2020-04-29 12:57:22 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Georg Ehrke <oc.list@georgehrke.com>
|
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
|
|
|
* @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
2018-03-26 20:30:40 +03:00
|
|
|
*
|
2019-12-03 21:57:53 +03:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
2018-03-26 20:30:40 +03:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2019-12-03 21:57:53 +03:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2018-03-26 20:30:40 +03:00
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
2019-12-03 21:57:53 +03:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2018-03-26 20:30:40 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Provisioning_API\Controller;
|
|
|
|
|
|
|
|
use OC\Accounts\AccountManager;
|
2021-02-16 17:51:25 +03:00
|
|
|
use OC\Group\Manager;
|
2018-11-07 18:35:51 +03:00
|
|
|
use OC\User\Backend;
|
2019-01-17 13:59:05 +03:00
|
|
|
use OC\User\NoUserException;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OC_Helper;
|
2020-12-01 16:33:22 +03:00
|
|
|
use OCP\Accounts\IAccountManager;
|
2018-03-26 20:30:40 +03:00
|
|
|
use OCP\AppFramework\OCS\OCSException;
|
|
|
|
use OCP\AppFramework\OCS\OCSNotFoundException;
|
2018-03-27 12:05:36 +03:00
|
|
|
use OCP\AppFramework\OCSController;
|
2018-03-26 20:30:40 +03:00
|
|
|
use OCP\Files\NotFoundException;
|
2018-03-27 12:05:36 +03:00
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\IGroupManager;
|
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\IUserManager;
|
|
|
|
use OCP\IUserSession;
|
2020-06-23 09:45:01 +03:00
|
|
|
use OCP\L10N\IFactory;
|
2018-11-07 18:35:51 +03:00
|
|
|
use OCP\User\Backend\ISetDisplayNameBackend;
|
|
|
|
use OCP\User\Backend\ISetPasswordBackend;
|
2018-03-26 20:30:40 +03:00
|
|
|
|
2018-03-27 12:05:36 +03:00
|
|
|
abstract class AUserData extends OCSController {
|
|
|
|
|
|
|
|
/** @var IUserManager */
|
|
|
|
protected $userManager;
|
|
|
|
/** @var IConfig */
|
|
|
|
protected $config;
|
2021-02-16 17:51:25 +03:00
|
|
|
/** @var IGroupManager|Manager */ // FIXME Requires a method that is not on the interface
|
2018-03-27 12:05:36 +03:00
|
|
|
protected $groupManager;
|
|
|
|
/** @var IUserSession */
|
|
|
|
protected $userSession;
|
|
|
|
/** @var AccountManager */
|
|
|
|
protected $accountManager;
|
2020-06-23 09:45:01 +03:00
|
|
|
/** @var IFactory */
|
|
|
|
protected $l10nFactory;
|
2018-03-27 12:05:36 +03:00
|
|
|
|
|
|
|
public function __construct(string $appName,
|
|
|
|
IRequest $request,
|
|
|
|
IUserManager $userManager,
|
|
|
|
IConfig $config,
|
|
|
|
IGroupManager $groupManager,
|
|
|
|
IUserSession $userSession,
|
2020-06-23 09:45:01 +03:00
|
|
|
AccountManager $accountManager,
|
|
|
|
IFactory $l10nFactory) {
|
2018-03-27 12:05:36 +03:00
|
|
|
parent::__construct($appName, $request);
|
|
|
|
|
|
|
|
$this->userManager = $userManager;
|
|
|
|
$this->config = $config;
|
|
|
|
$this->groupManager = $groupManager;
|
|
|
|
$this->userSession = $userSession;
|
|
|
|
$this->accountManager = $accountManager;
|
2020-06-23 09:45:01 +03:00
|
|
|
$this->l10nFactory = $l10nFactory;
|
2018-03-27 12:05:36 +03:00
|
|
|
}
|
2018-03-26 20:30:40 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* creates a array with all user data
|
|
|
|
*
|
2018-09-12 10:40:27 +03:00
|
|
|
* @param string $userId
|
2018-03-26 20:30:40 +03:00
|
|
|
* @return array
|
2019-01-17 13:59:05 +03:00
|
|
|
* @throws NotFoundException
|
2018-03-26 20:30:40 +03:00
|
|
|
* @throws OCSException
|
2019-01-17 13:59:05 +03:00
|
|
|
* @throws OCSNotFoundException
|
2018-03-26 20:30:40 +03:00
|
|
|
*/
|
2018-03-27 12:05:36 +03:00
|
|
|
protected function getUserData(string $userId): array {
|
2018-03-26 20:30:40 +03:00
|
|
|
$currentLoggedInUser = $this->userSession->getUser();
|
|
|
|
|
|
|
|
$data = [];
|
|
|
|
|
|
|
|
// Check if the target user exists
|
|
|
|
$targetUserObject = $this->userManager->get($userId);
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($targetUserObject === null) {
|
2018-03-26 20:30:40 +03:00
|
|
|
throw new OCSNotFoundException('User does not exist');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should be at least Admin Or SubAdmin!
|
2018-05-23 14:01:40 +03:00
|
|
|
if ($this->groupManager->isAdmin($currentLoggedInUser->getUID())
|
2018-03-26 20:30:40 +03:00
|
|
|
|| $this->groupManager->getSubAdmin()->isUserAccessible($currentLoggedInUser, $targetUserObject)) {
|
2020-04-10 15:19:56 +03:00
|
|
|
$data['enabled'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'enabled', 'true') === 'true';
|
2018-03-26 20:30:40 +03:00
|
|
|
} else {
|
|
|
|
// Check they are looking up themselves
|
2018-05-23 14:01:40 +03:00
|
|
|
if ($currentLoggedInUser->getUID() !== $targetUserObject->getUID()) {
|
2018-03-26 20:30:40 +03:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get groups data
|
|
|
|
$userAccount = $this->accountManager->getUser($targetUserObject);
|
|
|
|
$groups = $this->groupManager->getUserGroups($targetUserObject);
|
|
|
|
$gids = [];
|
|
|
|
foreach ($groups as $group) {
|
2018-06-15 17:42:39 +03:00
|
|
|
$gids[] = $group->getGID();
|
2018-03-26 20:30:40 +03:00
|
|
|
}
|
|
|
|
|
2019-01-17 13:59:05 +03:00
|
|
|
try {
|
|
|
|
# might be thrown by LDAP due to handling of users disappears
|
|
|
|
# from the external source (reasons unknown to us)
|
|
|
|
# cf. https://github.com/nextcloud/server/issues/12991
|
|
|
|
$data['storageLocation'] = $targetUserObject->getHome();
|
|
|
|
} catch (NoUserException $e) {
|
|
|
|
throw new OCSNotFoundException($e->getMessage(), $e);
|
|
|
|
}
|
|
|
|
|
2018-03-26 20:30:40 +03:00
|
|
|
// Find the data
|
|
|
|
$data['id'] = $targetUserObject->getUID();
|
|
|
|
$data['lastLogin'] = $targetUserObject->getLastLogin() * 1000;
|
|
|
|
$data['backend'] = $targetUserObject->getBackendClassName();
|
|
|
|
$data['subadmin'] = $this->getUserSubAdminGroupsData($targetUserObject->getUID());
|
|
|
|
$data['quota'] = $this->fillStorageInfo($targetUserObject->getUID());
|
2020-12-01 16:33:22 +03:00
|
|
|
$data[IAccountManager::PROPERTY_EMAIL] = $targetUserObject->getEMailAddress();
|
|
|
|
$data[IAccountManager::PROPERTY_DISPLAYNAME] = $targetUserObject->getDisplayName();
|
|
|
|
$data[IAccountManager::PROPERTY_PHONE] = $userAccount[IAccountManager::PROPERTY_PHONE]['value'];
|
|
|
|
$data[IAccountManager::PROPERTY_ADDRESS] = $userAccount[IAccountManager::PROPERTY_ADDRESS]['value'];
|
|
|
|
$data[IAccountManager::PROPERTY_WEBSITE] = $userAccount[IAccountManager::PROPERTY_WEBSITE]['value'];
|
|
|
|
$data[IAccountManager::PROPERTY_TWITTER] = $userAccount[IAccountManager::PROPERTY_TWITTER]['value'];
|
2018-03-26 20:30:40 +03:00
|
|
|
$data['groups'] = $gids;
|
2020-06-23 09:45:01 +03:00
|
|
|
$data['language'] = $this->l10nFactory->getUserLanguage($targetUserObject);
|
2018-06-27 20:09:09 +03:00
|
|
|
$data['locale'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'locale');
|
2018-03-26 20:30:40 +03:00
|
|
|
|
2018-11-07 18:35:51 +03:00
|
|
|
$backend = $targetUserObject->getBackend();
|
|
|
|
$data['backendCapabilities'] = [
|
|
|
|
'setDisplayName' => $backend instanceof ISetDisplayNameBackend || $backend->implementsActions(Backend::SET_DISPLAYNAME),
|
|
|
|
'setPassword' => $backend instanceof ISetPasswordBackend || $backend->implementsActions(Backend::SET_PASSWORD),
|
|
|
|
];
|
|
|
|
|
2018-03-26 20:30:40 +03:00
|
|
|
return $data;
|
2020-04-09 10:22:29 +03:00
|
|
|
}
|
2018-03-26 20:30:40 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the groups a user is a subadmin of
|
|
|
|
*
|
|
|
|
* @param string $userId
|
|
|
|
* @return array
|
|
|
|
* @throws OCSException
|
|
|
|
*/
|
|
|
|
protected function getUserSubAdminGroupsData(string $userId): array {
|
|
|
|
$user = $this->userManager->get($userId);
|
|
|
|
// Check if the user exists
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($user === null) {
|
2018-03-26 20:30:40 +03:00
|
|
|
throw new OCSNotFoundException('User does not exist');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the subadmin groups
|
|
|
|
$subAdminGroups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($user);
|
|
|
|
$groups = [];
|
|
|
|
foreach ($subAdminGroups as $key => $group) {
|
|
|
|
$groups[] = $group->getGID();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $groups;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $userId
|
|
|
|
* @return array
|
2021-02-16 17:51:25 +03:00
|
|
|
* @throws OCSException
|
2018-03-26 20:30:40 +03:00
|
|
|
*/
|
|
|
|
protected function fillStorageInfo(string $userId): array {
|
|
|
|
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) {
|
2018-04-05 18:12:52 +03:00
|
|
|
// User fs is not setup yet
|
|
|
|
$user = $this->userManager->get($userId);
|
|
|
|
if ($user === null) {
|
|
|
|
throw new OCSException('User does not exist', 101);
|
|
|
|
}
|
2018-05-26 11:51:24 +03:00
|
|
|
$quota = $user->getQuota();
|
|
|
|
if ($quota !== 'none') {
|
|
|
|
$quota = OC_Helper::computerFileSize($quota);
|
|
|
|
}
|
2018-04-05 18:12:52 +03:00
|
|
|
$data = [
|
2018-05-26 11:51:24 +03:00
|
|
|
'quota' => $quota !== false ? $quota : 'none',
|
2018-04-05 18:12:52 +03:00
|
|
|
'used' => 0
|
|
|
|
];
|
2018-03-26 20:30:40 +03:00
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
2018-03-27 12:05:36 +03:00
|
|
|
}
|