Esempio n. 1
0
        public override async Task <IdentityResult> UpdateAsync(AppIdentityUser identityUser,
                                                                CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            if (identityUser == null)
            {
                throw new ArgumentNullException(nameof(identityUser));
            }

            var user = await _mediator.Send(new UserByIdQuery { Id = identityUser.Id }, cancellationToken);

            _mapper.Map(identityUser, user);

            var commandResult = await _mediator.Send(new UpdateUserCommand { Dto = user }, cancellationToken);

            return(commandResult.Success
                ? IdentityResult.Success
                : IdentityResult.Failed(new IdentityError
            {
                Code = "update_user_failed",
                Description = $"An error occured while updating User with id {identityUser.Id}"
            }));
        }
Esempio n. 2
0
        public override async Task <IdentityResult> AddClaimsAsync(AppIdentityUser identityUser,
                                                                   IEnumerable <Claim> claims, CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            if (identityUser == null)
            {
                throw new ArgumentNullException(nameof(identityUser));
            }
            if (claims == null)
            {
                throw new ArgumentNullException(nameof(claims));
            }

            var user = await _mediator.Send(new UserByIdQuery { Id = identityUser.Id }, cancellationToken);

            if (user == null)
            {
                return(IdentityResult.Failed(new IdentityError
                {
                    Code = "user_not_found", Description = $"User cannot be found with id: {identityUser.Id}"
                }));
            }

            foreach (var claim in claims)
            {
                var existingClaims = await _mediator.Send(new UserClaimQuery
                {
                    UserId = identityUser.Id,
                    Type   = claim.Type,
                    Value  = claim.Value
                }, cancellationToken);

                if (existingClaims.Count == 0)
                {
                    var newClaim     = CreateUserClaim(identityUser, claim);
                    var userClaimDto = newClaim.ToClaimDto();

                    var commandResult = await _mediator.Send(new CreateUserClaimCommand { Dto = userClaimDto },
                                                             cancellationToken);

                    if (!commandResult.Success)
                    {
                        return(IdentityResult.Failed(new IdentityError
                        {
                            Code = "add_claim_error", Description = "An error occured during creating a user claim."
                        }));
                    }
                }
            }

            return(IdentityResult.Success);
        }
Esempio n. 3
0
        public override Task SetPasswordHashAsync(AppIdentityUser identityUser, string passwordHash,
                                                  CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            if (identityUser == null)
            {
                throw new ArgumentNullException(nameof(identityUser));
            }

            return(Task.FromResult(identityUser.PasswordHash = passwordHash));
        }
Esempio n. 4
0
        public override Task SetUserNameAsync(AppIdentityUser identityUser, string userName,
                                              CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            if (identityUser == null)
            {
                throw new ArgumentNullException(nameof(identityUser));
            }

            return(Task.FromResult(identityUser.UserName = userName));
        }
Esempio n. 5
0
        public override Task <string> GetUserIdAsync(AppIdentityUser identityUser,
                                                     CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            if (identityUser == null)
            {
                throw new ArgumentNullException(nameof(identityUser));
            }

            return(Task.FromResult(identityUser.Id.ToString()));
        }
Esempio n. 6
0
        public override async Task <IdentityResult> CreateAsync(AppIdentityUser identityUser,
                                                                CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            if (identityUser == null)
            {
                throw new ArgumentNullException(nameof(identityUser));
            }

            var newUser = new UserDto();

            _mapper.Map(identityUser, newUser);

            var commandResult = await _mediator.Send(new CreateUserCommand { Dto = newUser }, cancellationToken);

            if (!commandResult.Success)
            {
                return(IdentityResult.Failed(new IdentityError
                {
                    Code = "user_create_failed",
                    Description = "An error occured while creating a new User."
                }));
            }

            newUser.Id = commandResult.NewEntityId;
            _mapper.Map(newUser, identityUser);

            var claims = new List <Claim>
            {
                new AppIdentityUserClaim
                {
                    UserId     = identityUser.Id,
                    ClaimType  = ClaimTypes.Role,
                    ClaimValue = UserRole.Guest.ToString()
                }.ToClaim(),
                new AppIdentityUserClaim
                {
                    UserId     = identityUser.Id,
                    ClaimType  = AppClaimTypes.UserId,
                    ClaimValue = identityUser.Id.ToString()
                }.ToClaim()
            };

            var identityResult = await AddClaimsAsync(identityUser, claims, cancellationToken);

            return(identityResult);
        }
