From a73208d2f8921fc767a8dc150383daf0e7b42ff2 Mon Sep 17 00:00:00 2001 From: Uncled1023 Date: Wed, 18 May 2016 21:52:42 -0700 Subject: [PATCH] Added deletion of invalid accounts. --- ServerMaint/Program.cs | 9 +- Teknik/Areas/User/Utility/UserHelper.cs | 106 ++++++++++++------------ 2 files changed, 62 insertions(+), 53 deletions(-) diff --git a/ServerMaint/Program.cs b/ServerMaint/Program.cs index 85dcb14..abe7326 100644 --- a/ServerMaint/Program.cs +++ b/ServerMaint/Program.cs @@ -195,12 +195,17 @@ namespace ServerMaint foreach (User user in curUsers) { // If the username is reserved, don't clean it - if (!UserHelper.ValidUsername(db, config, user.Username)) + if (UserHelper.UsernameReserved(config, user.Username)) { continue; } // If the username is invalid, let's clean the sucker, data and all + if (!UserHelper.ValidUsername(config, user.Username)) + { + UserHelper.DeleteAccount(db, config, user); + continue; + } #region Inactivity Cleaning DateTime lastActivity = UserHelper.GetLastAccountActivity(db, config, user); @@ -258,7 +263,7 @@ namespace ServerMaint if (noData) { // They have no data, so safe to delete them. - UserHelper.DeleteUser(db, config, UserHelper.GetUser(db, user.Username)); + UserHelper.DeleteAccount(db, config, UserHelper.GetUser(db, user.Username)); totalUsers++; } continue; diff --git a/Teknik/Areas/User/Utility/UserHelper.cs b/Teknik/Areas/User/Utility/UserHelper.cs index d19ecfe..11dce56 100644 --- a/Teknik/Areas/User/Utility/UserHelper.cs +++ b/Teknik/Areas/User/Utility/UserHelper.cs @@ -22,6 +22,61 @@ namespace Teknik.Areas.Users.Utility public static class UserHelper { #region Account Management + public static List GetReservedUsernames(Config config) + { + List foundNames = new List(); + if (config != null) + { + string path = config.UserConfig.ReservedUsernameDefinitionFile; + if (File.Exists(path)) + { + string[] names = File.ReadAllLines(path); + foundNames = names.ToList(); + } + } + return foundNames; + } + + public static bool UsernameReserved(Config config, string username) + { + // Load reserved usernames + List reserved = GetReservedUsernames(config); + return (reserved.Exists(u => u.ToLower() == username.ToLower())); + } + + public static bool ValidUsername(Config config, string username) + { + bool isValid = true; + + // Must be something there + isValid &= !string.IsNullOrEmpty(username); + + // Is the format correct? + Regex reg = new Regex(config.UserConfig.UsernameFilter); + isValid &= reg.IsMatch(username); + + // Meets the min length? + isValid &= (username.Length >= config.UserConfig.MinUsernameLength); + + // Meets the max length? + isValid &= (username.Length <= config.UserConfig.MaxUsernameLength); + + return isValid; + } + + public static bool UsernameAvailable(TeknikEntities db, Config config, string username) + { + bool isAvailable = true; + + isAvailable &= ValidUsername(config, username); + isAvailable &= !UsernameReserved(config, username); + isAvailable &= !UserExists(db, username); + isAvailable &= !UserEmailExists(config, GetUserEmailAddress(config, username)); + isAvailable &= !UserGitExists(config, username); + + return isAvailable; + } + public static DateTime GetLastAccountActivity(TeknikEntities db, Config config, User user) { try @@ -112,21 +167,6 @@ namespace Teknik.Areas.Users.Utility #endregion #region User Management - public static List GetReservedUsernames(Config config) - { - List foundNames = new List(); - if (config != null) - { - string path = config.UserConfig.ReservedUsernameDefinitionFile; - if (File.Exists(path)) - { - string[] names = File.ReadAllLines(path); - foundNames = names.ToList(); - } - } - return foundNames; - } - public static User GetUser(TeknikEntities db, string username) { User user = db.Users.Where(b => b.Username == username).FirstOrDefault(); @@ -151,42 +191,6 @@ namespace Teknik.Areas.Users.Utility return false; } - public static bool ValidUsername(TeknikEntities db, Config config, string username) - { - bool isValid = true; - - // Must be something there - isValid &= !string.IsNullOrEmpty(username); - - // Is the format correct? - Regex reg = new Regex(config.UserConfig.UsernameFilter); - isValid &= reg.IsMatch(username); - - // Meets the min length? - isValid &= (username.Length >= config.UserConfig.MinUsernameLength); - - // Meets the max length? - isValid &= (username.Length <= config.UserConfig.MaxUsernameLength); - - // Load reserved usernames - List reserved = GetReservedUsernames(config); - isValid &= (reserved.Exists(u => u.ToLower() == username.ToLower())); - - return isValid; - } - - public static bool UsernameAvailable(TeknikEntities db, Config config, string username) - { - bool isAvailable = true; - - isAvailable &= ValidUsername(db, config, username); - isAvailable &= !UserExists(db, username); - isAvailable &= !UserEmailExists(config, GetUserEmailAddress(config, username)); - isAvailable &= !UserGitExists(config, username); - - return isAvailable; - } - public static DateTime UserLastActive(TeknikEntities db, Config config, User user) { try