feat: 实现 RBAC 权限系统和数据权限控制 (004-rbac-data-permission)
主要功能: - 实现完整的 RBAC 权限系统(账号、角色、权限的多对多关联) - 基于 owner_id + shop_id 的自动数据权限过滤 - 使用 PostgreSQL WITH RECURSIVE 查询下级账号 - Redis 缓存优化下级账号查询性能(30分钟过期) - 支持多租户数据隔离和层级权限管理 技术实现: - 新增 Account、Role、Permission 模型及关联关系表 - 实现 GORM Scopes 自动应用数据权限过滤 - 添加数据库迁移脚本(000002_rbac_data_permission、000003_add_owner_id_shop_id) - 完善错误码定义(1010-1027 为 RBAC 相关错误) - 重构 main.go 采用函数拆分提高可读性 测试覆盖: - 添加 Account、Role、Permission 的集成测试 - 添加数据权限过滤的单元测试和集成测试 - 添加下级账号查询和缓存的单元测试 - 添加 API 回归测试确保向后兼容 文档更新: - 更新 README.md 添加 RBAC 功能说明 - 更新 CLAUDE.md 添加技术栈和开发原则 - 添加 docs/004-rbac-data-permission/ 功能总结和使用指南 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
24
internal/routes/account.go
Normal file
24
internal/routes/account.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/handler"
|
||||
)
|
||||
|
||||
// registerAccountRoutes 注册账号相关路由
|
||||
func registerAccountRoutes(api fiber.Router, h *handler.AccountHandler) {
|
||||
accounts := api.Group("/accounts")
|
||||
|
||||
// 账号 CRUD
|
||||
accounts.Post("", h.Create) // POST /api/v1/accounts
|
||||
accounts.Get("", h.List) // GET /api/v1/accounts
|
||||
accounts.Get("/:id", h.Get) // GET /api/v1/accounts/:id
|
||||
accounts.Put("/:id", h.Update) // PUT /api/v1/accounts/:id
|
||||
accounts.Delete("/:id", h.Delete) // DELETE /api/v1/accounts/:id
|
||||
|
||||
// 账号-角色关联
|
||||
accounts.Post("/:id/roles", h.AssignRoles) // POST /api/v1/accounts/:id/roles
|
||||
accounts.Get("/:id/roles", h.GetRoles) // GET /api/v1/accounts/:id/roles
|
||||
accounts.Delete("/:account_id/roles/:role_id", h.RemoveRole) // DELETE /api/v1/accounts/:account_id/roles/:role_id
|
||||
}
|
||||
25
internal/routes/health.go
Normal file
25
internal/routes/health.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"github.com/break/junhong_cmp_fiber/pkg/response"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
// registerHealthRoutes 注册健康检查路由
|
||||
// 不需要认证,用于负载均衡器和监控系统
|
||||
func registerHealthRoutes(app *fiber.App) {
|
||||
// 健康检查
|
||||
app.Get("/health", func(c *fiber.Ctx) error {
|
||||
return response.Success(c, fiber.Map{
|
||||
"status": "healthy",
|
||||
"service": "junhong_cmp_fiber",
|
||||
})
|
||||
})
|
||||
|
||||
// 就绪检查
|
||||
app.Get("/ready", func(c *fiber.Ctx) error {
|
||||
return response.Success(c, fiber.Map{
|
||||
"status": "ready",
|
||||
})
|
||||
})
|
||||
}
|
||||
20
internal/routes/permission.go
Normal file
20
internal/routes/permission.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/handler"
|
||||
)
|
||||
|
||||
// registerPermissionRoutes 注册权限相关路由
|
||||
func registerPermissionRoutes(api fiber.Router, h *handler.PermissionHandler) {
|
||||
permissions := api.Group("/permissions")
|
||||
|
||||
// 权限 CRUD
|
||||
permissions.Post("", h.Create) // POST /api/v1/permissions
|
||||
permissions.Get("", h.List) // GET /api/v1/permissions
|
||||
permissions.Get("/tree", h.GetTree) // GET /api/v1/permissions/tree (注意:放在 :id 之前避免路由冲突)
|
||||
permissions.Get("/:id", h.Get) // GET /api/v1/permissions/:id
|
||||
permissions.Put("/:id", h.Update) // PUT /api/v1/permissions/:id
|
||||
permissions.Delete("/:id", h.Delete) // DELETE /api/v1/permissions/:id
|
||||
}
|
||||
24
internal/routes/role.go
Normal file
24
internal/routes/role.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/handler"
|
||||
)
|
||||
|
||||
// registerRoleRoutes 注册角色相关路由
|
||||
func registerRoleRoutes(api fiber.Router, h *handler.RoleHandler) {
|
||||
roles := api.Group("/roles")
|
||||
|
||||
// 角色 CRUD
|
||||
roles.Post("", h.Create) // POST /api/v1/roles
|
||||
roles.Get("", h.List) // GET /api/v1/roles
|
||||
roles.Get("/:id", h.Get) // GET /api/v1/roles/:id
|
||||
roles.Put("/:id", h.Update) // PUT /api/v1/roles/:id
|
||||
roles.Delete("/:id", h.Delete) // DELETE /api/v1/roles/:id
|
||||
|
||||
// 角色-权限关联
|
||||
roles.Post("/:id/permissions", h.AssignPermissions) // POST /api/v1/roles/:id/permissions
|
||||
roles.Get("/:id/permissions", h.GetPermissions) // GET /api/v1/roles/:id/permissions
|
||||
roles.Delete("/:role_id/permissions/:perm_id", h.RemovePermission) // DELETE /api/v1/roles/:role_id/permissions/:perm_id
|
||||
}
|
||||
38
internal/routes/routes.go
Normal file
38
internal/routes/routes.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/handler"
|
||||
)
|
||||
|
||||
// Services 容器,包含所有业务 Handler
|
||||
// 由 main 函数初始化并传递给路由注册函数
|
||||
type Services struct {
|
||||
// RBAC 相关 Handler
|
||||
AccountHandler *handler.AccountHandler
|
||||
RoleHandler *handler.RoleHandler
|
||||
PermissionHandler *handler.PermissionHandler
|
||||
}
|
||||
|
||||
// RegisterRoutes 路由注册总入口
|
||||
// 按业务模块调用各自的路由注册函数
|
||||
func RegisterRoutes(app *fiber.App, services *Services) {
|
||||
// API 路由组
|
||||
api := app.Group("/api/v1")
|
||||
|
||||
// 注册各模块路由
|
||||
registerHealthRoutes(app)
|
||||
registerTaskRoutes(api)
|
||||
|
||||
// RBAC 路由
|
||||
if services.AccountHandler != nil {
|
||||
registerAccountRoutes(api, services.AccountHandler)
|
||||
}
|
||||
if services.RoleHandler != nil {
|
||||
registerRoleRoutes(api, services.RoleHandler)
|
||||
}
|
||||
if services.PermissionHandler != nil {
|
||||
registerPermissionRoutes(api, services.PermissionHandler)
|
||||
}
|
||||
}
|
||||
21
internal/routes/task.go
Normal file
21
internal/routes/task.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"github.com/break/junhong_cmp_fiber/pkg/response"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
// registerTaskRoutes 注册任务相关路由
|
||||
// 用于异步任务状态查询等
|
||||
func registerTaskRoutes(api fiber.Router) {
|
||||
tasks := api.Group("/tasks")
|
||||
|
||||
// 获取任务状态(占位实现)
|
||||
tasks.Get("/:id", func(c *fiber.Ctx) error {
|
||||
taskID := c.Params("id")
|
||||
return response.Success(c, fiber.Map{
|
||||
"id": taskID,
|
||||
"status": "pending",
|
||||
})
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user