protected override async Task Handle(ChangePasswordCommand command, CancellationToken cancellationToken)
 {
     await _handler
     .Run(async() =>
     {
         await _passwordService.ChangeAsync(command.UserId, command.CurrentPassword,
                                            command.NewPassword);
     })
     .OnSuccess(async() =>
     {
         await _mediatRBus.PublishAsync(
             new PasswordChangedDomainEvent(command.Request.Id, command.UserId),
             cancellationToken);
     })
     .OnCustomError(async customException =>
     {
         await _mediatRBus.PublishAsync(
             new ChangePasswordRejectedDomainEvent(command.Request.Id, command.UserId,
                                                   customException.Code, customException.Message), cancellationToken);
     })
     .OnError(async(exception, logger) =>
     {
         logger.Error(exception, $"Error when changing a password for user with id: {command.UserId}.",
                      exception);
         await _mediatRBus.PublishAsync(
             new ChangePasswordRejectedDomainEvent(command.Request.Id, command.UserId, Codes.Error,
                                                   exception.Message), cancellationToken);
     })
     .ExecuteAsync();
 }
コード例 #2
0
 protected override async Task Handle(DeleteAccountCommand command, CancellationToken cancellationToken)
 {
     await _handler
     .Run(async() =>
     {
         await _userService.DeleteAsync(command.UserId, command.Soft);
         await _userService.SaveChangesAsync(cancellationToken);
     })
     .OnSuccess(async() =>
     {
         await _mediatRBus.PublishAsync(
             new AccountDeletedDomainEvent(command.Request.Id, command.UserId, command.Soft),
             cancellationToken);
     })
     .OnCustomError(async customException =>
     {
         await _mediatRBus.PublishAsync(
             new DeleteAccountRejectedDomainEvent(command.Request.Id, command.UserId, command.Soft,
                                                  customException.Code, customException.Message), cancellationToken);
     })
     .OnError(async(exception, logger) =>
     {
         logger.Error($"Error when deleting account for user with id: {command.UserId}.", exception);
         await _mediatRBus.PublishAsync(
             new DeleteAccountRejectedDomainEvent(command.Request.Id, command.UserId, command.Soft,
                                                  Codes.Error, exception.Message), cancellationToken);
     })
     .ExecuteAsync();
 }
        protected override async Task Handle(SignUpCommand command, CancellationToken cancellationToken)
        {
            var userId = Guid.NewGuid();

            await _handler
            .Run(async() =>
            {
                await _userService.SignUpAsync(userId, command.Email, command.UserName, command.Password,
                                               command.Request.Culture);

                await _userService.SaveChangesAsync(cancellationToken);
            })
            .OnSuccess(async() =>
            {
                var user = await _userService.GetAsync(userId);

                await _mediatRBus.PublishAsync(
                    new SignedUpDomainEvent(command.Request, user), cancellationToken);
            })
            .OnCustomError(async customException =>
            {
                await _mediatRBus.PublishAsync(
                    new SignUpRejectedDomainEvent(command.Request.Id, userId, customException.Message,
                                                  customException.Code), cancellationToken);
            })
            .OnError(async(exception, logger) =>
            {
                logger.Error($"Error occured while signing up a user with email: {command.Email}.", exception);
                await _mediatRBus.PublishAsync(
                    new SignUpRejectedDomainEvent(command.Request.Id, userId, exception.Message, Codes.Error),
                    cancellationToken);
            })
            .ExecuteAsync();
        }
 protected override async Task Handle(ActivateAccountCommand command, CancellationToken cancellationToken)
 {
     await _handler
     .Run(async() =>
     {
         await _userService.ActivateAsync(command.Email, command.Token);
         await _userService.SaveChangesAsync(cancellationToken);
     })
     .OnSuccess(async() =>
     {
         var user = await _userService.GetByEmailAsync(command.Email);
         await _mediatRBus.PublishAsync(
             new AccountActivatedDomainEvent(command.Request.Id, command.Email, user.Id),
             cancellationToken);
     })
     .OnCustomError(async customException =>
     {
         await _mediatRBus.PublishAsync(
             new ActivateAccountRejectedDomainEvent(command.Request.Id, command.Email,
                                                    customException.Code, customException.Message), cancellationToken);
     })
     .OnError(async(exception, logger) =>
     {
         logger.Error(exception, $"Error when activating account for user with email: {command.Email}.",
                      exception);
         await _mediatRBus.PublishAsync(
             new ActivateAccountRejectedDomainEvent(command.Request.Id, command.Email, Codes.Error,
                                                    exception.Message), cancellationToken);
     })
     .ExecuteAsync();
 }
