예제 #1
0
        public async Task <Entity.UserClaim> UpdateAsync(Entity.UserClaim entity, CancellationToken cancellationToken = default)
        {
            var claim = await GetClaimAsync(entity.Id, cancellationToken).ConfigureAwait(false);

            var user = await GetUserAsync(entity.UserId)
                       .ConfigureAwait(false);

            var result = await _userManager.RemoveClaimAsync(user, claim.ToClaim())
                         .ConfigureAwait(false);

            ChechResult(result);
            result = await _userManager.AddClaimAsync(user, entity.ToUserClaim().ToClaim())
                     .ConfigureAwait(false);

            ChechResult(result);
            _logger.LogInformation("Entity {EntityId} updated", entity.Id, entity);
            return(entity);
        }
        public async Task <Entity.UserClaim> CreateAsync(Entity.UserClaim entity, CancellationToken cancellationToken = default)
        {
            var user = await GetUserAsync(entity.UserId)
                       .ConfigureAwait(false);

            var claim  = entity.ToUserClaim().ToClaim();
            var result = await _userManager.AddClaimAsync(user, claim)
                         .ConfigureAwait(false);

            if (result.Succeeded)
            {
                entity.Id = Guid.NewGuid().ToString();
                _logger.LogInformation("Entity {EntityId} created", entity.Id, entity);
                return(entity);
            }
            throw new IdentityException
                  {
                      Errors = result.Errors
                  };
        }
        public async Task <Entity.UserClaim> UpdateAsync(Entity.UserClaim entity, CancellationToken cancellationToken = default)
        {
            var claim = await GetClaimAsync(entity.Id, cancellationToken).ConfigureAwait(false);

            if (claim == null)
            {
                throw new DbUpdateException($"Entity type {typeof(UserClaim).Name} at id {entity.Id} is not found");
            }

            var user = await GetUserAsync(entity.UserId)
                       .ConfigureAwait(false);

            var result = await _userManager.RemoveClaimAsync(user, claim.ToClaim())
                         .ConfigureAwait(false);

            ChechResult(result);
            result = await _userManager.AddClaimAsync(user, entity.ToUserClaim().ToClaim())
                     .ConfigureAwait(false);

            ChechResult(result);
            _logger.LogInformation("Entity {EntityId} updated", entity.Id, entity);
            return(entity);
        }