Skip to content

feat(admin): 添加管理员直接修改用户 API Key 分组的功能 #670

Merged
Wei-Shaw merged 3 commits into
Wei-Shaw:mainfrom
DaydreamCoding:feat/admin-apikey-group-update
Feb 28, 2026
Merged

feat(admin): 添加管理员直接修改用户 API Key 分组的功能 #670
Wei-Shaw merged 3 commits into
Wei-Shaw:mainfrom
DaydreamCoding:feat/admin-apikey-group-update

Conversation

@DaydreamCoding

Copy link
Copy Markdown
Contributor
  • 新增 PUT /api/v1/admin/api-keys/:id 端点,允许管理员修改任意用户 API Key 的分组绑定
  • 跳过用户级权限校验但保留分组有效性验证,修改后触发认证缓存失效
  • Service 层支持三态语义:nil=不修改,0=解绑,>0=绑定,<0=拒绝
  • 指针值拷贝保证安全隔离,负数 groupID 返回 400 INVALID_GROUP_ID
  • 前端 UserApiKeysModal 新增可点击的分组选择下拉框,支持多 Key 并发更新
  • 下拉支持视口翻转和滚动关闭,按钮有 disabled 和加载状态
  • 覆盖:后端 20 个单元测试 (Service 11 + Handler 9) + 前端 16 个 E2E 测试
  • golangci-lint 0 issues, make test-unit 全部通过

@DaydreamCoding DaydreamCoding changed the title feat(admin): 添加管理员直接修改用户 API Key 分组的功能 feat(admin): 添加管理员直接修改用户 API Key 分组的功能 Feb 27, 2026
QTom added 2 commits February 28, 2026 20:18
- 新增 PUT /api/v1/admin/api-keys/:id 端点,允许管理员修改任意用户 API Key 的分组绑定
- 跳过用户级权限校验但保留分组有效性验证,修改后触发认证缓存失效
- Service 层支持三态语义:nil=不修改,0=解绑,>0=绑定,<0=拒绝
- 指针值拷贝保证安全隔离,负数 groupID 返回 400 INVALID_GROUP_ID
- 前端 UserApiKeysModal 新增可点击的分组选择下拉框,支持多 Key 并发更新
- 下拉支持视口翻转和滚动关闭,按钮有 disabled 和加载状态
- 覆盖:后端 20 个单元测试 (Service 11 + Handler 9) + 前端 16 个 E2E 测试
- golangci-lint 0 issues, make test-unit 全部通过
## 核心功能
- 添加 AdminUpdateAPIKeyGroupID 服务方法,支持绑定/解绑/保持不变三态语义
- 实现 UserRepository.AddGroupToAllowedGroups 接口,自动同步专属分组权限
- 添加 HTTP PUT /api-keys/:id handler 端点,支持管理员直接修改 API Key 分组

## 事务一致性
- 使用 ent Tx 保证专属分组绑定时「添加权限」和「更新 Key」的原子性
- Repository 方法支持 clientFromContext,兼容事务内调用
- 事务失败时自动回滚,避免权限孤立

## 业务逻辑
- 订阅类型分组阻断,需通过订阅管理流程
- 非活跃分组拒绝绑定
- 负 ID 和非法 ID 验证
- 自动授权响应,告知管理员成功授权的分组

## 代码质量
- 16 个单元测试覆盖所有业务路径和边界用例
- 7 个 handler 集成测试覆盖 HTTP 层
- GroupRepo stub 返回克隆副本,防止测试间数据泄漏
- API 类型安全修复(PaginatedResponse<ApiKey>)
- 前端 ref 回调类型对齐 Vue 规范

## 国际化支持
- 中英文提示信息完整
- 自动授权成功/失败提示
@DaydreamCoding
DaydreamCoding force-pushed the feat/admin-apikey-group-update branch from dc25630 to 9a91815 Compare February 28, 2026 12:29
feat/admin-apikey-group-update 分支给 UserRepository 接口新增了
AddGroupToAllowedGroups 方法,需要在测试 stub 中补充实现以通过编译。
- sora_client_handler_test.go: stubUserRepoForHandler
- sora_generation_service_test.go: stubUserRepoForQuota
@Wei-Shaw
Wei-Shaw merged commit 4587c3e into Wei-Shaw:main Feb 28, 2026
3 of 4 checks passed
@DaydreamCoding
DaydreamCoding deleted the feat/admin-apikey-group-update branch March 1, 2026 11:48
xuebkgithub pushed a commit to xuebkgithub/sub2api that referenced this pull request Mar 3, 2026
…-group-update

feat(admin): 添加管理员直接修改用户 API Key 分组的功能
@PMExtra

PMExtra commented Mar 4, 2026

Copy link
Copy Markdown
Contributor

bug反馈:前端界面只能选择标准(余额)分组,无法选择订阅分组。

liafonx pushed a commit to liafonx/sub2api that referenced this pull request Mar 20, 2026
…-group-update

feat(admin): 添加管理员直接修改用户 API Key 分组的功能
langhun pushed a commit to langhun/sub2api that referenced this pull request May 20, 2026
…-group-update

feat(admin): 添加管理员直接修改用户 API Key 分组的功能
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.

3 participants