public CommandResult<ResetUserPasswordCommand> Execute(ResetUserPasswordCommand parameter) {
            var result = new CommandResult<ResetUserPasswordCommand>();
            var user = _repository.GetActiveByEmail(parameter.Email);

            if (user.PasswordResetToken.Verify(parameter.PasswordResetToken)) {
                user.ResetPassword(parameter.NewPassword);
            }
            else {
                result.AddError("The password reset link has expired; please request a new one");
                user.ResetPasswordFailed();
            }

            _repository.Update();

            return result;
        }
        protected override async Task HandleCommandAsync(StartReadingBookParameters input, CommandResult <string> commandResult)
        {
            var myBook = await _myBookRepository.FindAsync(input.Isbn);

            if (myBook == null)
            {
                commandResult.AddError("error_book_notfound", "Book not found");
                return;
            }

            var startedBook = myBook.StartReading(input.StartDate);

            await _myBookRepository.SaveAsync(startedBook);

            commandResult.Success(input.Isbn);
        }
        public CommandResult <RegisterUserCommand> Execute(RegisterUserCommand parameter)
        {
            var result = new CommandResult <RegisterUserCommand>();
            var user   = new User(parameter.Email, parameter.Password);

            if (_repository.TryGetByEmail(user.Email) != null)
            {
                result.AddError(p => p.Email, "Email address already exists");
            }

            if (result.Success)
            {
                _repository.Add(user);

                _mailClient.Send(_template.GetMessage(new ActivationMailTemplateParameters(user.Email, user.ActivationCode.Value), user.Email));
            }

            return(result);
        }
        public CommandResult <LogInUserCommand> Execute(LogInUserCommand parameter)
        {
            var result = new CommandResult <LogInUserCommand>();
            var user   = _repository.TryGetByEmail(parameter.Email);

            if (user == null || user.Status != UserStatus.Active || !user.Password.Verify(parameter.Password))
            {
                result.AddError("Invalid email or password");
                user?.LogInFailed();
            }
            else
            {
                user.LogIn();
            }

            _repository.Update();

            return(result);
        }
Пример #5
0
        public CommandResult <ChangeUserPasswordCommand> Execute(ChangeUserPasswordCommand parameter)
        {
            var result = new CommandResult <ChangeUserPasswordCommand>();
            var user   = _repository.GetActiveByEmail(parameter.Email);

            if (user.Password.Verify(parameter.CurrentPassword))
            {
                user.ChangePassword(parameter.NewPassword);
            }
            else
            {
                result.AddError(p => p.CurrentPassword, "Invalid password");
                user.ChangePasswordFailed();
            }

            _repository.Update();

            return(result);
        }
Пример #6
0
        public CommandResult <DeactivateUserCommand> Execute(DeactivateUserCommand parameter)
        {
            var result = new CommandResult <DeactivateUserCommand>();
            var user   = _repository.GetActiveByEmail(parameter.Email);

            if (user.Password.Verify(parameter.Password))
            {
                user.Deactivate();
            }
            else
            {
                result.AddError(p => p.Password, "Invalid password");
                user.DeactivationFailed();
            }

            _repository.Update();

            return(result);
        }
        public CommandResult <ConfirmUserEmailChangeCommand> Execute(ConfirmUserEmailChangeCommand command)
        {
            var result = new CommandResult <ConfirmUserEmailChangeCommand>();
            var user   = _repository.TryGetByEmail(command.Email);

            // Any error that occurs gets the same message to prevent leaking information
            if (user == null ||
                !int.TryParse(command.ConfirmationCode, out int confirmationCode) ||
                user.Status != UserStatus.Active ||
                user.NewEmailConfirmationCode != confirmationCode)
            {
                user?.ChangeEmailFailed();
                result.AddError(c => c.ConfirmationCode, "This confirmation code is invalid");
            }
            else
            {
                user.ChangeEmail();
            }

            _repository.Update();

            return(result);
        }
        public CommandResult <ActivateUserCommand> Execute(ActivateUserCommand parameter)
        {
            var result = new CommandResult <ActivateUserCommand>();
            var user   = _repository.TryGetByEmail(parameter.Email);

            // Any error that occurs gets the same message to prevent leaking information
            if (user == null ||
                !int.TryParse(parameter.ActivationCode, out int activationCode) ||
                user.Status != UserStatus.New ||
                user.ActivationCode != activationCode)
            {
                user?.ActivationFailed();
                result.AddError(p => p.ActivationCode, "This activation code is invalid");
            }
            else
            {
                user.Activate();
            }

            _repository.Update();

            return(result);
        }