public async Task <IActionResult> ChangeClaims(string ID) { if (ID == null) { return(RedirectToAction("Admins", "Owner")); } // new full list of claims Claims claims = new Claims(); // get properties for Claims List <PropertyInfo> properties = claims.GetType().GetProperties().ToList(); ApplicationUser userToUpdate = await _userManager.FindByIdAsync(ID); IList <Claim> hasClaims = await _userManager.GetClaimsAsync(userToUpdate); // if claim exist change the value to TRUE foreach (var claim in hasClaims) { foreach (var item in properties) { if (item.Name == claim.Type) { item.SetValue(claims, true); } } } // sort properties alphabetically by Name ViewBag.Properties = properties.OrderBy(x => x.Name); ViewBag.AdminName = $"{userToUpdate.Level}: {userToUpdate.FirstName} {userToUpdate.LastName}"; // user ID ViewBag.AID = userToUpdate.Id; return(View(claims)); }
public async Task <IActionResult> ChangeClaims(Claims claims, string ID = null) { if (claims == null || ID == null) { return(RedirectToAction("Admins", "Owner")); } // get user by ID ApplicationUser userToUpdate = await _userManager.FindByIdAsync(ID); List <PropertyInfo> properties = claims.GetType().GetProperties().ToList(); // remove claims before assigning new ones IList <Claim> hasClaims = await _userManager.GetClaimsAsync(userToUpdate); await _userManager.RemoveClaimsAsync(userToUpdate, hasClaims); // add new claims foreach (var item in properties) { if ((bool)item.GetValue(claims, null) == true) { await _userManager.AddClaimAsync(userToUpdate, new Claim(item.Name, "")); } } // log for editor ApplicationUser editor = await _userManager.GetUserAsync(User); await _db.Logs.AddAsync(await Log.New("Edit User", $"{userToUpdate.Level.ToUpper()}: {userToUpdate.FirstName} {userToUpdate.LastName}, CLAIMS were changed", _id, _db)); // log for edited await _db.Logs.AddAsync(await Log.New("Edit User", $"User's CLAIMS were changed by {editor.FirstName} {editor.LastName}", userToUpdate.Id, _db)); return(RedirectToAction("EditAdmin", "Account", new { ID = ID })); }