public ActionResult Create(User user) { try { if (Session["UserAccountID"] == null) return RedirectToAction("Validate", "Login"); User currentuser = (User)Session["User"]; ViewData["LoginInfo"] = Utility.BuildUserAccountString(user.Username, Convert.ToString(Session["UserAccountName"])); if (currentuser.IsAdmin) ViewData["txtIsAdmin"] = "true"; else throw new Exception("You are not authorized to access this page."); if (ModelState.IsValid) { // Set NULLs to Empty Strings user = FillNulls(user); user.AccountID = Convert.ToInt32(Request.Form["lstAllAccounts"]); string confirmpassword = Convert.ToString(Request.Form["txtConfirmPassword"]); string validation = ValidateInput(user, confirmpassword, false, true); if (!String.IsNullOrEmpty(validation)) { ViewData["AccountList"] = new SelectList(BuildAccountList(Convert.ToInt32(Request.Form["lstAllAccounts"])), "Value", "Text", Request.Form["lstAllAccounts"]); ViewData["ValidationMessage"] = validation; return View(user); } else { repository.CreateUser(user); CommonMethods.CreateActivityLog((User)Session["User"], "User", "Add", "Added user '" + user.Username + "' - ID: " + user.UserID.ToString()); return RedirectToAction("Index"); } } return View(user); } catch (Exception ex) { Helpers.SetupApplicationError("User", "Create POST", ex.Message); return RedirectToAction("Index", "ApplicationError"); } }
private string ValidateInput(User user, string confirmpassword, bool isEdit, bool passwordchanged) { if (user.AccountID == 0) return "Account ID is not valid."; if (String.IsNullOrEmpty(user.Username)) return "Username is required."; if (user.Username.Length < 6) return "Username must be at least 6 characters."; if (!isEdit) { IUserRepository urep = new EntityUserRepository(); User usercheck = urep.GetUserByUsername(user.Username); if (usercheck != null) return "This username already exists."; } if (String.IsNullOrEmpty(user.Password)) return "Password is required."; if (!isEdit || (isEdit && passwordchanged)) { if (user.Password != confirmpassword) return "Passwords do not match."; if (user.Password.Length < 6) return "Password must be at least 6 characters."; } if (String.IsNullOrEmpty(user.FirstName) || String.IsNullOrEmpty(user.LastName)) return "First Name and Last Name are required."; if (String.IsNullOrEmpty(user.EmailAddress)) return "Email address is required."; Regex regex = new Regex(@"^[a-z0-9,!#\$%&'\*\+/=\?\^_`\{\|}~-]+(\.[a-z0-9,!#\$%&'\*\+/=\?\^_`\{\|}~-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,})$"); if (!regex.IsMatch(user.EmailAddress)) return "Email address is invalid."; return String.Empty; }
private User FillNulls(User user) { if (user.EmailAddress == null) user.EmailAddress = String.Empty; return user; }
private User CreateNewUser() { User user = new User(); user.UserID = 0; user.AccountID = 0; user.Username = String.Empty; user.Password = String.Empty; user.FirstName = String.Empty; user.LastName = String.Empty; user.EmailAddress = String.Empty; user.IsActive = true; return user; }
public void CreateUser(User user) { db.Users.Add(user); db.SaveChanges(); }
public void UpdateUser(User user) { db.Entry(user).State = EntityState.Modified; db.SaveChanges(); }