public ActionResult DoAddTester(Tester tester) { string password = tester.Password; var validationErrors = GetTesterValidationErrors(tester); if (validationErrors.Count > 0) { return View("InvalidTester", validationErrors); } tester.Password = ""; context.Testers.Add(tester); context.SaveChanges(); WebSecurity.CreateAccount(tester.Username, password); return View("TesterAdded"); }
public ActionResult DoEditTester(Tester tester) { var validationErrors = GetTesterValidationErrors(tester); if (validationErrors.Count > 0) { return View("InvalidTester", validationErrors); } Tester entity = context.Testers.Where(t => t.TesterId == tester.TesterId).FirstOrDefault(); string resetToken = WebSecurity.GeneratePasswordResetToken(entity.Username); WebSecurity.ResetPassword(resetToken, tester.Password); entity.FirstName = tester.FirstName; entity.LastName = tester.LastName; entity.Email = tester.Email; entity.Telephone = tester.Telephone; context.SaveChanges(); return View("TesterEdited"); }
private List<string> GetTesterValidationErrors(Tester tester) { List<string> errors = new List<string>(); if (context.Testers.Any(t => t.Username == tester.Username)) { errors.Add("Tester already exists"); } if (string.IsNullOrEmpty(tester.Username) || string.IsNullOrWhiteSpace(tester.Username)) { errors.Add("The username should not be empty"); } else if (tester.Username.Length < 3) { errors.Add("The usernane should be at least 3 characters"); } else if (!Regex.IsMatch(tester.Username, "[A-Za-z][A-Za-z0-9]*")) { errors.Add("The username should start with a letter and contain only letters and numbers"); } if (string.IsNullOrEmpty(tester.Password) || string.IsNullOrWhiteSpace(tester.Password)) { errors.Add("The password should not be empty"); } else if (tester.Password.Length < 3) { errors.Add("The password should be at least 3 characters"); } if(string.IsNullOrEmpty(tester.Email) || string.IsNullOrWhiteSpace(tester.Email)) { errors.Add("The email should not be empty"); } else if (!Regex.IsMatch(tester.Email, "[A-Za-z][A-Za-z0-9_]+@[A-Za-z0-9][A-Za-z0-9_-]*\\.[A-Za-z]+")) { errors.Add("Invalid email format"); } if (string.IsNullOrEmpty(tester.FirstName) || string.IsNullOrWhiteSpace(tester.FirstName)) { errors.Add("The first name should not be empty"); } if (string.IsNullOrEmpty(tester.LastName) || string.IsNullOrWhiteSpace(tester.LastName)) { errors.Add("The last name should not be empty"); } if (string.IsNullOrEmpty(tester.Telephone) || string.IsNullOrWhiteSpace(tester.Telephone)) { errors.Add("The telephone should not be empty"); } return errors; }