Keep the ban state when an old copy of a user is saved
UpdateUserRecord wrote every column of the user it was given, so saving a copy taken before a ban (cached user, stored inline object, invoice event) lifted the ban. The banned flag and the admin key now always come from the database row and are left out of the save; only the new UpdateUserBan changes them, and the admin API uses it. Manual port of kravens/BitTipBot 05c0a4e and dfc8d13. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5.1
parent
39bddc2cb5
commit
1a3815902a
@@ -26,7 +26,7 @@ func (s Service) UnbanUser(w http.ResponseWriter, r *http.Request) {
|
||||
user.Banned = false
|
||||
adminSlice := strings.Split(user.Wallet.Adminkey, "_")
|
||||
user.Wallet.Adminkey = adminSlice[len(adminSlice)-1]
|
||||
err = telegram.UpdateUserRecord(user, *s.bot)
|
||||
err = telegram.UpdateUserBan(user, *s.bot)
|
||||
if err != nil {
|
||||
log.Errorf("[ADMIN] could not update user: %v", err)
|
||||
return
|
||||
@@ -52,7 +52,7 @@ func (s Service) BanUser(w http.ResponseWriter, r *http.Request) {
|
||||
user.Wallet.Adminkey = fmt.Sprintf("%s_%s", reason, user.Wallet.Adminkey)
|
||||
}
|
||||
user.Wallet.Adminkey = fmt.Sprintf("%s_%s", "banned", user.Wallet.Adminkey)
|
||||
err = telegram.UpdateUserRecord(user, *s.bot)
|
||||
err = telegram.UpdateUserBan(user, *s.bot)
|
||||
if err != nil {
|
||||
log.Errorf("[ADMIN] could not update user: %v", err)
|
||||
return
|
||||
|
||||
@@ -263,6 +263,21 @@ func debugStack() {
|
||||
func UpdateUserRecord(user *lnbits.User, bot TipBot) error {
|
||||
user.UpdatedAt = time.Now()
|
||||
|
||||
// The ban state (banned flag and the "banned_" admin key) always comes from
|
||||
// the database row. Callers may hold an old copy of the user, for example
|
||||
// an invoice event or inline object stored before a ban, and saving it must
|
||||
// not lift the ban. Only UpdateUserBan changes these two columns.
|
||||
keepBanState := false
|
||||
if user.Name != "" && user.Wallet != nil && user.Wallet.ID != "" {
|
||||
freshUser := &lnbits.User{Name: user.Name}
|
||||
if tx := bot.DB.Users.First(freshUser); tx.Error == nil && freshUser.Wallet != nil &&
|
||||
freshUser.Wallet.ID == user.Wallet.ID && freshUser.Wallet.Adminkey != "" {
|
||||
user.Wallet.Adminkey = freshUser.Wallet.Adminkey
|
||||
user.Banned = freshUser.Banned
|
||||
keepBanState = true
|
||||
}
|
||||
}
|
||||
|
||||
// There is a weird bug that makes the AnonID vanish. This is a workaround.
|
||||
// TODO -- Remove this after empty anon id bug is identified
|
||||
if user.AnonIDSha256 == "" {
|
||||
@@ -283,7 +298,12 @@ func UpdateUserRecord(user *lnbits.User, bot TipBot) error {
|
||||
log.Errorf("[UpdateUserRecord] UUID empty! Setting to: %s", user.UUID)
|
||||
}
|
||||
|
||||
tx := bot.DB.Users.Save(user)
|
||||
db := bot.DB.Users
|
||||
if keepBanState {
|
||||
// leave the columns out, so a ban written since the read above stays
|
||||
db = db.Omit("banned", "wallet_adminkey")
|
||||
}
|
||||
tx := db.Save(user)
|
||||
if tx.Error != nil {
|
||||
errmsg := fmt.Sprintf("[UpdateUserRecord] Error: Couldn't update %s's info in Database.", GetUserStr(user.Telegram))
|
||||
log.Errorln(errmsg)
|
||||
@@ -295,3 +315,18 @@ func UpdateUserRecord(user *lnbits.User, bot TipBot) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateUserBan writes the ban state of the user (banned flag and admin key)
|
||||
// to the database. UpdateUserRecord never changes these two columns for a
|
||||
// user that already has a wallet.
|
||||
func UpdateUserBan(user *lnbits.User, bot TipBot) error {
|
||||
tx := bot.DB.Users.Model(&lnbits.User{}).Where("name = ?", user.Name).
|
||||
Updates(map[string]interface{}{"banned": user.Banned, "wallet_adminkey": user.Wallet.Adminkey})
|
||||
if tx.Error != nil {
|
||||
return tx.Error
|
||||
}
|
||||
if bot.Cache.GoCacheStore != nil {
|
||||
updateCachedUser(user, bot)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user