Trait bump

Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
This commit is contained in:
John Molakvoæ (skjnldsv) 2018-03-26 19:30:40 +02:00
parent b023bfe38f
commit c103aaaf25
No known key found for this signature in database
GPG Key ID: 60C25B8C072916CF
5 changed files with 177 additions and 126 deletions

View File

@ -10,6 +10,7 @@ return array(
'OCA\\Provisioning_API\\Controller\\AppConfigController' => $baseDir . '/../lib/Controller/AppConfigController.php', 'OCA\\Provisioning_API\\Controller\\AppConfigController' => $baseDir . '/../lib/Controller/AppConfigController.php',
'OCA\\Provisioning_API\\Controller\\AppsController' => $baseDir . '/../lib/Controller/AppsController.php', 'OCA\\Provisioning_API\\Controller\\AppsController' => $baseDir . '/../lib/Controller/AppsController.php',
'OCA\\Provisioning_API\\Controller\\GroupsController' => $baseDir . '/../lib/Controller/GroupsController.php', 'OCA\\Provisioning_API\\Controller\\GroupsController' => $baseDir . '/../lib/Controller/GroupsController.php',
'OCA\\Provisioning_API\\Controller\\UserDataTrait' => $baseDir . '/../lib/Controller/UserDataTrait.php',
'OCA\\Provisioning_API\\Controller\\UsersController' => $baseDir . '/../lib/Controller/UsersController.php', 'OCA\\Provisioning_API\\Controller\\UsersController' => $baseDir . '/../lib/Controller/UsersController.php',
'OCA\\Provisioning_API\\FederatedFileSharingFactory' => $baseDir . '/../lib/FederatedFileSharingFactory.php', 'OCA\\Provisioning_API\\FederatedFileSharingFactory' => $baseDir . '/../lib/FederatedFileSharingFactory.php',
'OCA\\Provisioning_API\\Middleware\\Exceptions\\NotSubAdminException' => $baseDir . '/../lib/Middleware/Exceptions/NotSubAdminException.php', 'OCA\\Provisioning_API\\Middleware\\Exceptions\\NotSubAdminException' => $baseDir . '/../lib/Middleware/Exceptions/NotSubAdminException.php',

View File

@ -25,6 +25,7 @@ class ComposerStaticInitProvisioning_API
'OCA\\Provisioning_API\\Controller\\AppConfigController' => __DIR__ . '/..' . '/../lib/Controller/AppConfigController.php', 'OCA\\Provisioning_API\\Controller\\AppConfigController' => __DIR__ . '/..' . '/../lib/Controller/AppConfigController.php',
'OCA\\Provisioning_API\\Controller\\AppsController' => __DIR__ . '/..' . '/../lib/Controller/AppsController.php', 'OCA\\Provisioning_API\\Controller\\AppsController' => __DIR__ . '/..' . '/../lib/Controller/AppsController.php',
'OCA\\Provisioning_API\\Controller\\GroupsController' => __DIR__ . '/..' . '/../lib/Controller/GroupsController.php', 'OCA\\Provisioning_API\\Controller\\GroupsController' => __DIR__ . '/..' . '/../lib/Controller/GroupsController.php',
'OCA\\Provisioning_API\\Controller\\UserDataTrait' => __DIR__ . '/..' . '/../lib/Controller/UserDataTrait.php',
'OCA\\Provisioning_API\\Controller\\UsersController' => __DIR__ . '/..' . '/../lib/Controller/UsersController.php', 'OCA\\Provisioning_API\\Controller\\UsersController' => __DIR__ . '/..' . '/../lib/Controller/UsersController.php',
'OCA\\Provisioning_API\\FederatedFileSharingFactory' => __DIR__ . '/..' . '/../lib/FederatedFileSharingFactory.php', 'OCA\\Provisioning_API\\FederatedFileSharingFactory' => __DIR__ . '/..' . '/../lib/FederatedFileSharingFactory.php',
'OCA\\Provisioning_API\\Middleware\\Exceptions\\NotSubAdminException' => __DIR__ . '/..' . '/../lib/Middleware/Exceptions/NotSubAdminException.php', 'OCA\\Provisioning_API\\Middleware\\Exceptions\\NotSubAdminException' => __DIR__ . '/..' . '/../lib/Middleware/Exceptions/NotSubAdminException.php',

View File

@ -8,6 +8,7 @@ declare(strict_types=1);
* @author Morris Jobke <hey@morrisjobke.de> * @author Morris Jobke <hey@morrisjobke.de>
* @author Roeland Jago Douma <roeland@famdouma.nl> * @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Tom Needham <tom@owncloud.com> * @author Tom Needham <tom@owncloud.com>
* @author John Molakvoæ <skjnldsv@protonmail.com>
* *
* @license AGPL-3.0 * @license AGPL-3.0
* *
@ -27,55 +28,71 @@ declare(strict_types=1);
namespace OCA\Provisioning_API\Controller; namespace OCA\Provisioning_API\Controller;
use OC\Accounts\AccountManager;
use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCS\OCSException; use OCP\AppFramework\OCS\OCSException;
use OCP\AppFramework\OCS\OCSNotFoundException; use OCP\AppFramework\OCS\OCSNotFoundException;
use OCP\AppFramework\OCS\OCSForbiddenException; use OCP\AppFramework\OCS\OCSForbiddenException;
use OCP\AppFramework\OCSController; use OCP\AppFramework\OCSController;
use OCP\IConfig;
use OCP\IGroup; use OCP\IGroup;
use OCP\IGroupManager; use OCP\IGroupManager;
use OCP\ILogger; use OCP\ILogger;
use OCP\IRequest; use OCP\IRequest;
use OCP\IUserManager;
use OCP\IUserSession; use OCP\IUserSession;
use OCP\IUser; use OCP\IUser;
use OCA\Provisioning_API\Controller\UsersController;
class GroupsController extends OCSController { class GroupsController extends OCSController {
use UserDataTrait;
/** @var IUserManager */
private $userManager;
/** @var IGroupManager */ /** @var IGroupManager */
private $groupManager; private $groupManager;
/** @var IUserSession */ /** @var IUserSession */
private $userSession; private $userSession;
/** @var IConfig */
private $config;
/** @var AccountManager */
private $accountManager;
/** @var ILogger */ /** @var ILogger */
private $logger; private $logger;
/** @var UsersController */
private $userController;
/** /**
* @param string $appName * @param string $appName
* @param IRequest $request * @param IRequest $request
* @param IUserManager $userManager
* @param IConfig $config
* @param IGroupManager $groupManager * @param IGroupManager $groupManager
* @param IUserSession $userSession * @param IUserSession $userSession
* @param AccountManager $accountManager
* @param ILogger $logger * @param ILogger $logger
* @param UsersController $userController * @param UsersController $userController
*/ */
public function __construct( public function __construct(
string $appName, string $appName,
IRequest $request, IRequest $request,
IUserManager $userManager,
IConfig $config,
IGroupManager $groupManager, IGroupManager $groupManager,
IUserSession $userSession, IUserSession $userSession,
ILogger $logger, AccountManager $accountManager,
UsersController $userController) { ILogger $logger) {
parent::__construct($appName, $request); parent::__construct($appName, $request);
$this->groupManager = $groupManager; $this->groupManager = $groupManager;
$this->userManager = $userManager;
$this->config = $config;
$this->userSession = $userSession; $this->userSession = $userSession;
$this->accountManager = $accountManager;
$this->logger = $logger; $this->logger = $logger;
$this->userController = $userController;
} }
/** /**
@ -196,12 +213,16 @@ class GroupsController extends OCSController {
$users = $this->groupManager->get($groupId)->getUsers(); $users = $this->groupManager->get($groupId)->getUsers();
// Extract required number // Extract required number
$users = array_slice($users, $offset, $limit); $users = array_slice($users, $offset, $limit);
$users = array_map(function($user) { $users = array_keys($users);
/** @var IUser $user */ $usersDetails = [];
return $this->userController->getUserData($user->getUID()); foreach ($users as $userId) {
}, $users); $userData = $this->getUserData($userId);
$users = array_values($users); // Do not insert empty entry
return new DataResponse(['users' => $users]); if(!empty($userData)) {
$usersDetails[$userId] = $userData;
}
}
return new DataResponse(['users' => $usersDetails]);
} }
throw new OCSException('User does not have access to specified group', \OCP\API::RESPOND_UNAUTHORISED); throw new OCSException('User does not have access to specified group', \OCP\API::RESPOND_UNAUTHORISED);

