mirror of
https://github.com/labring/FastGPT.git
synced 2025-08-02 12:48:30 +00:00

* faq * perf: navbar name and fix dataset selector * feat: app tag * perf: icon * fix: update workflow bug * perf: dataset ui * perf: menu * fix: ts * fix: auth file and app list ui * app list * app list * perf: init api * update per * log level
50 lines
1.4 KiB
TypeScript
50 lines
1.4 KiB
TypeScript
import { AuthModeType } from '../type';
|
|
import { DatasetFileSchema } from '@fastgpt/global/core/dataset/type';
|
|
import { parseHeaderCert } from '../controller';
|
|
import { getFileById } from '../../../common/file/gridfs/controller';
|
|
import { BucketNameEnum } from '@fastgpt/global/common/file/constants';
|
|
import { CommonErrEnum } from '@fastgpt/global/common/error/code/common';
|
|
import { OwnerPermissionVal, ReadPermissionVal } from '@fastgpt/global/support/permission/constant';
|
|
import { AuthPropsType, AuthResponseType } from '../type/auth';
|
|
import { Permission } from '@fastgpt/global/support/permission/controller';
|
|
|
|
export async function authFile({
|
|
fileId,
|
|
per = OwnerPermissionVal,
|
|
...props
|
|
}: AuthPropsType & {
|
|
fileId: string;
|
|
}): Promise<
|
|
AuthResponseType & {
|
|
file: DatasetFileSchema;
|
|
}
|
|
> {
|
|
const authRes = await parseHeaderCert(props);
|
|
const { teamId, tmbId } = authRes;
|
|
|
|
const file = await getFileById({ bucketName: BucketNameEnum.dataset, fileId });
|
|
|
|
if (!file) {
|
|
return Promise.reject(CommonErrEnum.fileNotFound);
|
|
}
|
|
|
|
if (file.metadata?.teamId !== teamId) {
|
|
return Promise.reject(CommonErrEnum.unAuthFile);
|
|
}
|
|
|
|
const permission = new Permission({
|
|
per: ReadPermissionVal,
|
|
isOwner: file.metadata?.tmbId === tmbId
|
|
});
|
|
|
|
if (!permission.checkPer(per)) {
|
|
return Promise.reject(CommonErrEnum.unAuthFile);
|
|
}
|
|
|
|
return {
|
|
...authRes,
|
|
permission,
|
|
file
|
|
};
|
|
}
|