public async Task <ActionResult> ManageClaim(string id) { RoleClaimsViewModel viewModel = new RoleClaimsViewModel(); if (!String.IsNullOrEmpty(id)) { IdentityRole identityRole = await roleManager.FindByIdAsync(id); if (identityRole != null) { //Get claims associated with the role IList <Claim> roleClaimList = await roleManager.GetClaimsAsync(identityRole); List <string> roleClaimTypeList = new List <string>(); foreach (var roleClaim in roleClaimList) { roleClaimTypeList.Add(roleClaim.Type); } viewModel.Id = identityRole.Id; viewModel.RoleName = identityRole.Name; viewModel.RoleClaims = new List <ClaimsViewModel>(); foreach (var claimName in ClaimNames.ClaimName) { viewModel.RoleClaims.Add(new ClaimsViewModel() { ClaimName = claimName, HasClaim = roleClaimTypeList.Contains(claimName) }); } return(View("ManageClaim", viewModel)); } } return(RedirectToAction("Index")); }
public async Task <IActionResult> ManageRoleClaims(RoleClaimsViewModel model) { var role = await roleManager.FindByIdAsync(model.RoleId); if (role == null) { ViewBag.ErrorMessage = $"Role with Id = {model.RoleId} cannot be found"; return(View("NotFound")); } var claims = await roleManager.GetClaimsAsync(role); foreach (Claim c in claims) { var result = await roleManager.RemoveClaimAsync(role, c); if (!result.Succeeded) { ModelState.AddModelError("", "Cannot remove user existing claims"); return(View(model)); } } foreach (RoleClaim rc in model.Cliams) { var result = await roleManager.AddClaimAsync(role, new Claim(rc.ClaimType, rc.IsSelected ? "true" : "false")); if (!result.Succeeded) { ModelState.AddModelError("", "Cannot add selected claims to user"); return(View(model)); } } return(RedirectToAction("EditRole", new { Id = model.RoleId })); }
public async Task <ActionResult> Edit([Bind(Include = "RoleId,RoleName,RoleDescription,Claims")] RoleClaimsViewModel model) { if (ModelState.IsValid) { ApplicationRole role = await RoleManager.FindByIdAsync(model.RoleId); if (role == null) { return(HttpNotFound()); } // remove old claims in this role role.Claims.Clear(); // edit role props role.Name = model.RoleName; role.Description = model.RoleDescription; DbContext.Entry(role).State = EntityState.Modified; // add role claims IEnumerable <string> claimIds = model.Claims.Where(x => x.Status == true).Select(x => x.ClaimId); RoleManager.AddClaims(role.Id, claimIds); DbContext.SaveChanges(); return(RedirectToAction("Index")); } return(View(model)); }
public async System.Threading.Tasks.Task <IHttpActionResult> EditClaimsAsync(string id) { var role = await _roleManager.FindByIdAsync(id); var claimGroups = _claimedActionsProvider.GetClaimGroups(); var assignedClaims = await _roleManager.GetClaimsAsync(role.Name); var result = new RoleClaimsViewModel() { RoleId = role.Id, RoleName = role.Name, }; foreach (var claimGroup in claimGroups) { var claimGroupModel = new RoleClaimsViewModel.ClaimGroup() { GroupId = claimGroup.GroupId, GroupName = claimGroup.GroupName, GroupClaimsCheckboxes = claimGroup.Claims .Select(c => new SelectListViewModel() { Value = String.Format("{0}#{1}", claimGroup.GroupId, c), Text = c, Selected = assignedClaims.Any(ac => ac.Type == claimGroup.GroupId.ToString() && ac.Value == c) }).ToList() }; result.ClaimGroups.Add(claimGroupModel); } return(CCOk(result)); }
public async Task <ActionResult> ManageClaim(string id, RoleClaimsViewModel viewModel) { if (ModelState.IsValid) { if (!String.IsNullOrEmpty(id)) { IdentityRole identityRole = await roleManager.FindByIdAsync(id); if (identityRole != null) { //Get claims associated with the role IList <Claim> roleClaimList = await roleManager.GetClaimsAsync(identityRole); //Extract the claim type List <string> roleClaimTypeList = new List <string>(); foreach (var roleClaim in roleClaimList) { roleClaimTypeList.Add(roleClaim.Type); } foreach (var roleClaim in viewModel.RoleClaims) { //create a new claim with the claim name Claim claim = new Claim(roleClaim.ClaimName, ""); //get the associated claim from the role's claim list Claim associatedClaim = roleClaimList.Where(x => x.Type == roleClaim.ClaimName).FirstOrDefault(); if (roleClaim.HasClaim && !roleClaimTypeList.Contains(roleClaim.ClaimName)) { IdentityResult claimResult = await roleManager.AddClaimAsync(identityRole, claim); if (!claimResult.Succeeded) { //TODO log details and display some sort of error } } else if (!roleClaim.HasClaim && roleClaimTypeList.Contains(roleClaim.ClaimName)) { IdentityResult claimResult = await roleManager.RemoveClaimAsync(identityRole, associatedClaim); if (!claimResult.Succeeded) { //TODO log details and display some sort of error } } } } return(RedirectToAction("Index")); } } return(View(viewModel)); }
public async Task <ActionResult> ManageClaim(string id, RoleClaimsViewModel viewModel) { if (ModelState.IsValid) { if (!String.IsNullOrEmpty(id)) { IdentityRole identityRole = await _roleManager.FindByIdAsync(id); if (identityRole != null) { //Get claims associated with the role IList <Claim> roleClaimList = await _roleManager.GetClaimsAsync(identityRole); //Extract the claim type List <string> roleClaimTypeList = new List <string>(); foreach (var roleClaim in roleClaimList) { roleClaimTypeList.Add(roleClaim.Type); } foreach (var roleClaim in viewModel.RoleClaims) { //create a new claim with the claim name Claim claim = new Claim(roleClaim.ClaimName, ""); //get the associated claim from the role's claim list Claim associatedClaim = roleClaimList.Where(x => x.Type == roleClaim.ClaimName).FirstOrDefault(); if (roleClaim.HasClaim && !roleClaimTypeList.Contains(roleClaim.ClaimName)) { IdentityResult claimResult = await _roleManager.AddClaimAsync(identityRole, claim); if (!claimResult.Succeeded) { _logger.LogError(LoggingEvents.UserConfiguration, LoggingErrorText.addClaimFailed, roleClaim.ClaimName, identityRole, _userManager.GetUserName(User), GetDataErrors.GetErrors(claimResult)); } } else if (!roleClaim.HasClaim && roleClaimTypeList.Contains(roleClaim.ClaimName)) { IdentityResult claimResult = await _roleManager.RemoveClaimAsync(identityRole, associatedClaim); if (!claimResult.Succeeded) { _logger.LogError(LoggingEvents.UserConfiguration, LoggingErrorText.removeClaimFailed, roleClaim.ClaimName, identityRole, _userManager.GetUserName(User), GetDataErrors.GetErrors(claimResult)); } } } } return(RedirectToAction("Index")); } } return(View(viewModel)); }
public ActionResult Index() { List <RoleClaimsViewModel> roleClaimsList = new List <RoleClaimsViewModel>(); foreach (var applicationRole in RoleManager.Roles.ToList()) { RoleClaimsViewModel roleClaims = this.GetRoleClaimsViewModel(applicationRole); roleClaimsList.Add(roleClaims); } return(View(roleClaimsList)); }
public async Task <IHttpActionResult> Put([FromBody] RoleClaimsViewModel viewModel) { var role = await _roleManager.FindByIdAsync(viewModel.RoleId); var roleClaims = await _roleManager.GetClaimsAsync(role.Name); //TODO: Metodo feo, buscar la forma eficiente de comparar listas foreach (var removedClaim in roleClaims) { await _roleManager.RemoveClaimAsync(role.Id, removedClaim); } var submittedClaims = viewModel .SelectedClaims .Select(s => { var tokens = s.Split('#'); if (tokens.Count() != 2) { throw new Exception(String.Format("Claim {0} se encuentra en diferente formato", s)); } return(new Claim(tokens[0], tokens[1])); }).ToList(); roleClaims = await _roleManager.GetClaimsAsync(role.Name); foreach (var submittedClaim in submittedClaims) { var hasClaim = roleClaims.Any(c => c.Value == submittedClaim.Value && c.Type == submittedClaim.Type); if (!hasClaim) { await _roleManager.AddClaimAsync(role.Id, submittedClaim); } } roleClaims = await _roleManager.GetClaimsAsync(role.Name); //TODO: LOGOUT al usuario cuando se cambien los permisos. return(Created(Request.RequestUri + "/" + role.Id, viewModel)); //return await // Task.FromResult( // Created( // Request, // Url.Link("getClaims", new{controller = "Claims",id = role.Id}) // ) // ); }
public async System.Threading.Tasks.Task <IHttpActionResult> EditClaimsAsync([FromBody] RoleClaimsViewModel viewModel) { var role = await _roleManager.FindByIdAsync(viewModel.RoleId); role.Name = viewModel.RoleName; var roleResult = await _roleManager.UpdateAsync(role); var roleClaims = await _roleManager.GetClaimsAsync(role.Name); // this is ugly. Deletes all the claims and adds them back in. // can be done in a better fashion foreach (var removedClaim in roleClaims) { await _roleManager.RemoveClaimAsync(role.Id, removedClaim); } var submittedClaims = viewModel .SelectedClaims .Select(s => { var tokens = s.Split('#'); if (tokens.Count() != 2) { throw new Exception(String.Format("Claim {0} can't be processed because it is in incorrect format", s)); } return(new Claim(tokens[0], tokens[1])); }).ToList(); roleClaims = await _roleManager.GetClaimsAsync(role.Name); foreach (var submittedClaim in submittedClaims) { var hasClaim = roleClaims.Any(c => c.Value == submittedClaim.Value && c.Type == submittedClaim.Type); if (!hasClaim) { await _roleManager.AddClaimAsync(role.Id, submittedClaim); } } roleClaims = await _roleManager.GetClaimsAsync(role.Name); var cacheKey = ApplicationRole.GetCacheKey(role.Name); System.Web.HttpContext.Current.Cache.Remove(cacheKey); return(CCOk(roleClaims)); }
public async Task <ActionResult> Edit(string id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } ApplicationRole applicationRole = await RoleManager.FindByIdAsync(id); if (applicationRole == null) { return(HttpNotFound()); } RoleClaimsViewModel roleClaims = this.GetRoleClaimsViewModel(applicationRole); return(View(roleClaims)); }
public ActionResult Details(string id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } ApplicationRole applicationRole = DbContext.Roles.Find(id); if (applicationRole == null) { return(HttpNotFound()); } RoleClaimsViewModel roleClaims = this.GetRoleClaimsViewModel(applicationRole); return(View(roleClaims)); }
// GET: Role/Details/5 public ActionResult Details(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Role role = db.Roles.Find(id); if (role == null) { return(HttpNotFound()); } RoleClaimsViewModel model = new RoleClaimsViewModel { RoleId = role.ID, RoleName = role.Name, RoleClaims = role.RoleClaims }; return(View(model)); }
// Helpers private RoleClaimsViewModel GetRoleClaimsViewModel(ApplicationRole applicationRole) { RoleClaimsViewModel roleWithAllClaims = new RoleClaimsViewModel { RoleId = applicationRole.Id, RoleName = applicationRole.Name, RoleDescription = applicationRole.Description, Claims = DbContext.Claims.ToList().Select(x => new ClaimViewModel { ClaimId = x.Id, ClaimType = x.ClaimType, ClaimValue = x.ClaimValue, Description = x.Description, Status = applicationRole.Claims.Any(r => r.Id == x.Id) }).ToList() }; return(roleWithAllClaims); }
public async Task <IHttpActionResult> Get() { var claimPrincipal = ((ApiController)ActionContext.ControllerContext.Controller).User as ClaimsPrincipal; var rol = claimPrincipal.Claims.FirstOrDefault(x => x.Type.ToLower() == "rol"); if (rol == null) { throw new Exception("No existe"); } var role = await _roleManager.FindByNameAsync(rol.Value); var claimGroups = _claimedActionsProvider.GetClaimGroups(); var assignedClaims = await _roleManager.GetClaimsAsync(role.Name); var viewModel = new RoleClaimsViewModel() { RoleId = role.Id, RoleName = role.Name, }; foreach (var claimGroup in claimGroups) { var claimGroupModel = new RoleClaimsViewModel.ClaimGroup() { GroupId = claimGroup.GroupId, GroupName = claimGroup.GroupName, GroupClaimsCheckboxes = claimGroup.Claims .Select(c => new SelectListItem() { Value = String.Format("{0}#{1}", claimGroup.GroupId, c), Text = c, Selected = assignedClaims.Any(ac => ac.Type == claimGroup.GroupId.ToString() && ac.Value == c) }).ToList() }; viewModel.ClaimGroups.Add(claimGroupModel); } return(Ok(viewModel)); }
public async Task <IActionResult> ManageRoleClaims(string roleId) { var role = await roleManager.FindByIdAsync(roleId); if (role == null) { ViewBag.ErrorMessage = $"Role with Id = {roleId} cannot be found"; return(View("NotFound")); } var existingRoleClaims = await roleManager.GetClaimsAsync(role); var model = new RoleClaimsViewModel { RoleId = roleId }; foreach (Claim claim in ClaimsStore.AllClaims) { RoleClaim roleClaim = new RoleClaim { 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 (existingRoleClaims.Any(c => c.Type == claim.Type && c.Value == "true")) { roleClaim.IsSelected = true; } model.Cliams.Add(roleClaim); } return(View(model)); }