feat(tenant): 按租户隔离用户身份与后台权限
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user