Skip to content

Commit a6d7169

Browse files
committed
chore: initv4130
1 parent 1f38771 commit a6d7169

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import type { ApiRequestProps, ApiResponseType } from '@fastgpt/service/type/next';
2+
import { NextAPI } from '@/service/middleware/entry';
3+
import { authCert } from '@fastgpt/service/support/permission/auth/common';
4+
import { MongoApp } from '@fastgpt/service/core/app/schema';
5+
import { AppFolderTypeList } from '@fastgpt/global/core/app/constants';
6+
import { syncChildrenPermission } from '@fastgpt/service/support/permission/inheritPermission';
7+
import { mongoSessionRun } from '@fastgpt/service/common/mongo/sessionRun';
8+
import { PerResourceTypeEnum } from '@fastgpt/global/support/permission/constant';
9+
import { getResourceClbs } from '@fastgpt/service/support/permission/controller';
10+
import { addLog } from '@fastgpt/service/common/system/log';
11+
import { MongoDataset } from '@fastgpt/service/core/dataset/schema';
12+
import { DatasetTypeEnum } from '@fastgpt/global/core/dataset/constants';
13+
14+
export type SyncAppChatLogQuery = {};
15+
16+
export type SyncAppChatLogBody = {
17+
batchSize?: number;
18+
};
19+
20+
export type SyncAppChatLogResponse = {};
21+
22+
/**
23+
* 初始化脚本 v4.13.0
24+
* 权限表数据更新:直接对系统内所有资源(没有 parentId 顶层的 Folder,以及没有 inheritPermission 的有 parentId 的Folder),执行一次 syncChildrenPermission 函数。
25+
* 包括:App 和 Dataset 的权限同步
26+
*/
27+
async function handler(
28+
req: ApiRequestProps<SyncAppChatLogBody, SyncAppChatLogQuery>,
29+
res: ApiResponseType<SyncAppChatLogResponse>
30+
) {
31+
await authCert({ req, authRoot: true });
32+
// 1. update App's
33+
const appFolders = await MongoApp.find({
34+
$or: [
35+
{
36+
parentId: null,
37+
type: { $in: AppFolderTypeList }
38+
},
39+
{
40+
parentId: { $exists: true },
41+
inheritPermission: false,
42+
type: { $in: AppFolderTypeList }
43+
}
44+
]
45+
}).lean();
46+
addLog.info(`start sync app children permission, total: ${appFolders.length}`);
47+
for (const folder of appFolders) {
48+
await mongoSessionRun(async (session) => {
49+
const clbs = await getResourceClbs({
50+
resourceType: PerResourceTypeEnum.app,
51+
resourceId: folder._id,
52+
teamId: folder.teamId,
53+
session
54+
});
55+
await syncChildrenPermission({
56+
folderTypeList: AppFolderTypeList,
57+
resource: folder,
58+
session,
59+
resourceModel: MongoApp,
60+
resourceType: PerResourceTypeEnum.app,
61+
collaborators: clbs
62+
});
63+
});
64+
addLog.debug(
65+
`sync app children permission, folderId: ${folder._id}, progress: ${appFolders.indexOf(folder) + 1}/${appFolders.length}`
66+
);
67+
}
68+
addLog.info('sync app children permission completed');
69+
70+
// 2. update Dataset's
71+
const datasetFolders = await MongoDataset.find({
72+
$or: [
73+
{
74+
parentId: null,
75+
type: DatasetTypeEnum.folder
76+
},
77+
{
78+
parentId: { $exists: true },
79+
inheritPermission: false,
80+
type: DatasetTypeEnum.folder
81+
}
82+
]
83+
}).lean();
84+
85+
addLog.info(`start sync dataset children permission, total: ${datasetFolders.length}`);
86+
87+
for (const folder of datasetFolders) {
88+
await mongoSessionRun(async (session) => {
89+
const clbs = await getResourceClbs({
90+
resourceType: PerResourceTypeEnum.dataset,
91+
resourceId: folder._id,
92+
teamId: folder.teamId,
93+
session
94+
});
95+
96+
await syncChildrenPermission({
97+
folderTypeList: [DatasetTypeEnum.folder],
98+
resource: folder,
99+
session,
100+
resourceModel: MongoDataset,
101+
resourceType: PerResourceTypeEnum.dataset,
102+
collaborators: clbs
103+
});
104+
});
105+
106+
addLog.debug(
107+
`sync dataset children permission, folderId: ${folder._id}, progress: ${datasetFolders.indexOf(folder) + 1}/${datasetFolders.length}`
108+
);
109+
}
110+
111+
addLog.info('sync dataset children permission completed');
112+
113+
return {
114+
message: 'App and Dataset permission sync completed successfully'
115+
};
116+
}
117+
118+
export default NextAPI(handler);

0 commit comments

Comments
 (0)