public async Task <IActionResult> AddUserRoles(SelectUserRolesViewModel userRole) { if (ModelState.IsValid) { List <string> selectedList = new List <string>(); userRole.Roles.FindAll(p => p.Selected).ForEach(p => selectedList.Add(p.RoleName)); foreach (var item in selectedList) { ApplicationUser user = await UserManager.FindByIdAsync(userRole.Id); //如这个用户不在这个角色里 则修改 if (!await UserManager.IsInRoleAsync(user, item)) { IdentityResult result = await UserManager.AddToRoleAsync(user, item); if (!result.Succeeded) { AddErrors(result); } } } } return(RedirectToAction("RegisteredUsers", "Account")); }
public ActionResult UserRoles(SelectUserRolesViewModel model) { if (ModelState.IsValid) { var idManager = new IdentityManager(); var Db = new ApplicationDbContext(); var user = Db.Users.First(u => u.Email == model.Email); idManager.ClearUserRoles(user.Id); foreach (var role in model.Roles) { if (role.Selected) { idManager.AddUserToRole(user.Id, role.RoleName); } } return(RedirectToAction("index")); } return(View()); }
public ActionResult UserRoles(SelectUserRolesViewModel model) { if (ModelState.IsValid) { var idManager = new IdentityManager(); var Db = new ApplicationDbContext(); var user = Db.Users.First(u => u.UserName == model.UserName); //idManager.ClearUserRoles(user.Id); foreach (var role in model.Roles) { if (role.Selected) { idManager.AddUserToRole(user.Id, role.RoleName); } else { idManager.ClearUserRoles(user.Id, role.RoleName); } } return(RedirectToAction("Index", "Account", new { errorCode = 7, })); } return(View()); }
public ActionResult UserRoles(string id) { var user = UserService.FindAsync(u => u.UserName == id).Result; var model = new SelectUserRolesViewModel(user); return(View(model)); }
public ActionResult UserRoles(string id, string name = null) { var user = UserManager.FindById(id); var model = new SelectUserRolesViewModel(user); return(View(model)); }
public ActionResult UserRoles(string id) { var user = _context.Users.First(u => u.Id == id); var model = new SelectUserRolesViewModel(user, _context); return(View(model)); }
public IHttpActionResult UserRoles(SelectUserRolesViewModel eu) { var Db = new ApplicationDbContext(); var user = Db.Users.First(u => u.UserName == eu.UserName); var model = new SelectUserRolesViewModel(user); return(Ok(model)); }
public ActionResult UserRoles(string id) { var Db = new ApplicationDbContext(); var user = Db.Users.First(u => u.UserName == id); var model = new SelectUserRolesViewModel(user); return(View(model)); }
public ActionResult UserRoles(string id) { var Db = new UniversityofLouisvilleVaccine.DataContexts.ApplicationDbContext(); var user = Db.Users.First(u => u.UserName == id); var model = new SelectUserRolesViewModel(user); return(View(model)); }
public ActionResult UserRoles(string id) { var Db = DatabaseFactory.Get(); var user = Db.Users.First(u => u.Id == id); var model = new SelectUserRolesViewModel(user, DatabaseFactory); return(View(model)); }
public IHttpActionResult DeleteConfirmed(SelectUserRolesViewModel eu) { var Db = new ApplicationDbContext(); var user = Db.Users.First(u => u.UserName == eu.UserName); Db.Users.Remove(user); Db.SaveChanges(); return(Ok()); }
public ActionResult UserRoles(SelectUserRolesViewModel model) { if (ModelState.IsValid) { var idManager = new IdentityManager(); var user = UserService.FindAsync(u => u.UserName == model.UserName).Result; idManager.ClearUserRoles(user.Id); foreach (var role in model.Roles) { IdentityUserRole IdentityUserRole = new IdentityUserRole(); IdentityRole loadedRole = identityRoleService.Get(r => r.Name == role.RoleName); IdentityUserRole.RoleId = loadedRole.Id; IdentityUserRole.UserId = user.Id; List <IdentityUserRole> iUserRoles = IdentityUserRoleService.FindAllAsync(ur => ur.UserId == IdentityUserRole.UserId).Result; bool exist = false; if (role.Selected) { foreach (var IdentityRoles in iUserRoles) { if (IdentityRoles.RoleId == loadedRole.Id) { exist = true; } } if (!exist) { IdentityUserRoleService.Add(IdentityUserRole); IdentityUserRoleService.Commit(); } } else { try { foreach (var IdentityRoles in iUserRoles) { if (IdentityRoles.RoleId == IdentityUserRole.RoleId && IdentityRoles.UserId == IdentityUserRole.UserId) { IdentityUserRoleService.Delete(IdentityRoles); IdentityUserRoleService.Commit(); } } } catch { throw; } } } return(RedirectToAction("index")); } return(View()); }
public ActionResult UserRoles(string id) { if (id != null) { var db = new ApplicationDbContext(); var user = db.Users.FirstOrDefault(u => u.UserName == id); var model = new SelectUserRolesViewModel(user); return(View(model)); } return(View("Users")); }
public IHttpActionResult Delete(SelectUserRolesViewModel eu) { var Db = new ApplicationDbContext(); var user = Db.Users.First(u => u.UserName == eu.UserName); var model = new EditUserViewModel(user); if (user == null) { return(Ok("HttpNotFound")); } return(Ok(model)); }
public ActionResult EditUserRoles(string id) { if (string.IsNullOrEmpty(id)) { id = User.Identity.GetUserName(); } var Db = new ApplicationDbContext(); var user = Db.Users.First(u => u.UserName == id); var model = new SelectUserRolesViewModel(user); return(View(model)); }
//[Authorize(Roles = "Admin")] public ActionResult Index() { var users = _db.Users; var model = new List <SelectUserRolesViewModel>(); foreach (var user in users) { var u = new SelectUserRolesViewModel(user); u.Id = user.Id; model.Add(u); } return(View(model)); }
public async Task <ActionResult> ManagementForRole(ApplicationUser model) { var user = await UserManager.FindByIdAsync(model.Id); if (user == null) { return(RedirectToAction("Management")); } SelectUserRolesViewModel viewModel = new SelectUserRolesViewModel(user, RoleManager.Roles); viewModel.Roles.RemoveAll(m => m.RoleName == "su"); return(View(viewModel)); }
public ActionResult UserRoles(SelectUserRolesViewModel model) { if (ModelState.IsValid) { var user = UserManager.FindById(model.UserId); var currentUserRoleNames = user.Roles.ToArray(); UserManager.RemoveFromRoles(user.Id, currentUserRoleNames); UserManager.AddToRoles(user.Id, model.Roles.Where(mr => mr.Selected).Select(mr => mr.RoleName).ToArray()); return(RedirectToAction("Manage")); } return(View(model)); }
public async Task <ActionResult> UserRoles(SelectUserRolesViewModel model) { if (ModelState.IsValid) { IdentityResult result; foreach (SelectRoleEditorViewModel role in model.Roles) { // Only administrators can provide administrator access bool isAdminRestricted = !User.IsInRole("Administrator") && role.RoleName == "Administrator" ? true : false; bool isUserInRole = await UserManager.IsInRoleAsync(model.UserId, role.RoleName); // If role is deselected & user is assigned to this role - remove role from user if (isAdminRestricted) { // TODO: provide error reporting - must be an administrator to modify administrator accounts } else if (!role.Selected && isUserInRole) { result = await UserManager.RemoveFromRoleAsync(model.UserId, role.RoleName); if (!result.Succeeded) { // TODO: provide error reporting } } else if (role.Selected && !isUserInRole) { result = await UserManager.AddToRoleAsync(model.UserId, role.RoleName); if (!result.Succeeded) { // TODO: provide error reporting } } else { // TODO: provide error reporting } } // Display user account index view return(RedirectToAction("index")); } // Re-display current user roles view return(View()); }
public ActionResult UserRoles(SelectUserRolesViewModel model) { if (ModelState.IsValid) { var idManager = new IdentityManager(); var user = UserService.FindAsync(u => u.UserName == model.UserName).Result; idManager.ClearUserRoles(user.Id); foreach (var role in model.Roles) { if (role.Selected) { idManager.AddUserToRole(user.Id, role.RoleName); } } return(RedirectToAction("index")); } return(View()); }
public async Task <ActionResult> UserRoles(SelectUserRolesViewModel model) { if (!ModelState.IsValid) { return(View()); } var userBeingModified = await _userManager.FindByNameAsync(model.UserName); foreach (var role in model.Roles) { // Only administrators can provide administrator access if (!User.IsInRole("Administrator") && role.RoleName == "Administrator") { continue; } bool userIsInRole = await _userManager.IsInRoleAsync(userBeingModified, role.RoleName); if ((role.Selected && userIsInRole) || (!role.Selected && !userIsInRole)) // then we don't care { continue; } var result = new IdentityResult(); if (role.Selected && !userIsInRole) { result = await _userManager.AddToRoleAsync(userBeingModified, role.RoleName); } if (!role.Selected && userIsInRole) { result = await _userManager.RemoveFromRoleAsync(userBeingModified, role.RoleName); } if (!result.Succeeded) { throw new Exception("AccountController, UserRoles method, `result` failed: " + result.Errors); } } return(RedirectToAction("Index")); }
public async Task <ActionResult> AgentForRole(AgentUser model) { ViewData["ReturnUrl"] = Utils.GetUrlReferrer(); var userid = User.Identity.GetUserId <int>(); if (!AgentUserBLL.CheckUser(userid, model.Id)) { return(RedirectToAction("AgentList")); } var user = await UserManager.FindByIdAsync(model.Id); if (user == null) { return(RedirectToAction("AgentList")); } var viewModel = new SelectUserRolesViewModel(user, RoleManager.Roles); return(View(viewModel)); }
public ActionResult Index(string searchString1) { var users = from x in db.Users select x; if (!String.IsNullOrEmpty(searchString1)) { users = users.Where(x => x.Email.Contains(searchString1)); } var model = new List <SelectUserRolesViewModel>(); foreach (var user in users) { var u = new SelectUserRolesViewModel(user); u.Id = user.Id; model.Add(u); } return(View(model)); }
public IHttpActionResult UserRolesConfirmed(SelectUserRolesViewModel model) { var idManager = new IdentityManager(); var Db = new ApplicationDbContext(); var user = Db.Users.First(u => u.UserName == model.UserName); //idManager.ClearUserRoles(user.Id); foreach (var role in model.Roles) { if (role.Selected) { idManager.AddUserToRole(user.Id, role.RoleName); } else { idManager.ClearUserRoles(user.Id, role.RoleName); } } return(Ok()); }
public ActionResult UserRoles(SelectUserRolesViewModel model) { var Db = new ApplicationDbContext(); var idManager = new IdentityManager(Db); var user = Db.Users.First(u => u.UserName == model.UserName); if (User.Identity.Name == user.UserName) { ViewBag.Error = "You cannot edit your own role assignments"; return(RedirectToAction("UserRoles", model)); } idManager.ClearUserRoles(user.Id); foreach (var role in model.Roles) { if (role.Selected) { idManager.AddUserToRole(user.Id, role.RoleName); } } Db.SaveChanges(); return(RedirectToAction("IndexUser")); }
public async Task <ActionResult> UserRoles(SelectUserRolesViewModel model) { if (!ModelState.IsValid) { return(View()); } foreach (var role in model.Roles) { // Only administrators can provide administrator access var user = await UserManager.FindByNameAsync(model.UserName); bool isAdminRestricted = !User.IsInRole("Administrator") && role.RoleName == "Administrator" ? true : false; bool isUserInRole = await UserManager.IsInRoleAsync(user, role.RoleName); // If role is deselected & user is assigned to this role - remove role from user // TODO: provide error reporting - must be an administrator to modify administrator accounts if (!isAdminRestricted) { var result = new IdentityResult(); if (!role.Selected && isUserInRole) { result = await UserManager.RemoveFromRoleAsync(user, role.RoleName); } else if (role.Selected && !isUserInRole) { result = await UserManager.AddToRoleAsync(user, role.RoleName); } if (!result.Succeeded) { throw new Exception("AccountController, UserRoles method, `result` failed: " + result.Errors); } } } // Display user account index view return(RedirectToAction("index")); // Re-display current user roles view }
public virtual ActionResult UserRoles(SelectUserRolesViewModel model) { if (ModelState.IsValid) { var idManager = new IdentityManager(); var Db = new ApplicationDbContext(); var user = Db.Users.First(u => u.UserName == model.UserName); idManager.ClearUserRoles(user.Id); foreach (var role in model.Roles) { if (role.Selected) { idManager.AddUserToRole(user.Id, role.RoleName); } } return RedirectToAction("index"); } return View(); }
public virtual ActionResult UserRoles(int id) { var Db = new ApplicationDbContext(); var user = Db.Users.First(u => u.Id == id); var model = new SelectUserRolesViewModel(user); return View(model); }
public async Task <ActionResult> ManagementForRole(Dictionary <string, string> queryvalues, SelectUserRolesViewModel model) { if (ModelState.IsValid) { var user = await UserManager.FindByNameAsync(model.UserName); IdentityResult res; foreach (var role in model.Roles) { if (role.Selected) { if (await UserManager.IsInRoleAsync(user.Id, role.RoleName)) { await UserManager.RemoveFromRoleAsync(user.Id, role.RoleName); } res = await UserManager.AddToRoleAsync(user.Id, role.RoleName); if (!res.Succeeded) { return(View("Error", res.Errors)); } } else { if (await UserManager.IsInRoleAsync(user.Id, role.RoleName)) { res = await UserManager.RemoveFromRoleAsync(user.Id, role.RoleName); if (!res.Succeeded) { return(View("Error", res.Errors)); } } } } return(RedirectToAction("Management")); } return(View(model)); }