public async Task <FormUserViewModel> CheckUserParticipationAsync(int id) { var fu = await _db.FormUsers.Where(a => a.QFormID.Equals(id)).ToListAsync(); var u = await _db.Users.Where(c => c.UserName.Equals(HttpContext.Current.User.Identity.Name)).FirstOrDefaultAsync(); FormUserViewModel vm = new FormUserViewModel(); vm.Fu = fu; vm.au = u; return(vm); }
public IActionResult New() { ViewData["userNameAttributes"] = new { @class = "form-control", autocomplete = "off" }; var appRoles = _roleManager.Roles; var model = new FormUserViewModel { RolesListItems = RolesList(appRoles, null) }; return(View("FormUsers", model)); }
public async Task <IActionResult> Edit(string id) { var user = await _userManager.FindByIdAsync(id); if (user == null) { return(NotFound()); } var userRoles = await _userManager.GetRolesAsync(user); var appRoles = _roleManager.Roles; var model = new FormUserViewModel { Id = user.Id, UserName = user.UserName, RolesListItems = RolesList(appRoles, userRoles) }; return(View("FormUsers", model)); }
public async Task <IActionResult> Save(FormUserViewModel model) { var appRoles = _roleManager.Roles; if (string.IsNullOrEmpty(model.Id)) { ViewData["userNameAttributes"] = new { @class = "form-control", autocomplete = "off" }; if (string.IsNullOrEmpty(model.Password)) { ModelState.AddModelError("Password", "A senha é obrigatória."); } if (string.IsNullOrEmpty(model.PasswordConfirmation)) { ModelState.AddModelError("PasswordConfirmation", "A confirmação é obrigatória."); } if (!string.IsNullOrEmpty(model.Password) && !string.IsNullOrEmpty(model.PasswordConfirmation) && model.Password != model.PasswordConfirmation) { ModelState.AddModelError("PasswordConfirmation", "A confirmação de senha não confere."); } var existingUser = await _userManager.FindByNameAsync(model.UserName); if (existingUser != null) { ModelState.AddModelError("UserName", "O usuário informado já possui cadastro."); } if (!ModelState.IsValid) { model.RolesListItems = RolesList(appRoles, null); return(View("FormUsers", model)); } var user = new IdentityUser(model.UserName); var status = await _userManager.CreateAsync(user, model.Password); if (status.Succeeded) { foreach (var role in model.SelectedRoles) { await _userManager.AddToRoleAsync(user, role); } } else { foreach (var error in status.Errors) { ModelState.AddModelError("", error.Description); model.RolesListItems = RolesList(appRoles, await _userManager.GetRolesAsync(user)); return(View("FormUsers", model)); } } } else { var user = await _userManager.FindByIdAsync(model.Id); if (!ModelState.IsValid) { model.RolesListItems = RolesList(appRoles, await _userManager.GetRolesAsync(user)); return(View("FormUsers", model)); } await _userManager.RemoveFromRolesAsync(user, appRoles.Select(s => s.Name)); foreach (var roleName in model.SelectedRoles) { await _userManager.AddToRoleAsync(user, roleName); } } return(RedirectToAction("Index")); }