forked from baron/baron-sso
fix: 내 정보(UpdateMe) 수정 시 커스텀 필드 로그인 ID 동기화 및 Metadata 필드 추가 (#440)
This commit is contained in:
@@ -87,10 +87,11 @@ type UserProfileResponse struct {
|
||||
}
|
||||
|
||||
type UpdateUserRequest struct {
|
||||
Name string `json:"name"`
|
||||
Phone string `json:"phone"`
|
||||
Department string `json:"department"`
|
||||
VerificationCode string `json:"verificationCode,omitempty"` // For phone change
|
||||
Name string `json:"name"`
|
||||
Phone string `json:"phone"`
|
||||
Department string `json:"department"`
|
||||
VerificationCode string `json:"verificationCode,omitempty"` // For phone change
|
||||
Metadata map[string]any `json:"metadata,omitempty"`
|
||||
}
|
||||
|
||||
// PasswordResetInitiateRequest is the request body for initiating a password reset.
|
||||
|
||||
@@ -5266,6 +5266,39 @@ func (h *AuthHandler) UpdateMe(c *fiber.Ctx) error {
|
||||
traits["department"] = req.Department
|
||||
}
|
||||
|
||||
// Merge custom metadata into traits
|
||||
if len(req.Metadata) > 0 {
|
||||
for k, v := range req.Metadata {
|
||||
// Do not overwrite core fields
|
||||
if _, isCore := map[string]bool{"email": true, "phone_number": true, "name": true, "department": true, "grade": true, "companyCode": true, "affiliationType": true, "id": true, "role": true, "tenant_id": true}[k]; !isCore {
|
||||
traits[k] = v
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// [LoginID Sync based on Tenant Settings]
|
||||
schemaCompCode := extractTraitString(traits, "companyCode")
|
||||
if schemaCompCode != "" && h.TenantService != nil {
|
||||
if tenant, err := h.TenantService.GetTenantBySlug(c.Context(), schemaCompCode); err == nil && tenant != nil {
|
||||
if loginIDField, ok := tenant.Config["loginIdField"].(string); ok && loginIDField != "" {
|
||||
// Search in Metadata (could be flat or namespaced)
|
||||
if val, exists := req.Metadata[loginIDField]; exists {
|
||||
if loginIDStr, ok := val.(string); ok && loginIDStr != "" {
|
||||
traits["id"] = loginIDStr
|
||||
}
|
||||
} else if namespaced, exists := req.Metadata[tenant.ID]; exists {
|
||||
if subMeta, ok := namespaced.(map[string]any); ok {
|
||||
if val, exists := subMeta[loginIDField]; exists {
|
||||
if loginIDStr, ok := val.(string); ok && loginIDStr != "" {
|
||||
traits["id"] = loginIDStr
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err := h.updateKratosIdentity(identityID, traits); err != nil {
|
||||
slog.Error("Failed to update profile in Kratos", "error", err)
|
||||
return errorJSON(c, fiber.StatusInternalServerError, "프로필 업데이트에 실패했습니다.")
|
||||
|
||||
Reference in New Issue
Block a user