feat(tenant): 按租户隔离用户身份与后台权限

This commit is contained in:
yml
2026-11-15 15:37:23 +08:00
parent ca57f604bf
commit abf0fce89b
43 changed files with 1572 additions and 196 deletions
@@ -8,6 +8,7 @@ import (
"hfb_sys/backend/internal/auditlog"
"hfb_sys/backend/internal/logging"
"hfb_sys/backend/internal/model"
"hfb_sys/backend/internal/tenancy"
"github.com/redis/go-redis/v9"
"gorm.io/gorm"
@@ -79,6 +80,9 @@ func (r *Repository) FindByID(ctx context.Context, id uint64) (*RoleDTO, error)
}
func (r *Repository) Create(ctx context.Context, req CreateRoleRequest) (*RoleDTO, error) {
if err := r.requirePlatformSuperAdmin(ctx); err != nil {
return nil, err
}
role := model.Role{
Code: req.Code,
Name: req.Name,
@@ -91,6 +95,9 @@ func (r *Repository) Create(ctx context.Context, req CreateRoleRequest) (*RoleDT
}
func (r *Repository) Update(ctx context.Context, id uint64, req UpdateRoleRequest) (*RoleDTO, error) {
if err := r.requirePlatformSuperAdmin(ctx); err != nil {
return nil, err
}
db := r.db.WithContext(ctx)
var role model.Role
if err := db.First(&role, id).Error; err != nil {
@@ -105,6 +112,9 @@ func (r *Repository) Update(ctx context.Context, id uint64, req UpdateRoleReques
}
func (r *Repository) Delete(ctx context.Context, id uint64) error {
if err := r.requirePlatformSuperAdmin(ctx); err != nil {
return err
}
db := r.db.WithContext(ctx)
var role model.Role
if err := db.First(&role, id).Error; err != nil {
@@ -137,6 +147,9 @@ func (r *Repository) Delete(ctx context.Context, id uint64) error {
}
func (r *Repository) AssignPermissions(ctx context.Context, roleID uint64, permIDs []uint64) error {
if err := r.requirePlatformSuperAdmin(ctx); err != nil {
return err
}
db := r.db.WithContext(ctx)
var role model.Role
if err := db.First(&role, roleID).Error; err != nil {
@@ -253,14 +266,55 @@ func (r *Repository) invalidateAdminPermCaches(ctx context.Context, adminIDs []u
if r.redis == nil || len(adminIDs) == 0 {
return
}
keys := make([]string, 0, len(adminIDs))
for _, id := range adminIDs {
keys = append(keys, fmt.Sprintf("admin:perms:%d", id))
patterns := []string{
fmt.Sprintf("admin:perms:%d", id),
fmt.Sprintf("admin:perms:%d:*", id),
}
for _, pattern := range patterns {
var cursor uint64
for {
keys, next, err := r.redis.Scan(ctx, cursor, pattern, 100).Result()
if err != nil {
return
}
if len(keys) > 0 {
_ = r.redis.Del(ctx, keys...).Err()
}
cursor = next
if cursor == 0 {
break
}
}
}
}
_ = r.redis.Del(ctx, keys...).Err()
}
var ErrProtectedRole = errors.New("protected role")
func (r *Repository) requirePlatformSuperAdmin(ctx context.Context) error {
if tenancy.IDFromContext(ctx) == 0 {
return nil
}
adminID := logging.AdminIDFromContext(ctx)
if adminID == 0 {
return ErrPlatformAdminRequired
}
var count int64
if err := r.db.WithContext(ctx).Table("admin_user_roles AS aur").
Joins("JOIN roles AS r ON r.id = aur.role_id").
Where("aur.admin_user_id = ? AND r.code = ?", adminID, "super_admin").
Count(&count).Error; err != nil {
return err
}
if count == 0 {
return ErrPlatformAdminRequired
}
return nil
}
var (
ErrProtectedRole = errors.New("protected role")
ErrPlatformAdminRequired = errors.New("platform super admin required")
)
func IsNotFound(err error) bool {
return errors.Is(err, gorm.ErrRecordNotFound)