View File

@ -0,0 +1,137 @@
<?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;
}
}

View File

@ -12,6 +12,7 @@ declare(strict_types=1);
* @author Roeland Jago Douma <roeland@famdouma.nl> * @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Thomas Müller <thomas.mueller@tmit.eu> * @author Thomas Müller <thomas.mueller@tmit.eu>
* @author Tom Needham <tom@owncloud.com> * @author Tom Needham <tom@owncloud.com>
* @author John Molakvoæ <skjnldsv@protonmail.com>
* *
* @license AGPL-3.0 * @license AGPL-3.0
* *
@ -54,6 +55,8 @@ use OCP\Security\ISecureRandom;
class UsersController extends OCSController { class UsersController extends OCSController {
use UserDataTrait;
/** @var IUserManager */ /** @var IUserManager */
private $userManager; private $userManager;
/** @var IConfig */ /** @var IConfig */
@ -334,62 +337,6 @@ class UsersController extends OCSController {
throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
} }
/**
* 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 OCSException('The requested user could not be found', \OCP\API::RESPOND_NOT_FOUND);
}
// 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;
}
/** /**
* @NoAdminRequired * @NoAdminRequired
* @NoSubAdminRequired * @NoSubAdminRequired
@ -855,30 +802,6 @@ class UsersController extends OCSController {
} }
} }
/**
* 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 OCSException('User does not exist', 101);
}
// Get the subadmin groups
$subAdminGroups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($user);
$groups = [];
foreach ($subAdminGroups as $key => $group) {
$groups[] = $group->getGID();
}
return $groups;
}
/** /**
* Get the groups a user is a subadmin of * Get the groups a user is a subadmin of
* *
@ -891,38 +814,6 @@ class UsersController extends OCSController {
return new DataResponse($groups); return new DataResponse($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) {
// User fs is not setup yet
$user = $this->userManager->get($userId);
if ($user === null) {
throw new OCSException('User does not exist', 101);
}
$quota = OC_Helper::computerFileSize($user->getQuota());
$data = [
'quota' => $quota ? $quota : 'none',
'used' => 0
];
}
return $data;
}
/** /**
* @NoAdminRequired * @NoAdminRequired
* @PasswordConfirmationRequired * @PasswordConfirmationRequired