Example #1
0
        public AccountBanned buildAccountBanned(DataRow accBan)
        {
            var accountBanned = new AccountBanned();

            accountBanned.banDate   = UtilityHelper.UnixStampToDateTime(accBan["bandate"].ToString());
            accountBanned.unbanDate = UtilityHelper.UnixStampToDateTime(accBan["unbandate"].ToString());
            accountBanned.by        = accBan["bannedby"].ToString();
            accountBanned.reason    = accBan["banreason"].ToString();
            accountBanned.isActive  = Convert.ToBoolean(accBan["active"]);

            return(accountBanned);
        }
Example #2
0
        public string accountBannedToSQL(uint id, AccountBanned ba)
        {
            if (ba != null)
            {
                string[] columns = { "id", "bandate", "unbandate", "bannedby", "banreason", "active" };
                object[] values  = { id,   UtilityHelper.DateTimeToUnixStamp(ba.banDate), UtilityHelper.DateTimeToUnixStamp(ba.unbanDate),
                                     ba.by, ba.reason,                                     Convert.ToSByte(ba.isActive) };

                return(deleteFromDatabase("account_banned", "id", id.ToString()) + insertToDatabase("account_banned", columns, values));
            }

            return(null);
        }
Example #3
0
        public async Task <IActionResult> BanAccount([FromBody] BanAccountModel model)
        {
            var user = await TokenHelper.GetUser(User, _userManager);

            if (user == null)
            {
                return(RequestHandler.Unauthorized());
            }

            var isAdmin = await user.IsUserAdmin(_userManager);

            if (!isAdmin)
            {
                return(RequestHandler.Unauthorized());
            }

            var account = await _authContext.Account.FirstOrDefaultAsync(x => x.Id == model.AccountId);

            if (account == null)
            {
                return(RequestHandler.BadRequest("Account does not exist"));
            }

            var banData = await _authContext.AccountBanned.AnyAsync(x => x.AccountId == model.AccountId && x.Active == 1);

            if (banData)
            {
                return(RequestHandler.BadRequest($"Account {account.Username} is already banned"));
            }

            var now = (int)DateTimeOffset.UtcNow.ToUnixTimeSeconds();

            var ban = new AccountBanned
            {
                AccountId = model.AccountId,
                Active    = 1,
                BanReason = model.Reason,
                UnbanDate = model.UnBanDate,
                BanDate   = now,
                BannedBy  = user.UserName
            };

            await _authContext.AccountBanned.AddAsync(ban);

            await _authContext.SaveChangesAsync();

            return(Ok(account));
        }
Example #4
0
        /// <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);
        }