Exemple #1
0
        public IActionResult ChangeUserPassword(string newPassword, string newPasswordConfirmation)
        {
            Guid userID = ViewBag.Message = TempData["userID"];
            var  user   = _dbAccessUser.GetUserByGuid(userID).ReturnedObject;

            if (DateTime.Now < user.PasswordRecoveryExpirationTime)
            {
                if (!newPassword.Equals(newPasswordConfirmation))
                {
                    return(Content(UserMessages.ConfirmPasswordNoMatch));
                }

                if ((_passwordValidationManager.CheckPassword(newPassword).Any(x => !x.Item1)))
                {
                    return(Content(_passwordValidationManager.CheckPassword(newPassword).FirstOrDefault().Item2));
                }

                if ((_passwordValidationManager.CheckPassword(newPasswordConfirmation).Any(x => !x.Item1)))
                {
                    return(Content(_passwordValidationManager.CheckPassword(newPasswordConfirmation).FirstOrDefault().Item2));
                }

                _dbAccessUser.UpdatePassword(userID, newPassword);

                return(View("GenericUserInformation", new GenericInformationModelView("Password change", UserMessages.PasswordHasBeenChanged)));
            }

            return(View("GenericUserInformation", new GenericInformationModelView("Password change", UserMessages.PasswordChangeTimeout)));
        }
        public IActionResult PerformUserRegister(string accountName, string userPassword, string confirmUserPassword, string userEmail)
        {
            User user = new User();

            try
            {
                var emptyFieldsValidationResult = _passwordValidationManager.ValidateEmptyUserInput(accountName, userPassword, confirmUserPassword, userEmail);

                if (emptyFieldsValidationResult.Any(x => x.Item1 == false))
                {
                    // Return information to user about empty input fields
                    return(Content(string.Concat(emptyFieldsValidationResult.Select(x => x.Item2 + Environment.NewLine))));
                }

                List <(bool, string)> passwordPolicyValidation = new List <(bool, string)>();

                passwordPolicyValidation.AddRange(_passwordValidationManager.CheckUsername(accountName));
                passwordPolicyValidation.AddRange(_passwordValidationManager.CheckPassword(userPassword));
                passwordPolicyValidation.Add(_passwordValidationManager.CheckPasswordMatch(userPassword, confirmUserPassword));
                passwordPolicyValidation.Add(_passwordValidationManager.CheckEmail(userEmail));

                if (passwordPolicyValidation.All(x => x.Item1))
                {
                    user = _dbAccessUser.CreateNew(accountName, userPassword, userEmail).ReturnedObject as User;
                }
                else
                {
                    // Return information to user why registration fails
                    return(Content(passwordPolicyValidation.Where(v => v.Item1 == false).Select(s => s.Item2).ToArray().First()));
                }

                string activationLink = Url.Action("Confirmation", "Register", new
                {
                    userID = user.VerificationToken.ToString()
                }, protocol: HttpContext.Request.Scheme);

                EmailSendStatus emailSendStatus = new EmailManager().SendMail(user.Email, "World of Warcraft Character Comparer: Verify account!",
                                                                              $"<p> Thank you for registration {user.Nickname}." +
                                                                              $"<p>To verify your account please click on following link:</p>" +
                                                                              $"<a href=\"{activationLink}\">Activate my account!</a>");

                if (!emailSendStatus.SendSuccessfully)
                {
                    _dbAccessUser.RemoveByID(user.ID);
                    _logger.LogInformation($"Removing user from database {user.Nickname}, {user.Email}, {user.ID}");
                    _logger.LogError($"Error while sending activation email. {emailSendStatus.SendEmailException.Message}");
                    return(View("GenericUserInformation", new GenericInformationModelView("Ops! Registration failed", UserMessages.ServiceErrorTechnicalProblems)));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Error occour while registering user. {ex.Message}");
                return(View("Error", ex));
            }

            return(View("UserRegistrationCompleted", user));
        }