refactor: update user serialization in Yggdrasil handler to use SerializeUser for improved properties handling
Some checks failed
SonarQube Analysis / sonarqube (push) Has been cancelled

This commit is contained in:
lafay
2025-11-30 19:00:59 +08:00
parent 4188ee1555
commit bdd2be5dc5
2 changed files with 19 additions and 6 deletions

View File

@@ -234,10 +234,8 @@ func Authenticate(c *gin.Context) {
response.SelectedProfile = service.SerializeProfile(db, loggerInstance, redisClient, *selectedProfile) response.SelectedProfile = service.SerializeProfile(db, loggerInstance, redisClient, *selectedProfile)
} }
if request.RequestUser { if request.RequestUser {
response.User = map[string]interface{}{ // 使用 SerializeUser 来正确处理 Properties 字段
"id": userId, response.User = service.SerializeUser(loggerInstance, user, UUID)
"properties": user.Properties,
}
} }
// 返回认证响应 // 返回认证响应

View File

@@ -92,7 +92,22 @@ func SerializeUser(logger *zap.Logger, u *model.User, UUID string) map[string]in
data := map[string]interface{}{ data := map[string]interface{}{
"id": UUID, "id": UUID,
"properties": u.Properties,
} }
// 正确处理 *datatypes.JSON 指针类型
// 如果 Properties 为 nil则设置为 nil否则解引用并解析为 JSON 值
if u.Properties == nil {
data["properties"] = nil
} else {
// datatypes.JSON 是 []byte 类型,需要解析为实际的 JSON 值
var propertiesValue interface{}
if err := json.Unmarshal(*u.Properties, &propertiesValue); err != nil {
logger.Warn("[WARN] 解析用户Properties失败使用空值", zap.Error(err))
data["properties"] = nil
} else {
data["properties"] = propertiesValue
}
}
return data return data
} }