Repository navigation
fix: knowledge details document and image permission error - #8286
wangdan-fit2cloud merged 1 commit into
Conversation
| :show="showQuestions" | ||
| :knowledge-id="targetKnowledgeId" | ||
| :document-id="targetDocumentId" | ||
| :paragraph-id="targetParagraphId" |
There was a problem hiding this comment.
Here's my review of the diff:
Issues & Observations
1. Inconsistent behavior: isWorkspaceSharedResource returns false vs. other branches
function knowledgeImagePermission(method) {
if (isSystemResource()) return Boolean(perm.knowledge.system.image[method]())
if (isSystemSharedResource()) return Boolean(perm.knowledge.share.image[method]())
if (isWorkspaceSharedResource()) return false // 共享知识库在工作空间侧只读
return Boolean(perm.knowledge.workspace.image[method](props.knowledgeId))
}This hard-codes permission resolution per resource type. It works but duplicates logic from knowledgeDocPermission. Consider extracting a generic higher-order helper (e.g., resourcePermission(getterKey, method)) to avoid repeating this branching pattern every time a new permission is added. This is now duplicated at least three times.
2. Comment language inconsistency
Comments are in Chinese (// 共享知识库在工作空间侧只读, 知识库「问题」只读权限判断) while you requested English answers and the project otherwise appears to mix. Keep consistent with the codebase convention (if it's Chinese elsewhere, fine — just confirm).
3. hasProblemReadPermission() naming / semantics
- The name suggests a boolean, but it also embeds resource-type dispatch that could become stale if new resource types are added to
DOCUMENT_RESOURCE_TYPEor the perm structure. - Since
showQuestionsis derived viacomputed(() => hasProblemReadPermission()), note that if any of the underlyingperm.*getters are reactive refs/proxies, this will recompute correctly; if they are plain functions reading non-reactive state, the computed won't update. Verify reactivity of the perm object.
4. resourceType prop is optional but drives crucial behavior
resourceType?: DocumentResourceType
const isImageDocument = computed(() => props.resourceType === DOCUMENT_RESOURCE_TYPE.IMAGE)If the parent doesn't pass resourceType, images fall back to the doc-permission check (knowledgeDocPermission('docEdit')), which may incorrectly gate editing. Options:
- Make it required, or
- Derive it from
resourceApi/context instead of relying on the caller.
5. Missing newline / formatting in template condition
v-if="!isWorkspaceSharedResource() && (isImageDocument ? knowledgeImagePermission('edit') : knowledgeDocPermission('docEdit'))"Long inline ternary in template hurts readability. Extract to a computed like canEditParagraph:
const canEditParagraph = computed(() =>
!isWorkspaceSharedResource() &&
(isImageDocument.value ? knowledgeImagePermission('edit') : knowledgeDocPermission('docEdit'))
)Then v-if="canEditParagraph". This also avoids re-evaluating permission calls on every render.
6. Potential redundant permission evaluation per render
Both canEditParagraph (above) and hasProblemReadPermission() call permission functions directly in render. If these getters have side effects or are expensive, caching via computed is preferable. The showQuestions computed already helps, but canEditParagraph currently does not.
7. showQuestions always-overrides child visibility
The child component receives :show="showQuestions". Confirm the child treats show as "fully hide" (not merely disable interaction). If it previously always showed questions for some flows (e.g., preview mode), adding show may unintentionally hide them. Also consider whether show should short-circuit v-model updates, i.e., prevent loading question APIs when hidden — ideally pass it down so the child can v-if internally and skip network requests.
8. KnowledgeImagePermissionMethod only supports 'edit'
Fine for now, but if image permissions later need view/delete, consider widening to match the shape used by KnowledgeDocPermissionMethod for symmetry and reuse.
9. Type import ordering
DocumentResourceType was added to an existing import type { ... } from '@/api/types' line; ensure alphabetical/lint ordering matches project conventions (some setups enforce sorted imports). Run ESLint to confirm.
10. perm.knowledge.*.image[method] guards
The code assumes perm.knowledge.system.image, perm.knowledge.share.image, etc., all exist and expose edit. If these were introduced alongside this change, ensure the source of truth (perm) defines them for all resource types; otherwise add optional chaining or explicit feature-flag gating to avoid runtime undefined-access errors.
Summary of suggested changes
- Extract a shared permission-dispatch helper to remove triple duplication.
- Extract
canEditParagraphcomputed and use it in the template. - Consider making
resourceTyperequired or deriving it from context. - Verify
permreactivity for the new computeds. - Ensure the child respects
showby skipping its own data fetch/permission calls when false. - Confirm lint passes for import sorting and run unit tests covering image/shared/system/workspace combinations for both edit and problem-read.
| /> | ||
| </div> | ||
| <template #footer> | ||
| <!-- 取消添加分段 --> |
There was a problem hiding this comment.
Review
No major irregularities. The diff is clean and consistent with the existing patterns (isSystemResource, perm.*.problemRead). Notes below are minor.
1. Import path consistency (verify)
import { perm } from '@/permission'Most codebases in this style import permissions at a specific granularity. Confirm the symbol exists and returns functions (it's called as perm.knowledge.system.problemRead() and perm.knowledge.workspace.problemRead(id) — i.e. methods, not booleans). If problemRead is a static boolean, calling it will throw at runtime. Double-check the API shape.
2. Naming / intent
knowledgeQuestionPermission()reads like a getter but acts as a permission check. A clearer name:canShowKnowledgeQuestions()orhasQuestionReadPerm(). Also, it takes no args yet closes overprops.knowledgeId— acceptable, but a named param would be more explicit:function canReadQuestions(knowledgeId: string): boolean { ... }
3. Called directly in template
:show="knowledgeQuestionPermission()"This runs on every re-render (cheap here, but if perm.*.problemRead ever becomes async or expensive it'll thrash). Safer pattern: a computed.
const showQuestions = computed(() => {
if (isSystemResource()) return Boolean(perm.knowledge.system.problemRead())
if (isSystemSharedResource()) return Boolean(perm.knowledge.share.problemRead())
if (isWorkspaceSharedResource()) return false
return Boolean(perm.knowledge.workspace.problemRead(props.knowledgeId))
})Then :show="showQuestions".
This also gives you a single place to unit-test the branch logic.
4. Ordering of branches (behavioral assumption)
The isWorkspaceSharedResource() → false branch means "shared KB is read-only on the workspace side." That encodes a product decision inside a view component. Consider moving the "which resource context am I in?" resolution into resource-context.ts (e.g. getResourceScope()), so this file only maps scope → permission. Keeps the switch testable and avoids drift if another context is added later.
Otherwise, ordering is correct: system → shared → workspace-shared → workspace.
5. Default fallthrough
If none of the is* guards match (e.g. an unknown/new context), it silently falls through to perm.knowledge.workspace.problemRead(props.knowledgeId). If that's intentional, fine; if a new context should be denied by default, make the default explicit:
return Boolean(perm.knowledge.workspace.problemRead(props.knowledgeId)) // default: own workspace6. props.knowledgeId vs targetKnowledgeId
The template passes :knowledge-id="targetKnowledgeId", but the permission check uses props.knowledgeId. Confirm these are always the same at render time. During open(...) there may be a tick where targetKnowledgeId is set but props.knowledgeId isn't (or vice versa) — then show could disagree with what ParagraphQuestions actually queries. If they can differ, use targetKnowledgeId for the check too:
function canReadQuestions(kid: string) { ... } // pass targetKnowledgeId
:show="canReadQuestions(targetKnowledgeId)"7. Comment language
Code comments (知识库「问题」只读权限判断, 共享知识库在工作空间侧只读) are Chinese while the project apparently mixes English naming. Not a bug — flag only if the repo lint/convention requires English comments.
8. defineExpose / reactivity (unchanged, just FYI)
knowledgeQuestionPermission is not exposed — fine, since it's only used in the template. No issue.
Summary of suggested changes
| # | Severity | Change |
|---|---|---|
| 1 | Check | Verify perm.*.problemRead are methods, not booleans |
| 3 | Low | Wrap in computed for clarity/perf/ testability |
| 6 | Low–Med | Align props.knowledgeId with targetKnowledgeId |
| 4 | Low | Move scope→permission mapping out of the view |
| 2 | Nit | Rename to canReadQuestions / canShowQuestions |
| <ParagraphQuestions v-model="relatedQuestions" :show="knowledgeQuestionPermission()" :knowledge-id="knowledgeId" document-id="" /> | ||
| </div> | ||
| <template #footer> | ||
| <!-- 取消分段编辑 --> |
There was a problem hiding this comment.
The diff is syntactically valid. Main concerns are reactivity, permission semantics, and ensuring the UI permission is actually enforced.
Potential issues
-
Permission function is called directly in the template
knowledgeQuestionPermission()runs on every render and is not cached.- If permission/resource state can change while the dialog is open, it may not update as expected unless all accessed state is reactive.
- Prefer a
computed.
-
showmay only hide the component, not enforce permission- If
ParagraphQuestionsstill mounts, fetches data, or keepsrelatedQuestions, the user may still submit previously selected questions. - Use
v-ifif the component should not mount without permission, and sanitize on submit:
if (!canReadQuestions.value) relatedQuestions.value = []
- Backend/API must also enforce the permission; do not rely on this UI check.
- If
-
Permission name vs. actual operation
- The comment says “read permission” (
problemRead), butParagraphQuestionsusesv-model, implying editing/selection. - If the component allows editing, a read-only permission may be insufficient. Verify whether it should check read, create, or update permission.
- If workspace-shared knowledge bases should be read-only, hiding the component may be wrong — consider a
readonly/disabledmode instead.
- The comment says “read permission” (
-
Workspace-shared branch is hardcoded to
false- Confirm this is the intended business rule.
- Also verify branch ordering: if a workspace-shared resource can also satisfy
isSystemSharedResource(), theisWorkspaceSharedResource()branch will never be reached.
-
Ensure
ParagraphQuestionsactually defines ashowprop- If it does not,
:showbecomes a fallthrough attribute and has no effect.
- If it does not,
-
Ensure permission getters return synchronous booleans
Boolean(perm.knowledge...())will betruefor aPromise. If any permission method is async, this code is incorrect.
-
Stale
relatedQuestions- If permission changes or
knowledgeIdchanges, previously selected questions may remain. Clear them when permission becomes false.
- If permission changes or
Recommended refactor
const canReadQuestions = computed(() => {
if (isSystemResource()) return Boolean(perm.knowledge.system.problemRead())
if (isSystemSharedResource()) return Boolean(perm.knowledge.share.problemRead())
if (isWorkspaceSharedResource()) return false
return Boolean(perm.knowledge.workspace.problemRead(props.knowledgeId))
})
watch(canReadQuestions, (allowed) => {
if (!allowed) relatedQuestions.value = []
})Template:
<ParagraphQuestions
v-if="canReadQuestions"
v-model="relatedQuestions"
:knowledge-id="knowledgeId"
document-id=""
/>Or, if show is required:
<ParagraphQuestions
v-model="relatedQuestions"
:show="canReadQuestions"
:knowledge-id="knowledgeId"
document-id=""
/>Optimization suggestions
- Use
computedinstead of a template function call. - Consider renaming to a boolean-style name such as
canReadQuestions. - Add tests for the permission matrix: system, system-shared, workspace-shared, and normal workspace.
- Centralize this permission logic if similar checks are used elsewhere.
fix: knowledge details document and image permission error