コード例 #5
0
        protected override async Task Handle(SignInCommand command, CancellationToken cancellationToken)
        {
            var user = await _userService.GetByEmailAsync(command.Email);

            await _handler
            .Run(async() =>
            {
                await _authenticationService.SignInAsync(command.SessionId, command.Email,
                                                         command.Password, command.IpAddress, command.UserAgent);

                await _authenticationService.SaveChangesAsync(cancellationToken);
            })
            .OnSuccess(async() =>
                       await _mediatRBus.PublishAsync(new SignedInDomainEvent(
                                                          command.Request.Id, user.Id, user.Email, user.Username), cancellationToken))
            .OnCustomError(async customException =>
                           await _mediatRBus.PublishAsync(
                               new SignInRejectedDomainEvent(
                                   command.Request.Id, user.Id, customException.Code, customException.Message),
                               cancellationToken))
            .OnError(async(exception, logger) =>
            {
                logger.Error($"Error occured while logging in a user wid id: {user.Id}.", exception);
                await _mediatRBus.PublishAsync(
                    new SignInRejectedDomainEvent(command.Request.Id, user.Id, Codes.Error, exception.Message),
                    cancellationToken);
            })
            .ExecuteAsync();
        }
コード例 #6
0
 protected override async Task Handle(DisableTwoFactorAuthenticationCommand command,
                                      CancellationToken cancellationToken)
 {
     await _handler
     .Run(async() =>
     {
         await _userService.DisableTwoFactorAuthorization(command.UserId);
         await _userService.SaveChangesAsync(cancellationToken);
     })
     .OnSuccess(async() =>
     {
         await _mediatRBus.PublishAsync(
             new TwoFactorAuthenticationDisabledDomainEvent(command.Request.Id, command.UserId),
             cancellationToken);
     })
     .OnCustomError(async customException =>
     {
         await _mediatRBus.PublishAsync(
             new DisableTwoFactorAuthenticationRejectedDomainEvent(command.Request.Id, command.UserId,
                                                                   customException.Message, customException.Code),
             cancellationToken);
     })
     .OnError(async(exception, logger) =>
     {
         logger.Error($"Error occured when disabling two factor auth for user with id: {command.UserId}",
                      exception);
         await _mediatRBus.PublishAsync(
             new DisableTwoFactorAuthenticationRejectedDomainEvent(command.Request.Id, command.UserId,
                                                                   exception.Message, Codes.Error),
             cancellationToken);
     })
     .ExecuteAsync();
 }
 protected override async Task Handle(UnlockAccountCommand command, CancellationToken cancellationToken)
 {
     await _handler
     .Run(async() =>
     {
         await _userService.UnlockAsync(command.UnlockUserId);
         await _userService.SaveChangesAsync(cancellationToken);
     })
     .OnSuccess(async() =>
     {
         await _mediatRBus.PublishAsync(
             new AccountUnlockedDomainEvent(command.Request.Id, command.UserId, command.UnlockUserId),
             cancellationToken);
     })
     .OnCustomError(async customException =>
     {
         await _mediatRBus.PublishAsync(
             new UnlockAccountRejectedDomainEvent(command.Request.Id, command.UserId, command.UnlockUserId,
                                                  customException.Message, customException.Code), cancellationToken);
     })
     .OnError(async(exception, logger) =>
     {
         logger.Error($"Error occured when unlocking a account for user with id: {command.UserId}.",
                      exception);
         await _mediatRBus.PublishAsync(
             new UnlockAccountRejectedDomainEvent(command.Request.Id, command.UserId, command.UnlockUserId,
                                                  exception.Message, Codes.Error), cancellationToken);
     })
     .ExecuteAsync();
 }
        protected override async Task Handle(ResetPasswordCommand command, CancellationToken cancellationToken)
        {
            var operationId = Guid.NewGuid();

            await _handler
            .Run(async() =>
            {
                await _passwordService.ResetAsync(operationId, command.Email);
                await _passwordService.SaveChangesAsync(cancellationToken);
            })
            .OnSuccess(async() =>
            {
                await _mediatRBus.PublishAsync(
                    new ResetPasswordInitiatedDomainEvent(command.Request, operationId, command.Email,
                                                          command.Endpoint), cancellationToken);
            })
            .OnCustomError(async customException =>
            {
                await _mediatRBus.PublishAsync(
                    new ResetPasswordRejectedDomainEvent(command.Request.Id, command.Email,
                                                         customException.Message, customException.Code), cancellationToken);
            })
            .OnError(async(exception, logger) =>
            {
                logger.Error($"Error occured when resetting a password for user with email {command.Email}",
                             exception);
                await _mediatRBus.PublishAsync(
                    new ResetPasswordRejectedDomainEvent(command.Request.Id, command.Email,
                                                         exception.Message, Codes.Error), cancellationToken);
            })
            .ExecuteAsync();
        }
        protected override async Task Handle(UploadAvatarCommand command, CancellationToken cancellationToken)
        {
            await _handler
            .Run(async() =>
            {
                var avatar = _fileResolver.FromBase64(command.FileBase64, command.Filename,
                                                      command.FileContentType);

                if (avatar.HasNoValue())
                {
                    throw new ServiceException(Codes.AvatarIsInvalid,
                                               $"Avatar with filename: {command.Filename} is invalid.");
                }

                await _avatarService.AddOrUpdateAsync(command.UserId, avatar);
            })
            .OnSuccess(async() =>
            {
                var avatarUrl = await _avatarService.GetUrlAsync(command.UserId);
                await _mediatRBus.PublishAsync(new AvatarUploadedDomainEvent(command.Request.Id, command.UserId,
                                                                             avatarUrl), cancellationToken);
            })
            .OnCustomError(async customException =>
            {
                await _mediatRBus.PublishAsync(new UploadAvatarRejectedDomainEvent(command.Request.Id,
                                                                                   command.UserId, customException.Code, customException.Message), cancellationToken);
            })
            .OnError(async(exception, logger) =>
            {
                logger.Error($"Error occured when uploading avatar for user with id: {command.UserId}", exception);
                await _mediatRBus.PublishAsync(new UploadAvatarRejectedDomainEvent(command.Request.Id,
                                                                                   command.UserId, Codes.Error, exception.Message), cancellationToken);
            })
            .ExecuteAsync();
        }
