fixed bug: ws message is sent to all users also when user has nothing changed

main
alex 2023-06-25 23:32:36 +02:00
parent 8b056c3191
commit fab7ce7da1
1 changed files with 61 additions and 26 deletions

View File

@ -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,
},
})
}