perf: dataset data auth (#3015)

This commit is contained in:
Archer
2024-10-29 15:44:46 +08:00
committed by GitHub
parent 1e02544c3a
commit ee718750e2
3 changed files with 51 additions and 48 deletions

View File

@@ -191,7 +191,7 @@ export type DatasetDataItemType = {
chunkIndex: number; chunkIndex: number;
indexes: DatasetDataIndexItemType[]; indexes: DatasetDataIndexItemType[];
isOwner: boolean; isOwner: boolean;
canWrite: boolean; // permission: DatasetPermission;
}; };
/* --------------- file ---------------------- */ /* --------------- file ---------------------- */
@@ -212,7 +212,7 @@ export type DatasetFileSchema = {
/* ============= search =============== */ /* ============= search =============== */
export type SearchDataResponseItemType = Omit< export type SearchDataResponseItemType = Omit<
DatasetDataItemType, DatasetDataItemType,
'teamId' | 'indexes' | 'isOwner' | 'canWrite' 'teamId' | 'indexes' | 'isOwner'
> & { > & {
score: { type: `${SearchScoreTypeEnum}`; value: number; index: number }[]; score: { type: `${SearchScoreTypeEnum}`; value: number; index: number }[];
// score: number; // score: number;

View File

@@ -157,6 +157,7 @@ export const authDataset = async ({
dataset dataset
}; };
}; };
// the temporary solution for authDatasetCollection is getting the // the temporary solution for authDatasetCollection is getting the
export async function authDatasetCollection({ export async function authDatasetCollection({
collectionId, collectionId,
@@ -194,55 +195,58 @@ export async function authDatasetCollection({
}; };
} }
export async function authDatasetFile({ // export async function authDatasetFile({
fileId, // fileId,
per, // per,
...props // ...props
}: AuthModeType & { // }: AuthModeType & {
fileId: string; // fileId: string;
}): Promise< // }): Promise<
AuthResponseType<DatasetPermission> & { // AuthResponseType<DatasetPermission> & {
file: DatasetFileSchema; // file: DatasetFileSchema;
} // }
> { // > {
const { teamId, tmbId, isRoot } = await parseHeaderCert(props); // const { teamId, tmbId, isRoot } = await parseHeaderCert(props);
const [file, collection] = await Promise.all([ // const [file, collection] = await Promise.all([
getFileById({ bucketName: BucketNameEnum.dataset, fileId }), // getFileById({ bucketName: BucketNameEnum.dataset, fileId }),
MongoDatasetCollection.findOne({ // MongoDatasetCollection.findOne({
teamId, // teamId,
fileId // fileId
}) // })
]); // ]);
if (!file) { // if (!file) {
return Promise.reject(CommonErrEnum.fileNotFound); // return Promise.reject(CommonErrEnum.fileNotFound);
} // }
if (!collection) { // if (!collection) {
return Promise.reject(DatasetErrEnum.unAuthDatasetFile); // return Promise.reject(DatasetErrEnum.unAuthDatasetFile);
} // }
try { // try {
const { permission } = await authDatasetCollection({ // const { permission } = await authDatasetCollection({
...props, // ...props,
collectionId: collection._id, // collectionId: collection._id,
per, // per,
isRoot // isRoot
}); // });
return { // return {
teamId, // teamId,
tmbId, // tmbId,
file, // file,
permission, // permission,
isRoot // isRoot
}; // };
} catch (error) { // } catch (error) {
return Promise.reject(DatasetErrEnum.unAuthDatasetFile); // return Promise.reject(DatasetErrEnum.unAuthDatasetFile);
} // }
} // }
/*
DatasetData permission is inherited from collection.
*/
export async function authDatasetData({ export async function authDatasetData({
dataId, dataId,
...props ...props
@@ -273,8 +277,8 @@ export async function authDatasetData({
collectionId: String(datasetData.collectionId), collectionId: String(datasetData.collectionId),
sourceName: result.collection.name || '', sourceName: result.collection.name || '',
sourceId: result.collection?.fileId || result.collection?.rawLink, sourceId: result.collection?.fileId || result.collection?.rawLink,
isOwner: String(datasetData.tmbId) === String(result.tmbId), isOwner: String(datasetData.tmbId) === String(result.tmbId)
canWrite: result.permission.hasWritePer // permission: result.permission
}; };
return { return {

View File

@@ -5,7 +5,6 @@ import { NextAPI } from '@/service/middleware/entry';
import { WritePermissionVal } from '@fastgpt/global/support/permission/constant'; import { WritePermissionVal } from '@fastgpt/global/support/permission/constant';
import { authDatasetData } from '@fastgpt/service/support/permission/dataset/auth'; import { authDatasetData } from '@fastgpt/service/support/permission/dataset/auth';
import { ApiRequestProps } from '@fastgpt/service/type/next'; import { ApiRequestProps } from '@fastgpt/service/type/next';
import { MongoDatasetData } from '@fastgpt/service/core/dataset/data/schema';
async function handler(req: ApiRequestProps<UpdateDatasetDataProps>) { async function handler(req: ApiRequestProps<UpdateDatasetDataProps>) {
const { dataId, q, a, indexes = [] } = req.body; const { dataId, q, a, indexes = [] } = req.body;