44 lines
1.4 KiB
TypeScript
44 lines
1.4 KiB
TypeScript
import { canUserAccessPrivateTaskDetail } from '@/lib/user-permission-inherit'
|
|
|
|
export const TASK_DETAIL_PRIVACY_SELECT = {
|
|
taskDetailPrivate: true,
|
|
taskDetailDemandType: true,
|
|
taskDetailRequesterUserId: true,
|
|
} as const
|
|
|
|
export interface TaskDetailPrivacyMaterial {
|
|
materialType?: string | null
|
|
taskDetailPrivate?: boolean | null
|
|
taskDetailRequesterUserId?: string | null
|
|
}
|
|
|
|
export function isPrivateTaskDetail(material: TaskDetailPrivacyMaterial): boolean {
|
|
return material.taskDetailPrivate === true
|
|
}
|
|
|
|
/** 素材级准入先于上传者/管理员/分享/类型权限,不能被这些旧放行路径绕过。 */
|
|
export async function canAccessMaterialPrivacy(
|
|
material: TaskDetailPrivacyMaterial,
|
|
userId?: string | null,
|
|
): Promise<boolean> {
|
|
if (!isPrivateTaskDetail(material)) return true
|
|
if (!userId) return false
|
|
if (material.taskDetailRequesterUserId === userId) return true
|
|
return canUserAccessPrivateTaskDetail(userId)
|
|
}
|
|
|
|
export function privateTaskDetailAccessWhere(userId: string, canAccessAll: boolean) {
|
|
return {
|
|
materialType: '任务-详情',
|
|
taskDetailPrivate: true,
|
|
...(canAccessAll ? {} : { taskDetailRequesterUserId: userId }),
|
|
}
|
|
}
|
|
|
|
export async function buildMaterialPrivacyWhere(userId: string) {
|
|
return { OR: [
|
|
{ taskDetailPrivate: false },
|
|
privateTaskDetailAccessWhere(userId, await canUserAccessPrivateTaskDetail(userId)),
|
|
] }
|
|
}
|