Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 53 additions & 25 deletions apps/provisioning_api/lib/Controller/AUserData.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@
use OC\Group\Manager;
use OC\User\Backend;
use OC\User\NoUserException;
use OC_Helper;
use OCA\Provisioning_API\ResponseDefinitions;
use OCP\Accounts\IAccountManager;
use OCP\Accounts\PropertyDoesNotExistException;
use OCP\AppFramework\Http;
use OCP\AppFramework\OCS\OCSException;
use OCP\AppFramework\OCS\OCSNotFoundException;
use OCP\AppFramework\OCSController;
use OCP\Files\FileInfo;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\IConfig;
use OCP\IGroupManager;
Expand All @@ -52,6 +53,7 @@
use OCP\L10N\IFactory;
use OCP\User\Backend\ISetDisplayNameBackend;
use OCP\User\Backend\ISetPasswordBackend;
use OCP\Util;

/**
* @psalm-import-type Provisioning_APIUserDetails from ResponseDefinitions
Expand Down Expand Up @@ -251,34 +253,62 @@ protected function getUserSubAdminGroupsData(string $userId): array {
}

/**
* @param string $userId
* @param IUser $user
* @return Provisioning_APIUserDetailsQuota
* @throws OCSException
*/
protected function fillStorageInfo(string $userId): array {
try {
\OC_Util::tearDownFS();
\OC_Util::setupFS($userId);
$storage = OC_Helper::getStorageInfo('/', null, true, false);
$data = [
'free' => $storage['free'],
'used' => $storage['used'],
'total' => $storage['total'],
'relative' => $storage['relative'],
self::USER_FIELD_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);
protected function fillStorageInfo(IUser $user): array {
$includeExternal = $this->config->getSystemValueBool('quota_include_external_storage');
$userId = $user->getUID();

$quota = $user->getQuota();
if ($quota === 'none') {
$quota = FileInfo::SPACE_UNLIMITED;
} else {
$quota = Util::computerFileSize($quota);
if ($quota === false) {
$quota = FileInfo::SPACE_UNLIMITED;
}
$quota = $user->getQuota();
if ($quota !== 'none') {
$quota = OC_Helper::computerFileSize($quota);
}

try {
if ($includeExternal) {
\OC_Util::tearDownFS();
\OC_Util::setupFS($user->getUID());
$storage = \OC_Helper::getStorageInfo('/', null, true, false);
$data = [
'free' => $storage['free'],
'used' => $storage['used'],
'total' => $storage['total'],
'relative' => $storage['relative'],
self::USER_FIELD_QUOTA => $storage['quota'],
];
} else {
$userFileInfo = $this->rootFolder->getUserFolder($userId)->getStorage()->getCache()->get('');
$used = $userFileInfo->getSize();

if ($quota > 0) {
// prevent division by zero or error codes (negative values)
$relative = round(($used / $quota) * 10000) / 100;
$free = $quota - $used;
$total = $quota;
} else {
$relative = 0;
$free = FileInfo::SPACE_UNLIMITED;
$total = FileInfo::SPACE_UNLIMITED;
}

$data = [
'free' => $free,
'used' => $used,
'total' => $total,
'relative' => $relative,
self::USER_FIELD_QUOTA => $quota,
];
}
} catch (NotFoundException $ex) {
$data = [
self::USER_FIELD_QUOTA => $quota !== false ? $quota : 'none',
self::USER_FIELD_QUOTA => $quota >= 0 ? $quota : 'none',
'used' => 0
];
} catch (\Exception $e) {
Expand All @@ -290,8 +320,6 @@ protected function fillStorageInfo(string $userId): array {
'exception' => $e,
]
);
/* In case the Exception left things in a bad state */
\OC_Util::tearDownFS();
return [];
}
return $data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class GroupsControllerTest extends \Test\TestCase {
/** @var GroupsController|\PHPUnit\Framework\MockObject\MockObject */
protected $api;

private IRootFolder $rootFolder;


protected function setUp(): void {
parent::setUp();
Expand All @@ -78,6 +80,7 @@ protected function setUp(): void {
$this->accountManager = $this->createMock(IAccountManager::class);
$this->l10nFactory = $this->createMock(IFactory::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->rootFolder = $this->createMock(IRootFolder::class);

$this->subAdminManager = $this->createMock(SubAdmin::class);

Expand All @@ -95,6 +98,7 @@ protected function setUp(): void {
$this->userSession,
$this->accountManager,
$this->l10nFactory,
$this->rootFolder,
$this->logger
])
->setMethods(['fillStorageInfo'])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class UsersControllerTest extends TestCase {
private $knownUserService;
/** @var IEventDispatcher|MockObject */
private $eventDispatcher;
private IRootFolder $rootFolder;
/** @var IPhoneNumberUtil */
private $phoneNumberUtil;

Expand All @@ -128,6 +129,7 @@ protected function setUp(): void {
$this->knownUserService = $this->createMock(KnownUserService::class);
$this->eventDispatcher = $this->createMock(IEventDispatcher::class);
$this->phoneNumberUtil = new PhoneNumberUtil();
$this->rootFolder = $this->createMock(IRootFolder::class);

$this->api = $this->getMockBuilder(UsersController::class)
->setConstructorArgs([
Expand Down Expand Up @@ -1157,7 +1159,7 @@ public function testGetUserDataAsAdmin() {
$this->api
->expects($this->once())
->method('fillStorageInfo')
->with('UID')
->with($targetUser)
->willReturn(['DummyValue']);

$backend = $this->createMock(UserInterface::class);
Expand Down Expand Up @@ -1287,7 +1289,7 @@ public function testGetUserDataAsSubAdminAndUserIsAccessible() {
$this->api
->expects($this->once())
->method('fillStorageInfo')
->with('UID')
->with($targetUser)
->willReturn(['DummyValue']);

$backend = $this->createMock(UserInterface::class);
Expand Down Expand Up @@ -1464,7 +1466,7 @@ public function testGetUserDataAsSubAdminSelfLookup() {
$this->api
->expects($this->once())
->method('fillStorageInfo')
->with('UID')
->with($targetUser)
->willReturn(['DummyValue']);

$backend = $this->createMock(UserInterface::class);
Expand Down