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

Added warning email option for invalid accounts.

This commit is contained in:
Uncled1023 2016-05-19 10:28:48 -07:00
parent 618df3385f
commit bd55b29de9
2 changed files with 177 additions and 78 deletions

View File

@ -25,6 +25,9 @@ namespace ServerMaint
[Option('g', "clean-git", DefaultValue = false, Required = false, HelpText = "Clean all unused git accounts")] [Option('g', "clean-git", DefaultValue = false, Required = false, HelpText = "Clean all unused git accounts")]
public bool CleanGit { get; set; } public bool CleanGit { get; set; }
[Option('w', "warn", DefaultValue = false, Required = false, HelpText = "Warn all invalid accounts")]
public bool WarnAccounts { get; set; }
[Option('s', "scan", DefaultValue = false, Required = false, HelpText = "Scan all uploads for viruses")] [Option('s', "scan", DefaultValue = false, Required = false, HelpText = "Scan all uploads for viruses")]
public bool ScanUploads { get; set; } public bool ScanUploads { get; set; }

View File

@ -53,10 +53,16 @@ namespace ServerMaint
ScanUploads(config, db); ScanUploads(config, db);
} }
// Warns all the invalid accounts via email
if (options.WarnAccounts)
{
WarnInvalidAccounts(config, db);
}
// Cleans all inactive users // Cleans all inactive users
if (options.CleanUsers) if (options.CleanUsers)
{ {
CleanUsers(config, db, options.DaysBeforeDeletion); CleanAccounts(config, db, options.DaysBeforeDeletion);
} }
// Cleans the email for unused accounts // Cleans the email for unused accounts
@ -186,106 +192,97 @@ namespace ServerMaint
Output(string.Format("Scanning Complete. {0} Scanned | {1} Viruses Found | {2} Total Files", totalScans, totalViruses, totalCount)); Output(string.Format("Scanning Complete. {0} Scanned | {1} Viruses Found | {2} Total Files", totalScans, totalViruses, totalCount));
} }
public static void CleanUsers(Config config, TeknikEntities db, int maxDays) public static void WarnInvalidAccounts(Config config, TeknikEntities db)
{ {
int totalUsers = 0;
Output(string.Format("[{0}] Started Cleaning of Inactive Users.", DateTime.Now)); Output(string.Format("[{0}] Started Warning of Invalid Accounts.", DateTime.Now));
List<User> curUsers = db.Users.ToList(); List<string> invalidAccounts = GetInvalidAccounts(config, db);
foreach (User user in curUsers)
foreach (string account in invalidAccounts)
{ {
// If the username is reserved, don't clean it // Let's send them an email :D
if (UserHelper.UsernameReserved(config, user.Username)) string email = UserHelper.GetUserEmailAddress(config, account);
{
continue;
}
// If the username is invalid, let's clean the sucker, data and all SmtpClient client = new SmtpClient();
if (!UserHelper.ValidUsername(config, user.Username)) client.Host = config.ContactConfig.Host;
{ client.Port = config.ContactConfig.Port;
UserHelper.DeleteAccount(db, config, user); client.EnableSsl = config.ContactConfig.SSL;
continue; client.DeliveryMethod = SmtpDeliveryMethod.Network;
} client.UseDefaultCredentials = true;
client.Credentials = new NetworkCredential(config.ContactConfig.Username, config.ContactConfig.Password);
client.Timeout = 5000;
#region Inactivity Cleaning MailMessage mail = new MailMessage(config.SupportEmail, email);
DateTime lastActivity = UserHelper.GetLastAccountActivity(db, config, user); mail.Subject = "Invalid Account Notice";
mail.Body = string.Format(@"
The account {0} does not meet the requirements for a valid username.
TimeSpan inactiveTime = DateTime.Now.Subtract(lastActivity); The username must match the following Regex Pattern: {1}
It must also be greater than or equal to {2} characters in length, and less than or equal to {3} characters in length.
// If older than max days, check their current usage This email is to let you know that this account will be deleted in {4} days ({5}) in order to comply with the username restrictions. If you would like to keep your data, you should create a new account and transfer the data over to the new account.
if (inactiveTime >= new TimeSpan(maxDays, 0, 0, 0, 0))
{
// Check the user's usage of the service.
bool noData = true;
// Any blog comments? In order to make the process as easy as possible, you can reply to this email to ask for your current account to be renamed to another available account. This would keep all your data intact, and just require you to change all references to your email/git/user to the new username. If you wish to do this, please respond within {6} days ({7}).
var blogCom = db.BlogComments.Include("Users").Where(c => c.UserId == user.UserId);
noData &= !(blogCom != null && blogCom.Any());
// Any blog posts? Thank you for your continued use of Teknik!
var blogPosts = db.BlogPosts.Include("Blog").Include("Blog.Users").Where(p => p.Blog.UserId == user.UserId);
noData &= !(blogPosts != null && blogPosts.Any());
// Any podcast comments? - Teknik Administration", account, config.UserConfig.UsernameFilter, config.UserConfig.MinUsernameLength, config.UserConfig.MaxUsernameLength, 30, DateTime.Now.AddDays(30).ToShortDateString(), 15, DateTime.Now.AddDays(15).ToShortDateString());
var podCom = db.PodcastComments.Include("Users").Where(p => p.UserId == user.UserId); mail.BodyEncoding = UTF8Encoding.UTF8;
noData &= !(podCom != null && podCom.Any()); mail.DeliveryNotificationOptions = DeliveryNotificationOptions.Never;
// Any email? client.Send(mail);
if (config.EmailConfig.Enabled)
{
var app = new hMailServer.Application();
app.Connect();
app.Authenticate(config.EmailConfig.Username, config.EmailConfig.Password);
try
{
var domain = app.Domains.ItemByName[config.EmailConfig.Domain];
var account = domain.Accounts.ItemByAddress[UserHelper.GetUserEmailAddress(config, user.Username)];
noData &= ((account.Messages.Count == 0) && ((int)account.Size == 0));
}
catch { }
}
// Any git repos?
if (config.GitConfig.Enabled)
{
string email = UserHelper.GetUserEmailAddress(config, user.Username);
// We need to check the actual git database
MysqlDatabase mySQL = new MysqlDatabase(config.GitConfig.Database);
string sql = @"SELECT * FROM gogs.repository
LEFT JOIN gogs.action ON gogs.user.id = gogs.action.act_user_id
WHERE gogs.user.login_name = {0}";
var results = mySQL.Query(sql, new object[] { email });
noData &= !(results != null && results.Any());
}
if (noData)
{
// They have no data, so safe to delete them.
UserHelper.DeleteAccount(db, config, UserHelper.GetUser(db, user.Username));
totalUsers++;
}
continue;
}
#endregion
} }
if (totalUsers > 0) Output(string.Format("[{0}] Finished Warning of Invalid Accounts. {1} Accounts Warned.", DateTime.Now, invalidAccounts.Count));
}
public static void CleanAccounts(Config config, TeknikEntities db, int maxDays)
{
Output(string.Format("[{0}] Started Cleaning of Inactive/Invalid Users.", DateTime.Now));
List<string> invalidAccounts = GetInvalidAccounts(config, db);
List<string> inactiveAccounts = GetInactiveAccounts(config, db, maxDays);
// Delete invalid accounts
foreach (string account in invalidAccounts)
{
UserHelper.DeleteAccount(db, config, UserHelper.GetUser(db, account));
}
if (invalidAccounts.Count > 0)
{ {
// Add to transparency report if any users were removed // Add to transparency report if any users were removed
Takedown report = db.Takedowns.Create(); Takedown report = db.Takedowns.Create();
report.Requester = TAKEDOWN_REPORTER; report.Requester = TAKEDOWN_REPORTER;
report.RequesterContact = config.SupportEmail; report.RequesterContact = config.SupportEmail;
report.DateRequested = DateTime.Now; report.DateRequested = DateTime.Now;
report.Reason = "User Inactive"; report.Reason = "Username Invalid";
report.ActionTaken = string.Format("{0} Users Removed", totalUsers); report.ActionTaken = string.Format("{0} Accounts Removed", invalidAccounts.Count);
report.DateActionTaken = DateTime.Now; report.DateActionTaken = DateTime.Now;
db.Takedowns.Add(report); db.Takedowns.Add(report);
db.SaveChanges(); db.SaveChanges();
} }
Output(string.Format("[{0}] Finished Cleaning of Inactive Users. {1} Users Removed.", DateTime.Now, totalUsers)); // Delete inactive accounts
foreach (string account in inactiveAccounts)
{
UserHelper.DeleteAccount(db, config, UserHelper.GetUser(db, account));
}
if (invalidAccounts.Count > 0)
{
// Add to transparency report if any users were removed
Takedown report = db.Takedowns.Create();
report.Requester = TAKEDOWN_REPORTER;
report.RequesterContact = config.SupportEmail;
report.DateRequested = DateTime.Now;
report.Reason = "Account Inactive";
report.ActionTaken = string.Format("{0} Accounts Removed", inactiveAccounts.Count);
report.DateActionTaken = DateTime.Now;
db.Takedowns.Add(report);
db.SaveChanges();
}
Output(string.Format("[{0}] Finished Cleaning of Inactive/Invalid Users. {1} Accounts Removed.", DateTime.Now, invalidAccounts.Count + inactiveAccounts.Count));
} }
public static void CleanEmail(Config config, TeknikEntities db) public static void CleanEmail(Config config, TeknikEntities db)
@ -404,6 +401,105 @@ namespace ServerMaint
Output(string.Format("[{0}] Finished Generating Last Activity List.", DateTime.Now)); Output(string.Format("[{0}] Finished Generating Last Activity List.", DateTime.Now));
} }
public static List<string> GetInvalidAccounts(Config config, TeknikEntities db)
{
List<string> foundUsers = new List<string>();
List<User> curUsers = db.Users.ToList();
foreach (User user in curUsers)
{
// If the username is reserved, don't worry about it
if (UserHelper.UsernameReserved(config, user.Username))
{
continue;
}
// If the username is invalid, let's add it to the list
if (!UserHelper.ValidUsername(config, user.Username))
{
foundUsers.Add(user.Username);
continue;
}
}
return foundUsers;
}
public static List<string> GetInactiveAccounts(Config config, TeknikEntities db, int maxDays)
{
List<string> foundUsers = new List<string>();
List<User> curUsers = db.Users.ToList();
foreach (User user in curUsers)
{
// If the username is reserved, don't worry about it
if (UserHelper.UsernameReserved(config, user.Username))
{
continue;
}
#region Inactivity Finding
DateTime lastActivity = UserHelper.GetLastAccountActivity(db, config, user);
TimeSpan inactiveTime = DateTime.Now.Subtract(lastActivity);
// If older than max days, check their current usage
if (inactiveTime >= new TimeSpan(maxDays, 0, 0, 0, 0))
{
// Check the user's usage of the service.
bool noData = true;
// Any blog comments?
var blogCom = db.BlogComments.Include("Users").Where(c => c.UserId == user.UserId);
noData &= !(blogCom != null && blogCom.Any());
// Any blog posts?
var blogPosts = db.BlogPosts.Include("Blog").Include("Blog.Users").Where(p => p.Blog.UserId == user.UserId);
noData &= !(blogPosts != null && blogPosts.Any());
// Any podcast comments?
var podCom = db.PodcastComments.Include("Users").Where(p => p.UserId == user.UserId);
noData &= !(podCom != null && podCom.Any());
// Any email?
if (config.EmailConfig.Enabled)
{
var app = new hMailServer.Application();
app.Connect();
app.Authenticate(config.EmailConfig.Username, config.EmailConfig.Password);
try
{
var domain = app.Domains.ItemByName[config.EmailConfig.Domain];
var account = domain.Accounts.ItemByAddress[UserHelper.GetUserEmailAddress(config, user.Username)];
noData &= ((account.Messages.Count == 0) && ((int)account.Size == 0));
}
catch { }
}
// Any git repos?
if (config.GitConfig.Enabled)
{
string email = UserHelper.GetUserEmailAddress(config, user.Username);
// We need to check the actual git database
MysqlDatabase mySQL = new MysqlDatabase(config.GitConfig.Database);
string sql = @"SELECT * FROM gogs.repository
LEFT JOIN gogs.action ON gogs.user.id = gogs.action.act_user_id
WHERE gogs.user.login_name = {0}";
var results = mySQL.Query(sql, new object[] { email });
noData &= !(results != null && results.Any());
}
if (noData)
{
// They have no data, so safe to delete them.
foundUsers.Add(user.Username);
}
continue;
}
#endregion
}
return foundUsers;
}
public static void Output(string message) public static void Output(string message)
{ {
Console.WriteLine(message); Console.WriteLine(message);