Ejemplo n.º 1
0
        /// <summary>
        /// Adds the given IPBan
        /// </summary>
        /// <param name="toAdd">the dto containing the values to add as a new entity</param>
        /// <returns>the ID of the new IPBan, or 0 if it failed</returns>
        public static async Task <int> AddNewIPBanAsync(IPBanDto toAdd)
        {
            if (toAdd == null)
            {
                return(0);
            }

            var toInsert = new IPBanEntity()
            {
                IPSegment1       = toAdd.IPSegment1,
                IPSegment2       = toAdd.IPSegment2,
                IPSegment3       = toAdd.IPSegment3,
                IPSegment4       = toAdd.IPSegment4,
                Range            = toAdd.Range,
                Reason           = toAdd.Reason,
                IPBanSetByUserID = toAdd.IPBanSetByUserID
            };

            using (var adapter = new DataAccessAdapter())
            {
                var result = await adapter.SaveEntityAsync(toInsert).ConfigureAwait(false);

                return(result ? toInsert.IPBanID : 0);
            }
        }
Ejemplo n.º 2
0
        /// <summary>Creates a new, empty IPBanEntity object.</summary>
        /// <returns>A new, empty IPBanEntity object.</returns>
        public override IEntity Create()
        {
            IEntity toReturn = new IPBanEntity();

            // __LLBLGENPRO_USER_CODE_REGION_START CreateNewIPBan
            // __LLBLGENPRO_USER_CODE_REGION_END
            return(toReturn);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Checks if passed in IP address, in string format matches an IP ban. If the ip address matches an ip ban, the matching IP ban is returned
        /// </summary>
        /// <param name="allCachedIPBans">All cached IP bans from the cache.</param>
        /// <param name="ipAddressUser">The ip address of the user, in string format.</param>
        /// <returns>null if the user doesn't match any IP ban, or the first IPBan entity it matches</returns>
        public static IPBanEntity GetIPBanMatchingUserIPAddress(Dictionary <int, Dictionary <string, IPBanEntity> > allCachedIPBans, string ipAddressUser)
        {
            var ipAddressSegments = ipAddressUser.Split('.');

            if (ipAddressSegments.Length != 4)
            {
                // can't do matching as the ip address has less or more segments
                return(null);
            }

            Dictionary <string, IPBanEntity> rangeBans = null;
            var         ipTemp   = new StringBuilder();
            IPBanEntity toReturn = null;

            // for each segment we'll check if it, combined with the previous segments, results in a match. If not, the next segment will be tried.
            for (var i = 0; i < 4; i++)
            {
                // check range ban. Build ip address segments from ip address passed in and check that with the segments in the range ban dictionary
                if (i > 0)
                {
                    ipTemp.Append(".");
                }

                ipTemp.Append(ipAddressSegments[i]);
                rangeBans = allCachedIPBans.GetValue(8 * (i + 1));
                if (rangeBans == null)
                {
                    // no range bans with this range, continue
                    continue;
                }

                if (rangeBans.TryGetValue(ipTemp.ToString(), out toReturn))
                {
                    // we have a match!
                    break;
                }
            }

            return(toReturn);
        }
Ejemplo n.º 4
0
        private void Page_Load(object sender, System.EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string banComplainAddress = ApplicationAdapter.GetIPBanComplainEmailAddress();
                lnkBanComplaintEmailAddress.Text         = banComplainAddress;
                lnkBanComplaintEmailAddress.NavigateUrl += banComplainAddress;

                // get the set of IP-bans for the given IP address
                string      ipAddressUser = Request.UserHostAddress;
                IPBanEntity matchingBan   = SecurityGuiHelper.GetIPBanMatchingUserIPAddress(CacheManager.GetAllIPBans(), ipAddressUser);

                if (matchingBan != null)
                {
                    // has to match a ban
                    lblIPBanDate.Text  = matchingBan.IPBanSetOn.ToString("dd-MMM-yyyy HH:mm:ss");
                    lblIPBanRange.Text = string.Format("{0}.{1}.{2}.{3} / {4}", matchingBan.IPSegment1, matchingBan.IPSegment2, matchingBan.IPSegment3,
                                                       matchingBan.IPSegment4, matchingBan.Range);
                    lblIPBanReason.Text = matchingBan.Reason;
                }
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Deletes the user with the ID passed in. Will reset all posts made by the user to the userid 0.
        /// </summary>
        /// <param name="userId">The user ID.</param>
        /// <remarks>Can't delete user 0</remarks>
        /// <returns>true if succeeded, false otherwise</returns>
        public static async Task <bool> DeleteUserAsync(int userId)
        {
            if (userId <= 1)
            {
                // can't delete the Anonymous user nor the admin user
                return(false);
            }

            var toDelete = await UserGuiHelper.GetUserAsync(userId);

            if (toDelete == null)
            {
                // user doesn't exist
                return(false);
            }

            if (toDelete.NickName == "Admin")
            {
                // can't delete admin
                return(false);
            }

            using (var adapter = new DataAccessAdapter())
            {
                await adapter.StartTransactionAsync(IsolationLevel.ReadCommitted, "DeleteUser").ConfigureAwait(false);

                try
                {
                    // we'll first update all PostedByUserId fields of all messages which are posted by the user to delete.
                    var messageUpdater = new MessageEntity {
                        PostedByUserID = 0
                    };

                    // reset to AC.
                    await adapter.UpdateEntitiesDirectlyAsync(messageUpdater, new RelationPredicateBucket(MessageFields.PostedByUserID.Equal(userId)))
                    .ConfigureAwait(false);

                    // set the startuser of threads started by this user to 0
                    var threadUpdater = new ThreadEntity {
                        StartedByUserID = 0
                    };
                    await adapter.UpdateEntitiesDirectlyAsync(threadUpdater, new RelationPredicateBucket(ThreadFields.StartedByUserID.Equal(userId)))
                    .ConfigureAwait(false);

                    // remove the user from the UserRoles set, as the user shouldn't be in any roles.
                    await adapter.DeleteEntitiesDirectlyAsync(typeof(RoleUserEntity), new RelationPredicateBucket(RoleUserFields.UserID.Equal(userId)))
                    .ConfigureAwait(false);

                    // delete all bookmarks of user
                    await adapter.DeleteEntitiesDirectlyAsync(typeof(BookmarkEntity), new RelationPredicateBucket(BookmarkFields.UserID.Equal(userId)))
                    .ConfigureAwait(false);

                    // delete all audit data
                    // first fetch it, then delete all entities from the collection, as the audit data is in an inheritance hierarchy of TargetPerEntity which can't
                    // be deleted directly from the db.
                    var auditData = await adapter.FetchQueryAsync(new QueryFactory().User.Where(UserFields.UserID.Equal(userId))).ConfigureAwait(false);

                    await adapter.DeleteEntityCollectionAsync(auditData).ConfigureAwait(false);

                    // set IP bans set by this user to userid 0
                    var ipbanUpdater = new IPBanEntity {
                        IPBanSetByUserID = 0
                    };
                    await adapter.UpdateEntitiesDirectlyAsync(ipbanUpdater, new RelationPredicateBucket(IPBanFields.IPBanSetByUserID.Equal(userId)))
                    .ConfigureAwait(false);

                    // delete threadsubscriptions
                    await adapter.DeleteEntitiesDirectlyAsync(typeof(ThreadSubscriptionEntity), new RelationPredicateBucket(ThreadSubscriptionFields.UserID.Equal(userId)))
                    .ConfigureAwait(false);

                    // remove supportqueuethread claims
                    await adapter.DeleteEntitiesDirectlyAsync(typeof(SupportQueueThreadEntity),
                                                              new RelationPredicateBucket(SupportQueueThreadFields.ClaimedByUserID.Equal(userId)))
                    .ConfigureAwait(false);

                    // set all placed in queue references to userid 0, so the threads stay in the queues.
                    var supportQueueThreadUpdater = new SupportQueueThreadEntity {
                        PlacedInQueueByUserID = 0
                    };
                    await adapter.UpdateEntitiesDirectlyAsync(supportQueueThreadUpdater,
                                                              new RelationPredicateBucket(SupportQueueThreadFields.PlacedInQueueByUserID.Equal(userId)))
                    .ConfigureAwait(false);

                    // now delete the actual user entity
                    await adapter.DeleteEntityAsync(toDelete).ConfigureAwait(false);

                    adapter.Commit();
                    return(true);
                }
                catch
                {
                    adapter.Rollback();
                    throw;
                }
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Deletes the user with the ID passed in. Will reset all posts made by the user to the userid 0.
        /// </summary>
        /// <param name="userID">The user ID.</param>
        /// <remarks>Can't delete user 0</remarks>
        /// <returns>true if succeeded, false otherwise</returns>
        public static bool DeleteUser(int userID)
        {
            if (userID == 0)
            {
                // can't delete the Anonymous coward user.
                return(false);
            }

            UserEntity toDelete = UserGuiHelper.GetUser(userID);

            if (toDelete == null)
            {
                // user doesn't exist
                return(false);
            }

            // all actions have to take place in a transaction.
            Transaction trans = new Transaction(IsolationLevel.ReadCommitted, "DeleteUser");

            try
            {
                // we'll first update all PostedByUserId fields of all messages which are posted by the user to delete.
                MessageEntity messageUpdater = new MessageEntity();
                messageUpdater.PostedByUserID = 0;      // reset to AC.
                MessageCollection messages = new MessageCollection();
                trans.Add(messages);                    // add to the transaction
                // update all entities directly in the DB, which match the following filter and update them with the new values set in messageUpdater.
                messages.UpdateMulti(messageUpdater, (MessageFields.PostedByUserID == userID));

                // set the startuser of threads started by this user to 0
                ThreadEntity threadUpdater = new ThreadEntity();
                threadUpdater.StartedByUserID = 0;
                ThreadCollection threads = new ThreadCollection();
                trans.Add(threads);
                threads.UpdateMulti(threadUpdater, (ThreadFields.StartedByUserID == userID));

                // remove the user from the UserRoles set, as the user shouldn't be in any roles.
                RoleUserCollection roleUsersDeleter = new RoleUserCollection();
                trans.Add(roleUsersDeleter);
                // delete all entities directly from the DB which match the following filter.
                roleUsersDeleter.DeleteMulti(RoleUserFields.UserID == userID);

                // delete all bookmarks of user
                BookmarkCollection bookmarkDeleter = new BookmarkCollection();
                trans.Add(bookmarkDeleter);
                // delete all bookmarks for this user directly from the DB using the following filter.
                bookmarkDeleter.DeleteMulti(BookmarkFields.UserID == userID);

                // delete all audit data
                AuditDataCoreCollection auditDataDeleter = new AuditDataCoreCollection();
                // first fetch it, then delete all entities from the collection, as the audit data is in an inheritance hierarchy of TargetPerEntity which can't
                // be deleted directly from the db.
                trans.Add(auditDataDeleter);
                auditDataDeleter.GetMulti(AuditDataCoreFields.UserID == userID);
                auditDataDeleter.DeleteMulti();

                // set IP bans set by this user to userid 0
                IPBanEntity ipbanUpdater = new IPBanEntity();
                ipbanUpdater.IPBanSetByUserID = 0;
                IPBanCollection ipBans = new IPBanCollection();
                trans.Add(ipBans);
                ipBans.UpdateMulti(ipbanUpdater, (IPBanFields.IPBanSetByUserID == userID));

                // delete threadsubscriptions
                ThreadSubscriptionCollection threadSubscriptionsDeleter = new ThreadSubscriptionCollection();
                trans.Add(threadSubscriptionsDeleter);
                threadSubscriptionsDeleter.DeleteMulti(ThreadSubscriptionFields.UserID == userID);

                // remove supportqueuethread claims
                SupportQueueThreadCollection supportQueueThreads = new SupportQueueThreadCollection();
                trans.Add(supportQueueThreads);
                supportQueueThreads.DeleteMulti(SupportQueueThreadFields.ClaimedByUserID == userID);

                // set all placed in queue references to userid 0, so the threads stay in the queues.
                SupportQueueThreadEntity supportQueueThreadUpdater = new SupportQueueThreadEntity();
                supportQueueThreadUpdater.PlacedInQueueByUserID = 0;
                supportQueueThreads.UpdateMulti(supportQueueThreadUpdater, (SupportQueueThreadFields.PlacedInQueueByUserID == userID));

                // now delete the actual user entity
                trans.Add(toDelete);
                toDelete.Delete();

                // all done
                trans.Commit();
                return(true);
            }
            catch
            {
                trans.Rollback();
                throw;
            }
            finally
            {
                trans.Dispose();
            }
        }