Skip to content

fix: permission for debug and save buttons - #8095

Merged
peslyn merged 1 commit into
v3from
pr@v3@fix_permission_for_debug_and_save_buttons
Nov 23, 2026
Merged

peslyn merged 1 commit into
v3from
pr@v3@fix_permission_for_debug_and_save_buttons

Conversation

@shaohuzhang1

Copy link
Copy Markdown
Contributor

fix: permission for debug and save buttons

<el-button v-if="canEdit" type="primary" :disabled="disabled || !hasChanges || applying" @click="handleSave">{{ t('common.actions.save') }}</el-button>
</div>
</div>
</template>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the review of the code changes, covering potential issues, logic irregularities, and optimization suggestions.

1. Critical Logic Bug: Redundant/Incorrect done() Call

Location: Inside handleBeforeClose, specifically within the .then() block of the main confirmation dialog (lines ~215-216).

    .then(() => {
      handleSave()
      done() // <-- POTENTIAL ISSUE HERE
    })
  • Issue: In Element Plus (and similar modal libraries), the before-close function receives a done callback to close the dialog. If you call done() inside a promise chain after an asynchronous action (like handleSave), you are manually closing the dialog. However, typically handleSave emits an event, which might trigger parent component logic.
  • The Bug: If handleSave fails or is blocked by validation/prompting in the parent, calling done() immediately closes the UI without handling the error state properly. More importantly, in many patterns, if you emit a save event and the parent handles the closing, you should not call done() here. If you do want to close it locally, ensure handleSave doesn't rely on async success confirmation before closing.
  • Specific Irregularity: The original code likely relied on the user clicking "Cancel" in the confirm dialog or letting the dialog stay open until the parent updated it. Adding done() forces closure. If handleSave is synchronous emission, this is fine. But if handleSave triggers an API call, closing the modal immediately prevents showing loading states or errors related to saving within this modal context.
  • Recommendation: Verify if handleSave returns a promise or if the parent needs to confirm closure. If the goal is simply to close the UI, it's okay, but ensure UX consistency (no sudden close if save fails elsewhere).

2. Logical Inconsistency in handleBeforeClose

Location: The new if (!props.canEdit) block vs. the existing block.

  // 无编辑权限:只保留「直接关闭」,不提供保存,任何关闭方式都直接丢弃改动。
  if (!props.canEdit) {
    return MsgConfirm(t('common.dialogs.notice'), t('workflow.defaultModel.unsavedMessage'), {
      confirmButtonText: t('workflow.defaultModel.closeDirectly'),
      showCancelButton: false,
      confirmButtonType: 'primary',
    })
      .then(() => done())
      .catch(() => done())
  }
  • Irregularity: When canEdit is false, there is a warning about "unsavedMessage", but the buttons say "Close Directly". This implies that even read-only users might have accidental changes? No, if canEdit is false, inputs should presumably be disabled.
  • Potential Issue: If inputs are truly disabled/read-only, hasChanges.value should always be false. If so, the before-close hook shouldn't even be trying to warn about unsaved changes. You might want to check if (!props.canEdit || !hasChanges.value) at the start of handleBeforeClose to just return done() immediately without any confirmation dialog for read-only view. Showing a confirmation dialog to a read-only user who didn't touch anything is bad UX.

3. Prop Definition & Type Safety

Location: withDefaults(defineProps<...>(), { canEdit: true })

  • Positive: Good practice using withDefaults.
  • Suggestion: Ensure modelApi usage is consistent. The type typeof ModelApi | typeof SystemModelApi ... suggests these are classes. Verify that new modelApi() or modelApi.method() is handled correctly downstream based on the union type. TypeScript might complain about method access if signatures differ significantly between APIs.

4. Template Optimization & UX

Location: Footer template changes.

<el-button plain v-if="canEdit" :disabled="disabled" ...>{{ ... }}</el-button>
  • Suggestion: Instead of just hiding buttons with v-if="canEdit", consider if they should be :disabled="!canEdit" and shown greyed out. Hidden controls can be confusing for users wondering why the layout shifts or where the save button went. However, if read-only mode is strict, hiding them is cleaner.
  • Consistency: Cancel button remains visible even when canEdit is false (presumably to allow closing). Save/Apply-to-all are hidden. This is logically sound for a strict read-only mode.

5. handleSave Guard

Location:

function handleSave() {
  if (props.disabled || !props.canEdit || !hasChanges.value) return
  emit('save', cloneDeep(modelSettings.value))
}
  • Good: Correctly guards against saving when !canEdit.
  • Note: Since canEdit is used in the template to hide the Save button, this guard in JS is technically redundant unless handleSave is exposed or called programmatically via defineExpose. It is good defensive coding though.

Summary of Recommendations

  1. Check done() placement in .then(): Ensure that forcing the dialog closed immediately after emitting 'save' aligns with your application's global state management. If the parent component usually controls closing, remove done() there.
  2. Simplify Read-Only Experience: Add an early return in handleBeforeClose if !props.canEdit && !hasChanges.value (or just !props.canEdit) to bypass the confirmation dialog entirely for read-only users, assuming inputs are disabled. A confirmation dialog for "No Changes / Cannot Edit" is often unnecessary noise.
  3. TypeScript Check: Run tsc to ensure that calling methods on props.modelApi works with the union type definition without excessive casting.

