Files
junhong_cmp_fiber/pkg/errors/context.go
huang eaa70ac255 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>
2025-11-18 16:44:06 +08:00

91 lines
2.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package errors
import (
"github.com/gofiber/fiber/v2"
"go.uber.org/zap"
"github.com/break/junhong_cmp_fiber/pkg/constants"
)
// ErrorContext 错误发生时的请求上下文(用于日志记录)
type ErrorContext struct {
RequestID string // 请求 ID唯一标识
Method string // HTTP 方法
Path string // 请求路径
Query string // Query 参数
Body string // 请求 Body限制 50KB
IP string // 客户端 IP
UserAgent string // User-Agent
UserID string // 用户 ID如果已认证
}
const (
// MaxBodyLogSize 请求 Body 日志记录最大字节数50KB
MaxBodyLogSize = 50 * 1024
)
// FromFiberContext 从 Fiber Context 提取错误上下文
func FromFiberContext(c *fiber.Ctx) *ErrorContext {
ctx := &ErrorContext{
Method: c.Method(),
Path: c.Path(),
Query: c.Request().URI().QueryArgs().String(),
IP: c.IP(),
UserAgent: c.Get("User-Agent"),
}
// 提取 Request ID
if rid := c.Locals(constants.ContextKeyRequestID); rid != nil {
ctx.RequestID = rid.(string)
}
if ctx.RequestID == "" {
ctx.RequestID = c.Get("X-Request-ID")
}
// 提取 User ID如果已认证
if uid := c.Locals(constants.ContextKeyUserID); uid != nil {
if userID, ok := uid.(string); ok {
ctx.UserID = userID
}
}
// 提取请求 Body限制 50KB
bodyBytes := c.Body()
if len(bodyBytes) > 0 {
if len(bodyBytes) > MaxBodyLogSize {
// 超过限制时截断并添加提示
ctx.Body = string(bodyBytes[:MaxBodyLogSize]) + " ... (truncated)"
} else {
ctx.Body = string(bodyBytes)
}
}
return ctx
}
// ToLogFields 转换为 Zap 日志字段
func (ec *ErrorContext) ToLogFields() []zap.Field {
fields := []zap.Field{
zap.String("request_id", ec.RequestID),
zap.String("method", ec.Method),
zap.String("path", ec.Path),
zap.String("ip", ec.IP),
}
// 可选字段(非空时添加)
if ec.Query != "" {
fields = append(fields, zap.String("query", ec.Query))
}
if ec.Body != "" {
fields = append(fields, zap.String("body", ec.Body))
}
if ec.UserAgent != "" {
fields = append(fields, zap.String("user_agent", ec.UserAgent))
}
if ec.UserID != "" {
fields = append(fields, zap.String("user_id", ec.UserID))
}
return fields
}