Example #1
0
        public async Task HandleAsync(UserCreationCompletedIntegrationEvent integrationEvent, CancellationToken cancellationToken = default)
        {
            try
            {
                var getAccountResult = await _accountGetterService.GetByIdAsync(integrationEvent.UserId);

                var accountCreationCompletedIntegrationEvent =
                    new AccountCreationCompletedIntegrationEvent(integrationEvent.CorrelationId,
                                                                 getAccountResult.Value.Id);
                var publishIntegrationEvenTask = _integrationEventBus.PublishIntegrationEventAsync(accountCreationCompletedIntegrationEvent);

                var accountConfirmationToken = getAccountResult.Value.Tokens.SingleOrDefault(x => x.Type.Equals(TokenTypeEnumeration.AccountConfirmation));
                if (accountConfirmationToken != null)
                {
                    await _accountConfirmationRequestService.PublishAccountConfirmationRequestedIntegrationEventAsync(
                        getAccountResult.Value.Email, accountConfirmationToken.Value, integrationEvent.CorrelationId);
                }

                await publishIntegrationEvenTask;
            }
            catch (Exception e)
            {
                _logger.LogIntegrationEventError(ServiceComponentEnumeration.RivaIdentity, integrationEvent,
                                                 "accountId={accountId}, message={message}, stackTrace={stackTrace}", integrationEvent.UserId,
                                                 e.Message, e.StackTrace);

                var accountCreationCompletedIntegrationEventFailure =
                    new AccountCreationCompletedIntegrationEventFailure(integrationEvent.CorrelationId,
                                                                        IntegrationEventErrorCodeEnumeration.UnexpectedError.DisplayName,
                                                                        IntegrationEventErrorMessage.UnexpectedError, integrationEvent.UserId);
                await _integrationEventBus.PublishIntegrationEventAsync(accountCreationCompletedIntegrationEventFailure);
            }
        }
Example #2
0
        public async Task HandleAsync(UserCreationCompletedIntegrationEventFailure integrationEvent, CancellationToken cancellationToken)
        {
            var message = $"Could not finish {nameof(Account)} creation process.";

            _logger.LogIntegrationEventError(ServiceComponentEnumeration.RivaIdentity, integrationEvent,
                                             "accountId={accountId}, message={message}, reason={reason}, code={code}",
                                             integrationEvent.UserId, message, integrationEvent.Reason, integrationEvent.Code);

            var accountCreationCompletedIntegrationEventFailure =
                new AccountCreationCompletedIntegrationEventFailure(integrationEvent.CorrelationId,
                                                                    integrationEvent.Code, integrationEvent.Reason, integrationEvent.UserId);
            var completeAccountCreationProcessWithFailureTask =
                _integrationEventBus.PublishIntegrationEventAsync(accountCreationCompletedIntegrationEventFailure);

            try
            {
                var getAccountResult = await _accountGetterService.GetByIdAsync(integrationEvent.UserId);

                await _accountDataConsistencyService.DeleteAccountWithRelatedPersistedGrants(getAccountResult.Value);
            }
            catch (Exception e)
            {
                _logger.LogIntegrationEventError(ServiceComponentEnumeration.RivaIdentity, integrationEvent,
                                                 "accountId={accountId}, message={message}, stackTrace={stackTrace}", integrationEvent.UserId,
                                                 e.Message, e.StackTrace);
            }
            finally
            {
                await completeAccountCreationProcessWithFailureTask;
            }
        }
Example #3
0
        public async Task HandleAsync(ChangePasswordCommand command, CancellationToken cancellationToken = default)
        {
            var getAccountResult = await _accountGetterService.GetByIdAsync(command.AccountId);

            if (!getAccountResult.Success)
            {
                throw new ResourceNotFoundException(getAccountResult.Errors);
            }

            var passwordIsSetVerificationResult = _accountVerificationService.VerifyPasswordIsSet(getAccountResult.Value.PasswordHash);

            if (!passwordIsSetVerificationResult.Success)
            {
                throw new ValidationException(passwordIsSetVerificationResult.Errors);
            }

            var passwordIsCorrectVerificationResult = _accountVerificationService.VerifyPassword(getAccountResult.Value.PasswordHash, command.OldPassword);

            if (!passwordIsCorrectVerificationResult.Success)
            {
                throw new ValidationException(passwordIsCorrectVerificationResult.Errors);
            }

            getAccountResult.Value.ChangePassword(_passwordService.HashPassword(command.NewPassword), Guid.NewGuid());
            await _communicationBus.DispatchDomainEventsAsync(getAccountResult.Value, cancellationToken);

            await _accountRepository.UpdateAsync(getAccountResult.Value);
        }
