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
+11 -1
View File
@@ -1,11 +1,13 @@
package middleware
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"hfb_sys/backend/internal/tenancy"
"hfb_sys/backend/pkg/response"
"github.com/gin-gonic/gin"
@@ -89,7 +91,7 @@ func getPermCodes(c *gin.Context, rdb *redis.Client, adminID uint64) ([]string,
if rdb == nil {
return nil, errors.New("redis unavailable")
}
key := fmt.Sprintf("admin:perms:%d", adminID)
key := adminPermissionCacheKey(c.Request.Context(), adminID)
raw, err := rdb.Get(c.Request.Context(), key).Result()
if errors.Is(err, redis.Nil) {
return nil, nil // 缓存未命中,视为无权限
@@ -103,3 +105,11 @@ func getPermCodes(c *gin.Context, rdb *redis.Client, adminID uint64) ([]string,
}
return codes, nil
}
func adminPermissionCacheKey(ctx context.Context, adminID uint64) string {
key := fmt.Sprintf("admin:perms:%d", adminID)
if scope := tenancy.ScopeKey(ctx); scope != "" {
key += ":" + scope
}
return key
}