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:
2025-11-18 16:44:06 +08:00
parent e8eb5766cb
commit eaa70ac255
86 changed files with 15395 additions and 245 deletions

View File

@@ -19,7 +19,7 @@ func TestQueueClientEnqueue(t *testing.T) {
redisClient := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
defer redisClient.Close()
defer func() { _ = redisClient.Close() }()
ctx := context.Background()
redisClient.FlushDB(ctx)
@@ -27,7 +27,7 @@ func TestQueueClientEnqueue(t *testing.T) {
client := asynq.NewClient(asynq.RedisClientOpt{
Addr: "localhost:6379",
})
defer client.Close()
defer func() { _ = client.Close() }()
payload := map[string]string{
"request_id": "test-001",
@@ -50,7 +50,7 @@ func TestQueueClientEnqueueWithOptions(t *testing.T) {
redisClient := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
defer redisClient.Close()
defer func() { _ = redisClient.Close() }()
ctx := context.Background()
redisClient.FlushDB(ctx)
@@ -58,7 +58,7 @@ func TestQueueClientEnqueueWithOptions(t *testing.T) {
client := asynq.NewClient(asynq.RedisClientOpt{
Addr: "localhost:6379",
})
defer client.Close()
defer func() { _ = client.Close() }()
tests := []struct {
name string
@@ -139,7 +139,7 @@ func TestQueueClientTaskUniqueness(t *testing.T) {
redisClient := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
defer redisClient.Close()
defer func() { _ = redisClient.Close() }()
ctx := context.Background()
redisClient.FlushDB(ctx)
@@ -147,7 +147,7 @@ func TestQueueClientTaskUniqueness(t *testing.T) {
client := asynq.NewClient(asynq.RedisClientOpt{
Addr: "localhost:6379",
})
defer client.Close()
defer func() { _ = client.Close() }()
payload := map[string]string{
"request_id": "unique-001",
@@ -227,7 +227,7 @@ func TestTaskPayloadSizeLimit(t *testing.T) {
redisClient := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
defer redisClient.Close()
defer func() { _ = redisClient.Close() }()
ctx := context.Background()
redisClient.FlushDB(ctx)
@@ -235,7 +235,7 @@ func TestTaskPayloadSizeLimit(t *testing.T) {
client := asynq.NewClient(asynq.RedisClientOpt{
Addr: "localhost:6379",
})
defer client.Close()
defer func() { _ = client.Close() }()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
@@ -271,7 +271,7 @@ func TestTaskScheduling(t *testing.T) {
redisClient := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
defer redisClient.Close()
defer func() { _ = redisClient.Close() }()
ctx := context.Background()
redisClient.FlushDB(ctx)
@@ -279,7 +279,7 @@ func TestTaskScheduling(t *testing.T) {
client := asynq.NewClient(asynq.RedisClientOpt{
Addr: "localhost:6379",
})
defer client.Close()
defer func() { _ = client.Close() }()
tests := []struct {
name string
@@ -323,7 +323,7 @@ func TestQueueInspectorStats(t *testing.T) {
redisClient := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
defer redisClient.Close()
defer func() { _ = redisClient.Close() }()
ctx := context.Background()
redisClient.FlushDB(ctx)
@@ -331,7 +331,7 @@ func TestQueueInspectorStats(t *testing.T) {
client := asynq.NewClient(asynq.RedisClientOpt{
Addr: "localhost:6379",
})
defer client.Close()
defer func() { _ = client.Close() }()
// 提交一些任务
for i := 0; i < 5; i++ {
@@ -351,7 +351,7 @@ func TestQueueInspectorStats(t *testing.T) {
inspector := asynq.NewInspector(asynq.RedisClientOpt{
Addr: "localhost:6379",
})
defer inspector.Close()
defer func() { _ = inspector.Close() }()
info, err := inspector.GetQueueInfo(constants.QueueDefault)
require.NoError(t, err)
@@ -366,7 +366,7 @@ func TestTaskRetention(t *testing.T) {
redisClient := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
defer redisClient.Close()
defer func() { _ = redisClient.Close() }()
ctx := context.Background()
redisClient.FlushDB(ctx)
@@ -374,7 +374,7 @@ func TestTaskRetention(t *testing.T) {
client := asynq.NewClient(asynq.RedisClientOpt{
Addr: "localhost:6379",
})
defer client.Close()
defer func() { _ = client.Close() }()
payload := map[string]string{
"request_id": "retention-test-001",
@@ -398,7 +398,7 @@ func TestQueueDraining(t *testing.T) {
redisClient := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
defer redisClient.Close()
defer func() { _ = redisClient.Close() }()
ctx := context.Background()
redisClient.FlushDB(ctx)
@@ -406,7 +406,7 @@ func TestQueueDraining(t *testing.T) {
inspector := asynq.NewInspector(asynq.RedisClientOpt{
Addr: "localhost:6379",
})
defer inspector.Close()
defer func() { _ = inspector.Close() }()
// 暂停队列
err := inspector.PauseQueue(constants.QueueDefault)
@@ -432,7 +432,7 @@ func TestTaskCancellation(t *testing.T) {
redisClient := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
defer redisClient.Close()
defer func() { _ = redisClient.Close() }()
ctx := context.Background()
redisClient.FlushDB(ctx)
@@ -440,7 +440,7 @@ func TestTaskCancellation(t *testing.T) {
client := asynq.NewClient(asynq.RedisClientOpt{
Addr: "localhost:6379",
})
defer client.Close()
defer func() { _ = client.Close() }()
payload := map[string]string{
"request_id": "cancel-test-001",
@@ -458,7 +458,7 @@ func TestTaskCancellation(t *testing.T) {
inspector := asynq.NewInspector(asynq.RedisClientOpt{
Addr: "localhost:6379",
})
defer inspector.Close()
defer func() { _ = inspector.Close() }()
err = inspector.DeleteTask(constants.QueueDefault, info.ID)
require.NoError(t, err)
@@ -474,7 +474,7 @@ func TestBatchTaskEnqueue(t *testing.T) {
redisClient := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
defer redisClient.Close()
defer func() { _ = redisClient.Close() }()
ctx := context.Background()
redisClient.FlushDB(ctx)
@@ -482,7 +482,7 @@ func TestBatchTaskEnqueue(t *testing.T) {
client := asynq.NewClient(asynq.RedisClientOpt{
Addr: "localhost:6379",
})
defer client.Close()
defer func() { _ = client.Close() }()
// 批量创建任务
batchSize := 100
@@ -503,7 +503,7 @@ func TestBatchTaskEnqueue(t *testing.T) {
inspector := asynq.NewInspector(asynq.RedisClientOpt{
Addr: "localhost:6379",
})
defer inspector.Close()
defer func() { _ = inspector.Close() }()
info, err := inspector.GetQueueInfo(constants.QueueDefault)
require.NoError(t, err)
@@ -515,7 +515,7 @@ func TestTaskGrouping(t *testing.T) {
redisClient := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
defer redisClient.Close()
defer func() { _ = redisClient.Close() }()
ctx := context.Background()
redisClient.FlushDB(ctx)
@@ -523,7 +523,7 @@ func TestTaskGrouping(t *testing.T) {
client := asynq.NewClient(asynq.RedisClientOpt{
Addr: "localhost:6379",
})
defer client.Close()
defer func() { _ = client.Close() }()
// 提交分组任务
groupKey := "email-batch-001"
@@ -547,7 +547,7 @@ func TestTaskGrouping(t *testing.T) {
inspector := asynq.NewInspector(asynq.RedisClientOpt{
Addr: "localhost:6379",
})
defer inspector.Close()
defer func() { _ = inspector.Close() }()
info, err := inspector.GetQueueInfo(constants.QueueDefault)
require.NoError(t, err)