/// <summary> /// Gets all IP bans cached in the cache. /// </summary> /// <returns>Dictionary with per range (key) a dictionary with all IP addresses as keys, with the segments falling into the range concatenated /// to eachother with a '.'</returns> public static Dictionary <int, Dictionary <string, IPBanEntity> > GetAllIPBans() { Cache activeCache = HttpRuntime.Cache; Dictionary <int, Dictionary <string, IPBanEntity> > toReturn = (Dictionary <int, Dictionary <string, IPBanEntity> >)activeCache[CacheKeys.AllIPBans]; if (toReturn == null) { // not there, store it. IPBanCollection allIPBans = SecurityGuiHelper.GetAllIPBans(0, 0, false); toReturn = new Dictionary <int, Dictionary <string, IPBanEntity> >(); foreach (IPBanEntity currentIPBan in allIPBans) { Dictionary <string, IPBanEntity> ipAddresses = null; if (!toReturn.TryGetValue(currentIPBan.Range, out ipAddresses)) { // not there yet, add ipAddresses = new Dictionary <string, IPBanEntity>(); toReturn.Add(currentIPBan.Range, ipAddresses); } // add ip address with segments in range to ipAddresses' key list. string key = string.Empty; switch (currentIPBan.Range) { case 8: key = currentIPBan.IPSegment1.ToString(); break; case 16: key = String.Format("{0}.{1}", currentIPBan.IPSegment1, currentIPBan.IPSegment2); break; case 24: key = String.Format("{0}.{1}.{2}", currentIPBan.IPSegment1, currentIPBan.IPSegment2, currentIPBan.IPSegment3); break; case 32: key = String.Format("{0}.{1}.{2}.{3}", currentIPBan.IPSegment1, currentIPBan.IPSegment2, currentIPBan.IPSegment3, currentIPBan.IPSegment4); break; default: // illegal range, ignore continue; } if (!ipAddresses.ContainsKey(key)) { ipAddresses.Add(key, currentIPBan); } } // just store it in the cache without any dependency activeCache.Insert(CacheKeys.AllIPBans, toReturn); } return(toReturn); }
/// <summary> /// Gets all IP ban entities, sorted by range for the page specified. /// </summary> /// <param name="pageNo">The page number for the page to read. Specify 0 to fetch all ip bans.</param> /// <param name="pageSize">Size of the page to fetch. Specify 0 to fetch all ip bans.</param> /// <param name="prefetchUser">If set to true, it will prefetch the user entity into the ipBan entity, for the user who set the IPBan</param> /// <returns> /// the collection of ipban entities requested /// </returns> public static IPBanCollection GetAllIPBans(int pageNo, int pageSize, bool prefetchUser) { IPBanCollection toReturn = new IPBanCollection(); PrefetchPath path = null; if (prefetchUser) { // build the Prefetch path to fetch the user as well path = new PrefetchPath((int)EntityType.IPBanEntity); path.Add(IPBanEntity.PrefetchPathSetByUser); } toReturn.GetMulti(null, 0, new SortExpression(IPBanFields.Range.Ascending()), null, path, pageNo, pageSize); return(toReturn); }
/// <summary> /// Gets the total IP ban count as they're stored in the DB now. /// </summary> /// <returns>the # of IPBans stored in the database.</returns> public static int GetTotalIPBanCount() { IPBanCollection toReturn = new IPBanCollection(); return(toReturn.GetDbCount()); }
/// <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(); } }
/// <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(); } }