- 4 表设计: permissions, roles, role_permissions(m2m), user_roles - 多租户隔离: 角色和用户角色分配按 tenant_id 隔离 - 权限并集: 多角色用户取权限并集,无继承 - 系统角色保护: is_system 角色不可修改/删除 - 种子数据: 7 个默认权限 + 3 个系统角色(super_admin/tenant_admin/member) - 权限中间件: RequirePermission 支持按路由粒度控制 - API 路由: /api/v1/rbac/ 下完整 CRUD 端点 - fx 注册: RBAC Module 集成到 app.go
46 lines
1.2 KiB
Go
46 lines
1.2 KiB
Go
package infrastructure
|
|
|
|
import (
|
|
"context"
|
|
|
|
"mengstack/internal/modules/rbac/domain"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type permRepo struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewPermissionRepository(db *gorm.DB) domain.PermissionRepository {
|
|
return &permRepo{db: db}
|
|
}
|
|
|
|
func (r *permRepo) Create(ctx context.Context, perm *domain.Permission) error {
|
|
return r.db.WithContext(ctx).Create(perm).Error
|
|
}
|
|
|
|
func (r *permRepo) FindByID(ctx context.Context, id uint) (*domain.Permission, error) {
|
|
var perm domain.Permission
|
|
err := r.db.WithContext(ctx).First(&perm, id).Error
|
|
return &perm, err
|
|
}
|
|
|
|
func (r *permRepo) FindByCode(ctx context.Context, code string) (*domain.Permission, error) {
|
|
var perm domain.Permission
|
|
err := r.db.WithContext(ctx).Where("code = ?", code).First(&perm).Error
|
|
return &perm, err
|
|
}
|
|
|
|
func (r *permRepo) FindAll(ctx context.Context) ([]domain.Permission, error) {
|
|
var perms []domain.Permission
|
|
err := r.db.WithContext(ctx).Order("module, code").Find(&perms).Error
|
|
return perms, err
|
|
}
|
|
|
|
func (r *permRepo) FindByModule(ctx context.Context, module string) ([]domain.Permission, error) {
|
|
var perms []domain.Permission
|
|
err := r.db.WithContext(ctx).Where("module = ?", module).Order("code").Find(&perms).Error
|
|
return perms, err
|
|
}
|