internal void BanAddedCheck(object sender, BanPreAddEventArgs args) { //Only perform validation if the event has not been cancelled before we got here if (args.Valid) { //We consider a ban valid to add if no other *current* bans exist for the identifier provided. //E.g., if a previous ban has expired, a new ban is valid. //However, if a previous ban on the provided identifier is still in effect, a new ban is not valid args.Valid = !Bans.Any(b => b.Value.Identifier == args.Identifier && b.Value.ExpirationDateTime > DateTime.UtcNow); args.Message = args.Valid ? null : "a current ban for this identifier already exists."; } }
/// <summary> /// Adds a new ban for the given identifier. Returns a Ban object if the ban was added, else null /// </summary> /// <param name="identifier"></param> /// <param name="reason"></param> /// <param name="banningUser"></param> /// <param name="fromDate"></param> /// <param name="toDate"></param> /// <returns></returns> public AddBanResult InsertBan(string identifier, string reason, string banningUser, DateTime fromDate, DateTime toDate) { BanPreAddEventArgs args = new BanPreAddEventArgs { Identifier = identifier, Reason = reason, BanningUser = banningUser, BanDateTime = fromDate, ExpirationDateTime = toDate }; OnBanPreAdd?.Invoke(this, args); if (!args.Valid) { string message = $"Ban was not valid: {(args.Message ?? "no further information provided.")}"; return(new AddBanResult { Message = message }); } string query = "INSERT INTO PlayerBans (Identifier, Reason, BanningUser, Date, Expiration) VALUES (@0, @1, @2, @3, @4);"; if (database.GetSqlType() == SqlType.Mysql) { query += "SELECT CAST(LAST_INSERT_ID() as INT);"; } else { query += "SELECT CAST(last_insert_rowid() as INT);"; } int ticketId = database.QueryScalar <int>(query, identifier, reason, banningUser, fromDate.Ticks, toDate.Ticks); if (ticketId == 0) { return(new AddBanResult { Message = "Database insert failed." }); } Ban b = new Ban(ticketId, identifier, reason, banningUser, fromDate, toDate); _bans.Add(ticketId, b); OnBanPostAdd?.Invoke(this, new BanEventArgs { Ban = b }); return(new AddBanResult { Ban = b }); }
/// <summary> /// Adds a new ban for the given identifier. Returns a Ban object if the ban was added, else null /// </summary> /// <param name="identifier"></param> /// <param name="reason"></param> /// <param name="banningUser"></param> /// <param name="fromDate"></param> /// <param name="toDate"></param> /// <returns></returns> public AddBanResult InsertBan(string identifier, string reason, string banningUser, DateTime fromDate, DateTime toDate) { BanPreAddEventArgs args = new BanPreAddEventArgs { Identifier = identifier, Reason = reason, BanningUser = banningUser, BanDateTime = fromDate, ExpirationDateTime = toDate }; return(InsertBan(args)); }