コード例 #10
0
 protected override async Task Handle(EditUserCommand command, CancellationToken cancellationToken)
 {
     await _handler
     .Run(async() =>
     {
         await _userService.UpdateAsync(command.UserId, command.Name, command.FirstName,
                                        command.LastName, command.Address.Street, command.Address.City, command.Address.State,
                                        command.Address.Country, command.Address.ZipCode);
         await _userService.SaveChangesAsync(cancellationToken);
     })
     .OnSuccess(async() =>
     {
         await _mediatRBus.PublishAsync(new UserEditedDomainEvent(command.Request.Id, command.UserId),
                                        cancellationToken);
     })
     .OnCustomError(async customException =>
     {
         await _mediatRBus.PublishAsync(
             new EditUserRejectedDomainEvent(command.Request.Id, command.UserId, customException.Code,
                                             customException.Message), cancellationToken);
     })
     .OnError(async(exception, logger) =>
     {
         logger.Error($"Error when editing user with id: {command.UserId}.", exception);
         await _mediatRBus.PublishAsync(
             new EditUserRejectedDomainEvent(command.Request.Id, command.UserId, Codes.Error,
                                             exception.Message), cancellationToken);
     })
     .ExecuteAsync();
 }
コード例 #11
0
 protected override async Task Handle(SetNewPasswordCommand command, CancellationToken cancellationToken)
 {
     await _handler
     .Run(async() =>
     {
         await _passwordService.SetNewAsync(command.Email, command.Token, command.Password);
         await _passwordService.SaveChangesAsync(cancellationToken);
     })
     .OnSuccess(async() =>
     {
         await _mediatRBus.PublishAsync(new NewPasswordSetDomainEvent(command.Request.Id, command.Email),
                                        cancellationToken);
     })
     .OnCustomError(async customException =>
     {
         await _mediatRBus.PublishAsync(
             new SetNewPasswordRejectedDomainEvent(command.Request.Id, customException.Code,
                                                   customException.Message, command.Email), cancellationToken);
     })
     .OnError(async(exception, logger) =>
     {
         logger.Error($"Error occured when setting a new password user with email: {command.Email}.",
                      exception);
         await _mediatRBus.PublishAsync(
             new SetNewPasswordRejectedDomainEvent(command.Request.Id, Codes.Error,
                                                   exception.Message, command.Email), cancellationToken);
     })
     .ExecuteAsync();
 }
