public async Task <IActionResult> Create([FromBody] RoleViewModel viewModel) { Expression <Func <Role, bool> > filter = existingRole => existingRole.NormalizedName == viewModel.NormalizedName; bool roleExists = await repository.AnyAsync <Role>(filter); if (roleExists) { ModelState.AddModelError("Role", $"Role {viewModel.Name} already exists."); return(BadRequest(ModelState)); } var role = mapper.Map <Role>(viewModel); role.NormalizedName = viewModel.NormalizedName; if (viewModel.Claims != null) { foreach (var claim in viewModel.Claims) { if (!PermissionClaims.GetAll().Contains(claim)) { ModelState.AddModelError("Claims", $"Claim {claim} does not exist."); return(BadRequest(ModelState)); } role.Claims.Add(new IdentityRoleClaim <string>() { RoleId = role.Id, ClaimType = CustomClaimTypes.Permission, ClaimValue = claim }); } } repository.Create(role); await repository.SaveAsync(); return(Created($"/api/v1/roles/{role.Id}", new { message = "Role was created successfully!" })); }
public async Task <IActionResult> Update([FromRoute] string id, [FromBody] RoleViewModel viewModel) { Role role = await repository.GetByIdAsync <Role>(id, _includeProperties); if (role == null) { return(NotFound(new { message = "Role does not exist!" })); } if (role.NormalizedName == "ADMIN") { return(Forbid()); } if (viewModel.NormalizedName != role.NormalizedName) { Expression <Func <Role, bool> > filter = existingRole => existingRole.NormalizedName == viewModel.NormalizedName; bool roleExists = await repository.AnyAsync <Role>(filter); if (roleExists) { ModelState.AddModelError("Role", $"Role {viewModel.Name} already exists."); return(BadRequest(ModelState)); } } if (!String.IsNullOrEmpty(role.Name)) { role.Name = viewModel.Name; role.NormalizedName = viewModel.NormalizedName; } if (!String.IsNullOrEmpty(viewModel.Description)) { role.Description = viewModel.Description; } if (viewModel.Claims != null) { role.Claims.Clear(); var distinctClaims = viewModel.Claims.Distinct().ToList(); foreach (var claim in distinctClaims) { if (!PermissionClaims.GetAll().Contains(claim)) { ModelState.AddModelError("Claims", $"Claim {claim} does not exist."); return(BadRequest(ModelState)); } role.Claims.Add(new IdentityRoleClaim <string>() { RoleId = role.Id, ClaimType = CustomClaimTypes.Permission, ClaimValue = claim }); } repository.Delete <IdentityRoleClaim <string> >(claim => claim.RoleId == role.Id); } repository.Update(role); await repository.SaveAsync(); return(NoContent()); }
public static bool UserHasClaimPermission(this HttpContext context, PermissionClaims permission) { if (context.User.Identity.IsAuthenticated) { return(context.User.HasClaim(CustomClaimTypes.Permission, permission.ToString())); } return(GetAnonymousRoles(context, permission).Result); }
private static async Task <bool> GetAnonymousRoles(HttpContext httpContext, PermissionClaims permission) { //TODO validar performance string roleName = permission.ToString(); var roleManager = httpContext.RequestServices.GetService <RoleManager <IdentityRole> >(); var role = await roleManager.FindByNameAsync("Anonymous"); var claims = await roleManager.GetClaimsAsync(role); var hasClaim = claims.Any(x => x.Type == CustomClaimTypes.Permission && x.Value == roleName); return(hasClaim); }
private static Task <bool> GetAnonymousRoles(HttpContext httpContext, PermissionClaims permission) { string roleName = permission.ToString(); var claims = httpContext.Items[GetAnonymousRolesCacheKey] as IList <Claim>; if (claims != null) { var hasClaimFromCache = claims.Any(x => x.Type == CustomClaimTypes.Permission && x.Value == roleName); return(Task.FromResult(hasClaimFromCache)); } return(GetAnonymousRolesFromDb(httpContext, permission)); }
public UserViewModel CreateViewModel(string ownerId, object contentKey = null) { var viewModel = new UserViewModel(); viewModel.AllRoleClaims = PermissionClaims.AllRoleBloggerClaims(); viewModel.AllRoles = _roleService.GetAll(); if (contentKey != null) { var user = _userService.GetProfile((string)contentKey); viewModel.User = user.User; viewModel.Roles = user.Roles.Select(x => x.Role); viewModel.UserClaims = user.UserClaims; } return(viewModel); }
public RoleViewModel CreateViewModel(string ownerId, object contentKey = null) { var viewModel = new RoleViewModel(); if (contentKey != null) { var role = _roleService.GetRoleClaims(contentKey.ToString()); viewModel.Role = role.Role; viewModel.AssignedClaims = role.Claims; } else { viewModel.Role = new Services.Models.Role(); } viewModel.AllRoleClaims = PermissionClaims.AllRoleClaims(); return(viewModel); }
private static async Task <bool> GetAnonymousRolesFromDb(HttpContext httpContext, PermissionClaims permission) { string roleName = permission.ToString(); var roleManager = httpContext.RequestServices.GetService <RoleManager <IdentityRole> >(); var role = await roleManager.FindByNameAsync("Anonymous"); IList <Claim> claims = await roleManager.GetClaimsAsync(role); httpContext.Items[GetAnonymousRolesCacheKey] = claims; var hasClaim = claims.Any(x => x.Type == CustomClaimTypes.Permission && x.Value == roleName); return(hasClaim); }