1
0
mirror of https://git.teknik.io/Teknikode/Teknik.git synced 2023-08-02 14:16:22 +02:00

Added deletion of invalid accounts.

This commit is contained in:
Uncled1023 2016-05-18 21:52:42 -07:00
parent d3b6e67edc
commit a73208d2f8
2 changed files with 62 additions and 53 deletions

View File

@ -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;

View File

@ -22,6 +22,61 @@ namespace Teknik.Areas.Users.Utility
public static class UserHelper
{
#region Account Management
public static List<string> GetReservedUsernames(Config config)
{
List<string> foundNames = new List<string>();
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<string> 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<string> GetReservedUsernames(Config config)
{
List<string> foundNames = new List<string>();
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<string> 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