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

Separated User add/edit/delete into a overall account to cover user/email/git.

This commit is contained in:
Uncled1023 2016-05-18 17:06:21 -07:00
parent f586cfe515
commit d3b6e67edc
3 changed files with 102 additions and 38 deletions

View File

@ -194,14 +194,16 @@ namespace ServerMaint
List<User> curUsers = db.Users.ToList();
foreach (User user in curUsers)
{
// If the username isn't valid, don't clean it (Reserved, not formatted correctly, etc)
// If the username is reserved, don't clean it
if (!UserHelper.ValidUsername(db, config, user.Username))
{
continue;
}
// If the username is invalid, let's clean the sucker, data and all
#region Inactivity Cleaning
DateTime lastActivity = UserHelper.GetLastActivity(db, config, user);
DateTime lastActivity = UserHelper.GetLastAccountActivity(db, config, user);
TimeSpan inactiveTime = DateTime.Now.Subtract(lastActivity);
@ -383,7 +385,7 @@ namespace ServerMaint
{
sb.AppendLine(string.Format("{0},{1},{2},{3},{4},{5}",
user.Username,
UserHelper.GetLastActivity(db, config, user).ToString("g"),
UserHelper.GetLastAccountActivity(db, config, user).ToString("g"),
user.JoinDate.ToString("g"),
user.LastSeen.ToString("g"),
UserHelper.UserEmailLastActive(config, UserHelper.GetUserEmailAddress(config, user.Username)).ToString("g"),

View File

@ -55,7 +55,7 @@ namespace Teknik.Areas.Users.Controllers
model.Email = string.Format("{0}@{1}", user.Username, Config.EmailConfig.Domain);
}
model.JoinDate = user.JoinDate;
model.LastSeen = UserHelper.GetLastActivity(db, Config, user);
model.LastSeen = UserHelper.GetLastAccountActivity(db, Config, user);
model.UserSettings = user.UserSettings;
model.BlogSettings = user.BlogSettings;
@ -236,7 +236,7 @@ namespace Teknik.Areas.Users.Controllers
newUser.BlogSettings = new BlogSettings();
newUser.UploadSettings = new UploadSettings();
UserHelper.AddUser(db, Config, newUser, model.Password);
UserHelper.AddAccount(db, Config, newUser, model.Password);
}
catch (Exception ex)
{
@ -293,7 +293,7 @@ namespace Teknik.Areas.Users.Controllers
user.UploadSettings.SaveKey = saveKey;
user.UploadSettings.ServerSideEncrypt = serverSideEncrypt;
UserHelper.EditUser(db, Config, user, changePass, newPass);
UserHelper.EditAccount(db, Config, user, changePass, newPass);
return Json(new { result = true });
}
return Json(new { error = "User does not exist" });
@ -316,7 +316,7 @@ namespace Teknik.Areas.Users.Controllers
User user = UserHelper.GetUser(db, User.Identity.Name);
if (user != null)
{
UserHelper.DeleteUser(db, Config, user);
UserHelper.DeleteAccount(db, Config, user);
// Sign Out
Logout();
return Json(new { result = true });

View File

@ -21,6 +21,97 @@ namespace Teknik.Areas.Users.Utility
{
public static class UserHelper
{
#region Account Management
public static DateTime GetLastAccountActivity(TeknikEntities db, Config config, User user)
{
try
{
DateTime lastActive = new DateTime(1900, 1, 1);
DateTime emailLastActive = UserEmailLastActive(config, GetUserEmailAddress(config, user.Username));
if (lastActive < emailLastActive)
lastActive = emailLastActive;
DateTime gitLastActive = UserGitLastActive(config, user.Username);
if (lastActive < gitLastActive)
lastActive = gitLastActive;
DateTime userLastActive = UserLastActive(db, config, user);
if (lastActive < userLastActive)
lastActive = userLastActive;
return lastActive;
}
catch (Exception ex)
{
throw new Exception("Unable to determine last account activity.", ex);
}
}
public static void AddAccount(TeknikEntities db, Config config, User user, string password)
{
try
{
// Create an Email Account
AddUserEmail(config, GetUserEmailAddress(config, user.Username), password);
// Create a Git Account
AddUserGit(config, user.Username, password);
// Add User
AddUser(db, config, user, password);
}
catch (Exception ex)
{
throw new Exception("Unable to create account.", ex);
}
}
public static void EditAccount(TeknikEntities db, Config config, User user, bool changePass, string password)
{
try
{
// Changing Password?
if (changePass)
{
// Change email password
EditUserEmailPassword(config, GetUserEmailAddress(config, user.Username), password);
// Update Git password
EditUserGitPassword(config, user.Username, password);
}
// Update User
EditUser(db, config, user, changePass, password);
}
catch (Exception ex)
{
throw new Exception("Unable to edit account.", ex);
}
}
public static void DeleteAccount(TeknikEntities db, Config config, User user)
{
try
{
// Delete Email Account
if (UserEmailExists(config, GetUserEmailAddress(config, user.Username)))
DeleteUserEmail(config, GetUserEmailAddress(config, user.Username));
// Delete Git Account
if (UserGitExists(config, user.Username))
DeleteUserGit(config, user.Username);
// Delete User Account
DeleteUser(db, config, user);
}
catch (Exception ex)
{
throw new Exception("Unable to delete account.", ex);
}
}
#endregion
#region User Management
public static List<string> GetReservedUsernames(Config config)
{
List<string> foundNames = new List<string>();
@ -36,7 +127,6 @@ namespace Teknik.Areas.Users.Utility
return foundNames;
}
#region User Management
public static User GetUser(TeknikEntities db, string username)
{
User user = db.Users.Where(b => b.Username == username).FirstOrDefault();
@ -97,20 +187,12 @@ namespace Teknik.Areas.Users.Utility
return isAvailable;
}
public static DateTime GetLastActivity(TeknikEntities db, Config config, User user)
public static DateTime UserLastActive(TeknikEntities db, Config config, User user)
{
try
{
DateTime lastActive = new DateTime(1900, 1, 1);
DateTime emailLastActive = UserEmailLastActive(config, GetUserEmailAddress(config, user.Username));
if (lastActive < emailLastActive)
lastActive = emailLastActive;
DateTime gitLastActive = UserGitLastActive(config, user.Username);
if (lastActive < gitLastActive)
lastActive = gitLastActive;
if (lastActive < user.LastSeen)
lastActive = user.LastSeen;
@ -118,7 +200,7 @@ namespace Teknik.Areas.Users.Utility
}
catch (Exception ex)
{
throw new Exception("Unable to determine last activity.", ex);
throw new Exception("Unable to determine last user activity.", ex);
}
}
@ -126,12 +208,6 @@ namespace Teknik.Areas.Users.Utility
{
try
{
// Create an Email Account
AddUserEmail(config, GetUserEmailAddress(config, user.Username), password);
// Create a Git Account
AddUserGit(config, user.Username, password);
// Add User
user.HashedPassword = SHA384.Hash(user.Username, password);
db.Users.Add(user);
@ -156,12 +232,6 @@ namespace Teknik.Areas.Users.Utility
// Changing Password?
if (changePass)
{
// Change email password
EditUserEmailPassword(config, GetUserEmailAddress(config, user.Username), password);
// Update Git password
EditUserGitPassword(config, user.Username, password);
// Update User password
user.HashedPassword = SHA384.Hash(user.Username, password);
}
@ -178,14 +248,6 @@ namespace Teknik.Areas.Users.Utility
{
try
{
// Delete Email Account
if (UserEmailExists(config, GetUserEmailAddress(config, user.Username)))
DeleteUserEmail(config, GetUserEmailAddress(config, user.Username));
// Delete Git Account
if (UserGitExists(config, user.Username))
DeleteUserGit(config, user.Username);
// Update uploads
List<Upload.Models.Upload> uploads = db.Uploads.Include("User").Where(u => u.User.Username == user.Username).ToList();
if (uploads != null)