Exemple #1
0
        public async Task <Guid> CreateRoleAsync(ApplicationRole role, IEnumerable <string> claims)
        {
            if (claims == null)
            {
                claims = new string[] { };
            }

            ValidateClaims(claims);

            CheckAccess(role, EntityAction.Create);

            var result = await _roleManager.CreateAsync(role);

            if (!result.Succeeded)
            {
                var errors = string.Join(Environment.NewLine, result.Errors.Select(e => e.Description));
                throw new BadRequestException(
                          $"Failed to create role \"{role.Name}\". Errors: {errors}");
            }

            // WTF?
            role = await _roleManager.FindByIdAsync(role.Id.ToString());

            if (role == null)
            {
                throw new ItemNotFoundException(role.Id.ToString(), "role");
            }

            foreach (string claim in claims.Distinct())
            {
                var permission = _claimsService.GetPermissionByValue(claim);
                result = await _roleManager.AddClaimAsync(role,
                                                          new Claim(CustomClaimTypes.Permission, permission.ToString()));

                if (!result.Succeeded)
                {
                    await DeleteRoleAsync(role);

                    var errors = string.Join(Environment.NewLine, result.Errors.Select(e => e.Description));
                    throw new BadRequestException(
                              $"Failed to add claims to role \"{role.Name}\". Errors: {errors}");
                }
            }

            return(role.Id);
        }
 public PermissionDto Convert(IdentityRoleClaim <Guid> source, PermissionDto destination, ResolutionContext context)
 {
     return(Mapper.Map <PermissionDto>(_claimsService.GetPermissionByValue(source.ClaimValue)));
 }