Repository navigation
fix(audit): collection #8070
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shortlight5980
wants to merge
1
commit into
labring:main
Choose a base branch
from
shortlight5980:audit-collection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+948
−86
Open
fix(audit): collection #8070
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import { DatasetCollectionTypeMap } from '@fastgpt/global/core/dataset/constants'; | ||
| import { getDatasetCollectionPaths } from './paths'; | ||
|
|
||
| /** 未知集合类型没有展示名,回退原始枚举值,避免审计里出现空对象类型。 */ | ||
| export const getCollectionAuditObjectType = (type: string): string => | ||
| DatasetCollectionTypeMap[type as keyof typeof DatasetCollectionTypeMap]?.name ?? type; | ||
|
|
||
| /** | ||
| * 生成集合审计日志的位置字段(`知识库名/父目录/子目录`),不包含集合自身。 | ||
| * | ||
| * 位置只服务于日志展示:调用方此时已完成鉴权与写入,取路径失败不能影响主流程, | ||
| * 因此这里吞掉查询异常并退回知识库名。 | ||
| */ | ||
| export const getCollectionAuditLocation = async ({ | ||
| datasetName, | ||
| parentId | ||
| }: { | ||
| datasetName: string; | ||
| parentId?: string | null; | ||
| }): Promise<string> => { | ||
| const paths = await getDatasetCollectionPaths({ parentId: parentId ?? undefined }).catch( | ||
| () => [] | ||
| ); | ||
| return [datasetName, ...paths.map((path) => path.parentName)].filter(Boolean).join('/'); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import type { | ||
| ParentIdType, | ||
| ParentTreePathItemType | ||
| } from '@fastgpt/global/common/parentFolder/type'; | ||
| import { MongoDatasetCollection } from './schema'; | ||
|
|
||
| /** | ||
| * 递归查询集合的祖先目录链(从根到直接父级,不包含集合自身)。 | ||
| * | ||
| * 递归而非聚合:调用方都已经校验过父目录深度,层级有限;且每一层都命中 `_id` 主键查询。 | ||
| * 集合不存在或父级断链时提前返回已收集的部分,不会抛错。 | ||
| * | ||
| * 带环检测而非信任数据:历史脏数据可能出现父子互指,此时每层都是 `await` 主键查询, | ||
| * 不会爆栈而是无限查询下去,必须用 visited 兜底。命中环时保留已收集的链路并停止向上, | ||
| * 与「父级断链」语义保持一致——审计位置只用于展示,不能因此让主流程报错。 | ||
| */ | ||
| export async function getDatasetCollectionPaths({ | ||
| parentId = '' | ||
| }: { | ||
| parentId?: string; | ||
| }): Promise<ParentTreePathItemType[]> { | ||
| const visited = new Set<string>(); | ||
|
|
||
| async function find(parentId: ParentIdType): Promise<ParentTreePathItemType[]> { | ||
| if (!parentId) { | ||
| return []; | ||
| } | ||
| // 已访问过说明链路成环,直接截断,避免无限查询 | ||
| if (visited.has(String(parentId))) { | ||
| return []; | ||
| } | ||
| visited.add(String(parentId)); | ||
|
|
||
| const parent = await MongoDatasetCollection.findOne({ _id: parentId }, 'name parentId'); | ||
|
|
||
| if (!parent) return []; | ||
|
|
||
| const paths = await find(parent.parentId ?? undefined); | ||
| paths.push({ parentId, parentName: parent.name }); | ||
|
|
||
| return paths; | ||
| } | ||
|
|
||
| return find(parentId); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.