Esempio n. 7
0
        public override Task <bool> HasPasswordAsync(AppIdentityUser identityUser,
                                                     CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            if (identityUser == null)
            {
                throw new ArgumentNullException(nameof(identityUser));
            }

            return(Task.FromResult(!string.IsNullOrEmpty(identityUser.PasswordHash) &&
                                   string.IsNullOrWhiteSpace(identityUser.PasswordHash) &&
                                   identityUser.PasswordHash.Length > 0));
        }
Esempio n. 8
0
        public override async Task <IdentityResult> ReplaceClaimAsync(AppIdentityUser identityUser, Claim claim,
                                                                      Claim newClaim, CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            if (identityUser == null)
            {
                throw new ArgumentNullException(nameof(identityUser));
            }
            if (claim == null)
            {
                throw new ArgumentNullException(nameof(claim));
            }
            if (newClaim == null)
            {
                throw new ArgumentNullException(nameof(newClaim));
            }

            var matchedClaims = await _mediator.Send(new UserClaimQuery
            {
                UserId = identityUser.Id,
                Type   = claim.Type,
                Value  = claim.Value
            }, cancellationToken);

            foreach (var matchedClaim in matchedClaims)
            {
                matchedClaim.ClaimType  = newClaim.Type;
                matchedClaim.ClaimValue = newClaim.Value;

                var commandResult = await _mediator.Send(new UpdateUserClaimCommand { Dto = matchedClaim }, cancellationToken);

                if (!commandResult.Success)
                {
                    return(IdentityResult.Failed(new IdentityError
                    {
                        Code = "replace_claim_failed",
                        Description = $"An error happened during replacing Claim with id {matchedClaim.Id}"
                    }));
                }
            }

            return(IdentityResult.Success);
        }
Esempio n. 9
0
        public override async Task <IList <Claim> > GetClaimsAsync(AppIdentityUser identityUser,
                                                                   CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            if (identityUser == null)
            {
                throw new ArgumentNullException(nameof(identityUser));
            }

            var claimDtos = await _mediator.Send(new UserClaimQuery
            {
                UserId = identityUser.Id
            }, cancellationToken);

            var claims = claimDtos.Select(x => x.ToClaim()).ToList();

            return(claims);
        }
Esempio n. 10
0
        public override async Task <IdentityResult> RemoveClaimsAsync(AppIdentityUser identityUser,
                                                                      IEnumerable <Claim> claims, CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            if (identityUser == null)
            {
                throw new ArgumentNullException(nameof(identityUser));
            }
            if (claims == null)
            {
                throw new ArgumentNullException(nameof(claims));
            }

            foreach (var claim in claims)
            {
                var userClaims = await _mediator.Send(new UserClaimQuery
                {
                    UserId = identityUser.Id,
                    Type   = claim.Type,
                    Value  = claim.Value
                }, cancellationToken);

                foreach (var userClaim in userClaims)
                {
                    var commandResult = await _mediator.Send(new DeleteUserClaimCommand { Id = userClaim.Id }, cancellationToken);

                    if (!commandResult.Success)
                    {
                        return(IdentityResult.Failed(new IdentityError
                        {
                            Code = "remove_claim_failed",
                            Description = $"An error happened during removing Claim with id {userClaim.Id}"
                        }));
                    }
                }
            }

            return(IdentityResult.Success);
        }
Esempio n. 11
0
 protected override Task <IdentityUserToken <int> > FindTokenAsync(AppIdentityUser user, string loginProvider,
                                                                   string name, CancellationToken cancellationToken)
 {
     throw new NotImplementedException();
 }
Esempio n. 12
0
 public override Task <IList <UserLoginInfo> > GetLoginsAsync(AppIdentityUser user,
                                                              CancellationToken cancellationToken = default)
 {
     throw new NotImplementedException();
 }
Esempio n. 13
0
 public override Task RemoveLoginAsync(AppIdentityUser user, string loginProvider, string providerKey,
                                       CancellationToken cancellationToken = default)
 {
     throw new NotImplementedException();
 }
Esempio n. 14
0
 public override Task AddLoginAsync(AppIdentityUser user, UserLoginInfo login,
                                    CancellationToken cancellationToken = default)
 {
     throw new NotImplementedException();
 }