From 204acb886c571f5504ed0a0e78f4304e27195c2f Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Sun, 8 Nov 2015 15:47:52 +0200 Subject: [PATCH] protect account overwrite --- engine/storage_map.go | 12 ++++++++++++ engine/storage_mongo.go | 12 ++++++++++++ engine/storage_redis.go | 12 ++++++++++++ 3 files changed, 36 insertions(+) diff --git a/engine/storage_map.go b/engine/storage_map.go index 76b695484..da13aec99 100644 --- a/engine/storage_map.go +++ b/engine/storage_map.go @@ -456,6 +456,18 @@ func (ms *MapStorage) GetAccount(key string) (ub *Account, err error) { } func (ms *MapStorage) SetAccount(ub *Account) (err error) { + // never override existing account with an empty one + // UPDATE: if all balances expired and were clean it makes + // sense to write empty balance map + if len(ub.BalanceMap) == 0 { + if ac, err := ms.GetAccount(ub.Id); err == nil && !ac.allBalancesExpired() { + ac.ActionTriggers = ub.ActionTriggers + ac.UnitCounters = ub.UnitCounters + ac.AllowNegative = ub.AllowNegative + ac.Disabled = ub.Disabled + ub = ac + } + } result, err := ms.ms.Marshal(ub) ms.dict[utils.ACCOUNT_PREFIX+ub.Id] = result return diff --git a/engine/storage_mongo.go b/engine/storage_mongo.go index 5450d063d..3edad9cc9 100644 --- a/engine/storage_mongo.go +++ b/engine/storage_mongo.go @@ -732,6 +732,18 @@ func (ms *MongoStorage) GetAccount(key string) (result *Account, err error) { } func (ms *MongoStorage) SetAccount(acc *Account) error { + // never override existing account with an empty one + // UPDATE: if all balances expired and were cleaned it makes + // sense to write empty balance map + if len(acc.BalanceMap) == 0 { + if ac, err := ms.GetAccount(acc.Id); err == nil && !ac.allBalancesExpired() { + ac.ActionTriggers = acc.ActionTriggers + ac.UnitCounters = acc.UnitCounters + ac.AllowNegative = acc.AllowNegative + ac.Disabled = acc.Disabled + acc = ac + } + } _, err := ms.db.C(colAcc).Upsert(bson.M{"id": acc.Id}, acc) return err } diff --git a/engine/storage_redis.go b/engine/storage_redis.go index e91f23da5..72ccbb664 100644 --- a/engine/storage_redis.go +++ b/engine/storage_redis.go @@ -555,6 +555,18 @@ func (rs *RedisStorage) GetAccount(key string) (ub *Account, err error) { } func (rs *RedisStorage) SetAccount(ub *Account) (err error) { + // never override existing account with an empty one + // UPDATE: if all balances expired and were cleaned it makes + // sense to write empty balance map + if len(ub.BalanceMap) == 0 { + if ac, err := rs.GetAccount(ub.Id); err == nil && !ac.allBalancesExpired() { + ac.ActionTriggers = ub.ActionTriggers + ac.UnitCounters = ub.UnitCounters + ac.AllowNegative = ub.AllowNegative + ac.Disabled = ub.Disabled + ub = ac + } + } result, err := rs.ms.Marshal(ub) err = rs.db.Set(utils.ACCOUNT_PREFIX+ub.Id, result) return