fixed bug: ws message is sent to all users also when user has nothing changed
parent
8b056c3191
commit
fab7ce7da1
|
@ -221,7 +221,6 @@ func UpdateUserProfile(conn *websocket.Conn, changes map[string]interface{}) {
|
||||||
|
|
||||||
var user structs.User
|
var user structs.User
|
||||||
var updates = make(map[string]interface{})
|
var updates = make(map[string]interface{})
|
||||||
var changesResult = make(map[string]uint8)
|
|
||||||
|
|
||||||
if changes["username"] != nil {
|
if changes["username"] != nil {
|
||||||
username := changes["username"].(string)
|
username := changes["username"].(string)
|
||||||
|
@ -230,9 +229,18 @@ func UpdateUserProfile(conn *websocket.Conn, changes map[string]interface{}) {
|
||||||
if isUsernameAvailable(username) {
|
if isUsernameAvailable(username) {
|
||||||
user.Username = username
|
user.Username = username
|
||||||
updates["Username"] = username
|
updates["Username"] = username
|
||||||
changesResult["Username"] = 0
|
|
||||||
} else {
|
} 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) {
|
if isEmailAvailable(email) {
|
||||||
user.Email = email
|
user.Email = email
|
||||||
updates["Email"] = email
|
updates["Email"] = email
|
||||||
changesResult["Email"] = 0
|
|
||||||
} else {
|
} 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)
|
decodedOldPassword, err := base64.StdEncoding.DecodeString(oldPassword)
|
||||||
decodedNewPassword, err1 := base64.StdEncoding.DecodeString(newPassword)
|
decodedNewPassword, err1 := base64.StdEncoding.DecodeString(newPassword)
|
||||||
|
|
||||||
if err == nil && err1 == nil {
|
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
|
log.Error().Msgf("Error decoding old or new password %s %s", err.Error(), err1.Error())
|
||||||
database.DB.Select("password").First(&user, "id = ?", userId)
|
|
||||||
|
|
||||||
if err := bcrypt.CompareHashAndPassword([]byte(user.Password), decodedOldPassword); err == nil {
|
SendMessageOnlyToSessionId(sessionId, structs.SendSocketMessage{
|
||||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(decodedNewPassword), bcrypt.DefaultCost)
|
Cmd: utils.SentCmdUserProfileUpdated,
|
||||||
|
Body: struct {
|
||||||
|
UserId string
|
||||||
|
Result uint8
|
||||||
|
}{
|
||||||
|
UserId: userId,
|
||||||
|
Result: 2,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if err == nil {
|
if utils.IsPasswordLengthValid(string(decodedOldPassword)) { // only affected if username was manipulated as min and max is provided in web ui
|
||||||
user.Password = string(hashedPassword)
|
database.DB.Select("password").First(&user, "id = ?", userId)
|
||||||
} else {
|
|
||||||
log.Error().Msgf("Failed to generate hash password %s", err.Error())
|
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 {
|
} else {
|
||||||
log.Error().Msg("Incorrect password")
|
log.Error().Msgf("Failed to generate hash password %s", err.Error())
|
||||||
changesResult["Password"] = 1
|
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
} else {
|
log.Error().Msg("Incorrect password")
|
||||||
if err != nil {
|
|
||||||
log.Error().Msgf("Failed to decode old password %s", err.Error())
|
SendMessageOnlyToSessionId(sessionId, structs.SendSocketMessage{
|
||||||
}
|
Cmd: utils.SentCmdUserProfileUpdated,
|
||||||
if err1 != nil {
|
Body: struct {
|
||||||
log.Error().Msgf("Failed to decode new password %s", err1.Error())
|
UserId string
|
||||||
|
Result uint8
|
||||||
|
}{
|
||||||
|
UserId: userId,
|
||||||
|
Result: 2,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Debug().Msgf("changes %v %v", len(changes), changes)
|
||||||
|
|
||||||
if len(changes) > 0 {
|
if len(changes) > 0 {
|
||||||
user.UpdatedAt = time.Now()
|
user.UpdatedAt = time.Now()
|
||||||
|
|
||||||
|
@ -298,11 +335,9 @@ func UpdateUserProfile(conn *websocket.Conn, changes map[string]interface{}) {
|
||||||
Body: struct {
|
Body: struct {
|
||||||
UserId string
|
UserId string
|
||||||
Changes map[string]interface{}
|
Changes map[string]interface{}
|
||||||
Result map[string]uint8
|
|
||||||
}{
|
}{
|
||||||
UserId: userId,
|
UserId: userId,
|
||||||
Changes: updates,
|
Changes: updates,
|
||||||
Result: changesResult,
|
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue