From fab7ce7da1bab2d4a4d7bb1b26b36ba97e467035 Mon Sep 17 00:00:00 2001 From: alex Date: Sun, 25 Jun 2023 23:32:36 +0200 Subject: [PATCH] fixed bug: ws message is sent to all users also when user has nothing changed --- socketclients/socketclients.go | 87 ++++++++++++++++++++++++---------- 1 file changed, 61 insertions(+), 26 deletions(-) diff --git a/socketclients/socketclients.go b/socketclients/socketclients.go index 13a1ec1..303accc 100644 --- a/socketclients/socketclients.go +++ b/socketclients/socketclients.go @@ -221,7 +221,6 @@ func UpdateUserProfile(conn *websocket.Conn, changes map[string]interface{}) { var user structs.User var updates = make(map[string]interface{}) - var changesResult = make(map[string]uint8) if changes["username"] != nil { username := changes["username"].(string) @@ -230,9 +229,18 @@ func UpdateUserProfile(conn *websocket.Conn, changes map[string]interface{}) { if isUsernameAvailable(username) { user.Username = username updates["Username"] = username - changesResult["Username"] = 0 } else { - changesResult["Username"] = 1 + SendMessageOnlyToSessionId(sessionId, structs.SendSocketMessage{ + Cmd: utils.SentCmdUserProfileUpdated, + Body: struct { + UserId string + Result uint8 + }{ + UserId: userId, + Result: 0, + }, + }) + return } } } @@ -243,9 +251,18 @@ func UpdateUserProfile(conn *websocket.Conn, changes map[string]interface{}) { if isEmailAvailable(email) { user.Email = email updates["Email"] = email - changesResult["Email"] = 0 } else { - changesResult["Email"] = 1 + SendMessageOnlyToSessionId(sessionId, structs.SendSocketMessage{ + Cmd: utils.SentCmdUserProfileUpdated, + Body: struct { + UserId string + Result uint8 + }{ + UserId: userId, + Result: 1, + }, + }) + return } } @@ -256,33 +273,53 @@ func UpdateUserProfile(conn *websocket.Conn, changes map[string]interface{}) { decodedOldPassword, err := base64.StdEncoding.DecodeString(oldPassword) decodedNewPassword, err1 := base64.StdEncoding.DecodeString(newPassword) - if err == nil && err1 == nil { - if utils.IsPasswordLengthValid(string(decodedOldPassword)) { // only affected if username was manipulated as min and max is provided in web ui - database.DB.Select("password").First(&user, "id = ?", userId) + if err != nil || err1 != nil { + log.Error().Msgf("Error decoding old or new password %s %s", err.Error(), err1.Error()) - if err := bcrypt.CompareHashAndPassword([]byte(user.Password), decodedOldPassword); err == nil { - hashedPassword, err := bcrypt.GenerateFromPassword([]byte(decodedNewPassword), bcrypt.DefaultCost) + SendMessageOnlyToSessionId(sessionId, structs.SendSocketMessage{ + Cmd: utils.SentCmdUserProfileUpdated, + Body: struct { + UserId string + Result uint8 + }{ + UserId: userId, + Result: 2, + }, + }) + return + } - if err == nil { - user.Password = string(hashedPassword) - } else { - log.Error().Msgf("Failed to generate hash password %s", err.Error()) - } + if utils.IsPasswordLengthValid(string(decodedOldPassword)) { // only affected if username was manipulated as min and max is provided in web ui + database.DB.Select("password").First(&user, "id = ?", userId) + + if err := bcrypt.CompareHashAndPassword([]byte(user.Password), decodedOldPassword); err == nil { + hashedPassword, err := bcrypt.GenerateFromPassword([]byte(decodedNewPassword), bcrypt.DefaultCost) + + if err == nil { + user.Password = string(hashedPassword) } else { - log.Error().Msg("Incorrect password") - changesResult["Password"] = 1 + log.Error().Msgf("Failed to generate hash password %s", err.Error()) } - } - } else { - if err != nil { - log.Error().Msgf("Failed to decode old password %s", err.Error()) - } - if err1 != nil { - log.Error().Msgf("Failed to decode new password %s", err1.Error()) + } else { + log.Error().Msg("Incorrect password") + + SendMessageOnlyToSessionId(sessionId, structs.SendSocketMessage{ + Cmd: utils.SentCmdUserProfileUpdated, + Body: struct { + UserId string + Result uint8 + }{ + UserId: userId, + Result: 2, + }, + }) + return } } } + log.Debug().Msgf("changes %v %v", len(changes), changes) + if len(changes) > 0 { user.UpdatedAt = time.Now() @@ -298,11 +335,9 @@ func UpdateUserProfile(conn *websocket.Conn, changes map[string]interface{}) { Body: struct { UserId string Changes map[string]interface{} - Result map[string]uint8 }{ UserId: userId, Changes: updates, - Result: changesResult, }, }) }