Skip to content

Commit

Permalink
fix: alldataset get dataset without folders. omit the permission check (
Browse files Browse the repository at this point in the history
  • Loading branch information
FinleyGe authored Oct 31, 2024
1 parent eb365fe commit bc171db
Showing 1 changed file with 13 additions and 82 deletions.
95 changes: 13 additions & 82 deletions projects/app/src/pages/api/core/dataset/allDataset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,100 +3,31 @@ import { MongoDataset } from '@fastgpt/service/core/dataset/schema';
import { getVectorModel } from '@fastgpt/service/core/ai/model';
import type { DatasetSimpleItemType } from '@fastgpt/global/core/dataset/type.d';
import { NextAPI } from '@/service/middleware/entry';
import {
PerResourceTypeEnum,
ReadPermissionVal
} from '@fastgpt/global/support/permission/constant';
import { MongoResourcePermission } from '@fastgpt/service/support/permission/schema';
import { DatasetPermission } from '@fastgpt/global/support/permission/dataset/controller';
import { ReadPermissionVal } from '@fastgpt/global/support/permission/constant';
import { authUserPer } from '@fastgpt/service/support/permission/user/auth';
import { DatasetDefaultPermissionVal } from '@fastgpt/global/support/permission/dataset/constant';
import { getGroupsByTmbId } from '@fastgpt/service/support/permission/memberGroup/controllers';
import { getGroupPer } from '@fastgpt/service/support/permission/controller';
import { DatasetTypeEnum } from '@fastgpt/global/core/dataset/constants';

/* get all dataset by teamId or tmbId */
async function handler(req: NextApiRequest): Promise<DatasetSimpleItemType[]> {
const {
teamId,
tmbId,
permission: myPer
} = await authUserPer({
const { teamId } = await authUserPer({
req,
authToken: true,
authApiKey: true,
per: ReadPermissionVal
});

const myGroupIds = (
await getGroupsByTmbId({
tmbId,
teamId
})
).map((item) => String(item._id));

const [myDatasets, perList] = await Promise.all([
MongoDataset.find({
teamId
})
.sort({
updateTime: -1
})
.lean(),
MongoResourcePermission.find({
resourceType: PerResourceTypeEnum.dataset,
teamId,
resourceId: {
$exists: true
}
}).lean()
]);

const filterDatasets = myDatasets
.map((dataset) => {
const per = (() => {
const myPerList = perList.filter(
(item) =>
String(item.tmbId) === String(tmbId) || myGroupIds.includes(String(item.groupId))
);

const getPer = (id: string) => {
const tmbPer = myPerList.find(
(item) => String(item.resourceId) === id && !!item.tmbId
)?.permission;
const groupPer = getGroupPer(
myPerList
.filter(
(item) =>
String(item.resourceId) === id && myGroupIds.includes(String(item.groupId))
)
.map((item) => item.permission)
);

return new DatasetPermission({
per: tmbPer ?? groupPer ?? DatasetDefaultPermissionVal,
isOwner: String(dataset.tmbId) === String(tmbId) || myPer.isOwner
});
};

const parentDataset = myDatasets.find(
(item) => String(item._id) === String(dataset.parentId)
);

if (dataset.inheritPermission && dataset.parentId && parentDataset) {
return getPer(parentDataset._id);
} else {
return getPer(dataset._id);
}
})();

return {
...dataset,
permission: per
};
const myDatasets = await MongoDataset.find({
teamId,
type: {
$ne: DatasetTypeEnum.folder
}
})
.sort({
updateTime: -1
})
.filter((app) => app.permission.hasReadPer);
.lean();

return filterDatasets.map((item) => ({
return myDatasets.map((item) => ({
_id: item._id,
avatar: item.avatar,
name: item.name,
Expand Down

0 comments on commit bc171db

Please sign in to comment.