public async Task<UserViewModel> GetUserByNameAsync(string name) { var user = await _users.GetUserByNameAsync(name); if (user == null) { return null; } var viewModel = new UserViewModel { UserName = user.UserName, Email = user.Email, DisplayName = user.DisplayName }; var userRoles = await _users.GetRolesForUserAsync(user); viewModel.SelectedRole = userRoles.Count() > 1 ? userRoles.FirstOrDefault() : userRoles.SingleOrDefault(); viewModel.LoadUserRoles(await _roles.GetAllRolesAsync()); return viewModel; }
public async Task<bool> CreateAsync(UserViewModel model) { if (!_modelState.IsValid) { return false; } var existingUser = await _users.GetUserByNameAsync(model.UserName); if (existingUser != null) { _modelState.AddModelError(string.Empty, "The user already exists!"); return false; } if (string.IsNullOrWhiteSpace(model.NewPassword)) { _modelState.AddModelError(string.Empty, "You must type a password."); return false; } var newUser = new CmsUser { DisplayName = model.DisplayName, Email = model.Email, UserName = model.UserName }; await _users.CreateAsync(newUser, model.NewPassword); await _users.AddUserToRoleAsync(newUser, model.SelectedRole); return true; }
public async Task<ActionResult> Create() { var model = new UserViewModel(); model.LoadUserRoles(await _roleRepository.GetAllRolesAsync()); return View(model); }
public ActionResult Edit(UserViewModel model) { using (var userStore = new CmsUserStore()) using (var userManager = new CmsUserManager(userStore)) { var user = userStore.FindByNameAsync(model.UserName).Result; if (user == null) { return HttpNotFound(); } if (!ModelState.IsValid) { return View(model); } if (!string.IsNullOrWhiteSpace(model.NewPassword)) { if (string.IsNullOrWhiteSpace(model.CurrentPassword)) { ModelState.AddModelError(string.Empty, "The current password must be supplied"); return View(model); } var passwordVerified = userManager.PasswordHasher.VerifyHashedPassword(user.PasswordHash, model.CurrentPassword); if (passwordVerified != PasswordVerificationResult.Success) { ModelState.AddModelError(string.Empty, "The current password does match our records."); return View(model); } var newHashedPassword = userManager.PasswordHasher.HashPassword(model.NewPassword); user.PasswordHash = newHashedPassword; } user.Email = model.Email; user.DisplayName = model.DisplayName; var updateResult = userManager.UpdateAsync(user).Result; if (updateResult.Succeeded) { return RedirectToAction("index"); } else { ModelState.AddModelError(string.Empty, "An error occurred. Please try again."); return View(model); } } }
public async Task<ActionResult> Create(UserViewModel model) { var completed = await _users.CreateAsync(model); if (completed) { return RedirectToAction("index"); } return View(model); }
public async Task<ActionResult> Edit(UserViewModel model) { var userUpdated = await _users.UpdateUser(model); if (userUpdated) { return RedirectToAction("index"); } return View(model); }
public async Task<ActionResult> Create(UserViewModel model) { if (await _users.Create(model)) { return RedirectToAction("index"); } else { return View(model); } }
public async Task<UserViewModel> GetUserByNameAsync(string name) { var user = await _users.GetUserByNameAsync(name); if (user == null) { return null; } var viewModel = new UserViewModel { UserName = user.UserName, Email = user.Email, DisplayName = user.DisplayName }; return viewModel; }
public async Task<bool> UpdateUser(UserViewModel model) { var user = await _users.GetUserByNameAsync(model.UserName); if (user == null) { _modelState.AddModelError(string.Empty, "The specified user does not exist."); return false; } if (!_modelState.IsValid) { return false; } if (!string.IsNullOrWhiteSpace(model.NewPassword)) { if (string.IsNullOrWhiteSpace(model.CurrentPassword)) { _modelState.AddModelError(string.Empty, "The current password must be supplied"); return false; } var passwordVerified = _users.VerifyUserPassword(user.PasswordHash, model.CurrentPassword); if (!passwordVerified) { _modelState.AddModelError(string.Empty, "The current password does match our records."); return false; } var newHashedPassword = _users.HashPassword(model.NewPassword); user.PasswordHash = newHashedPassword; } user.Email = model.Email; user.DisplayName = model.DisplayName; await _users.UpdateAsync(user); return true; }
public ActionResult Edit(string username) { using (var userStore = new CmsUserStore()) using (var userManager = new CmsUserManager(userStore)) { var user = userStore.FindByNameAsync(username).Result; if (user == null) { return HttpNotFound(); } var viewModel = new UserViewModel { UserName = user.UserName, Email = user.Email }; return View(viewModel); } }
public async Task<ActionResult> Register(UserViewModel model) { var user = new CmsUser { UserName = model.Email, Email = model.Email }; var completed = await _users.CreateAsync(model); if (completed) { //await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false); //created user //log them in //var authManager = HttpContext.Current.GetOwinContext().Authentication; //var userIdentity - CmsUserManager.CreateIdentity() // authManager.SignIn(userIdentity); //Response.Redirect(""); return RedirectToAction("index", "admin"); } return View(model); }
public async Task<bool> Create(UserViewModel model) { if (!_modelState.IsValid) { return false; } if (string.IsNullOrWhiteSpace(model.NewPassword)) { _modelState.AddModelError(string.Empty, "You must type a password."); return false; } var newUser = new CmsUser { DisplayName = model.DisplayName, Email = model.Email, UserName = model.UserName }; await _users.CreateAsync(newUser, model.NewPassword); return true; }
public async Task<ActionResult> Edit(UserViewModel model, string username) { var currentUser = User.Identity.Name; var isAdmin = User.IsInRole("admin"); if (!isAdmin && !string.Equals(currentUser, username, StringComparison.CurrentCultureIgnoreCase)) { return new HttpUnauthorizedResult(); } var userUpdated = await _users.UpdateUser(model); if (userUpdated) { if (isAdmin) { return RedirectToAction("index"); } return RedirectToAction("index", "admin"); } return View(model); }
public async Task<bool> UpdateUser(UserViewModel model) { var user = await _users.GetUserByNameAsync(model.UserName); if (user == null) { _modelState.AddModelError(string.Empty, "The specified user does not exist."); return false; } if (!_modelState.IsValid) { return false; } if (!string.IsNullOrWhiteSpace(model.NewPassword)) { if (string.IsNullOrWhiteSpace(model.CurrentPassword)) { _modelState.AddModelError(string.Empty, "The current password must be supplied"); return false; } var passwordVerified = _users.VerifyUserPassword(user.PasswordHash, model.CurrentPassword); if (!passwordVerified) { _modelState.AddModelError(string.Empty, "The current password does match our records."); return false; } var newHashedPassword = _users.HashPassword(model.NewPassword); user.PasswordHash = newHashedPassword; } user.Email = model.Email; user.DisplayName = model.DisplayName; user.FirstName = model.FirstName; user.LastName = model.LastName; user.CompanyName = model.CompanyName; user.CompanyAddress = model.CompanyAddress; await _users.UpdateAsync(user); var roles = await _users.GetRolesForUserAsync(user); await _users.RemoveUserFromRoleAsync(user, roles.ToArray()); await _users.AddUserToRoleAsync(user, model.SelectedRole); return true; }
public async Task<ActionResult> Create() { var model = new UserViewModel(); return View(model); }