diff options
author | Robin Appelman <robin@icewind.nl> | 2017-06-12 15:24:55 +0200 |
---|---|---|
committer | Robin Appelman <robin@icewind.nl> | 2017-06-15 14:06:54 +0200 |
commit | 2e8e6f95b99151edc0b236e474b32f1760af006a (patch) | |
tree | 6229a1a35f1fdbc93b11520dece04b5997d92da8 /settings/Controller | |
parent | a74901fce17da6d88dbb82373fff523b834d692d (diff) | |
download | nextcloud-server-2e8e6f95b99151edc0b236e474b32f1760af006a.tar.gz nextcloud-server-2e8e6f95b99151edc0b236e474b32f1760af006a.zip |
show used space in user list
Signed-off-by: Robin Appelman <robin@icewind.nl>
Diffstat (limited to 'settings/Controller')
-rw-r--r-- | settings/Controller/UsersController.php | 105 |
1 files changed, 61 insertions, 44 deletions
diff --git a/settings/Controller/UsersController.php b/settings/Controller/UsersController.php index e217e189463..102eb615847 100644 --- a/settings/Controller/UsersController.php +++ b/settings/Controller/UsersController.php @@ -40,6 +40,7 @@ use OCP\AppFramework\Controller; use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Utility\ITimeFactory; use OCP\BackgroundJob\IJobList; +use OCP\Files\Config\IUserMountCache; use OCP\IConfig; use OCP\IGroupManager; use OCP\IL10N; @@ -53,6 +54,7 @@ use OCP\Mail\IMailer; use OCP\IAvatarManager; use OCP\Security\ICrypto; use OCP\Security\ISecureRandom; +use OCP\Util; /** * @package OC\Settings\Controller @@ -96,6 +98,8 @@ class UsersController extends Controller { private $keyManager; /** @var IJobList */ private $jobList; + /** @var IUserMountCache */ + private $userMountCache; /** * @param string $appName @@ -118,6 +122,7 @@ class UsersController extends Controller { * @param ICrypto $crypto * @param Manager $keyManager * @param IJobList $jobList + * @param IUserMountCache $userMountCache */ public function __construct($appName, IRequest $request, @@ -138,7 +143,8 @@ class UsersController extends Controller { ITimeFactory $timeFactory, ICrypto $crypto, Manager $keyManager, - IJobList $jobList) { + IJobList $jobList, + IUserMountCache $userMountCache) { parent::__construct($appName, $request); $this->userManager = $userManager; $this->groupManager = $groupManager; @@ -157,10 +163,11 @@ class UsersController extends Controller { $this->crypto = $crypto; $this->keyManager = $keyManager; $this->jobList = $jobList; + $this->userMountCache = $userMountCache; // check for encryption state - TODO see formatUserForIndex $this->isEncryptionAppEnabled = $appManager->isEnabledForUser('encryption'); - if($this->isEncryptionAppEnabled) { + if ($this->isEncryptionAppEnabled) { // putting this directly in empty is possible in PHP 5.5+ $result = $config->getAppValue('encryption', 'recoveryAdminEnabled', 0); $this->isRestoreEnabled = !empty($result); @@ -200,7 +207,7 @@ class UsersController extends Controller { } $subAdminGroups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($user); - foreach($subAdminGroups as $key => $subAdminGroup) { + foreach ($subAdminGroups as $key => $subAdminGroup) { $subAdminGroups[$key] = $subAdminGroup->getGID(); } @@ -222,6 +229,7 @@ class UsersController extends Controller { 'groups' => (empty($userGroups)) ? $this->groupManager->getUserGroupIds($user) : $userGroups, 'subadmin' => $subAdminGroups, 'quota' => $user->getQuota(), + 'quota_bytes' => Util::computerFileSize($user->getQuota()), 'storageLocation' => $user->getHome(), 'lastLogin' => $user->getLastLogin() * 1000, 'backend' => $user->getBackendClassName(), @@ -258,29 +266,31 @@ class UsersController extends Controller { */ public function index($offset = 0, $limit = 10, $gid = '', $pattern = '', $backend = '') { // Remove backends - if(!empty($backend)) { + if (!empty($backend)) { $activeBackends = $this->userManager->getBackends(); $this->userManager->clearBackends(); - foreach($activeBackends as $singleActiveBackend) { - if($backend === get_class($singleActiveBackend)) { + foreach ($activeBackends as $singleActiveBackend) { + if ($backend === get_class($singleActiveBackend)) { $this->userManager->registerBackend($singleActiveBackend); break; } } } + $userObjects = []; $users = []; if ($this->isAdmin) { - if($gid !== '' && $gid !== '_disabledUsers') { + if ($gid !== '' && $gid !== '_disabledUsers') { $batch = $this->getUsersForUID($this->groupManager->displayNamesInGroup($gid, $pattern, $limit, $offset)); } else { $batch = $this->userManager->search($pattern, $limit, $offset); } foreach ($batch as $user) { - if( ($gid !== '_disabledUsers' && $user->isEnabled()) || + if (($gid !== '_disabledUsers' && $user->isEnabled()) || ($gid === '_disabledUsers' && !$user->isEnabled()) ) { + $userObjects[] = $user; $users[] = $this->formatUserForIndex($user); } } @@ -295,17 +305,17 @@ class UsersController extends Controller { $subAdminOfGroups = $gids; // Set the $gid parameter to an empty value if the subadmin has no rights to access a specific group - if($gid !== '' && $gid !== '_disabledUsers' && !in_array($gid, $subAdminOfGroups)) { + if ($gid !== '' && $gid !== '_disabledUsers' && !in_array($gid, $subAdminOfGroups)) { $gid = ''; } // Batch all groups the user is subadmin of when a group is specified $batch = []; - if($gid === '') { - foreach($subAdminOfGroups as $group) { + if ($gid === '') { + foreach ($subAdminOfGroups as $group) { $groupUsers = $this->groupManager->displayNamesInGroup($group, $pattern, $limit, $offset); - foreach($groupUsers as $uid => $displayName) { + foreach ($groupUsers as $uid => $displayName) { $batch[$uid] = $displayName; } } @@ -320,14 +330,21 @@ class UsersController extends Controller { $this->groupManager->getUserGroupIds($user), $subAdminOfGroups )); - if( ($gid !== '_disabledUsers' && $user->isEnabled()) || + if (($gid !== '_disabledUsers' && $user->isEnabled()) || ($gid === '_disabledUsers' && !$user->isEnabled()) ) { + $userObjects[] = $user; $users[] = $this->formatUserForIndex($user, $userGroups); } } } + $usedSpace = $this->userMountCache->getUsedSpaceForUsers($userObjects); + + foreach ($users as &$userData) { + $userData['size'] = isset($usedSpace[$userData['name']]) ? $usedSpace[$userData['name']] : 0; + } + return new DataResponse($users); } @@ -341,8 +358,8 @@ class UsersController extends Controller { * @param string $email * @return DataResponse */ - public function create($username, $password, array $groups=[], $email='') { - if($email !== '' && !$this->mailer->validateMailAddress($email)) { + public function create($username, $password, array $groups = [], $email = '') { + if ($email !== '' && !$this->mailer->validateMailAddress($email)) { return new DataResponse( [ 'message' => (string)$this->l10n->t('Invalid mail address') @@ -357,7 +374,7 @@ class UsersController extends Controller { if (!empty($groups)) { foreach ($groups as $key => $group) { $groupObject = $this->groupManager->get($group); - if($groupObject === null) { + if ($groupObject === null) { unset($groups[$key]); continue; } @@ -411,18 +428,18 @@ class UsersController extends Controller { } return new DataResponse( [ - 'message' => (string) $message, + 'message' => (string)$message, ], Http::STATUS_FORBIDDEN ); } - if($user instanceof IUser) { - if($groups !== null) { - foreach($groups as $groupName) { + if ($user instanceof IUser) { + if ($groups !== null) { + foreach ($groups as $groupName) { $group = $this->groupManager->get($groupName); - if(empty($group)) { + if (empty($group)) { $group = $this->groupManager->createGroup($groupName); } $group->addUser($user); @@ -431,12 +448,12 @@ class UsersController extends Controller { /** * Send new user mail only if a mail is set */ - if($email !== '') { + if ($email !== '') { $user->setEMailAddress($email); try { $emailTemplate = $this->newUserMailHelper->generateTemplate($user, $generatePasswordResetToken); $this->newUserMailHelper->sendMail($user, $emailTemplate); - } catch(\Exception $e) { + } catch (\Exception $e) { $this->log->error("Can't send new user mail to $email: " . $e->getMessage(), ['app' => 'settings']); } } @@ -451,7 +468,7 @@ class UsersController extends Controller { return new DataResponse( [ - 'message' => (string) $this->l10n->t('Unable to create user.') + 'message' => (string)$this->l10n->t('Unable to create user.') ], Http::STATUS_FORBIDDEN ); @@ -469,19 +486,19 @@ class UsersController extends Controller { $userId = $this->userSession->getUser()->getUID(); $user = $this->userManager->get($id); - if($userId === $id) { + if ($userId === $id) { return new DataResponse( [ 'status' => 'error', 'data' => [ - 'message' => (string) $this->l10n->t('Unable to delete user.') + 'message' => (string)$this->l10n->t('Unable to delete user.') ] ], Http::STATUS_FORBIDDEN ); } - if(!$this->isAdmin && !$this->groupManager->getSubAdmin()->isUserAccessible($this->userSession->getUser(), $user)) { + if (!$this->isAdmin && !$this->groupManager->getSubAdmin()->isUserAccessible($this->userSession->getUser(), $user)) { return new DataResponse( [ 'status' => 'error', @@ -493,8 +510,8 @@ class UsersController extends Controller { ); } - if($user) { - if($user->delete()) { + if ($user) { + if ($user->delete()) { return new DataResponse( [ 'status' => 'success', @@ -527,10 +544,10 @@ class UsersController extends Controller { */ public function setEnabled($id, $enabled) { $enabled = (bool)$enabled; - if($enabled) { - $errorMsgGeneral = (string) $this->l10n->t('Error while enabling user.'); + if ($enabled) { + $errorMsgGeneral = (string)$this->l10n->t('Error while enabling user.'); } else { - $errorMsgGeneral = (string) $this->l10n->t('Error while disabling user.'); + $errorMsgGeneral = (string)$this->l10n->t('Error while disabling user.'); } $userId = $this->userSession->getUser()->getUID(); @@ -547,13 +564,13 @@ class UsersController extends Controller { ); } - if($user) { + if ($user) { if (!$this->isAdmin && !$this->groupManager->getSubAdmin()->isUserAccessible($this->userSession->getUser(), $user)) { return new DataResponse( [ 'status' => 'error', 'data' => [ - 'message' => (string) $this->l10n->t('Authentication error') + 'message' => (string)$this->l10n->t('Authentication error') ] ], Http::STATUS_FORBIDDEN @@ -714,7 +731,7 @@ class UsersController extends Controller { [ 'status' => 'error', 'data' => [ - 'message' => (string) $this->l10n->t('Invalid mail address') + 'message' => (string)$this->l10n->t('Invalid mail address') ] ], Http::STATUS_UNPROCESSABLE_ENTITY @@ -725,7 +742,7 @@ class UsersController extends Controller { $data = $this->accountManager->getUser($user); - $data[AccountManager::PROPERTY_AVATAR] = ['scope' => $avatarScope]; + $data[AccountManager::PROPERTY_AVATAR] = ['scope' => $avatarScope]; if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) { $data[AccountManager::PROPERTY_DISPLAYNAME] = ['value' => $displayname, 'scope' => $displaynameScope]; $data[AccountManager::PROPERTY_EMAIL] = ['value' => $email, 'scope' => $emailScope]; @@ -758,7 +775,7 @@ class UsersController extends Controller { 'websiteScope' => $data[AccountManager::PROPERTY_WEBSITE]['scope'], 'address' => $data[AccountManager::PROPERTY_ADDRESS]['value'], 'addressScope' => $data[AccountManager::PROPERTY_ADDRESS]['scope'], - 'message' => (string) $this->l10n->t('Settings saved') + 'message' => (string)$this->l10n->t('Settings saved') ] ], Http::STATUS_OK @@ -835,7 +852,7 @@ class UsersController extends Controller { $uniqueUsers = []; foreach ($groups as $group) { - foreach($group->getUsers() as $uid => $displayName) { + foreach ($group->getUsers() as $uid => $displayName) { $uniqueUsers[$uid] = true; } } @@ -929,19 +946,19 @@ class UsersController extends Controller { [ 'status' => 'error', 'data' => [ - 'message' => (string) $this->l10n->t('Forbidden') + 'message' => (string)$this->l10n->t('Forbidden') ] ], Http::STATUS_FORBIDDEN ); } - if($mailAddress !== '' && !$this->mailer->validateMailAddress($mailAddress)) { + if ($mailAddress !== '' && !$this->mailer->validateMailAddress($mailAddress)) { return new DataResponse( [ 'status' => 'error', 'data' => [ - 'message' => (string) $this->l10n->t('Invalid mail address') + 'message' => (string)$this->l10n->t('Invalid mail address') ] ], Http::STATUS_UNPROCESSABLE_ENTITY @@ -953,7 +970,7 @@ class UsersController extends Controller { [ 'status' => 'error', 'data' => [ - 'message' => (string) $this->l10n->t('Invalid user') + 'message' => (string)$this->l10n->t('Invalid user') ] ], Http::STATUS_UNPROCESSABLE_ENTITY @@ -966,7 +983,7 @@ class UsersController extends Controller { [ 'status' => 'error', 'data' => [ - 'message' => (string) $this->l10n->t('Unable to change mail address') + 'message' => (string)$this->l10n->t('Unable to change mail address') ] ], Http::STATUS_FORBIDDEN @@ -984,7 +1001,7 @@ class UsersController extends Controller { 'data' => [ 'username' => $id, 'mailAddress' => $mailAddress, - 'message' => (string) $this->l10n->t('Email saved') + 'message' => (string)$this->l10n->t('Email saved') ] ], Http::STATUS_OK |