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

View File

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

View File

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