public async Task <ActionResult> Edit([Bind(Include = "Id,Email,Password,Roles")] ChangeUserModelView applicationUser) { if (applicationUser.Id == null) { throw new HttpException(400, "Bad Request"); } if (ModelState.IsValid) { IdentityResult result; var user = UserManager.FindById(applicationUser.Id); if (applicationUser.Password != user.PasswordHash) { await UserManager.RemovePasswordAsync(applicationUser.Id); result = await UserManager.AddPasswordAsync(applicationUser.Id, applicationUser.Password); if (!result.Succeeded) { throw new HttpException(400, "Bad Request"); } } if (applicationUser.Email != null) { user.Email = applicationUser.Email; user.UserName = applicationUser.Email; result = await UserManager.UpdateAsync(user); if (!result.Succeeded) { throw new HttpException(400, "Bad Request"); } } foreach (var role in applicationUser.Roles) { if (role.Checked) { await UserManager.AddToRoleAsync(applicationUser.Id, role.RoleName); } else { await UserManager.RemoveFromRolesAsync(applicationUser.Id, role.RoleName); } } TempData["alert"] = "Zmieniłes dane użytkownika!"; return(RedirectToAction("Index")); } ; return(View(applicationUser)); }
// GET: ApplicationUsers/Edit/5 public ActionResult Edit(string id) { if (id == null) { throw new HttpException(400, "Bad Request"); } ApplicationUser applicationUser = db.Users.Find(id); ChangeUserModelView changeUserModelView = new ChangeUserModelView() { Id = applicationUser.Id, Email = applicationUser.Email, Password = applicationUser.PasswordHash }; foreach (var role in db.Roles) { CheckRoleBoxViewModel checkRoleBoxViewModel = new CheckRoleBoxViewModel() { RoleId = role.Id, RoleName = role.Name, Checked = false }; foreach (var roleUser in role.Users) { if (roleUser.UserId == applicationUser.Id) { checkRoleBoxViewModel.Checked = true; } } changeUserModelView.Roles.Add(checkRoleBoxViewModel); } if (applicationUser == null) { return(HttpNotFound()); } return(View(changeUserModelView)); }