public ActionResult Create(User user) { if (ModelState.IsValid) { db.Users.Add(user); db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.RoleID = new SelectList(db.Roles, "RoleID", "RoleName", user.RoleID); return View(user); }
public ActionResult Edit(User user) { if (ModelState.IsValid) { db.Entry(user).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.RoleID = new SelectList(db.Roles, "RoleID", "RoleName", user.RoleID); return View(user); }
public void CreateUser(string username, string name, string password, string email, string roleName) { Role role = GetRole(roleName); if (string.IsNullOrEmpty(username.Trim())) throw new ArgumentException("The user name provided is invalid. Please check the value and try again."); if (string.IsNullOrEmpty(name.Trim())) throw new ArgumentException("The name provided is invalid. Please check the value and try again."); if (string.IsNullOrEmpty(password.Trim())) throw new ArgumentException("The password provided is invalid. Please enter a valid password value."); if (string.IsNullOrEmpty(email.Trim())) throw new ArgumentException("The e-mail address provided is invalid. Please check the value and try again."); if (!RoleExists(role)) throw new ArgumentException("The role selected for this user does not exist! Contact an administrator!"); if (this.context.Users.Any(user => user.UserName == username)) throw new ArgumentException("Username already exists. Please enter a different user name."); User newUser = new User() { UserName = username, Name = name, Password = FormsAuthentication.HashPasswordForStoringInConfigFile(password.Trim(), "md5"), Email = email, RoleID = role.RoleID }; try { AddUser(newUser); } catch (ArgumentException ae) { throw ae; } catch (Exception e) { throw new ArgumentException("The authentication provider returned an error. Please verify your entry and try again. " + "If the problem persists, please contact your system administrator."); } // Immediately persist the user data Save(); }
private void AddUser(User user) { if (UserExists(user)) throw new ArgumentException(TooManyUser); context.Users.Add(user); }
public bool UserExists(User user) { if (user == null) return false; return (context.Users.SingleOrDefault(u => u.UserID == user.UserID || u.UserName == user.UserName) != null); }
public Role GetRoleForUser(User user) { if (!UserExists(user)) throw new ArgumentException(MissingUser); return user.Role; }
public void DeleteUser(User user) { if (!UserExists(user)) throw new ArgumentException(MissingUser); context.Users.Remove(user); }