137 lines
4.4 KiB
PHP
137 lines
4.4 KiB
PHP
|
<?php
|
||
|
declare(strict_types=1);
|
||
|
/**
|
||
|
* @copyright Copyright (c) 2018 John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
|
||
|
*
|
||
|
* @license AGPL-3.0
|
||
|
*
|
||
|
* 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.
|
||
|
*
|
||
|
* This program is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
* GNU Affero General Public License for more details.
|
||
|
*
|
||
|
* 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/>
|
||
|
*
|
||
|
*/
|
||
|
|
||
|
namespace OCA\Provisioning_API\Controller;
|
||
|
|
||
|
use OC\Accounts\AccountManager;
|
||
|
use OCP\AppFramework\Http\DataResponse;
|
||
|
use OCP\AppFramework\OCS\OCSException;
|
||
|
use OCP\AppFramework\OCS\OCSNotFoundException;
|
||
|
use OCP\AppFramework\OCS\OCSForbiddenException;
|
||
|
use OCP\Files\NotFoundException;
|
||
|
use OC_Helper;
|
||
|
|
||
|
trait UserDataTrait {
|
||
|
|
||
|
/**
|
||
|
* creates a array with all user data
|
||
|
*
|
||
|
* @param $userId
|
||
|
* @return array
|
||
|
* @throws OCSException
|
||
|
*/
|
||
|
public function getUserData(string $userId): array {
|
||
|
$currentLoggedInUser = $this->userSession->getUser();
|
||
|
|
||
|
$data = [];
|
||
|
|
||
|
// Check if the target user exists
|
||
|
$targetUserObject = $this->userManager->get($userId);
|
||
|
if($targetUserObject === null) {
|
||
|
throw new OCSNotFoundException('User does not exist');
|
||
|
}
|
||
|
|
||
|
// Should be at least Admin Or SubAdmin!
|
||
|
if( $this->groupManager->isAdmin($currentLoggedInUser->getUID())
|
||
|
|| $this->groupManager->getSubAdmin()->isUserAccessible($currentLoggedInUser, $targetUserObject)) {
|
||
|
$data['enabled'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'enabled', 'true');
|
||
|
} else {
|
||
|
// Check they are looking up themselves
|
||
|
if($currentLoggedInUser->getUID() !== $targetUserObject->getUID()) {
|
||
|
return $data;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Get groups data
|
||
|
$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();
|
||
|
$data['storageLocation'] = $targetUserObject->getHome();
|
||
|
$data['lastLogin'] = $targetUserObject->getLastLogin() * 1000;
|
||
|
$data['backend'] = $targetUserObject->getBackendClassName();
|
||
|
$data['subadmin'] = $this->getUserSubAdminGroupsData($targetUserObject->getUID());
|
||
|
$data['quota'] = $this->fillStorageInfo($targetUserObject->getUID());
|
||
|
$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'];
|
||
|
$data['groups'] = $gids;
|
||
|
$data['language'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'lang');
|
||
|
|
||
|
return $data;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 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
|
||
|
if($user === null) {
|
||
|
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
|
||
|
* @throws \OCP\Files\NotFoundException
|
||
|
*/
|
||
|
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) {
|
||
|
$data = [];
|
||
|
}
|
||
|
return $data;
|
||
|
}
|
||
|
|
||
|
}
|