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>
  • Loading branch information
provokateurin committed Oct 8, 2024
1 parent de45b72 commit 455ec5f
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 14 deletions.
34 changes: 24 additions & 10 deletions lib/Folder/FolderManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
use Psr\Log\LoggerInterface;

class FolderManager {
public const SPACE_DEFAULT = -4;

public function __construct(
private IDBConnection $connection,
private IGroupManager $groupManager,
Expand Down Expand Up @@ -64,7 +66,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 @@ -120,7 +122,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 @@ -163,7 +165,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 @@ -270,7 +272,7 @@ public function getFolder(int $id, int $rootStorageId = 0): ?array {
'id' => $id,
'mount_point' => (string)$row['mount_point'],
'groups' => $applicableMap[$id] ?? [],
'quota' => (int)$row['quota'],
'quota' => $this->getRealQuota((int)$row['quota']),
'size' => $row['size'] ?: 0,
'acl' => (bool)$row['acl'],
'manage' => $this->getManageAcl($folderMappings)
Expand Down Expand Up @@ -491,7 +493,7 @@ public function getFoldersForGroup(string $groupId, int $rootStorageId = 0): arr
'folder_id' => (int)$folder['folder_id'],
'mount_point' => (string)$folder['mount_point'],
'permissions' => (int)$folder['group_permissions'],
'quota' => (int)$folder['quota'],
'quota' => $this->getRealQuota((int)$folder['quota']),
'acl' => (bool)$folder['acl'],
'rootCacheEntry' => (isset($folder['fileid'])) ? Cache::cacheEntryFromData($folder, $this->mimeTypeLoader) : null
], $result));
Expand Down Expand Up @@ -546,7 +548,7 @@ public function getFoldersForGroups(array $groupIds, int $rootStorageId = 0): ar
'folder_id' => (int)$folder['folder_id'],
'mount_point' => (string)$folder['mount_point'],
'permissions' => (int)$folder['group_permissions'],
'quota' => (int)$folder['quota'],
'quota' => $this->getRealQuota((int)$folder['quota']),
'acl' => (bool)$folder['acl'],
'rootCacheEntry' => (isset($folder['fileid'])) ? Cache::cacheEntryFromData($folder, $this->mimeTypeLoader) : null
], $result);
Expand Down Expand Up @@ -606,7 +608,7 @@ public function getFoldersFromCircleMemberships(IUser $user, int $rootStorageId
'folder_id' => (int)$folder['folder_id'],
'mount_point' => (string)$folder['mount_point'],
'permissions' => (int)$folder['group_permissions'],
'quota' => (int)$folder['quota'],
'quota' => $this->getRealQuota((int)$folder['quota']),
'acl' => (bool)$folder['acl'],
'rootCacheEntry' => (isset($folder['fileid'])) ? Cache::cacheEntryFromData($folder, $this->mimeTypeLoader) : null
], $query->executeQuery()->fetchAll());
Expand All @@ -617,14 +619,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 @@ -913,4 +913,18 @@ public function getCirclesManager(): ?CirclesManager {
return null;
}
}

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 @@ -16,9 +16,11 @@ import AsyncSelect from 'react-select/async'
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
74 changes: 70 additions & 4 deletions tests/Folder/FolderManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,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(
Server::get(IDBConnection::class),
$this->groupManager,
Expand Down Expand Up @@ -89,6 +85,11 @@ private function assertHasFolders(array $folders): void {
}

public function testCreateFolder(): void {
$this->config->expects($this->any())
->method('getSystemValueInt')
->with('groupfolders.quota.default', FileInfo::SPACE_UNLIMITED)
->willReturn(FileInfo::SPACE_UNLIMITED);

$this->manager->createFolder('foo');

$this->assertHasFolders([
Expand All @@ -97,6 +98,11 @@ public function testCreateFolder(): void {
}

public function testSetMountpoint(): void {
$this->config->expects($this->any())
->method('getSystemValueInt')
->with('groupfolders.quota.default', FileInfo::SPACE_UNLIMITED)
->willReturn(FileInfo::SPACE_UNLIMITED);

$folderId1 = $this->manager->createFolder('foo');
$this->manager->createFolder('bar');

Expand All @@ -109,6 +115,11 @@ public function testSetMountpoint(): void {
}

public function testAddApplicable(): void {
$this->config->expects($this->any())
->method('getSystemValueInt')
->with('groupfolders.quota.default', FileInfo::SPACE_UNLIMITED)
->willReturn(FileInfo::SPACE_UNLIMITED);

$folderId1 = $this->manager->createFolder('foo');
$folderId2 = $this->manager->createFolder('bar');
$this->manager->addApplicableGroup($folderId1, 'g1');
Expand Down Expand Up @@ -154,6 +165,11 @@ public function testAddApplicable(): void {
}

public function testSetPermissions(): void {
$this->config->expects($this->any())
->method('getSystemValueInt')
->with('groupfolders.quota.default', FileInfo::SPACE_UNLIMITED)
->willReturn(FileInfo::SPACE_UNLIMITED);

$folderId1 = $this->manager->createFolder('foo');
$this->manager->addApplicableGroup($folderId1, 'g1');
$this->manager->addApplicableGroup($folderId1, 'g2');
Expand Down Expand Up @@ -182,6 +198,11 @@ public function testSetPermissions(): void {
}

public function testRemoveApplicable(): void {
$this->config->expects($this->any())
->method('getSystemValueInt')
->with('groupfolders.quota.default', FileInfo::SPACE_UNLIMITED)
->willReturn(FileInfo::SPACE_UNLIMITED);

$folderId1 = $this->manager->createFolder('foo');
$folderId2 = $this->manager->createFolder('bar');
$this->manager->addApplicableGroup($folderId1, 'g1');
Expand Down Expand Up @@ -225,6 +246,11 @@ public function testRemoveApplicable(): void {
}

public function testRemoveFolder(): void {
$this->config->expects($this->any())
->method('getSystemValueInt')
->with('groupfolders.quota.default', FileInfo::SPACE_UNLIMITED)
->willReturn(FileInfo::SPACE_UNLIMITED);

$folderId1 = $this->manager->createFolder('foo');
$this->manager->createFolder('bar');

Expand All @@ -236,6 +262,11 @@ public function testRemoveFolder(): void {
}

public function testRenameFolder(): void {
$this->config->expects($this->any())
->method('getSystemValueInt')
->with('groupfolders.quota.default', FileInfo::SPACE_UNLIMITED)
->willReturn(FileInfo::SPACE_UNLIMITED);

$folderId1 = $this->manager->createFolder('foo');
$this->manager->createFolder('other');

Expand All @@ -248,6 +279,11 @@ public function testRenameFolder(): void {
}

public function testSetACL(): void {
$this->config->expects($this->any())
->method('getSystemValueInt')
->with('groupfolders.quota.default', FileInfo::SPACE_UNLIMITED)
->willReturn(FileInfo::SPACE_UNLIMITED);

$folderId1 = $this->manager->createFolder('foo');
$this->manager->createFolder('other');

Expand All @@ -267,6 +303,11 @@ public function testSetACL(): void {
}

public function testGetFoldersForGroup(): void {
$this->config->expects($this->any())
->method('getSystemValueInt')
->with('groupfolders.quota.default', FileInfo::SPACE_UNLIMITED)
->willReturn(FileInfo::SPACE_UNLIMITED);

$folderId1 = $this->manager->createFolder('foo');
$this->manager->addApplicableGroup($folderId1, 'g1');
$this->manager->addApplicableGroup($folderId1, 'g2');
Expand All @@ -280,6 +321,11 @@ public function testGetFoldersForGroup(): void {
}

public function testGetFoldersForGroups(): void {
$this->config->expects($this->any())
->method('getSystemValueInt')
->with('groupfolders.quota.default', FileInfo::SPACE_UNLIMITED)
->willReturn(FileInfo::SPACE_UNLIMITED);

$folderId1 = $this->manager->createFolder('foo');
$this->manager->addApplicableGroup($folderId1, 'g1');
$this->manager->addApplicableGroup($folderId1, 'g2');
Expand Down Expand Up @@ -400,4 +446,24 @@ public function testGetFolderPermissionsForUserMerge(): void {
$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 455ec5f

Please sign in to comment.