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 DeleteUser(TeknikEntities db, Config config, User user) { try { // Update uploads List<Upload.Models.Upload> uploads = db.Uploads.Where(u => u.User.Username == user.Username).ToList(); if (uploads != null) { foreach (Upload.Models.Upload upload in uploads) { upload.UserId = null; db.Entry(upload).State = EntityState.Modified; } db.SaveChanges(); } // Update pastes List<Paste.Models.Paste> pastes = db.Pastes.Where(u => u.User.Username == user.Username).ToList(); if (pastes != null) { foreach (Paste.Models.Paste paste in pastes) { paste.UserId = null; db.Entry(paste).State = EntityState.Modified; } db.SaveChanges(); } // Update shortened urls List<ShortenedUrl> shortUrls = db.ShortenedUrls.Where(u => u.User.Username == user.Username).ToList(); if (shortUrls != null) { foreach (ShortenedUrl shortUrl in shortUrls) { shortUrl.UserId = null; db.Entry(shortUrl).State = EntityState.Modified; } db.SaveChanges(); } // Delete Blogs Blog.Models.Blog blog = db.Blogs.Where(u => u.User.Username == user.Username).FirstOrDefault(); if (blog != null) { db.Blogs.Remove(blog); db.SaveChanges(); } // Delete post comments List<BlogPostComment> postComments = db.BlogComments.Where(u => u.User.Username == user.Username).ToList(); if (postComments != null) { foreach (BlogPostComment postComment in postComments) { db.BlogComments.Remove(postComment); } db.SaveChanges(); } // Delete podcast comments List<Podcast.Models.PodcastComment> podComments = db.PodcastComments.Where(u => u.User.Username == user.Username).ToList(); if (podComments != null) { foreach (Podcast.Models.PodcastComment podComment in podComments) { db.PodcastComments.Remove(podComment); } db.SaveChanges(); } // Delete Recovery Email Verifications List<RecoveryEmailVerification> verCodes = db.RecoveryEmailVerifications.Where(r => r.User.Username == user.Username).ToList(); if (verCodes != null) { foreach (RecoveryEmailVerification verCode in verCodes) { db.RecoveryEmailVerifications.Remove(verCode); } db.SaveChanges(); } // Delete Password Reset Verifications List<ResetPasswordVerification> verPass = db.ResetPasswordVerifications.Where(r => r.User.Username == user.Username).ToList(); if (verPass != null) { foreach (ResetPasswordVerification ver in verPass) { db.ResetPasswordVerifications.Remove(ver); } db.SaveChanges(); } // Delete User db.Users.Remove(user); db.SaveChanges(); } catch (Exception ex) { throw new Exception(string.Format("Unable to delete user {0}.", user.Username), ex); } }
public static string CreateResetPasswordVerification(TeknikEntities db, Config config, User user) { // Check to see if there already is a verification code for the user List<ResetPasswordVerification> verCodes = db.ResetPasswordVerifications.Where(r => r.User.Username == user.Username).ToList(); if (verCodes != null && verCodes.Any()) { foreach (ResetPasswordVerification verCode in verCodes) { db.ResetPasswordVerifications.Remove(verCode); } } // Create a new verification code and add it string verifyCode = Helpers.Utility.RandomString(24); ResetPasswordVerification ver = new ResetPasswordVerification(); ver.UserId = user.UserId; ver.Code = verifyCode; ver.DateCreated = DateTime.Now; db.ResetPasswordVerifications.Add(ver); db.SaveChanges(); return verifyCode; }
public static void AddUser(TeknikEntities db, Config config, User user, string password) { try { // Add User user.HashedPassword = GeneratePassword(config, user, password); db.Users.Add(user); db.SaveChanges(); // Generate blog for the user var newBlog = db.Blogs.Create(); newBlog.UserId = user.UserId; db.Blogs.Add(newBlog); db.SaveChanges(); } catch (Exception ex) { throw new Exception("Unable to create user.", ex); } }
public static void EditUser(TeknikEntities db, Config config, User user, bool changePass, string password) { try { // Changing Password? if (changePass) { // Update User password user.HashedPassword = SHA384.Hash(user.Username.ToLower(), password).ToHex(); } db.Entry(user).State = EntityState.Modified; db.SaveChanges(); } catch (Exception ex) { throw new Exception(string.Format("Unable to edit user {0}.", user.Username), ex); } }
public static bool UserPasswordCorrect(TeknikEntities db, Config config, User user, string password) { try { string hash = GeneratePassword(config, user, password); return db.Users.Any(b => b.Username == user.Username && b.HashedPassword == hash); } catch (Exception ex) { throw new Exception("Unable to determine if password is correct.", ex); } }
public static void TransferUser(TeknikEntities db, Config config, User user, string password) { try { List<TransferType> transfers = user.Transfers.ToList(); for (int i = 0; i < transfers.Count; i++) { TransferType transfer = transfers[i]; switch (transfer.Type) { case TransferTypes.Sha256Password: case TransferTypes.CaseSensitivePassword: case TransferTypes.ASCIIPassword: user.HashedPassword = SHA384.Hash(user.Username.ToLower(), password).ToHex(); break; default: break; } user.Transfers.Remove(transfer); } db.Entry(user).State = EntityState.Modified; db.SaveChanges(); } catch (Exception ex) { throw new Exception("Unable to transfer user info.", ex); } }
public static DateTime UserLastActive(TeknikEntities db, Config config, User user) { try { DateTime lastActive = new DateTime(1900, 1, 1); if (lastActive < user.LastSeen) lastActive = user.LastSeen; return lastActive; } catch (Exception ex) { throw new Exception("Unable to determine last user activity.", 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); } }
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 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 string GeneratePassword(Config config, User user, string password) { try { string username = user.Username.ToLower(); if (user.Transfers.ToList().Exists(t => t.Type == TransferTypes.CaseSensitivePassword)) { username = user.Username; } byte[] hashBytes = SHA384.Hash(username, password); string hash = hashBytes.ToHex(); if (user.Transfers.ToList().Exists(t => t.Type == TransferTypes.ASCIIPassword)) { hash = Encoding.ASCII.GetString(hashBytes); } if (user.Transfers.ToList().Exists(t => t.Type == TransferTypes.Sha256Password)) { hash = SHA256.Hash(password, config.Salt1, config.Salt2); } return hash; } catch (Exception ex) { throw new Exception("Unable to generate password.", ex); } }