Skip to content

Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.

fix(backend:shares): centralize nested external storage scope resolution

johaven5c6c01199d3f
diff --git a/backend/src/applications/comments/services/comments-manager.service.ts b/backend/src/applications/comments/services/comments-manager.service.ts
index a715ddfc..49d98384 100644
--- a/backend/src/applications/comments/services/comments-manager.service.ts
+++ b/backend/src/applications/comments/services/comments-manager.service.ts
@@ -36,7 +36,7 @@ export class CommentsManager {
// If path is empty a file with path = '.' and name = '.' will be created
// The space browser does not support this kind of file and will remove it
// Maybe to implement later
- throw new HttpException(`Not supported on this kind of ${space.dbFile.spaceExternalRootId ? 'space root' : 'share'}`, HttpStatus.BAD_REQUEST)
+ throw new HttpException('Not supported on this kind of location', HttpStatus.BAD_REQUEST)
}
let fileId: number
if (createCommentDto.fileId > 0) {
diff --git a/backend/src/applications/comments/services/comments-queries.service.ts b/backend/src/applications/comments/services/comments-queries.service.ts
index fe47b562..2360fdfa 100644
--- a/backend/src/applications/comments/services/comments-queries.service.ts
+++ b/backend/src/applications/comments/services/comments-queries.service.ts
@@ -7,6 +7,7 @@ import { dbCheckAffectedRows, dbGetInsertedId } from '../../../infrastructure/da
import { filePathSQL, files } from '../../files/schemas/files.schema'
import { UserMailNotification } from '../../notifications/interfaces/user-mail-notification.interface'
import { shares } from '../../shares/schemas/shares.schema'
+import { externalShareScopeSQL } from '../../shares/utils/external-share-scope.sql'
import { SharesQueries } from '../../shares/services/shares-queries.service'
import { SPACE_ALIAS, SPACE_REPOSITORY } from '../../spaces/constants/spaces'
import { spacesRoots } from '../../spaces/schemas/spaces-roots.schema'
@@ -40,7 +41,7 @@ export class CommentsQueries {
return this.db
.select({
...getTableColumns(comments),
- author: { login: users.login, fullName: userFullNameSQL(users), email: users.email, isAuthor: sql`${users.id} = ${userId}`.mapWith(Boolean) },
+ author: { login: users.login, fullName: userFullNameSQL(users), email: users.email, isAuthor: eq(users.id, userId).mapWith(Boolean) },
isFileOwner: sql`${+isFileOwner}`.mapWith(Boolean)
})
.from(comments)
@@ -100,6 +101,13 @@ export class CommentsQueries {
getRecentsFromShares(userId: number, shareIds: number[], limit: number) {
const shareFile: any = alias(files, 'shareFile')
+ const externalShareTargets = this.db
+ .select({ id: shares.id, parentId: shares.parentId, externalPath: shares.externalPath })
+ .from(shares)
+ .where(and(isNull(shares.fileId), isNotNull(shares.externalPath), inArray(shares.id, shareIds)))
+ const externalShareScope = externalShareScopeSQL(externalShareTargets, 'commentExternalShareScope', {
+ oneTargetPerStorage: true
+ })
return this.db
.select({
id: comments.id,
@@ -126,13 +134,14 @@ export class CommentsQueries {
.leftJoin(shareFile, eq(shareFile.id, shares.fileId))
.leftJoin(spaces, eq(spaces.id, shareFile.spaceId))
.leftJoin(spacesRoots, eq(spacesRoots.spaceId, spaces.id))
+ .leftJoin(externalShareScope.table, eq(externalShareScope.targetShareId, shares.id))
.leftJoin(
files,
or(
// file linked to the share
eq(files.id, shareFile.id),
// all files with an external share id
- and(isNull(shareFile.id), eq(files.shareExternalId, shares.id)),
+ and(isNull(shareFile.id), eq(files.shareExternalId, externalShareScope.storageShareId)),
// all files under the share
and(
isNotNull(shareFile.id),
diff --git a/backend/src/applications/shares/services/shares-queries.service.ts b/backend/src/applications/shares/services/shares-queries.service.ts
index 09d068f4..4b38d98c 100644
--- a/backend/src/applications/shares/services/shares-queries.service.ts
+++ b/backend/src/applications/shares/services/shares-queries.service.ts
@@ -20,8 +20,10 @@ import {
import { fileHasCommentsSubquerySQL } from '../../comments/schemas/comments.schema'
import type { FileProps } from '../../files/interfaces/file-props.interface'
import type { FileSpace } from '../../files/interfaces/file-space.interface'
+import { filesFavorites } from '../../files/schemas/files-favorites.schema'
import { filePathSQL, files } from '../../files/schemas/files.schema'
import { links } from '../../links/schemas/links.schema'
+import type { SpaceBrowseDetails } from '../../spaces/interfaces/space-files.interface'
import { SpaceEnv } from '../../spaces/models/space-env.model'
import { spacesRoots } from '../../spaces/schemas/spaces-roots.schema'
import { spaceGroupConcatPermissions, spaces } from '../../spaces/schemas/spaces.schema'
@@ -47,6 +49,7 @@ import type { ShareMembers } from '../schemas/share-members.interface'
import type { Share } from '../schemas/share.interface'
import { sharesMembers } from '../schemas/shares-members.schema'
import { shares } from '../schemas/shares.schema'
+import { externalShareScopeSQL } from '../utils/external-share-scope.sql'
@Injectable()
export class SharesQueries {
@@ -704,37 +707,7 @@ export class SharesQueries {
// For A -> B -> C, the result maps A, B and C to the storage scope A.
// A missing or non-external parent stops the traversal, leaving the
// malformed external share without a scope so it can be filtered below.
- const externalRootScope = sql`
- (
- WITH RECURSIVE targetShares (id, parentId, externalPath) AS
- (${externalShareTargets}),
- ancestors (targetId, id, parentId, externalPath) AS
- (SELECT targetShares.id,
- targetShares.id,
- targetShares.parentId,
- targetShares.externalPath
- FROM targetShares
- UNION
- SELECT ancestors.targetId,
- ${externalParentShare.id},
- ${externalParentShare.parentId},
- ${externalParentShare.externalPath}
- FROM ${shares} AS externalParentShare
- INNER JOIN ancestors ON ${externalParentShare.id} = ancestors.parentId
- WHERE ${externalParentShare.externalPath} IS NOT NULL),
- rootShare (targetId, externalRootShareId) AS
- (SELECT ancestors.targetId,
- ancestors.id
- FROM ancestors
- WHERE ancestors.parentId IS NULL
- AND ancestors.externalPath IS NOT NULL)
- SELECT rootShare.targetId,
- rootShare.externalRootShareId
- FROM rootShare
- ) AS ${sql.identifier(externalRootScopeAlias)}
- `
- const externalRootScopeTargetId = sql<number>`${sql.identifier(externalRootScopeAlias)}.${sql.identifier('targetId')}`
- const externalRootShareId = sql<number | null>`${sql.identifier(externalRootScopeAlias)}.${sql.identifier('externalRootShareId')}`
+ const externalRootScope = externalShareScopeSQL(externalShareTargets, 'shareRootExternalScope')
const fromUser = this.fromUserQuery(selectUnion, filters).$dynamic()
const fromGroups = this.fromGroupsQuery(selectUnion, filters).$dynamic()
const fromAdminShares = this.fromAdminSharesQuery({ ...selectUnion, rootPermissions: sql`${SHARE_ALL_OPERATIONS}` }, filters).$dynamic()
@@ -798,7 +771,7 @@ export class SharesQueries {
spaceAlias: unionAlias.originSpaceAlias,
spaceExternalRootId: unionAlias.originSpaceExternalRootId,
spaceRootExternalPath: unionAlias.originSpaceRootExternalPath,
- shareExternalId: externalRootShareId
+ shareExternalId: externalRootScope.storageShareId
},
root: {
id: unionAlias.rootId,
@@ -835,7 +808,7 @@ export class SharesQueries {
// Non-external shares do not need this scope. An external share must
// resolve one: otherwise omit it so `updateRootFile()` cannot mistake
// the share-record owner for the owner of its external storage.
- .where(or(isNull(unionAlias.rootExternalPath), isNotNull(externalRootShareId)))
+ .where(or(isNull(unionAlias.rootExternalPath), isNotNull(externalRootScope.storageShareId)))
.groupBy(unionAlias.rootId)
.prepare()
}
@@ -856,8 +829,6 @@ export class SharesQueries {
async permissions(userId: number, shareAlias: string, isAdmin: number = 0): Promise<Partial<SpaceEnv>> {
if (!this.sharePermissionsQuery) {
const shareSpaceRoot: any = alias(spacesRoots, 'shareSpaceRoot')
- const permissionParentShare: any = alias(shares, 'permissionParentShare')
- const externalRootScopeAlias = 'externalRootScope'
// Make every file operation through a nested external share address the
// same file records and locks as an operation through its external root.
// This storage ancestry is independent from permission inheritance:
@@ -867,47 +838,11 @@ export class SharesQueries {
// a null `externalParentShareId` and retain their existing fallback scope.
// A missing or non-external parent produces no root row, so the inner join
// denies access to a malformed external child before any file operation.
- const externalRootScope = sql`
- (
- WITH RECURSIVE targetShare (id, parentId, externalPath) AS
- (SELECT ${shares.id},
- ${shares.parentId},
- ${shares.externalPath}
- FROM ${shares}
- WHERE ${shares.alias} = ${sql.placeholder('shareAlias')}),
- ancestors (targetId, id, parentId, externalPath) AS
- (SELECT targetShare.id,
- targetShare.id,
- targetShare.parentId,
- targetShare.externalPath
- FROM targetShare
- WHERE targetShare.externalPath IS NOT NULL
- AND targetShare.parentId IS NOT NULL
- UNION
- SELECT ancestors.targetId,
- ${permissionParentShare.id},
- ${permissionParentShare.parentId},
- ${permissionParentShare.externalPath}
- FROM ${shares} AS permissionParentShare
- INNER JOIN ancestors ON ${permissionParentShare.id} = ancestors.parentId
- WHERE ${permissionParentShare.externalPath} IS NOT NULL),
- rootShare (targetId, externalRootShareId) AS
- (SELECT ancestors.targetId,
- ancestors.id
- FROM ancestors
- WHERE ancestors.parentId IS NULL
- AND ancestors.externalPath IS NOT NULL)
- SELECT targetShare.id AS targetId,
- rootShare.externalRootShareId
- FROM targetShare
- LEFT JOIN rootShare ON rootShare.targetId = targetShare.id
- WHERE targetShare.externalPath IS NULL
- OR targetShare.parentId IS NULL
- OR rootShare.externalRootShareId IS NOT NULL
- ) AS ${sql.identifier(externalRootScopeAlias)}
- `
- const externalRootScopeTargetId = sql<number>`${sql.identifier(externalRootScopeAlias)}.${sql.identifier('targetId')}`
- const externalRootShareId = sql<number | null>`${sql.identifier(externalRootScopeAlias)}.${sql.identifier('externalRootShareId')}`
+ const targetShare = this.db
+ .select({ id: shares.id, parentId: shares.parentId, externalPath: shares.externalPath })
+ .from(shares)
+ .where(eq(shares.alias, sql.placeholder('shareAlias')))
+ const externalRootScope = externalShareScopeSQL(targetShare, 'permissionExternalScope', { mapExternalRootToSelf: false })
const selectUnion: SpaceEnv | SelectedFields<any, any> = {
id: shares.id,
alias: shares.alias,
@@ -970,13 +905,13 @@ export class SharesQueries {
root: { id: unionAlias.rootSpaceRootId, externalPath: unionAlias.rootSpaceRootExternalPath }
},
externalPath: unionAlias.rootExternalPath,
- externalParentShareId: externalRootShareId
+ externalParentShareId: externalRootScope.storageShareId
}
}
this.sharePermissionsQuery = this.db
.select(select)
.from(unionAlias)
- .innerJoin(externalRootScope, eq(externalRootScopeTargetId, unionAlias.id))
+ .innerJoin(externalRootScope.table, eq(externalRootScope.targetShareId, unionAlias.id))
.groupBy(unionAlias.id)
.limit(1)
.prepare()
diff --git a/backend/src/applications/shares/utils/external-share-scope.sql.ts b/backend/src/applications/shares/utils/external-share-scope.sql.ts
new file mode 100644
index 00000000..c15a1689
--- /dev/null
+++ b/backend/src/applications/shares/utils/external-share-scope.sql.ts
@@ -0,0 +1,85 @@
+import { SQL, sql, SQLWrapper } from 'drizzle-orm'
+import { alias } from 'drizzle-orm/mysql-core'
+import { shares } from '../schemas/shares.schema'
+
+export interface ExternalShareScopeSQL {
+ table: SQL
+ targetShareId: SQL<number>
+ storageShareId: SQL<number | null>
+}
+
+interface ExternalShareScopeOptions {
+ mapExternalRootToSelf?: boolean
+ oneTargetPerStorage?: boolean
+}
+
+export function externalShareScopeSQL(targetShares: SQLWrapper, scopeAlias: string, options: ExternalShareScopeOptions = {}): ExternalShareScopeSQL {
+ // Maps each target share to the highest external ancestor used by `files.shareExternalId`. The target query must select, in this order, `id`,
+ // `parentId` and `externalPath`.
+ const parentShareAlias = `${scopeAlias}ParentShare`
+ const parentShare: any = alias(shares, parentShareAlias)
+ const resolvedTargetShareId = sql<number>`${sql.identifier('resolvedShares')}.${sql.identifier('targetShareId')}`
+ const resolvedStorageShareId = sql<number | null>`${sql.identifier('resolvedShares')}.${sql.identifier('storageShareId')}`
+ const ancestorTargetFilter =
+ options.mapExternalRootToSelf === false ? sql`WHERE targetShares.externalPath IS NOT NULL AND targetShares.parentId IS NOT NULL` : sql``
+ const externalRootTargetFilter = options.mapExternalRootToSelf === false ? sql`AND ancestors.id <> ancestors.targetShareId` : sql``
+ const selectScope = options.oneTargetPerStorage
+ ? sql`
+ SELECT COALESCE(
+ MAX(IF (${resolvedTargetShareId} = ${resolvedStorageShareId}, ${resolvedTargetShareId}, NULL)),
+ MIN(${resolvedTargetShareId})
+ ) AS targetShareId,
+ ${resolvedStorageShareId} AS storageShareId
+ FROM resolvedShares
+ WHERE ${resolvedStorageShareId} IS NOT NULL
+ GROUP BY ${resolvedStorageShareId}
+ `
+ : sql`
+ SELECT ${resolvedTargetShareId} AS targetShareId,
+ ${resolvedStorageShareId} AS storageShareId
+ FROM resolvedShares
+ `
+ const table = sql`
+ (
+ WITH RECURSIVE targetShares (id, parentId, externalPath) AS
+ (${targetShares}),
+ ancestors (targetShareId, id, parentId, externalPath) AS
+ (SELECT targetShares.id,
+ targetShares.id,
+ targetShares.parentId,
+ targetShares.externalPath
+ FROM targetShares
+ ${ancestorTargetFilter}
+ UNION
+ SELECT ancestors.targetShareId,
+ ${parentShare.id},
+ ${parentShare.parentId},
+ ${parentShare.externalPath}
+ FROM ${shares} AS ${sql.identifier(parentShareAlias)}
+ INNER JOIN ancestors ON ${parentShare.id} = ancestors.parentId
+ WHERE ${parentShare.externalPath} IS NOT NULL),
+ rootShares (targetShareId, storageShareId) AS
+ (SELECT ancestors.targetShareId,
+ ancestors.id
+ FROM ancestors
+ WHERE ancestors.parentId IS NULL
+ AND ancestors.externalPath IS NOT NULL
+ ${externalRootTargetFilter}),
+ resolvedShares (targetShareId, storageShareId) AS
+ (SELECT targetShares.id,
+ IF (targetShares.externalPath IS NULL, NULL, rootShares.storageShareId)
+ FROM targetShares
+ LEFT JOIN rootShares ON rootShares.targetShareId = targetShares.id
+ WHERE targetShares.externalPath IS NULL
+ OR targetShares.parentId IS NULL
+ OR rootShares.storageShareId IS NOT NULL)
+ ${selectScope}
+ ) AS ${sql.identifier(scopeAlias)}
+ `
+
+ return {
+ table,
+ targetShareId: sql<number>`${sql.identifier(scopeAlias)}.${sql.identifier('targetShareId')}`,
+ storageShareId: sql<number | null>`${sql.identifier(scopeAlias)}.${sql.identifier('storageShareId')}`
+ }
+}