feat(rbac): F3 前端 RBAC 按钮级控制
后端: - 新增 GET /user/permissions 端点返回当前用户权限码 - Login 响应附带 permissions 数组 - collectUserPermissionCodes 用 raw SQL JOIN 避免 GORM many2many 表名问题 - admin 用户返回 ['*'] 通配符 前端: - usePermission hook (hasPermission/hasAnyPermission/hasAllPermissions) - <HasPermission> 组件 (permission/anyOf/allOf + fallback) - zustand store 新增 permissions 字段 - Sidebar 菜单递归过滤 (42 菜单项全部标注 permission) - ServerManager: 添加/导入/批量删除按钮按权限显隐 - ServerTableRow: 终端/编辑/文件/删除按权限显隐 - SystemUserTab: 新建/导入/编辑/删除按钮按权限显隐 - MainLayout: 页面刷新自动拉取权限 - Login: 登录时存储 permissions 验证: - admin: permissions=['*'] 通配符 - viewer: 7 个只读权限 (dashboard:view/server:view/...) - viewer 无 server:create/delete - 10/10 回归测试通过 - npm run build 零错误
This commit is contained in:
1 parent
77c234b060
commit
be2a377f01
30 files changed
+2328
-462
No files matched your search
@@ -219,13 +219,17 @@ func (h *UserHandler) Login(c *gin.Context) {
|
||||
|
||||
h.recordLoginLog(c, user.Username, "success", "", "password")
|
||||
|
||||
// 聚合用户权限码(前端 RBAC 按钮级控制)
|
||||
permissions := collectUserPermissionCodes(h.db, user.ID, user.IsAdmin)
|
||||
|
||||
c.JSON(http.StatusOK, model.Response{
|
||||
Code: 200,
|
||||
Message: "登录成功",
|
||||
Data: model.LoginResponse{
|
||||
Token: token,
|
||||
ExpiresIn: h.cfg.JWT.ExpireHour * 3600,
|
||||
User: &user,
|
||||
Data: gin.H{
|
||||
"token": token,
|
||||
"expires_in": h.cfg.JWT.ExpireHour * 3600,
|
||||
"user": &user,
|
||||
"permissions": permissions,
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -321,6 +325,72 @@ func (h *UserHandler) GetProfile(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
// GetMyPermissions 获取当前用户的权限码列表(前端 RBAC 按钮级控制使用)
|
||||
// GET /api/v1/user/permissions
|
||||
// 返回扁平化的权限 code 数组(聚合用户所有角色的权限)。is_admin=true 返回 ["*"] 通配符。
|
||||
func (h *UserHandler) GetMyPermissions(c *gin.Context) {
|
||||
userID := middleware.GetUserID(c)
|
||||
isAdmin := middleware.GetIsAdmin(c)
|
||||
|
||||
// 管理员返回通配符
|
||||
if isAdmin {
|
||||
c.JSON(http.StatusOK, model.Response{
|
||||
Code: 200,
|
||||
Message: "success",
|
||||
Data: map[string]any{
|
||||
"permissions": []string{"*"},
|
||||
"is_admin": true,
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
codes := collectUserPermissionCodes(h.db, userID, false)
|
||||
|
||||
c.JSON(http.StatusOK, model.Response{
|
||||
Code: 200,
|
||||
Message: "success",
|
||||
Data: map[string]any{
|
||||
"permissions": codes,
|
||||
"is_admin": false,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// collectUserPermissionCodes 聚合用户的所有权限 code(用于 login 响应附加)
|
||||
// 容错设计:用 raw SQL 避免 GORM many2many 关联表名不一致问题
|
||||
func collectUserPermissionCodes(db *gorm.DB, userID uint, isAdmin bool) []string {
|
||||
if isAdmin {
|
||||
return []string{"*"}
|
||||
}
|
||||
// raw SQL: user → iam_user_roles → iam_roles → role_permissions → iam_permissions
|
||||
// 逐层 JOIN,任一层缺失自然返回空
|
||||
type permRow struct {
|
||||
Code string
|
||||
}
|
||||
var rows []permRow
|
||||
err := db.Raw(`
|
||||
SELECT DISTINCT p.code
|
||||
FROM iam_user_roles ur
|
||||
JOIN iam_roles r ON r.id = ur.role_id AND r.status = 1
|
||||
JOIN role_permissions rp ON rp.role_id = r.id
|
||||
JOIN iam_permissions p ON p.id = rp.permission_id AND p.status = 1
|
||||
WHERE ur.user_id = ?
|
||||
`, userID).Scan(&rows).Error
|
||||
if err != nil || len(rows) == 0 {
|
||||
return []string{}
|
||||
}
|
||||
permSet := make(map[string]struct{}, len(rows))
|
||||
for _, row := range rows {
|
||||
permSet[row.Code] = struct{}{}
|
||||
}
|
||||
codes := make([]string, 0, len(permSet))
|
||||
for code := range permSet {
|
||||
codes = append(codes, code)
|
||||
}
|
||||
return codes
|
||||
}
|
||||
|
||||
// UpdateProfile 更新当前用户信息
|
||||
// @Summary 更新当前用户信息
|
||||
// @Description 更新当前登录用户的信息
|
||||
|
||||
Reference in new issue
Block a user