Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.
TypeScript cloud-alternativecollaborationcollaborative-editingdropboxfile-serverfile-sharingfile-storagefile-syncfull-text-searchhomelabnextcloudon-premiseopen-sourceprivacy-firstprivate-cloudrealtimesecureself-hostedstorage-serverwebdav
fix(backend:files): filter recents by user permissions
johaven406881fa149c
diff --git a/backend/src/applications/files/services/files-queries.service.ts b/backend/src/applications/files/services/files-queries.service.tsindex c1424d6a..66525039 100644--- a/backend/src/applications/files/services/files-queries.service.ts+++ b/backend/src/applications/files/services/files-queries.service.ts@@ -281,7 +281,15 @@ export class FilesQueries {}}- getRecentsFromUser(userId: number, spaceIds: number[], shareIds: number[], limit = 10): Promise<FileRecent[]> {+ getRecentsFromUser(userId: number | undefined, spaceIds: number[], shareIds: number[], limit = 10): Promise<FileRecent[]> {+ const where: SQL[] = [+ ...(userId !== undefined ? [eq(filesRecents.ownerId, userId)] : []),+ ...(spaceIds.length ? [inArray(filesRecents.spaceId, spaceIds)] : []),+ ...(shareIds.length ? [inArray(filesRecents.shareId, shareIds)] : [])+ ]+ if (!where.length) {+ return Promise.resolve([])+ }return this.db.select({id: filesRecents.id,@@ -294,7 +302,7 @@ export class FilesQueries {mtime: filesRecents.mtime} satisfies FileRecent | SelectedFields<any, any>).from(filesRecents)- .where(or(eq(filesRecents.ownerId, userId), inArray(filesRecents.spaceId, spaceIds), inArray(filesRecents.shareId, shareIds)))+ .where(or(...where)).groupBy(filesRecents.id).orderBy(desc(filesRecents.mtime)).limit(limit)diff --git a/backend/src/applications/files/services/files-recents.service.spec.ts b/backend/src/applications/files/services/files-recents.service.spec.tsindex 27cd612f..b71289b3 100644--- a/backend/src/applications/files/services/files-recents.service.spec.ts+++ b/backend/src/applications/files/services/files-recents.service.spec.ts@@ -3,6 +3,7 @@ import { convertHumanTimeToMs } from '../../../common/functions'import { currentTimeStamp } from '../../../common/shared'import { SharesQueries } from '../../shares/services/shares-queries.service'import { SpacesQueries } from '../../spaces/services/spaces-queries.service'+import { USER_PERMISSION } from '../../users/constants/user'import { FilesQueries } from './files-queries.service'import { FilesRecents } from './files-recents.service'import { Mock } from 'vitest'@@ -53,6 +54,15 @@ describe(FilesRecents.name, () => {vi.clearAllMocks()})+ const userWithPermissions = (applications: USER_PERMISSION[] = [], props: Record<string, any> = {}) =>+ ({+ id: 7,+ isAdmin: false,+ applications,+ havePermission: (permission: USER_PERMISSION) => Boolean(props.isAdmin) || applications.includes(permission),+ ...props+ }) as any+it('should be defined', () => {expect(service).toBeDefined()})@@ -63,7 +73,10 @@ describe(FilesRecents.name, () => {sharesQueries.shareIds.mockResolvedValueOnce([20])filesQueries.getRecentsFromUser.mockResolvedValueOnce(recents)- const result = await service.getRecents({ id: 7, isAdmin: true } as any, 25)+ const result = await service.getRecents(+ userWithPermissions([USER_PERMISSION.PERSONAL_SPACE, USER_PERMISSION.SPACES, USER_PERMISSION.SHARES], { isAdmin: true }),+ 25+ )expect(spacesQueries.spaceIds).toHaveBeenCalledWith(7)expect(sharesQueries.shareIds).toHaveBeenCalledWith(7, 1)@@ -71,6 +84,42 @@ describe(FilesRecents.name, () => {expect(result).toBe(recents)})+ it('should only load personal recents when user only has personal space permission', async () => {+ await service.getRecents(userWithPermissions([USER_PERMISSION.PERSONAL_SPACE]), 10)++ expect(spacesQueries.spaceIds).not.toHaveBeenCalled()+ expect(sharesQueries.shareIds).not.toHaveBeenCalled()+ expect(filesQueries.getRecentsFromUser).toHaveBeenCalledWith(7, [], [], 10)+ })++ it('should only load space recents when user only has spaces permission', async () => {+ spacesQueries.spaceIds.mockResolvedValueOnce([10])++ await service.getRecents(userWithPermissions([USER_PERMISSION.SPACES]), 10)++ expect(spacesQueries.spaceIds).toHaveBeenCalledWith(7)+ expect(sharesQueries.shareIds).not.toHaveBeenCalled()+ expect(filesQueries.getRecentsFromUser).toHaveBeenCalledWith(undefined, [10], [], 10)+ })++ it('should only load share recents when user only has shares permission', async () => {+ sharesQueries.shareIds.mockResolvedValueOnce([20])++ await service.getRecents(userWithPermissions([USER_PERMISSION.SHARES]), 10)++ expect(spacesQueries.spaceIds).not.toHaveBeenCalled()+ expect(sharesQueries.shareIds).toHaveBeenCalledWith(7, 0)+ expect(filesQueries.getRecentsFromUser).toHaveBeenCalledWith(undefined, [], [20], 10)+ })++ it('should not load any recents source without matching permission', async () => {+ await service.getRecents(userWithPermissions(), 10)++ expect(spacesQueries.spaceIds).not.toHaveBeenCalled()+ expect(sharesQueries.shareIds).not.toHaveBeenCalled()+ expect(filesQueries.getRecentsFromUser).toHaveBeenCalledWith(undefined, [], [], 10)+ })+it('should ignore updateRecents when browsing trash repository', async () => {await service.updateRecents({ id: 5 } as any, { inTrashRepository: true, url: 'trash/personal' } as any, [])diff --git a/backend/src/applications/files/services/files-recents.service.ts b/backend/src/applications/files/services/files-recents.service.tsindex d41a7346..eaf6a8eb 100644--- a/backend/src/applications/files/services/files-recents.service.ts+++ b/backend/src/applications/files/services/files-recents.service.ts@@ -4,6 +4,7 @@ import { currentTimeStamp } from '../../../common/shared'import { SharesQueries } from '../../shares/services/shares-queries.service'import { SpaceEnv } from '../../spaces/models/space-env.model'import { SpacesQueries } from '../../spaces/services/spaces-queries.service'+import { USER_PERMISSION } from '../../users/constants/user'import { UserModel } from '../../users/models/user.model'import { FileProps } from '../interfaces/file-props.interface'import { FileRecentLocation } from '../interfaces/file-recent-location.interface'@@ -21,8 +22,12 @@ export class FilesRecents {) {}async getRecents(user: UserModel, limit: number): Promise<FileRecent[]> {- const [spaceIds, shareIds] = await Promise.all([this.spacesQueries.spaceIds(user.id), this.sharesQueries.shareIds(user.id, +user.isAdmin)])- return this.filesQueries.getRecentsFromUser(user.id, spaceIds, shareIds, limit)+ const [spaceIds, shareIds] = await Promise.all([+ user.havePermission(USER_PERMISSION.SPACES) ? this.spacesQueries.spaceIds(user.id) : Promise.resolve([]),+ user.havePermission(USER_PERMISSION.SHARES) ? this.sharesQueries.shareIds(user.id, +user.isAdmin) : Promise.resolve([])+ ])+ const ownerId = user.havePermission(USER_PERMISSION.PERSONAL_SPACE) ? user.id : undefined+ return this.filesQueries.getRecentsFromUser(ownerId, spaceIds, shareIds, limit)}async updateRecents(user: UserModel, space: SpaceEnv, files: FileProps[]): Promise<void> {