public CommandResult <ChangeUserEmailCommand> Execute(ChangeUserEmailCommand command) { var result = new CommandResult <ChangeUserEmailCommand>(); var user = _repository.GetActiveByEmail(command.CurrentEmail); var existingUser = _repository.TryGetByEmail(command.NewEmail); if (!user.Password.Verify(command.Password)) { result.AddError(c => c.Password, "Invalid password"); } if (existingUser != null) { result.AddError(c => c.NewEmail, "Email address already exists"); } if (result.Success) { user.RequestEmailChange(command.NewEmail); _mailClient.Send(_template.GetMessage(new ConfirmEmailMailTemplateParameters(user.Email, user.NewEmailConfirmationCode.Value), user.NewEmail)); } else { user.RequestEmailChangeFailed(); } _repository.Update(); return(result); }
public CommandResult <SendUserActivationCommand> Execute(SendUserActivationCommand parameter) { var result = new CommandResult <SendUserActivationCommand>(); var user = _repository.TryGetByEmail(parameter.Email); // Don't return errors to prevent leaking information if (user != null && user.Status == UserStatus.New) { user.GenerateActivationCode(); _repository.Update(); _mailClient.Send(_template.GetMessage(new ActivationMailTemplateParameters(user.Email, user.ActivationCode.Value), user.Email)); } return(result); }
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 <ForgotUserPasswordCommand> Execute(ForgotUserPasswordCommand parameter) { var result = new CommandResult <ForgotUserPasswordCommand>(); var user = _repository.TryGetByEmail(parameter.Email); // There is no error reporting to prevent information leaking if (user != null && user.Status == UserStatus.Active) { var token = user.GeneratePasswordResetToken(); _mailClient.Send(_template.GetMessage(new PasswordResetMailTemplateParameters(user.Email, token), user.Email)); } else { user?.PasswordResetRequestFailed(); } _repository.Update(); return(result); }