/// <summary> /// Tries to remove a ban from an account. /// </summary> /// <param name="accountID">The account to remove the ban from.</param> /// <param name="issuedBy">The name of the user or source that issued the unban.</param> /// <param name="failReason">When this method returns false, contains the reason why the unban failed to be added.</param> /// <returns> /// True if the unban was successfully added; otherwise false. /// </returns> public bool TryRemoveAccountBan(TAccountID accountID, string issuedBy, out BanManagerFailReason failReason) { // Check the parameters if (string.IsNullOrEmpty(issuedBy)) { failReason = BanManagerFailReason.NoIssuerProvided; if (log.IsInfoEnabled) { log.InfoFormat("Failed to unban account `{0}`: {1}", accountID, failReason.GetDetailedString()); } return(false); } // Try to remove the ban try { if (!TryRemoveBanInternal(accountID, issuedBy, out failReason)) { if (log.IsInfoEnabled) { log.InfoFormat("Failed to unban account `{0}`: {1}", accountID, failReason.GetDetailedString()); } return(false); } } catch (Exception ex) { failReason = BanManagerFailReason.ExceptionOccured; if (log.IsInfoEnabled) { log.InfoFormat("Failed to unban account `{0}`: {1}. Exception: {2}", accountID, failReason.GetDetailedString(), ex); } return(false); } // Raise the event OnAccountUnBanned(accountID); if (AccountUnBanned != null) { AccountUnBanned.Raise(this, BanningManagerAccountUnBannedEventArgs.Create(accountID, issuedBy)); } if (log.IsInfoEnabled) { log.InfoFormat("Successfully unbanned account `{0}` (issuedBy: {1}).", accountID, issuedBy); } failReason = BanManagerFailReason.Unknown; return(true); }
/// <summary> /// When overridden in the derived class, adds a ban to an account. /// </summary> /// <param name="accountID">The account to add the ban to.</param> /// <param name="length">How long the ban will last.</param> /// <param name="reason">The reason for the ban.</param> /// <param name="issuedBy">The name of the user or source that issued the ban.</param> /// <param name="failReason">When this method returns false, contains the reason why the ban failed to be added.</param> /// <returns> /// True if the ban was successfully added; otherwise false. /// </returns> protected override bool TryAddBanInternal(AccountID accountID, TimeSpan length, string reason, string issuedBy, out BanManagerFailReason failReason) { var q = DbController.GetQuery <InsertAccountBanQuery>(); var rowsAffected = q.ExecuteWithResult(accountID, length, reason, issuedBy); if (rowsAffected == 0) { failReason = BanManagerFailReason.FailedToAddToDatabase; return(false); } else { failReason = BanManagerFailReason.Unknown; return(true); } }
/// <summary> /// Tries to remove a ban from an account by specifying the user's name. /// </summary> /// <param name="userName">The name of the user to unban.</param> /// <param name="issuedBy">The name of the user or source that issued the unban.</param> /// <param name="failReason">When this method returns false, contains the reason why the ban failed to be removed.</param> /// <returns> /// True if the ban was successfully removed; otherwise false. /// </returns> public bool TryRemoveUserBan(string userName, string issuedBy, out BanManagerFailReason failReason) { // Get the account ID TAccountID accountID; if (!TryGetAccountIDFromUserName(userName, out accountID)) { const string errmsg = "Failed to unban user `{0}`: {1}"; failReason = BanManagerFailReason.InvalidUser; if (log.IsInfoEnabled) { log.InfoFormat(errmsg, userName, failReason.GetDetailedString()); } return(false); } // UnBan the account by ID return(TryRemoveAccountBan(accountID, issuedBy, out failReason)); }
/// <summary> /// Tries to add a ban to an account. /// </summary> /// <param name="accountName">The name of the account to ban.</param> /// <param name="length">How long the ban will last.</param> /// <param name="reason">The reason for the ban.</param> /// <param name="issuedBy">The name of the user or source that issued the ban.</param> /// <param name="failReason">When this method returns false, contains the reason why the ban failed to be added.</param> /// <returns> /// True if the ban was successfully added; otherwise false. /// </returns> public bool TryAddAccountBan(string accountName, TimeSpan length, string reason, string issuedBy, out BanManagerFailReason failReason) { // Get the account ID TAccountID accountID; if (!TryGetAccountIDFromAccountName(accountName, out accountID)) { const string errmsg = "Failed to ban account `{0}`: {1}"; failReason = BanManagerFailReason.InvalidAccount; if (log.IsInfoEnabled) { log.InfoFormat(errmsg, accountName, failReason.GetDetailedString()); } return(false); } // Ban the account by ID return(TryAddAccountBan(accountID, length, reason, issuedBy, out failReason)); }
/// <summary> /// Gets a string containing the details for the <see cref="BanManagerFailReason"/>. /// </summary> /// <param name="reason">The <see cref="BanManagerFailReason"/>.</param> /// <returns>The detailed reason string for the <paramref name="reason"/>.</returns> public static string GetDetailedString(this BanManagerFailReason reason) { switch (reason) { case BanManagerFailReason.Unknown: return("[Unspecified failure]"); case BanManagerFailReason.NegativeBanDuration: return("Invalid ban length - duration cannot be negative."); case BanManagerFailReason.NoIssuerProvided: return("A ban issuer must be provided."); case BanManagerFailReason.NoReasonProvided: return("A reason for the ban must be provided."); case BanManagerFailReason.ExceptionOccured: return("An exception occured while trying to add the ban."); case BanManagerFailReason.FailedToAddToDatabase: return("Failed to add the ban to the database."); case BanManagerFailReason.InvalidAccount: return("The given account does not exist."); case BanManagerFailReason.InvalidUser: return("The given user does not exist."); default: const string errmsg = "No detailed string provided for BanManagerFailReason `{0}`."; if (log.IsWarnEnabled) { log.WarnFormat(errmsg, reason); } Debug.Fail(string.Format(errmsg, reason)); return(reason + " [Details not available]"); } }
/// <summary> /// When overridden in the derived class, adds a ban to an account. /// </summary> /// <param name="accountID">The account to add the ban to.</param> /// <param name="length">How long the ban will last.</param> /// <param name="reason">The reason for the ban.</param> /// <param name="issuedBy">The name of the user or source that issued the ban.</param> /// <param name="failReason">When this method returns false, contains the reason why the ban failed to be added.</param> /// <returns> /// True if the ban was successfully added; otherwise false. /// </returns> protected abstract bool TryAddBanInternal(TAccountID accountID, TimeSpan length, string reason, string issuedBy, out BanManagerFailReason failReason);
/// <summary> /// Tries to add a ban to an account. /// </summary> /// <param name="accountID">The account to add the ban to.</param> /// <param name="length">How long the ban will last.</param> /// <param name="reason">The reason for the ban.</param> /// <param name="issuedBy">The name of the user or source that issued the ban.</param> /// <param name="failReason">When this method returns false, contains the reason why the ban failed to be added.</param> /// <returns> /// True if the ban was successfully added; otherwise false. /// </returns> public bool TryAddAccountBan(TAccountID accountID, TimeSpan length, string reason, string issuedBy, out BanManagerFailReason failReason) { // Check the parameters if (length.TotalMilliseconds < 0) { failReason = BanManagerFailReason.NegativeBanDuration; if (log.IsInfoEnabled) { log.InfoFormat("Failed to ban account `{0}`: {1}", accountID, failReason.GetDetailedString()); } return(false); } if (string.IsNullOrEmpty(reason)) { failReason = BanManagerFailReason.NoReasonProvided; if (log.IsInfoEnabled) { log.InfoFormat("Failed to ban account `{0}`: {1}", accountID, failReason.GetDetailedString()); } return(false); } if (string.IsNullOrEmpty(issuedBy)) { failReason = BanManagerFailReason.NoIssuerProvided; if (log.IsInfoEnabled) { log.InfoFormat("Failed to ban account `{0}`: {1}", accountID, failReason.GetDetailedString()); } return(false); } // Try to add the ban try { if (!TryAddBanInternal(accountID, length, reason, issuedBy, out failReason)) { if (log.IsInfoEnabled) { log.InfoFormat("Failed to ban account `{0}`: {1}", accountID, failReason.GetDetailedString()); } return(false); } } catch (Exception ex) { failReason = BanManagerFailReason.ExceptionOccured; if (log.IsInfoEnabled) { log.InfoFormat("Failed to ban account `{0}`: {1}. Exception: {2}", accountID, failReason.GetDetailedString(), ex); } return(false); } // Raise the event OnAccountBanned(accountID); if (AccountBanned != null) { AccountBanned.Raise(this, BanningManagerAccountBannedEventArgs.Create(accountID, length, reason, issuedBy)); } if (log.IsInfoEnabled) { log.InfoFormat("Successfully banned account `{0}` (length: {1}; reason: {2}; issuedBy: {3}).", accountID, length, reason, issuedBy); } failReason = BanManagerFailReason.Unknown; return(true); }
/// <summary> /// When overridden in the derived class, removes a ban from an account. /// </summary> /// <param name="accountID">The account to remove the ban from.</param> /// <param name="issuedBy">The name of the user or source that issued the unban.</param> /// <param name="failReason">When this method returns false, contains the reason why the unban failed to be removed.</param> /// <returns> /// True if the unban was successfully added; otherwise false. /// </returns> protected abstract bool TryRemoveBanInternal(TAccountID accountID, string issuedBy, out BanManagerFailReason failReason);
/// <summary> /// When overridden in the derived class, removes a ban from an account. /// </summary> /// <param name="accountID">The account to remove the ban from.</param> /// <param name="issuedBy">The name of the user or source that issued the unban.</param> /// <param name="failReason">When this method returns false, contains the reason why the ban failed to be removed.</param> /// <returns> /// True if the unban was successfully added; otherwise false. /// </returns> protected override bool TryRemoveBanInternal(AccountID accountID, string issuedBy, out BanManagerFailReason failReason) { var q = DbController.GetQuery <UpdateAccountUnBanQuery>(); var rowsAffected = q.ExecuteWithResult(accountID, issuedBy); if (rowsAffected == 0) { failReason = BanManagerFailReason.FailedToRemoveFromDatabase; return(false); } else { failReason = BanManagerFailReason.Unknown; return(true); } }