diff --git a/Refresh.GameServer/Database/GameDatabaseContext.Tokens.cs b/Refresh.GameServer/Database/GameDatabaseContext.Tokens.cs index 16208eb8..ca707bfa 100644 --- a/Refresh.GameServer/Database/GameDatabaseContext.Tokens.cs +++ b/Refresh.GameServer/Database/GameDatabaseContext.Tokens.cs @@ -169,8 +169,22 @@ public void AddIpVerificationRequest(GameUser user, string ipAddress) public void AddVerifiedIp(GameUser user, string ipAddress, IDateTimeProvider timeProvider) { + const int maxVerifiedIps = 3; + + int count = this.GameUserVerifiedIpRelations.Count(r => r.User == user); + int toRemove = count >= maxVerifiedIps ? count - maxVerifiedIps + 1 : 0; + this.Write(() => { + // Remove the oldest verified IPs if the user has too many (or will have too many after this one) + if (toRemove > 0) + this.GameUserVerifiedIpRelations.RemoveRange( + this.GameUserVerifiedIpRelations + .Where(r => r.User == user) + .OrderBy(r => r.VerifiedAt) + .AsEnumerable() + .Take(toRemove)); + this.GameUserVerifiedIpRelations.Add(new GameUserVerifiedIpRelation { User = user, diff --git a/Refresh.GameServer/Database/RealmDbSet.cs b/Refresh.GameServer/Database/RealmDbSet.cs index 273d18cc..c5024ca6 100644 --- a/Refresh.GameServer/Database/RealmDbSet.cs +++ b/Refresh.GameServer/Database/RealmDbSet.cs @@ -43,6 +43,12 @@ public void RemoveRange(IQueryable objs) { this._realm.RemoveRange(objs); } + + public void RemoveRange(IEnumerable objs) + { + foreach (T obj in objs) + this._realm.Remove(obj); + } public void RemoveRange(Expression> predicate) {