Skip to content

refactor: update user form handling to support optional role settings and improve payload submission - #8118

Merged
wxg0103 merged 1 commit into
v3from
pr@v3@fix_ce_user_group
Nov 24, 2026
Merged

wxg0103 merged 1 commit into
v3from
pr@v3@fix_ce_user_group

Conversation

@shaohuzhang1

Copy link
Copy Markdown
Contributor

refactor: update user form handling to support optional role settings and improve payload submission

@wxg0103
wxg0103 merged commit 66e8672 into v3 Nov 24, 2026
3 of 4 checks passed
@wxg0103
wxg0103 deleted the pr@v3@fix_ce_user_group branch November 24, 2026 07:48
<section v-if="hasRoleSetting">
<h4 class="mk-title-decoration mb-4 mt-4">{{ t('user.role.settingsTitle') }}</h4>
<MkFormList v-model="userForm.role_setting" :add-text="t('user.role.add')" :default-item="{ role_id: '', workspace_ids: [] }">
<template #default="{ index, item: roleAssignment }">

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.

Code Review

Potential Issues

1. Type mismatch in getSubmitPayload (Critical)

const payload: SystemUserPayload = { ...userForm }

SystemUserForm is defined as Omit<SystemUserPayload, 'role_setting'> & { role_setting: SystemUserRoleAssignment[] }. If SystemUserPayload's original role_setting type is not the same array type, the spread will produce an incompatible object; TS may silently allow it only because you then delete payload.role_setting. Verify that SystemUserPayload requires role_setting — if it does, deleting it and returning as SystemUserPayload is misleading. Consider making it optional:

type SubmitPayload = Omit<SystemUserPayload, 'role_setting'> & { role_setting?: SystemUserRoleAssignment[] }

2. CE mode still submits no role_setting, but UI shows workspace (Logical inconsistency)

In CE (hasRoleSetting === false):

  • showUserGroup is true → user group section is rendered.
  • But getSubmitPayload deletes role_setting, so the selected workspaces are never sent.

If CE backend expects workspace_ids some other way, this is correct; otherwise the UI collects data it discards. Confirm with backend contract.

3. isUserRole fallback semantics changed for CE

if (!hasRoleSetting.value) return roleId !== ROLE_TYPE.ADMIN

Previously isUserRole returned undefined/false when options weren't loaded. Now any non-admin role_id passes — including empty string '' (the default item's role_id). That means showUserGroup evaluates truthy while an empty role row exists. Combined with #2 this may be intentional but worth a comment.

4. resetData sets workspaceOptions to a default entry, but open() for edit overwrites

loadDefaultPassword/open don't reinitialize workspaceOptions in the non-EE path. If resetData runs before open, fine; if open is called twice without reset, stale state persists. Check call order.

5. computed(() => auth.isEE || auth.isPE) name

hasRoleSetting reads like a boolean flag but depends on runtime store reactivity — fine, but the name "setting" is ambiguous. Prefer supportsRoleSettings or isCommercialEdition.

Optimization Suggestions

6. Avoid spreading then deleting

Cleaner:

function getSubmitPayload(): SystemUserPayload {
  if (hasRoleSetting.value) return userForm
  const { role_setting, ...rest } = userForm
  return rest as SystemUserPayload
}

This makes intent explicit and avoids mutating a typed copy loosely.

7. Extract isAdminRole/isUserRole guard duplication

Both functions rely on ROLE_TYPE; consider grouping into a helper roleTypeOf(id).

8. DEFAULT_WORKSPACE_ID placement

Top-level constant near ROLE_TYPE imports would be more discoverable than in component scope (if reused elsewhere). If only used here, fine — but document why CE uses 'default'.

9. watch(showUserGroup) cleanup

Ensure the watcher properly clears role_setting[i].workspace_ids for all assignments, not just the first; the diff doesn't show this body — verify it handles multiple rows in CE where role_setting may contain empty items.

10. Minor: computed chained in template

v-if="hasRoleSetting" now replaces inline auth.isEE || auth.isPE in two places — good, but also apply to line 193 area already done. Consistent. ✔

Summary of Most Important Checks

  • ✅ Confirm SystemUserPayload type allows deletion of role_setting (or make it Partial).
  • ⚠️ Decide whether CE should submit workspace_ids (currently dropped).
  • ⚠️ Verify isUserRole('') returning true in CE doesn't cause empty-role warnings on submit.

role_setting?: SystemUserRoleAssignment[]
encrypted?: boolean
user_group_ids?: string[]
}

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.

Analysis of the change

The diff makes role_setting optional (?) in SystemUserPayload. Observations:

1. Correctness concern — context matters

  • If this interface is used for create/update (write) payloads, making it optional may be intentional (e.g., an update that doesn't touch roles).
  • If used for creation where roles are required, making it optional removes a compile-time guarantee. You'd then need runtime validation on the server, or you risk creating users with no roles.

2. Inconsistency within the interface

  • Most other array/optional fields already use ? (password?, user_group_ids?), so this is consistent stylistically.
  • However, verify the backend contract: if the API expects role_setting always present (even as []), sending it omitted vs. sending [] can have different semantics (clear vs. no-op). Confirm which behavior the endpoint implements.

3. Type safety / downstream usage

  • Any code doing payload.role_setting.map(...) or .length without a null check will now fail type-checking or throw at runtime. Search all consumers and add guards:
    payload.role_setting?.map(...)
  • Consider whether partial updates distinguish "field absent = don't change" from "field = [] = clear all roles." If not modeled explicitly, this is a source of silent bugs.

4. Better modeling suggestion

  • Separate types reduce ambiguity:
    export interface SystemUserPayload {         // create: roles required
      role_setting: SystemUserRoleAssignment[]
      ...
    }
    export interface SystemUserUpdatePayload {  // update: roles optional
      role_setting?: SystemUserRoleAssignment[]
      ...
    }
    Or use Partial<...> / Omit<> composition instead of one loosened interface serving both purposes.

5. Validation/serialization

  • Ensure the serializer omits undefined rather than emitting "role_setting": null, and that request validation reflects the new optionality (e.g., Zod/Joi schema, OpenAPI spec) to avoid client/server drift.

Summary: The change is syntactically fine and stylistically consistent, but verify (a) the API's required/optional contract and empty-vs-absent semantics, (b) that all consumers handle a possibly-undefined value, and (c) consider splitting create vs. update payload types for stronger guarantees.

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