mirror of
https://github.com/docmost/docmost.git
synced 2026-05-21 17:22:54 +08:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| bda7e0099c | |||
| 0369e727de | |||
| aa657e7bad |
@@ -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,11 +412,6 @@ export class PagePermissionRepo {
|
|||||||
canAccess: boolean;
|
canAccess: boolean;
|
||||||
canEdit: boolean;
|
canEdit: boolean;
|
||||||
}> {
|
}> {
|
||||||
return withCache(
|
|
||||||
this.cacheManager,
|
|
||||||
CacheKey.PAGE_CAN_EDIT(userId, pageId),
|
|
||||||
PERMISSION_CACHE_TTL_MS,
|
|
||||||
async () => {
|
|
||||||
const result = await sql<{
|
const result = await sql<{
|
||||||
canAccess: boolean | null;
|
canAccess: boolean | null;
|
||||||
canEdit: boolean | null;
|
canEdit: boolean | null;
|
||||||
@@ -430,8 +449,6 @@ export class PagePermissionRepo {
|
|||||||
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,11 +214,6 @@ export class SpaceMemberRepo {
|
|||||||
userId: string,
|
userId: string,
|
||||||
spaceId: string,
|
spaceId: string,
|
||||||
): Promise<UserSpaceRole[]> {
|
): Promise<UserSpaceRole[]> {
|
||||||
return withCache(
|
|
||||||
this.cacheManager,
|
|
||||||
CacheKey.SPACE_ROLES(userId, spaceId),
|
|
||||||
PERMISSION_CACHE_TTL_MS,
|
|
||||||
async () => {
|
|
||||||
const roles = await this.db
|
const roles = await this.db
|
||||||
.selectFrom('spaceMembers')
|
.selectFrom('spaceMembers')
|
||||||
.select(['userId', 'role'])
|
.select(['userId', 'role'])
|
||||||
@@ -235,11 +222,7 @@ export class SpaceMemberRepo {
|
|||||||
.unionAll(
|
.unionAll(
|
||||||
this.db
|
this.db
|
||||||
.selectFrom('spaceMembers')
|
.selectFrom('spaceMembers')
|
||||||
.innerJoin(
|
.innerJoin('groupUsers', 'groupUsers.groupId', 'spaceMembers.groupId')
|
||||||
'groupUsers',
|
|
||||||
'groupUsers.groupId',
|
|
||||||
'spaceMembers.groupId',
|
|
||||||
)
|
|
||||||
.select(['groupUsers.userId', 'spaceMembers.role'])
|
.select(['groupUsers.userId', 'spaceMembers.role'])
|
||||||
.where('groupUsers.userId', '=', userId)
|
.where('groupUsers.userId', '=', userId)
|
||||||
.where('spaceMembers.spaceId', '=', spaceId),
|
.where('spaceMembers.spaceId', '=', spaceId),
|
||||||
@@ -250,8 +233,6 @@ export class SpaceMemberRepo {
|
|||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
return roles;
|
return roles;
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async getUserIdsWithSpaceAccess(
|
async getUserIdsWithSpaceAccess(
|
||||||
|
|||||||
Reference in New Issue
Block a user