Skip to content

Commit

Permalink
fix(FolderManager): Use placeholder value for default quota instead o…
Browse files Browse the repository at this point in the history
…f the current value on creation

Signed-off-by: provokateurin <kate@provokateurin.de>

[skip ci]
  • Loading branch information
provokateurin authored and backportbot[bot] committed Oct 8, 2024
1 parent cd686c6 commit bae01aa
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
24 changes: 18 additions & 6 deletions lib/Folder/FolderManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function getAllFolders(): array {
'id' => $id,
'mount_point' => $row['mount_point'],
'groups' => $applicableMap[$id] ?? [],
'quota' => (int)$row['quota'],
'quota' => $this->getRealQuota((int)$row['quota']),
'size' => 0,
'acl' => (bool)$row['acl']
];
Expand Down Expand Up @@ -126,7 +126,7 @@ public function getAllFoldersWithSize(int $rootStorageId): array {
'id' => $id,
'mount_point' => $row['mount_point'],
'groups' => $applicableMap[$id] ?? [],
'quota' => (int)$row['quota'],
'quota' => $this->getRealQuota((int)$row['quota']),
'size' => $row['size'] ? (int)$row['size'] : 0,
'acl' => (bool)$row['acl'],
'manage' => $this->getManageAcl($mappings)
Expand Down Expand Up @@ -171,7 +171,7 @@ public function getAllFoldersForUserWithSize(int $rootStorageId, IUser $user): a
'id' => $id,
'mount_point' => $row['mount_point'],
'groups' => $applicableMap[$id] ?? [],
'quota' => (int)$row['quota'],
'quota' => $this->getRealQuota((int)$row['quota']),
'size' => $row['size'] ? (int)$row['size'] : 0,
'acl' => (bool)$row['acl'],
'manage' => $this->getManageAcl($mappings)
Expand Down Expand Up @@ -657,14 +657,12 @@ public function getFoldersFromCircleMemberships(IUser $user, int $rootStorageId
* @throws Exception
*/
public function createFolder(string $mountPoint): int {
$defaultQuota = $this->config->getSystemValueInt('groupfolders.quota.default', FileInfo::SPACE_UNLIMITED);

$query = $this->connection->getQueryBuilder();

$query->insert('group_folders')
->values([
'mount_point' => $query->createNamedParameter($mountPoint),
'quota' => $defaultQuota,
'quota' => self::SPACE_DEFAULT,
]);
$query->executeStatement();
$id = $query->getLastInsertId();
Expand Down Expand Up @@ -971,4 +969,18 @@ public function isCirclesAvailable(?CirclesManager &$circlesManager = null): boo

return true;
}

private function getRealQuota(int $quota): int {
if ($quota === self::SPACE_DEFAULT) {
$defaultQuota = $this->config->getSystemValueInt('groupfolders.quota.default', FileInfo::SPACE_UNLIMITED);
// Prevent setting the default quota option to be the default quota value creating an unresolvable self reference
if ($defaultQuota <= 0 && $defaultQuota !== FileInfo::SPACE_UNLIMITED) {
throw new \Exception('Default Groupfolder quota value ' . $defaultQuota . ' is not allowed');
}

return $defaultQuota;
}

return $quota;
}
}
2 changes: 2 additions & 0 deletions src/settings/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ import Thenable = JQuery.Thenable;
import AdminGroupSelect from './AdminGroupSelect'
import SubAdminGroupSelect from './SubAdminGroupSelect'
import { loadState } from '@nextcloud/initial-state'
import { t } from '@nextcloud/l10n'

const bytesInOneGibibyte = Math.pow(1024, 3)
const defaultQuotaOptions = {
Default: -4,
'1 GB': bytesInOneGibibyte,
'5 GB': bytesInOneGibibyte * 5,
'10 GB': bytesInOneGibibyte * 10,
Expand Down
24 changes: 20 additions & 4 deletions tests/Folder/FolderManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ protected function setUp(): void {
$this->logger = $this->createMock(LoggerInterface::class);
$this->eventDispatcher = $this->createMock(IEventDispatcher::class);
$this->config = $this->createMock(IConfig::class);
$this->config->expects($this->any())
->method('getSystemValueInt')
->with('groupfolders.quota.default', FileInfo::SPACE_UNLIMITED)
->willReturn(FileInfo::SPACE_UNLIMITED);
$this->manager = new FolderManager(
\OC::$server->getDatabaseConnection(),
$this->groupManager,
Expand Down Expand Up @@ -403,4 +399,24 @@ public function testGetFolderPermissionsForUserMerge() {
$permissions = $manager->getFolderPermissionsForUser($this->getUser(['g1', 'g2', 'g3']), 2);
$this->assertEquals(0, $permissions);
}

public function testQuotaDefaultValue(): void {
$folderId1 = $this->manager->createFolder('foo');

$exponent = 3;
$this->config->expects($this->any())
->method('getSystemValueInt')
->with('groupfolders.quota.default', FileInfo::SPACE_UNLIMITED)
->willReturnCallback(function () use (&$exponent) {
return 1024 ** ($exponent++);
});

/** @var array $folder */
$folder = $this->manager->getFolder($folderId1);
$this->assertEquals(1024 ** 3, $folder['quota']);

/** @var array $folder */
$folder = $this->manager->getFolder($folderId1);
$this->assertEquals(1024 ** 4, $folder['quota']);
}
}

0 comments on commit bae01aa

Please sign in to comment.