Compare commits

..

3 Commits

Author SHA1 Message Date
Philipinho bda7e0099c full implementation 2026-05-10 17:54:43 +01:00
Philipinho 0369e727de Merge branch 'main' into feat/labels 2026-05-09 17:11:37 +01:00
Philipinho aa657e7bad feat: labels (WIP) 2026-05-09 17:06:38 +01:00
4 changed files with 88 additions and 125 deletions
@@ -1,11 +1,3 @@
export const CacheKey = { export const CacheKey = {
LICENSE_VALID: (workspaceId: string) => `license:valid:${workspaceId}`, LICENSE_VALID: (workspaceId: string) => `license:valid:${workspaceId}`,
SPACE_ROLES: (userId: string, spaceId: string) =>
`perm:space-roles:${userId}:${spaceId}`,
PAGE_CAN_EDIT: (userId: string, pageId: string) =>
`perm:can-edit:${userId}:${pageId}`,
}; };
// Permission caches dedupe repeated checks within and across short request bursts.
// 5s keeps staleness on revocations bounded.
export const PERMISSION_CACHE_TTL_MS = 5_000;
@@ -1,27 +0,0 @@
import { Cache } from 'cache-manager';
export async function withCache<T>(
cacheManager: Cache,
key: string,
ttlMs: number,
fn: () => Promise<T>,
): Promise<T> {
try {
const cached = await cacheManager.get<{ v: T }>(key);
if (cached !== undefined && cached !== null) {
return cached.v;
}
} catch (err) {
console.warn(`[withCache] get failed for "${key}", falling back to source`, err);
}
const value = await fn();
try {
await cacheManager.set(key, { v: value }, ttlMs);
} catch (err) {
console.warn(`[withCache] set failed for "${key}"`, err);
}
return value;
}
@@ -1,6 +1,4 @@
import { Inject, Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { CACHE_MANAGER } from '@nestjs/cache-manager';
import { Cache } from 'cache-manager';
import { InjectKysely } from 'nestjs-kysely'; import { InjectKysely } from 'nestjs-kysely';
import { KyselyDB, KyselyTransaction } from '@docmost/db/types/kysely.types'; import { KyselyDB, KyselyTransaction } from '@docmost/db/types/kysely.types';
import { dbOrTx } from '@docmost/db/utils'; import { dbOrTx } from '@docmost/db/utils';
@@ -19,11 +17,6 @@ import {
executeWithCursorPagination, executeWithCursorPagination,
} from '@docmost/db/pagination/cursor-pagination'; } from '@docmost/db/pagination/cursor-pagination';
import { PagePermissionMember } from './types/page-permission.types'; import { PagePermissionMember } from './types/page-permission.types';
import { withCache } from '../../../common/helpers/with-cache';
import {
CacheKey,
PERMISSION_CACHE_TTL_MS,
} from '../../../common/helpers/cache-keys';
export { PagePermissionMember } from './types/page-permission.types'; export { PagePermissionMember } from './types/page-permission.types';
@@ -32,7 +25,6 @@ export class PagePermissionRepo {
constructor( constructor(
@InjectKysely() private readonly db: KyselyDB, @InjectKysely() private readonly db: KyselyDB,
private readonly groupRepo: GroupRepo, private readonly groupRepo: GroupRepo,
@Inject(CACHE_MANAGER) private readonly cacheManager: Cache,
) {} ) {}
async findPageAccessByPageId( async findPageAccessByPageId(
@@ -369,8 +361,40 @@ export class PagePermissionRepo {
* Check if user can access a page by verifying they have permission on ALL restricted ancestors. * Check if user can access a page by verifying they have permission on ALL restricted ancestors.
*/ */
async canUserAccessPage(userId: string, pageId: string): Promise<boolean> { async canUserAccessPage(userId: string, pageId: string): Promise<boolean> {
const { canAccess } = await this.canUserEditPage(userId, pageId); const deniedAncestor = await this.db
return canAccess; .withRecursive('ancestors', (qb) =>
qb
.selectFrom('pages')
.select(['pages.id as ancestorId', 'pages.parentPageId'])
.where('pages.id', '=', pageId)
.unionAll((eb) =>
eb
.selectFrom('pages')
.innerJoin('ancestors', 'ancestors.parentPageId', 'pages.id')
.select(['pages.id as ancestorId', 'pages.parentPageId']),
),
)
.selectFrom('ancestors')
.innerJoin('pageAccess', 'pageAccess.pageId', 'ancestors.ancestorId')
.leftJoin('pagePermissions', (join) =>
join
.onRef('pagePermissions.pageAccessId', '=', 'pageAccess.id')
.on((eb) =>
eb.or([
eb('pagePermissions.userId', '=', userId),
eb(
'pagePermissions.groupId',
'in',
this.userGroupIdsSubquery(eb, userId),
),
]),
),
)
.select('pageAccess.pageId')
.where('pagePermissions.id', 'is', null)
.executeTakeFirst();
return !deniedAncestor;
} }
/** /**
@@ -388,50 +412,43 @@ export class PagePermissionRepo {
canAccess: boolean; canAccess: boolean;
canEdit: boolean; canEdit: boolean;
}> { }> {
return withCache( const result = await sql<{
this.cacheManager, canAccess: boolean | null;
CacheKey.PAGE_CAN_EDIT(userId, pageId), canEdit: boolean | null;
PERMISSION_CACHE_TTL_MS, }>`
async () => { WITH RECURSIVE ancestors AS (
const result = await sql<{ SELECT id AS ancestor_id, parent_page_id, 0 AS depth
canAccess: boolean | null; FROM pages
canEdit: boolean | null; WHERE id = ${pageId}::uuid
}>` UNION ALL
WITH RECURSIVE ancestors AS ( SELECT p.id, p.parent_page_id, a.depth + 1
SELECT id AS ancestor_id, parent_page_id, 0 AS depth FROM pages p
FROM pages JOIN ancestors a ON a.parent_page_id = p.id
WHERE id = ${pageId}::uuid )
UNION ALL SELECT
SELECT p.id, p.parent_page_id, a.depth + 1 bool_and(pp.id IS NOT NULL) AS "canAccess",
FROM pages p -- nearest restricted ancestor's highest role wins (DESC: 'writer' > 'reader', NULLS LAST: no-permission after real roles)
JOIN ancestors a ON a.parent_page_id = p.id (array_agg(pp.role ORDER BY a.depth ASC, pp.role DESC NULLS LAST))[1] = 'writer' AS "canEdit"
FROM ancestors a
JOIN page_access pa ON pa.page_id = a.ancestor_id
LEFT JOIN page_permissions pp ON pp.page_access_id = pa.id
AND (
pp.user_id = ${userId}::uuid
OR pp.group_id IN (
SELECT gu.group_id FROM group_users gu WHERE gu.user_id = ${userId}::uuid
) )
SELECT )
bool_and(pp.id IS NOT NULL) AS "canAccess", `.execute(this.db);
-- nearest restricted ancestor's highest role wins (DESC: 'writer' > 'reader', NULLS LAST: no-permission after real roles)
(array_agg(pp.role ORDER BY a.depth ASC, pp.role DESC NULLS LAST))[1] = 'writer' AS "canEdit"
FROM ancestors a
JOIN page_access pa ON pa.page_id = a.ancestor_id
LEFT JOIN page_permissions pp ON pp.page_access_id = pa.id
AND (
pp.user_id = ${userId}::uuid
OR pp.group_id IN (
SELECT gu.group_id FROM group_users gu WHERE gu.user_id = ${userId}::uuid
)
)
`.execute(this.db);
const row = result.rows[0]; const row = result.rows[0];
if (!row || row.canAccess === null) { if (!row || row.canAccess === null) {
return { hasAnyRestriction: false, canAccess: true, canEdit: true }; return { hasAnyRestriction: false, canAccess: true, canEdit: true };
} }
return { return {
hasAnyRestriction: true, hasAnyRestriction: true,
canAccess: row.canAccess, canAccess: row.canAccess,
canEdit: row.canAccess && (row.canEdit ?? false), canEdit: row.canAccess && (row.canEdit ?? false),
}; };
},
);
} }
/** /**
@@ -1,6 +1,4 @@
import { BadRequestException, Inject, Injectable } from '@nestjs/common'; import { BadRequestException, Injectable } from '@nestjs/common';
import { CACHE_MANAGER } from '@nestjs/cache-manager';
import { Cache } from 'cache-manager';
import { InjectKysely } from 'nestjs-kysely'; import { InjectKysely } from 'nestjs-kysely';
import { KyselyDB, KyselyTransaction } from '@docmost/db/types/kysely.types'; import { KyselyDB, KyselyTransaction } from '@docmost/db/types/kysely.types';
import { dbOrTx } from '@docmost/db/utils'; import { dbOrTx } from '@docmost/db/utils';
@@ -15,11 +13,6 @@ import { MemberInfo, UserSpaceRole } from './types';
import { executeWithCursorPagination } from '@docmost/db/pagination/cursor-pagination'; import { executeWithCursorPagination } from '@docmost/db/pagination/cursor-pagination';
import { GroupRepo } from '@docmost/db/repos/group/group.repo'; import { GroupRepo } from '@docmost/db/repos/group/group.repo';
import { SpaceRepo } from '@docmost/db/repos/space/space.repo'; import { SpaceRepo } from '@docmost/db/repos/space/space.repo';
import { withCache } from '../../../common/helpers/with-cache';
import {
CacheKey,
PERMISSION_CACHE_TTL_MS,
} from '../../../common/helpers/cache-keys';
@Injectable() @Injectable()
export class SpaceMemberRepo { export class SpaceMemberRepo {
@@ -27,7 +20,6 @@ export class SpaceMemberRepo {
@InjectKysely() private readonly db: KyselyDB, @InjectKysely() private readonly db: KyselyDB,
private readonly groupRepo: GroupRepo, private readonly groupRepo: GroupRepo,
private readonly spaceRepo: SpaceRepo, private readonly spaceRepo: SpaceRepo,
@Inject(CACHE_MANAGER) private readonly cacheManager: Cache,
) {} ) {}
async insertSpaceMember( async insertSpaceMember(
@@ -222,36 +214,25 @@ export class SpaceMemberRepo {
userId: string, userId: string,
spaceId: string, spaceId: string,
): Promise<UserSpaceRole[]> { ): Promise<UserSpaceRole[]> {
return withCache( const roles = await this.db
this.cacheManager, .selectFrom('spaceMembers')
CacheKey.SPACE_ROLES(userId, spaceId), .select(['userId', 'role'])
PERMISSION_CACHE_TTL_MS, .where('userId', '=', userId)
async () => { .where('spaceId', '=', spaceId)
const roles = await this.db .unionAll(
this.db
.selectFrom('spaceMembers') .selectFrom('spaceMembers')
.select(['userId', 'role']) .innerJoin('groupUsers', 'groupUsers.groupId', 'spaceMembers.groupId')
.where('userId', '=', userId) .select(['groupUsers.userId', 'spaceMembers.role'])
.where('spaceId', '=', spaceId) .where('groupUsers.userId', '=', userId)
.unionAll( .where('spaceMembers.spaceId', '=', spaceId),
this.db )
.selectFrom('spaceMembers') .execute();
.innerJoin(
'groupUsers',
'groupUsers.groupId',
'spaceMembers.groupId',
)
.select(['groupUsers.userId', 'spaceMembers.role'])
.where('groupUsers.userId', '=', userId)
.where('spaceMembers.spaceId', '=', spaceId),
)
.execute();
if (!roles || roles.length === 0) { if (!roles || roles.length === 0) {
return undefined; return undefined;
} }
return roles; return roles;
},
);
} }
async getUserIdsWithSpaceAccess( async getUserIdsWithSpaceAccess(