## 核心功能 - 添加 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 规范 ## 国际化支持 - 中英文提示信息完整 - 自动授权成功/失败提示
64 lines
1.9 KiB
Go
64 lines
1.9 KiB
Go
package admin
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/handler/dto"
|
|
"github.com/Wei-Shaw/sub2api/internal/pkg/response"
|
|
"github.com/Wei-Shaw/sub2api/internal/service"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// AdminAPIKeyHandler handles admin API key management
|
|
type AdminAPIKeyHandler struct {
|
|
adminService service.AdminService
|
|
}
|
|
|
|
// NewAdminAPIKeyHandler creates a new admin API key handler
|
|
func NewAdminAPIKeyHandler(adminService service.AdminService) *AdminAPIKeyHandler {
|
|
return &AdminAPIKeyHandler{
|
|
adminService: adminService,
|
|
}
|
|
}
|
|
|
|
// AdminUpdateAPIKeyGroupRequest represents the request to update an API key's group
|
|
type AdminUpdateAPIKeyGroupRequest struct {
|
|
GroupID *int64 `json:"group_id"` // nil=不修改, 0=解绑, >0=绑定到目标分组
|
|
}
|
|
|
|
// UpdateGroup handles updating an API key's group binding
|
|
// PUT /api/v1/admin/api-keys/:id
|
|
func (h *AdminAPIKeyHandler) UpdateGroup(c *gin.Context) {
|
|
keyID, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
response.BadRequest(c, "Invalid API key ID")
|
|
return
|
|
}
|
|
|
|
var req AdminUpdateAPIKeyGroupRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.BadRequest(c, "Invalid request: "+err.Error())
|
|
return
|
|
}
|
|
|
|
result, err := h.adminService.AdminUpdateAPIKeyGroupID(c.Request.Context(), keyID, req.GroupID)
|
|
if err != nil {
|
|
response.ErrorFrom(c, err)
|
|
return
|
|
}
|
|
|
|
resp := struct {
|
|
APIKey *dto.APIKey `json:"api_key"`
|
|
AutoGrantedGroupAccess bool `json:"auto_granted_group_access"`
|
|
GrantedGroupID *int64 `json:"granted_group_id,omitempty"`
|
|
GrantedGroupName string `json:"granted_group_name,omitempty"`
|
|
}{
|
|
APIKey: dto.APIKeyFromService(result.APIKey),
|
|
AutoGrantedGroupAccess: result.AutoGrantedGroupAccess,
|
|
GrantedGroupID: result.GrantedGroupID,
|
|
GrantedGroupName: result.GrantedGroupName,
|
|
}
|
|
response.Success(c, resp)
|
|
}
|