Skip to content

Commit

Permalink
perf: dataset data auth (#3015)
Browse files Browse the repository at this point in the history
  • Loading branch information
c121914yu authored Oct 29, 2024
1 parent 1e02544 commit ee71875
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 48 deletions.
4 changes: 2 additions & 2 deletions packages/global/core/dataset/type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ export type DatasetDataItemType = {
chunkIndex: number;
indexes: DatasetDataIndexItemType[];
isOwner: boolean;
canWrite: boolean;
// permission: DatasetPermission;
};

/* --------------- file ---------------------- */
Expand All @@ -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;
Expand Down
94 changes: 49 additions & 45 deletions packages/service/support/permission/dataset/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ export const authDataset = async ({
dataset
};
};

// the temporary solution for authDatasetCollection is getting the
export async function authDatasetCollection({
collectionId,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion projects/app/src/pages/api/core/dataset/data/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit ee71875

Please sign in to comment.