Skip to content

feat: 新用户默认分组可设置 (DefaultUserGroup) - #6599

Open
Syz87 wants to merge 3 commits into
QuantumNous:mainfrom
Syz87:feature/default-user-group
Open

feat: 新用户默认分组可设置 (DefaultUserGroup)#6599
Syz87 wants to merge 3 commits into
QuantumNous:mainfrom
Syz87:feature/default-user-group

Conversation

@Syz87

@Syz87 Syz87 commented Aug 2, 2026

Copy link
Copy Markdown

概述

新增 DefaultUserGroup 配置项,允许管理员通过后台设置新注册用户的默认分组。

改动 (10 files, +37 lines)

后端: constants.go / option.go / user.go
前端: quota-settings-section + types + billing + section-registry
i18n: en/zh 补文案

兼容性

默认值 'default' 与原版行为完全一致,仅对新注册用户生效。


⚠️ 此 PR 代码含 AI 辅助生成(Claude Code)。

Summary by CodeRabbit

  • New Features

    • Added a system setting to configure the default group assigned to newly registered users.
    • New users are automatically assigned to the configured group when no group is specified.
    • Added English and Chinese translations and guidance for the setting.
  • Configuration

    • The default group is set to default and can be updated through quota settings.
    • The selected group must be non-empty and configured in Group Ratio settings.
    • Group Ratio updates cannot remove the current default group.

新增 DefaultUserGroup 配置项,允许管理员设置新注册用户的默认分组,
替代原有写死的 'default' 值。

后端:
- common/constants.go: DefaultUserGroup 变量(默认 'default')
- model/option.go: OptionMap 注册 + UpdateOption switch case
- model/user.go: Insert/InsertWithTx 在 Group 为空时自动赋值

前端:
- 管理端 Quota Settings 新增「Default User Group」输入框
- types/billing/index/section-registry 补字段传递
- en/zh i18n 补文案

AI-assisted by Claude Code.
@coderabbitai

coderabbitai Bot commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Currently processing new changes in this PR. This may take a few minutes, please wait...

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 2da0dc67-86f7-4833-bf7c-e574eba06a8f

📥 Commits

Reviewing files that changed from the base of the PR and between 337aa02 and 2d2046c.

📒 Files selected for processing (3)
  • common/constants.go
  • model/user.go
  • setting/ratio_setting/group_ratio.go
 _________________________________________________________________________________
< CodeRabbi is my uncle. He's a bit more orthodox. Feel lucky you got me instead. >
 ---------------------------------------------------------------------------------
  \
   \   (\__/)
       (•ㅅ•)
       /   づ

Walkthrough

The PR adds a configurable default group for newly registered users. The backend validates and applies the setting during user creation. The frontend exposes the setting in quota settings with English and Chinese translations. Git ignores two custom archive files.

Changes

Default user group

Layer / File(s) Summary
Backend default group assignment
common/constants.go, model/option.go, model/user.go
The backend registers and validates DefaultUserGroup, updates it from system options, and assigns it when a new user has an empty group.
System settings field
web/src/features/system-settings/types.ts, web/src/features/system-settings/billing/index.tsx, web/src/features/system-settings/billing/section-registry.tsx, web/src/features/system-settings/general/quota-settings-section.tsx, web/src/i18n/locales/*.json
The billing settings type, defaults, quota form, validation schema, and translations include DefaultUserGroup.

Custom archive ignore rules

Layer / File(s) Summary
Archive ignore rules
.gitignore
Git ignores new-api-custom.tar and new-api-custom.tar.gz.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: calcium-ion

Poem

A rabbit sets the group with care,
New users find their settings there.
The quota form shows what to choose,
In both languages, clear news.
Custom archives stay out of sight—
Hop, hop, shipped right!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the addition of a configurable default group for new users, which is the main change.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
model/option.go (1)

219-236: 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Reject non-existent DefaultUserGroup values.

validateOptionValue accepts any DefaultUserGroup value before UpdateOption saves it, and new users are created with common.DefaultUserGroup. Add a group-existence validation for this option key so admins cannot set an invalid default group.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@model/option.go` around lines 219 - 236, Update UpdateOption to validate
DefaultUserGroup values against existing groups before persisting the option,
rejecting unknown group names with an error. Add this check alongside
validateOptionValue and reuse the existing group lookup or model symbols rather
than saving the invalid value or updating OptionMap.
🧹 Nitpick comments (1)
web/src/features/system-settings/general/quota-settings-section.tsx (1)

287-303: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Wrap the placeholder text in t() for i18n consistency.

The Input placeholder uses the bare string 'default'. Other placeholders in this file, such as the TopUpLink field's t('https://example.com/topup'), wrap literal example text in t(...).

Wrap this placeholder the same way for consistency with the rest of the form.

♻️ Proposed fix
-                    <Input placeholder='default' {...field} />
+                    <Input placeholder={t('default')} {...field} />

As per path instructions, web/**/*.{tsx,ts}: "面向用户的文案必须使用 i18n;React 组件使用 useTranslation()t()".

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@web/src/features/system-settings/general/quota-settings-section.tsx` around
lines 287 - 303, Update the Input for the DefaultUserGroup field in the quota
settings section to pass the placeholder text through the existing translation
function t(), matching the i18n handling used by other user-facing placeholders
in the form.

Source: Path instructions

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@web/src/features/system-settings/general/quota-settings-section.tsx`:
- Line 59: Update the DefaultUserGroup field in the settings validation schema
to require at least one character, preventing an empty value from being saved.
Preserve its existing string validation while adding the minimum-length
constraint.

