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:
@@ -5,7 +5,6 @@ import (
|
||||
"fmt"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// globalConfig 保存当前配置,支持原子访问
|
||||
@@ -178,13 +177,3 @@ func Set(cfg *Config) error {
|
||||
globalConfig.Store(cfg)
|
||||
return nil
|
||||
}
|
||||
|
||||
// unsafeSet 无验证设置配置(仅供热重载内部使用)
|
||||
func unsafeSet(cfg *Config) {
|
||||
globalConfig.Store(cfg)
|
||||
}
|
||||
|
||||
// atomicSwap 原子地交换配置
|
||||
func atomicSwap(new *Config) *Config {
|
||||
return (*Config)(atomic.SwapPointer((*unsafe.Pointer)(unsafe.Pointer(&globalConfig)), unsafe.Pointer(new)))
|
||||
}
|
||||
|
||||
@@ -112,7 +112,7 @@ middleware:
|
||||
{
|
||||
name: "environment-specific config (dev)",
|
||||
setupEnv: func() {
|
||||
os.Setenv(constants.EnvConfigEnv, "dev")
|
||||
_ = os.Setenv(constants.EnvConfigEnv, "dev")
|
||||
},
|
||||
cleanupEnv: func() {
|
||||
_ = os.Unsetenv(constants.EnvConfigEnv)
|
||||
@@ -202,8 +202,8 @@ middleware:
|
||||
{
|
||||
name: "invalid YAML syntax",
|
||||
setupEnv: func() {
|
||||
os.Setenv(constants.EnvConfigPath, "")
|
||||
os.Setenv(constants.EnvConfigEnv, "")
|
||||
_ = os.Setenv(constants.EnvConfigPath, "")
|
||||
_ = os.Setenv(constants.EnvConfigEnv, "")
|
||||
},
|
||||
cleanupEnv: func() {
|
||||
_ = os.Unsetenv(constants.EnvConfigPath)
|
||||
@@ -230,7 +230,7 @@ server:
|
||||
{
|
||||
name: "validation error - invalid server address",
|
||||
setupEnv: func() {
|
||||
os.Setenv(constants.EnvConfigPath, "")
|
||||
_ = os.Setenv(constants.EnvConfigPath, "")
|
||||
},
|
||||
cleanupEnv: func() {
|
||||
_ = os.Unsetenv(constants.EnvConfigPath)
|
||||
@@ -287,7 +287,7 @@ middleware:
|
||||
{
|
||||
name: "validation error - timeout out of range",
|
||||
setupEnv: func() {
|
||||
os.Setenv(constants.EnvConfigPath, "")
|
||||
_ = os.Setenv(constants.EnvConfigPath, "")
|
||||
},
|
||||
cleanupEnv: func() {
|
||||
_ = os.Unsetenv(constants.EnvConfigPath)
|
||||
@@ -344,7 +344,7 @@ middleware:
|
||||
{
|
||||
name: "validation error - invalid redis port",
|
||||
setupEnv: func() {
|
||||
os.Setenv(constants.EnvConfigPath, "")
|
||||
_ = os.Setenv(constants.EnvConfigPath, "")
|
||||
},
|
||||
cleanupEnv: func() {
|
||||
_ = os.Unsetenv(constants.EnvConfigPath)
|
||||
|
||||
@@ -71,8 +71,8 @@ middleware:
|
||||
}
|
||||
|
||||
// Set config path
|
||||
os.Setenv(constants.EnvConfigPath, configFile)
|
||||
defer os.Unsetenv(constants.EnvConfigPath)
|
||||
_ = os.Setenv(constants.EnvConfigPath, configFile)
|
||||
defer func() { _ = os.Unsetenv(constants.EnvConfigPath) }()
|
||||
|
||||
// Load initial config
|
||||
cfg, err := Load()
|
||||
@@ -226,8 +226,8 @@ middleware:
|
||||
}
|
||||
|
||||
// Set config path
|
||||
os.Setenv(constants.EnvConfigPath, configFile)
|
||||
defer os.Unsetenv(constants.EnvConfigPath)
|
||||
_ = os.Setenv(constants.EnvConfigPath, configFile)
|
||||
defer func() { _ = os.Unsetenv(constants.EnvConfigPath) }()
|
||||
|
||||
// Load initial config
|
||||
cfg, err := Load()
|
||||
@@ -385,8 +385,8 @@ middleware:
|
||||
t.Fatalf("failed to create config file: %v", err)
|
||||
}
|
||||
|
||||
os.Setenv(constants.EnvConfigPath, configFile)
|
||||
defer os.Unsetenv(constants.EnvConfigPath)
|
||||
_ = os.Setenv(constants.EnvConfigPath, configFile)
|
||||
defer func() { _ = os.Unsetenv(constants.EnvConfigPath) }()
|
||||
|
||||
// Load config
|
||||
_, err := Load()
|
||||
|
||||
Reference in New Issue
Block a user