public ActionResult Edit(string email) { var member = Membership.GetUser(email); if (member != null) { var user = new UserModel { Email = member.Email, CreationDate = member.CreationDate, LastLoginDate = member.LastLoginDate, }; var profile = UserProfile.GetUserProfile(user.Email) ?? new UserProfile(); user.FirstName = profile.FirstName; user.LastName = profile.LastName; PopulateAssignedRolesData(user); return View(user); } ViewBag.PossibleRoles = Roles.GetAllRoles(); return View(); }
private void PopulateAssignedRolesData(UserModel user) { var allRoles = Roles.GetAllRoles(); var userRoles = new HashSet<string>(Roles.GetRolesForUser(user.Email)); var viewModel = allRoles.Select(role => new AssignedRole { RoleName = role, Assigned = userRoles.Contains(role) }).ToList(); ViewBag.PossibleRoles = viewModel; }
public ActionResult Index() { var members = Membership.GetAllUsers(); var viewModel = new List<UserModel>(); //Could really use automapper for this foreach (var m in members) { var member = m as MembershipUser; var user = new UserModel { Email = member.Email, CreationDate = member.CreationDate, LastLoginDate = member.LastLoginDate, IsApproved = member.IsApproved }; viewModel.Add(user); } return View(viewModel); }
private static void UpdateUserRoles(IEnumerable<string> selectedRoles, UserModel user) { var allRoles = Roles.GetAllRoles(); if (selectedRoles == null) { Roles.RemoveUserFromRoles(user.Email, allRoles); return; } var selectedRolesHs = new HashSet<string>(selectedRoles); var userRoles = new HashSet<string>(Roles.GetRolesForUser(user.Email)); foreach (var role in allRoles) { if (selectedRolesHs.Contains(role)) { if (!userRoles.Contains(role)) { Roles.AddUserToRole(user.Email,role); } } else { if (userRoles.Contains(role)) { Roles.RemoveUserFromRole(user.Email, role); } } } }
public ActionResult Edit(UserModel user,string[] selectedRoles) { if (ModelState.IsValid) { var member = Membership.GetUser(user.Email); //Update User if (member != null) { member.Email = user.Email; Membership.UpdateUser(member); } UpdateUserRoles(selectedRoles, user); //Save profile var profile = UserProfile.GetUserProfile(user.Email); profile.FirstName = user.FirstName; profile.LastName = user.LastName; profile.Save(); this.FlashInfo("Updated user"); return RedirectToAction("Index", "Account"); } // If we got this far, something failed, redisplay form PopulateAssignedRolesData(user); return View(user); }