Example #4
0
        public async Task <GetAccountOutputQuery> HandleAsync(GetAccountInputQuery inputQuery, CancellationToken cancellationToken = default)
        {
            var getAccountResult = await _accountGetterService.GetByIdAsync(inputQuery.AccountId);

            if (getAccountResult.Success)
            {
                return(_mapper.Map <Account, GetAccountOutputQuery>(getAccountResult.Value));
            }
            throw new ResourceNotFoundException(getAccountResult.Errors);
        }
Example #5
0
        public async Task HandleAsync(UpdateAccountRolesCommand command, CancellationToken cancellationToken = default)
        {
            var getAccountResult = await _accountGetterService.GetByIdAsync(command.AccountId);

            if (!getAccountResult.Success)
            {
                throw new ResourceNotFoundException(getAccountResult.Errors);
            }

            var rolesToRemove = getAccountResult.Value.Roles.Except(command.Roles).ToList();
            var rolesToAdd    = command.Roles.Except(getAccountResult.Value.Roles).ToList();

            var userRole = await _roleRepository.GetByNameAsync(DefaultRoleEnumeration.User.DisplayName);

            if (!command.Roles.Contains(userRole.Id))
            {
                var errors = new Collection <IError>
                {
                    new Error(AccountErrorCodeEnumeration.UserRoleIsNotRemovable, AccountErrorMessage.UserRoleIsNotRemovable)
                };
                throw new ValidationException(errors);
            }

            var rolesToCheck = await _roleRepository.FindByIdsAsync(command.Roles);

            var incorrectRoles = command.Roles.Except(rolesToCheck.Select(x => x.Id));

            if (incorrectRoles.Any())
            {
                var errors = new Collection <IError>
                {
                    new Error(RoleErrorCodeEnumeration.RolesNotFound, RoleErrorMessage.RolesNotFound)
                };
                throw new ValidationException(errors);
            }

            var correlationId = Guid.NewGuid();

            foreach (var role in rolesToRemove)
            {
                getAccountResult.Value.RemoveRole(role, correlationId);
            }

            foreach (var role in rolesToAdd)
            {
                getAccountResult.Value.AddRole(role, correlationId);
            }

            await _communicationBus.DispatchDomainEventsAsync(getAccountResult.Value, cancellationToken);

            await _accountRepository.UpdateAsync(getAccountResult.Value);
        }
Example #6
0
        public async Task GetByIdAsync_Should_Return__GetResult_With_Account()
        {
            var id      = Guid.NewGuid();
            var account = Account.Builder()
                          .SetId(id)
                          .SetEmail("*****@*****.**")
                          .SetConfirmed(true)
                          .SetPasswordHash("PasswordHash")
                          .SetSecurityStamp(Guid.NewGuid())
                          .SetCreated(DateTimeOffset.UtcNow)
                          .SetRoles(new List <Guid> {
                Guid.NewGuid()
            })
                          .Build();
            var expectedResult = GetResult <Account> .Ok(account);

            _accountRepositoryMock.Setup(x => x.GetByIdAsync(It.IsAny <Guid>())).ReturnsAsync(account);

            var result = await _accountGetterService.GetByIdAsync(id);

            result.Should().BeEquivalentTo(expectedResult);
        }
Example #7
0
        public async Task PersistAsync(Event evt)
        {
            if (evt is TokenIssuedSuccessEvent @event)
            {
                var getAccountResult = await _accountGetterService.GetByIdAsync(new Guid(@event.SubjectId));

                if (!getAccountResult.Success)
                {
                    return;
                }

                getAccountResult.Value.Login(Guid.NewGuid());
                await _communicationBus.DispatchDomainEventsAsync(getAccountResult.Value);

                await _accountRepository.UpdateAsync(getAccountResult.Value);
            }
        }
Example #8
0
        public async Task HandleAsync(DeleteAccountCommand command, CancellationToken cancellationToken = default)
        {
            var getAccountResult = await _accountGetterService.GetByIdAsync(command.AccountId);

            if (!getAccountResult.Success)
            {
                throw new ResourceNotFoundException(getAccountResult.Errors);
            }

            getAccountResult.Value.AddDeletedEvent(command.CorrelationId);
            await _communicationBus.DispatchDomainEventsAsync(getAccountResult.Value, cancellationToken);

            await _accountDataConsistencyService.DeleteAccountWithRelatedPersistedGrants(getAccountResult.Value);

            var accountDeletedIntegrationEvent = new AccountDeletedIntegrationEvent(command.CorrelationId, command.AccountId);
            await _integrationEventBus.PublishIntegrationEventAsync(accountDeletedIntegrationEvent);
        }