refactor(terminology): rename user-facing tenant labels to workspace
Align product copy, API docs, Swagger descriptions, and i18n strings with workspace terminology while keeping internal tenant identifiers and headers unchanged for API compatibility.
This commit is contained in:
+21
-21
@@ -155,8 +155,8 @@ func Auth(
|
||||
crossTenantSwitch := targetTenantID != user.TenantID
|
||||
tenantHeader := c.GetHeader("X-Tenant-ID")
|
||||
if tenantHeader != "" {
|
||||
// 解析目标租户ID。畸形 / 零值必须显式拒绝:静默忽略会让坏掉的
|
||||
// 前端/SDK 悄悄写错租户,反而看不到问题。与 RequirePathTenantMatch
|
||||
// 解析目标空间ID。畸形 / 零值必须显式拒绝:静默忽略会让坏掉的
|
||||
// 前端/SDK 悄悄写错空间,反而看不到问题。与 RequirePathTenantMatch
|
||||
// 中对 :id 的校验保持一致(非空、可解析、>0)。
|
||||
parsedTenantID, err := strconv.ParseUint(tenantHeader, 10, 64)
|
||||
if err != nil || parsedTenantID == 0 {
|
||||
@@ -169,11 +169,11 @@ func Auth(
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
// 检查用户是否有权限访问目标租户:自家租户、跨租户超管、或
|
||||
// 检查用户是否有权限访问目标空间:自家空间、跨空间超管、或
|
||||
// 有 active membership 行——三选一,由 IsTenantAccessible
|
||||
// 统一判定。
|
||||
if IsTenantAccessible(c.Request.Context(), user, parsedTenantID, memberService, cfg) {
|
||||
// 验证目标租户是否存在
|
||||
// 验证目标空间是否存在
|
||||
targetTenant, err := tenantService.GetTenantByID(c.Request.Context(), parsedTenantID)
|
||||
if err == nil && targetTenant != nil {
|
||||
targetTenantID = parsedTenantID
|
||||
@@ -182,16 +182,16 @@ func Auth(
|
||||
} else {
|
||||
log.Printf("Error getting target tenant by ID: %v, tenantID: %d", err, parsedTenantID)
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"error": "Invalid target tenant ID",
|
||||
"error": "Invalid target workspace ID",
|
||||
})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
} else {
|
||||
// 用户没有权限访问目标租户
|
||||
// 用户没有权限访问目标空间
|
||||
log.Printf("User %s attempted to access tenant %d without permission", user.ID, parsedTenantID)
|
||||
c.JSON(http.StatusForbidden, gin.H{
|
||||
"error": "Forbidden: insufficient permissions to access target tenant",
|
||||
"error": "Forbidden: insufficient permissions to access target workspace",
|
||||
})
|
||||
c.Abort()
|
||||
return
|
||||
@@ -210,25 +210,25 @@ func Auth(
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusConflict, gin.H{
|
||||
"error": "Tenant required",
|
||||
"error": "Workspace required",
|
||||
"code": "TENANT_REQUIRED",
|
||||
})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
// 获取租户信息(使用目标租户ID)
|
||||
// 获取空间信息(使用目标空间ID)
|
||||
tenant, err := tenantService.GetTenantByID(c.Request.Context(), targetTenantID)
|
||||
if err != nil {
|
||||
log.Printf("Error getting tenant by ID: %v, tenantID: %d, userID: %s", err, targetTenantID, user.ID)
|
||||
c.JSON(http.StatusUnauthorized, gin.H{
|
||||
"error": "Unauthorized: invalid tenant",
|
||||
"error": "Unauthorized: invalid workspace",
|
||||
})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
// 解析当前租户内的角色 (issue #1303)
|
||||
// 解析当前空间内的角色 (issue #1303)
|
||||
role, ok := resolveTenantRole(c.Request.Context(), memberService, user, targetTenantID, crossTenantSwitch, cfg)
|
||||
if !ok {
|
||||
// 强制 RBAC 时,缺少 active membership 即拒绝;fail-open 路径已在
|
||||
@@ -236,13 +236,13 @@ func Auth(
|
||||
logger.Warnf(c.Request.Context(),
|
||||
"User %s has no active membership in tenant %d", user.ID, targetTenantID)
|
||||
c.JSON(http.StatusForbidden, gin.H{
|
||||
"error": "Forbidden: not a member of the target tenant",
|
||||
"error": "Forbidden: not a member of the target workspace",
|
||||
})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
// 存储用户和租户信息到上下文
|
||||
// 存储用户和空间信息到上下文
|
||||
logger.Infof(c.Request.Context(),
|
||||
"[auth] resolved role=%s for user=%s in tenant=%d (jwt_tenant=%d, header=%q, cross_switch=%v)",
|
||||
role, user.ID, targetTenantID, jwtTenantID, tenantHeader, crossTenantSwitch)
|
||||
@@ -503,7 +503,7 @@ func verifyExternalUserJWT(tokenString string, tenantID uint64, secret string) (
|
||||
return "", errors.New("token not yet valid")
|
||||
}
|
||||
if got := principalTenantIDFromClaims(claims); got != tenantID {
|
||||
return "", fmt.Errorf("tenant mismatch: got %d want %d", got, tenantID)
|
||||
return "", fmt.Errorf("workspace mismatch: got %d want %d", got, tenantID)
|
||||
}
|
||||
sub, _ := claims["sub"].(string)
|
||||
sub = strings.TrimSpace(sub)
|
||||
@@ -637,8 +637,8 @@ func resolveTenantRole(
|
||||
user.ID, targetTenantID, statusInfo)
|
||||
}
|
||||
|
||||
// 2. 跨租户超管直通:CanAccessAllTenants 用户切到别的租户时不强制要求 membership。
|
||||
// 注意:这里只授予临时 Admin 角色,不写入 tenant_members,避免"看一眼别人租户"
|
||||
// 2. 跨空间超管直通:CanAccessAllTenants 用户切到别的空间时不强制要求 membership。
|
||||
// 注意:这里只授予临时 Admin 角色,不写入 tenant_members,避免"看一眼别人空间"
|
||||
// 意外升级为持久化所有权。
|
||||
if crossTenantSwitch && user.CanAccessAllTenants {
|
||||
logger.Infof(ctx,
|
||||
@@ -647,9 +647,9 @@ func resolveTenantRole(
|
||||
return types.TenantRoleAdmin, true
|
||||
}
|
||||
|
||||
// 3. 孤儿租户自愈:仅当用户登录的是自己的 home tenant、且该租户尚无任何活跃成员时
|
||||
// 允许自动晋升为 Owner。跨租户 switch / JWT 指向他人租户的场景一律不进入此分支,
|
||||
// 防止越权获得他人租户的 Owner 权限。
|
||||
// 3. 孤儿空间自愈:仅当用户登录的是自己的 home tenant、且该空间尚无任何活跃成员时
|
||||
// 允许自动晋升为 Owner。跨空间 switch / JWT 指向他人空间的场景一律不进入此分支,
|
||||
// 防止越权获得他人空间的 Owner 权限。
|
||||
isHomeTenant := !crossTenantSwitch && targetTenantID == user.TenantID
|
||||
if isHomeTenant {
|
||||
hasAny, anyErr := memberService.HasAnyMembers(ctx, targetTenantID)
|
||||
@@ -679,7 +679,7 @@ func resolveTenantRole(
|
||||
logger.Warnf(ctx,
|
||||
"[auth] resolveTenantRole step4 fail-open (EnableRBAC=false) -> Admin: user=%s tenant=%d",
|
||||
user.ID, targetTenantID)
|
||||
// fail-open 期间保持现有行为(每个登录用户在自己租户里都是"管理员")。
|
||||
// fail-open 期间保持现有行为(每个登录用户在自己空间里都是"管理员")。
|
||||
return types.TenantRoleAdmin, true
|
||||
}
|
||||
|
||||
@@ -687,7 +687,7 @@ func resolveTenantRole(
|
||||
func GetTenantIDFromContext(ctx context.Context) (uint64, error) {
|
||||
tenantID, ok := ctx.Value("tenantID").(uint64)
|
||||
if !ok {
|
||||
return 0, errors.New("tenant ID not found in context")
|
||||
return 0, errors.New("workspace ID not found in context")
|
||||
}
|
||||
return tenantID, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user