コード例 #12
0
 protected override async Task Handle(RemoveAvatarCommand command, CancellationToken cancellationToken)
 {
     await _handler
     .Run(async() =>
     {
         await _avatarService.RemoveAsync(command.UserId);
         await _avatarService.SaveChangesAsync(cancellationToken);
     })
     .OnSuccess(async() =>
     {
         await _mediatRBus.PublishAsync(new AvatarRemovedDomainEvent(command.Request.Id, command.UserId),
                                        cancellationToken);
     })
     .OnCustomError(async customException =>
     {
         await _mediatRBus.PublishAsync(new RemoveAvatarRejectedDomainEvent(command.Request.Id, command
                                                                            .UserId, customException.Message, customException.Code), cancellationToken);
     })
     .OnError(async(exception, logger) =>
     {
         logger.Error($"Error when removing avatar for user with id: {command.UserId}", exception);
         await _mediatRBus.PublishAsync(new RemoveAvatarRejectedDomainEvent(command.Request.Id, command
                                                                            .UserId, exception.Message, Codes.Error), cancellationToken);
     })
     .ExecuteAsync();
 }
        protected override async Task Handle(SendActivateAccountMessageCommand command,
                                             CancellationToken cancellationToken)
        {
            var operationId = Guid.NewGuid();

            await _handler
            .Run(async() =>
            {
                await _oneTimeSecuredOperationService.CreateAsync(operationId,
                                                                  OneTimeSecuredOperations.ActivateAccount, command.UserId, DateTime.UtcNow.AddDays(7));

                await _oneTimeSecuredOperationService.SaveChangesAsync(cancellationToken);
            })
            .OnSuccess(async() =>
            {
                var operation = await _oneTimeSecuredOperationService.GetAsync(operationId);

                await _mediatRBus.PublishAsync(new ActivateAccountSecuredOperationCreatedDomainEvent(
                                                   command.Request, command.UserId, command.Username, command.Email, operation.Id,
                                                   operation.Token, _appOptions.ActivationAccountUrl), cancellationToken);
            })
            .OnCustomError(async customException =>
            {
                await _mediatRBus.PublishAsync(new CreateActivateAccountSecuredOperationRejectedDomainEvent(
                                                   command.Request.Id, command.UserId, operationId, customException.Message,
                                                   customException.Code),
                                               cancellationToken);
            })
            .OnError(async(exception, logger) =>
            {
                logger.Error(
                    $"Error occured while creating a secured operation for user with id: {command.UserId}.",
                    exception);
                await _mediatRBus.PublishAsync(new CreateActivateAccountSecuredOperationRejectedDomainEvent(
                                                   command.Request.Id, command.UserId, operationId, exception.Message, Codes.Error),
                                               cancellationToken);
            }).ExecuteAsync();
        }
コード例 #14
0
 protected override async Task Handle(SignOutCommand command, CancellationToken cancellationToken)
 {
     await _handler
     .Run(async() =>
     {
         await _authenticationService.SignOutAsync(command.SessionId, command.UserId);
         await _authenticationService.SaveChangesAsync(cancellationToken);
     })
     .OnSuccess(async() =>
                await _mediatRBus.PublishAsync(
                    new SignedOutDomainEvent(command.Request.Id, command.UserId), cancellationToken))
     .OnCustomError(async customException =>
                    await _mediatRBus.PublishAsync(new SignOutRejectedDomainEvent(
                                                       command.Request.Id, command.UserId, customException.Message, customException.Code),
                                                   cancellationToken))
     .OnError(async(exception, logger) =>
     {
         logger.Error($"Error occured while signing out user with id: {command.UserId}.", exception);
         await _mediatRBus.PublishAsync(
             new SignOutRejectedDomainEvent(command.Request.Id, command.UserId, exception.Message,
                                            Codes.Error), cancellationToken);
     })
     .ExecuteAsync();
 }