public async Task <IActionResult> AdministrarPermisosUsuario(PermisosUsuarioViewModel model) { var user = await userManager.FindByIdAsync(model.UserId); if (user == null) { ViewBag.ErrorMessage = $"Usuario con Id = {model.UserId} no existe"; return(View("NotFound404", "Error")); } // Get all the user existing claims and delete them var claims = await userManager.GetClaimsAsync(user); var result = await userManager.RemoveClaimsAsync(user, claims); if (!result.Succeeded) { ModelState.AddModelError("", "No se puede eliminar usuario con permisos asignados"); return(View(model)); } // Add all the claims that are selected on the UI result = await userManager.AddClaimsAsync(user, model.Cliams.Where(c => c.IsSelected).Select(c => new Claim(c.ClaimType, c.ClaimType))); if (!result.Succeeded) { ModelState.AddModelError("", "No se puede agregar permisos al usuario"); return(View(model)); } return(RedirectToAction("EditarUsuario", new { Id = model.UserId })); }
public async Task <IActionResult> AdministrarPermisosUsuario(string userId) { var user = await userManager.FindByIdAsync(userId); if (user == null) { ViewBag.ErrorMessage = $"Usuario con Id = {userId} no existe"; return(View("NotFound404", "Error")); } // UserManager service GetClaimsAsync method gets all the current claims of the user var existingUserClaims = await userManager.GetClaimsAsync(user); var model = new PermisosUsuarioViewModel { UserId = userId }; // Loop through each claim we have in our application foreach (Claim claim in ClaimsStore.AllClaims) { UserClaim userClaim = new UserClaim { ClaimType = claim.Type }; // If the user has the claim, set IsSelected property to true, so the checkbox // next to the claim is checked on the UI if (existingUserClaims.Any(c => c.Type == claim.Type)) { userClaim.IsSelected = true; } model.Cliams.Add(userClaim); } return(View(model)); }