-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Api: return users details by groups #8904
Merged
+349
−220
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
68ae439
Api, return users details by groups
skjnldsv f4e84e1
Limit and offset
skjnldsv 4011ea9
Cast types directly
skjnldsv b023bfe
Fixed tests
skjnldsv c103aaa
Trait bump
skjnldsv 762002e
Fixed tests
skjnldsv 348171f
Use abstract subclass so members can be defined
nickvergessen eb4d7fb
Rebase to master
skjnldsv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
<?php | ||
declare(strict_types=1); | ||
/** | ||
* @copyright Copyright (c) 2018 John Molakvoæ (skjnldsv) <[email protected]> | ||
* | ||
* @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\OCS\OCSException; | ||
use OCP\AppFramework\OCS\OCSNotFoundException; | ||
use OCP\AppFramework\OCSController; | ||
use OCP\Files\NotFoundException; | ||
use OC_Helper; | ||
use OCP\IConfig; | ||
use OCP\IGroupManager; | ||
use OCP\IRequest; | ||
use OCP\IUserManager; | ||
use OCP\IUserSession; | ||
|
||
abstract class AUserData extends OCSController { | ||
|
||
/** @var IUserManager */ | ||
protected $userManager; | ||
/** @var IConfig */ | ||
protected $config; | ||
/** @var IGroupManager|\OC\Group\Manager */ // FIXME Requires a method that is not on the interface | ||
protected $groupManager; | ||
/** @var IUserSession */ | ||
protected $userSession; | ||
/** @var AccountManager */ | ||
protected $accountManager; | ||
|
||
/** | ||
* @param string $appName | ||
* @param IRequest $request | ||
* @param IUserManager $userManager | ||
* @param IConfig $config | ||
* @param IGroupManager $groupManager | ||
* @param IUserSession $userSession | ||
* @param AccountManager $accountManager | ||
*/ | ||
public function __construct(string $appName, | ||
IRequest $request, | ||
IUserManager $userManager, | ||
IConfig $config, | ||
IGroupManager $groupManager, | ||
IUserSession $userSession, | ||
AccountManager $accountManager) { | ||
parent::__construct($appName, $request); | ||
|
||
$this->userManager = $userManager; | ||
$this->config = $config; | ||
$this->groupManager = $groupManager; | ||
$this->userSession = $userSession; | ||
$this->accountManager = $accountManager; | ||
} | ||
|
||
/** | ||
* creates a array with all user data | ||
* | ||
* @param $userId | ||
* @return array | ||
* @throws OCSException | ||
*/ | ||
protected 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) { | ||
// 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; | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fallback to proper url design, above one is deprecated!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a note about this to #7827? But I guess it's not easily possible to drop this as it is part of the OCS standard 😉