---

Outside diff comments:
In `@model/option.go`:
- Around line 219-236: Update UpdateOption to validate DefaultUserGroup values
against existing groups before persisting the option, rejecting unknown group
names with an error. Add this check alongside validateOptionValue and reuse the
existing group lookup or model symbols rather than saving the invalid value or
updating OptionMap.

---

Nitpick comments:
In `@web/src/features/system-settings/general/quota-settings-section.tsx`:
- Around line 287-303: Update the Input for the DefaultUserGroup field in the
quota settings section to pass the placeholder text through the existing
translation function t(), matching the i18n handling used by other user-facing
placeholders in the form.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 2ac9e28a-298d-4cb6-8a45-03501488442b

📥 Commits

Reviewing files that changed from the base of the PR and between 0ab0202 and 28120fb.

📒 Files selected for processing (10)
  • .gitignore
  • common/constants.go
  • model/option.go
  • model/user.go
  • web/src/features/system-settings/billing/index.tsx
  • web/src/features/system-settings/billing/section-registry.tsx
  • web/src/features/system-settings/general/quota-settings-section.tsx
  • web/src/features/system-settings/types.ts
  • web/src/i18n/locales/en.json
  • web/src/i18n/locales/zh.json

Comment thread web/src/features/system-settings/general/quota-settings-section.tsx Outdated
根据 CodeRabbit review 建议修复:
- model/option.go: validateOptionValue 校验 DefaultUserGroup 非空且必须存在于分组比例设置(Group Ratio)中,防止管理员设置不存在的默认分组导致新用户路由失败
- quota-settings-section.tsx: DefaultUserGroup schema 加 min(1) 防止空值保存;placeholder 改用 t('default') 与同文件其他字段 i18n 一致

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
model/option.go (1)

542-543: 🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift

Synchronize reads and writes to common.DefaultUserGroup.

updateOptionMap writes common.DefaultUserGroup under common.OptionMapRWMutex, but User.Insert and User.InsertWithTx can read it concurrently without the same lock. Put these writes and reads on the same synchronized getter/setter path, or use another atomic configuration mechanism.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@model/option.go` around lines 542 - 543, Synchronize access to
common.DefaultUserGroup across the updateOptionMap case and the User.Insert and
User.InsertWithTx readers. Route both writes and reads through the same locked
getter/setter path, or apply an equivalent atomic configuration mechanism,
ensuring no direct unsynchronized access remains.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@model/option.go`:
- Around line 217-224: Update UpdateGroupRatioByJSONString so it validates the
configured common.DefaultUserGroup against the incoming group-ratio map before
replacing groupRatioMap, rejecting changes that remove the default group;
preserve the existing map update only after validation succeeds.

---

Outside diff comments:
In `@model/option.go`:
- Around line 542-543: Synchronize access to common.DefaultUserGroup across the
updateOptionMap case and the User.Insert and User.InsertWithTx readers. Route
both writes and reads through the same locked getter/setter path, or apply an
equivalent atomic configuration mechanism, ensuring no direct unsynchronized
access remains.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 042a919e-da90-4528-aa2c-d661b5e81bbd

📥 Commits

Reviewing files that changed from the base of the PR and between 28120fb and 337aa02.

📒 Files selected for processing (2)
  • model/option.go
  • web/src/features/system-settings/general/quota-settings-section.tsx
🚧 Files skipped from review as they are similar to previous changes (1)
  • web/src/features/system-settings/general/quota-settings-section.tsx

Comment thread model/option.go
- common/constants.go: 新增 GetDefaultUserGroup() 加锁读取(OptionMapRWMutex.RLock),与 updateOptionMap 的写锁配对,避免 User.Insert/InsertWithTx 并发读取竞态
- model/user.go: Insert/InsertWithTx 改用 common.GetDefaultUserGroup() 读取
- setting/ratio_setting/group_ratio.go: UpdateGroupRatioByJSONString 拒绝移除当前默认分组(DefaultUserGroup),防止新用户被分配到不存在的分组
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.

1 participant