<el-button v-if="canEdit" type="primary" :loading="loading" @click="handleSubmit">{{ t('common.actions.save') }}</el-button>
</template>
</MkDrawer>
</template>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here are the issues and suggestions:

Issues

1. currentModelId used before definition (potential bug)
The canEdit computed references currentModelId.value, but in the diff snippet, currentModelId is not defined until later (it's not visible in the shown context — likely defined below). If currentModelId is declared after the canEdit computed (i.e., with const currentModelId = ref(...) lower in the file), this throws a ReferenceError / TDZ error at setup time when the computed is first evaluated. Move canEdit below currentModelId's declaration.

2. Props/emits ordering & placement
canEdit is now declared between useI18n and defineProps. While functionally fine, it's cleaner to keep defineProps/defineEmits/defineOptions grouped near the top and put computed logic after all reactive state (currentModelId, loading, etc.) has been declared.

3. Button hidden vs disabled
Hiding the Save button (v-if="canEdit") changes UX: users without permissions see an editable form (assuming inputs aren't also gated by canEdit) but no Save button — confusing. Consider:

  • Either disable the whole form when !canEdit (best UX), or
  • Keep the button but :disabled="!canEdit".

Currently the form fields appear editable but cannot be saved, which is inconsistent.

4. Submit bypass risk
handleSubmit itself should still guard: if (!canEdit.value) return. Hiding the button is UI-only; programmatic calls (e.g. keyboard Enter, external open flow, tests) could still trigger submit. Defense-in-depth: re-check permission inside handleSubmit.

5. computed import
Ensure computed is imported from vue. The diff doesn't show it being added — if not already imported above line 11, this will break.

6. Permission reactivity
perm.model.system.modify() etc. — confirm these are reactive accessor functions that re-evaluate. If they read non-reactive nuqs/permission state, canEdit may not update on permission changes. Also isSystemResource() / isSystemSharedResource() — verify these are reactive (computed-backed) and don't rely on mutable globals read once.

7. Ordering of permission checks
isSystemResource() is checked before isSystemSharedResource(). If a model can be both system and shared, only the system branch runs. Confirm precedence is intended (usually system is a superset/most restrictive, so this is likely fine — just document it).

Optimization

  • Simplify with early return into a single expression if branches are mutually exclusive:

    const canEdit = computed(() => {
      if (!currentModelId.value) return false
      if (isSystemResource()) return perm.model.system.modify()
      if (isSystemSharedResource()) return perm.model.share.modify()
      return perm.model.workspace.modify(currentModelId.value)
    })

    This is already clear — no change needed there; just fix placement (issue build(deps): bump the pip group across 1 directory with 2 updates #1).

  • Consider memoizing nothing here; computed already caches. Fine.

Summary of required fixes

  1. Move canEdit after currentModelId declaration.
  2. Verify computed is imported.
  3. Add a permission guard inside handleSubmit.
  4. Decide UX: prefer disabling the form/button over hiding the button, to avoid an editable-but-unsavable form.

:can-edit="canEdit"
@save="emit('save', $event)"
@apply-to-all="emit('applyToAll', $event)"
@closed="handleDrawerClosed"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code snippet is a Vue 3 Composition API update. Here are the observations:

1. Correctness & Consistency (Good)

  • The change is consistent: canEdit is properly added to the props type definition, given a default value (true) in withDefaults, and bound to the child component template using the kebab-case syntax (:can-edit). This follows Vue conventions.

2. Potential Issues

  • Redundant Prop Transmission: The parent passes both disabled and canEdit to the child. These concepts often overlap (e.g., if something is disabled, it usually cannot be edited). If the child component only needs to know "is this view/editable?", sending two boolean flags might lead to inconsistent states (e.g., disabled=true but canEdit=true). Consider consolidating these into a single prop or ensuring the child logic strictly prioritizes one over the other.
  • Default Value Ambiguity: Defaults to true. Ensure that when consumers omit canEdit, the intended behavior is indeed "editable by default." If the component is sensitive (e.g., admin settings), false (read-only by default) is often safer.

3. Optimization/Style Suggestions

  • Typing: If ModelApi, SystemModelApi, and SharedModelApi share a common interface for the properties used by the component, consider using that shared interface instead of the union of specific classes. This simplifies types and reduces coupling.
  • Prop Naming: getGraphData as a prop name suggests a function is being passed. While valid, ensure this is intentional (dependency injection pattern) rather than passing data directly. If it's just to fetch fresh data on demand, consider using provide/inject or Pinia/Vuex for cleaner state management if applicable.

Verdict: No syntax errors. The main concern is logical consistency between disabled and canEdit.

@peslyn
peslyn merged commit cb1715c into v3 Nov 23, 2026
3 of 4 checks passed
@peslyn
peslyn deleted the pr@v3@fix_permission_for_debug_and_save_buttons branch November 23, 2026 09:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants