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:
122
internal/store/postgres/permission_store.go
Normal file
122
internal/store/postgres/permission_store.go
Normal file
@@ -0,0 +1,122 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store"
|
||||
)
|
||||
|
||||
// PermissionStore 权限数据访问层
|
||||
type PermissionStore struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewPermissionStore 创建权限 Store
|
||||
func NewPermissionStore(db *gorm.DB) *PermissionStore {
|
||||
return &PermissionStore{db: db}
|
||||
}
|
||||
|
||||
// Create 创建权限
|
||||
func (s *PermissionStore) Create(ctx context.Context, permission *model.Permission) error {
|
||||
return s.db.WithContext(ctx).Create(permission).Error
|
||||
}
|
||||
|
||||
// GetByID 根据 ID 获取权限
|
||||
func (s *PermissionStore) GetByID(ctx context.Context, id uint) (*model.Permission, error) {
|
||||
var permission model.Permission
|
||||
if err := s.db.WithContext(ctx).First(&permission, id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &permission, nil
|
||||
}
|
||||
|
||||
// GetByCode 根据权限编码获取权限
|
||||
func (s *PermissionStore) GetByCode(ctx context.Context, code string) (*model.Permission, error) {
|
||||
var permission model.Permission
|
||||
if err := s.db.WithContext(ctx).Where("perm_code = ?", code).First(&permission).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &permission, nil
|
||||
}
|
||||
|
||||
// Update 更新权限
|
||||
func (s *PermissionStore) Update(ctx context.Context, permission *model.Permission) error {
|
||||
return s.db.WithContext(ctx).Save(permission).Error
|
||||
}
|
||||
|
||||
// Delete 软删除权限
|
||||
func (s *PermissionStore) Delete(ctx context.Context, id uint) error {
|
||||
return s.db.WithContext(ctx).Delete(&model.Permission{}, id).Error
|
||||
}
|
||||
|
||||
// List 查询权限列表
|
||||
func (s *PermissionStore) List(ctx context.Context, opts *store.QueryOptions, filters map[string]interface{}) ([]*model.Permission, int64, error) {
|
||||
var permissions []*model.Permission
|
||||
var total int64
|
||||
|
||||
query := s.db.WithContext(ctx).Model(&model.Permission{})
|
||||
|
||||
// 应用过滤条件
|
||||
if name, ok := filters["perm_name"].(string); ok && name != "" {
|
||||
query = query.Where("perm_name LIKE ?", "%"+name+"%")
|
||||
}
|
||||
if code, ok := filters["perm_code"].(string); ok && code != "" {
|
||||
query = query.Where("perm_code LIKE ?", "%"+code+"%")
|
||||
}
|
||||
if permType, ok := filters["perm_type"].(int); ok {
|
||||
query = query.Where("perm_type = ?", permType)
|
||||
}
|
||||
if parentID, ok := filters["parent_id"].(uint); ok {
|
||||
query = query.Where("parent_id = ?", parentID)
|
||||
}
|
||||
if status, ok := filters["status"].(int); ok {
|
||||
query = query.Where("status = ?", status)
|
||||
}
|
||||
|
||||
// 计算总数
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
// 分页
|
||||
if opts == nil {
|
||||
opts = store.DefaultQueryOptions()
|
||||
}
|
||||
offset := (opts.Page - 1) * opts.PageSize
|
||||
query = query.Offset(offset).Limit(opts.PageSize)
|
||||
|
||||
// 排序
|
||||
if opts.OrderBy != "" {
|
||||
query = query.Order(opts.OrderBy)
|
||||
} else {
|
||||
query = query.Order("sort ASC, id ASC")
|
||||
}
|
||||
|
||||
// 执行查询
|
||||
if err := query.Find(&permissions).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return permissions, total, nil
|
||||
}
|
||||
|
||||
// GetByIDs 根据 ID 列表获取权限
|
||||
func (s *PermissionStore) GetByIDs(ctx context.Context, ids []uint) ([]*model.Permission, error) {
|
||||
var permissions []*model.Permission
|
||||
if err := s.db.WithContext(ctx).Where("id IN ?", ids).Find(&permissions).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return permissions, nil
|
||||
}
|
||||
|
||||
// GetAll 获取所有权限(用于构建权限树)
|
||||
func (s *PermissionStore) GetAll(ctx context.Context) ([]*model.Permission, error) {
|
||||
var permissions []*model.Permission
|
||||
if err := s.db.WithContext(ctx).Order("sort ASC, id ASC").Find(&permissions).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return permissions, nil
|
||||
}
|
||||
Reference in New Issue
Block a user