Ejemplo n.º 1
0
        public async Task <PartialViewResult> ForgotPassword(ForgotPasswordViewModel model)
        {
            if (!String.IsNullOrEmpty(model.UserName) && !String.IsNullOrEmpty(model.Email))
            {
                var user = await this.GetUserManager.FindByNameAsync(model.UserName);

                if (user != null)                  // Check if user exists
                {
                    if (user.Email != model.Email) // Check if user name matches with email
                    {
                        ModelState.AddModelError("", "User name or email address does not match our records. Please try again.");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "User name or email address does not match our records. Please try again.");
                }
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var template = HttpContext.Server.MapPath("~/App_Data/UserPasswordResetEmailTemplate.txt");

                    var message = System.IO.File.ReadAllText(template);

                    message = message.Replace("%username%", model.UserName).Replace("%email%", model.Email);

                    // TODO - Change "*****@*****.**" to the email address of admin group
                    Email.EmailService.SendEmail("*****@*****.**", "Request to reset user password.", message); // Send a request to reset user password to admin group email address
                }
                catch (System.IO.FileNotFoundException ex)
                {
                    // Log exception
                    ErrorHandlingUtilities.LogException(ErrorHandlingUtilities.GetExceptionDetails(ex));
                }
                catch (System.IO.IOException ex)
                {
                    // Log exception
                    ErrorHandlingUtilities.LogException(ErrorHandlingUtilities.GetExceptionDetails(ex));
                }

                var stringBuilder = new StringBuilder();

                stringBuilder.Append("<div class='text-center'><h4><strong>Success!</strong></h4></div>");

                stringBuilder.Append("<div class='row'><div class='col-md-12'><p></p></div><div class='col-md-offset-1 col-md-11'>Request to reset user password has been sent to the RAMS Administration.</div>");

                stringBuilder.Append("<div class='col-md-12'><p></p></div><div class='col-md-offset-1 col-md-11'>You will receive your new login credentials by an email once request is complete.</div>");

                stringBuilder.Append("<div class='col-md-12'><p></p></div><div class='col-md-offset-1 col-md-11'><strong>NOTE: </strong> Please remember NOT to share your login credentials with anyone.</div></div>");

                var userConfirmationViewModel = new UserConfirmationViewModel(stringBuilder.ToString());

                return(PartialView("_ForgotPasswordConfirmation", userConfirmationViewModel));
            }

            return(PartialView("_ForgotPassword", model));
        }
Ejemplo n.º 2
0
        public async Task <PartialViewResult> ChangePassword(ChangePasswordViewModel model)
        {
            var stringBuilder = new StringBuilder();

            var userConfirmationViewModel = new UserConfirmationViewModel();

            var identity = new ApplicationUser();

            // If any of password fields is empty, display _Error partial view with following error message "Not all required fields were entered."
            if (String.IsNullOrEmpty(model.CurrentPassword) || String.IsNullOrEmpty(model.Password) || String.IsNullOrEmpty(model.ConfirmPassword))
            {
                stringBuilder.Append("<div class='text-center'><h4><strong>Password could NOT be changed.</strong></h4></div>");

                stringBuilder.Append("<div class='row'><div class='col-md-12'><p></p></div><div class='col-md-offset-1 col-md-11'>Not all required fields were entered.</div>");

                stringBuilder.Append("<div class='col-md-12'><p></p></div><div class='col-md-offset-1 col-md-11'>Please ensure that all required fields are entered.</div></div>");

                userConfirmationViewModel = new UserConfirmationViewModel(stringBuilder.ToString());

                return(PartialView("_FailureConfirmation", userConfirmationViewModel));
            }

            if (!String.IsNullOrEmpty(model.CurrentPassword))
            {
                // If current password does not match database records, display _Error partial view with following error message "Current Password does NOT match our records."
                if ((identity = this.GetUserManager.Find(model.UserName, model.CurrentPassword)) == null)
                {
                    stringBuilder.Append("<div class='text-center'><h4><strong>Password could NOT be changed.</strong></h4></div>");

                    stringBuilder.Append("<div class='row'><div class='col-md-12'><p></p></div><div class='col-md-offset-1 col-md-11'>Current Password does NOT match our records.</div>");

                    stringBuilder.Append("<div class='col-md-12'><p></p></div><div class='col-md-offset-1 col-md-11'>Please try again using valid password.</div></div>");

                    userConfirmationViewModel = new UserConfirmationViewModel(stringBuilder.ToString());

                    return(PartialView("_FailureConfirmation", userConfirmationViewModel));
                }
            }

            if (!String.IsNullOrEmpty(model.Password))
            {
                // If password has less than 6 characters, display _Error partial view with following error message "Passwords must be at least 6 character long and up to 100 characters long."
                if (model.Password.Length < 6)
                {
                    stringBuilder.Append("<div class='text-center'><h4><strong>Password could NOT be changed.</strong></h4></div>");

                    stringBuilder.Append("<div class='row'><div class='col-md-12'><p></p></div><div class='col-md-offset-1 col-md-11'>Passwords must be at least 6 character long and up to 100 characters long.</div>");

                    stringBuilder.Append("<div class='col-md-12'><p></p></div><div class='col-md-offset-1 col-md-11'>Please try again using valid password pattern.</div></div>");

                    userConfirmationViewModel = new UserConfirmationViewModel(stringBuilder.ToString());

                    return(PartialView("_FailureConfirmation", userConfirmationViewModel));
                }
                // If password is invalid format, display _Error partial view with following error message "Passwords must have at least one non letter or digit character, least one lowercase ('a'-'z'), least one uppercase ('A'-'Z')."
                else if (!Utilities.RegexMatch(this.PasswordRegex, model.Password))
                {
                    stringBuilder.Append("<div class='text-center'><h4><strong>Password could NOT be changed.</strong></h4></div>");

                    stringBuilder.Append("<div class='row'><div class='col-md-12'><p></p></div><div class='col-md-offset-1 col-md-11'>New Passwords must have at least one non letter or digit character, least one lowercase ('a'-'z'), least one uppercase ('A'-'Z').</div>");

                    stringBuilder.Append("<div class='col-md-12'><p></p></div><div class='col-md-offset-1 col-md-11'>Please try again using valid password pattern.</div></div>");

                    userConfirmationViewModel = new UserConfirmationViewModel(stringBuilder.ToString());

                    return(PartialView("_FailureConfirmation", userConfirmationViewModel));
                }
            }

            if (ModelState.IsValid)
            {
                if (identity != null)
                {
                    // Attempt to change password
                    try
                    {
                        var result = await this.GetUserManager.ChangePasswordAsync(identity.Id, model.CurrentPassword, model.Password);

                        if (!result.Succeeded)
                        {
                            var message = String.Format("Password could not be changed from user {0}.", identity.UserName);

                            if (!Utilities.IsEmpty(result.Errors))
                            {
                                foreach (var error in result.Errors)
                                {
                                    message += " " + error;
                                }
                            }

                            throw new PasswordChangeException(message);
                        }

                        stringBuilder.Append("<div class='text-center'><h4><strong>Success!</strong></h4></div>");

                        stringBuilder.Append("<div class='row'><div class='col-md-12'><p></p></div><div class='col-md-offset-1 col-md-11'>User password has been successfully changed.</div>");

                        stringBuilder.Append("<div class='col-md-12'><p></p></div><div class='col-md-offset-1 col-md-11'>Please remember NOT to share your login credentials with anyone.</div></div>");

                        userConfirmationViewModel = new UserConfirmationViewModel(stringBuilder.ToString());

                        return(PartialView("_SuccessConfirmation", userConfirmationViewModel));
                    }
                    catch (PasswordChangeException ex)
                    {
                        // Log exception
                        ErrorHandlingUtilities.LogException(ErrorHandlingUtilities.GetExceptionDetails(ex));

                        stringBuilder.Append("<div class='text-center'><h4><strong>Password could NOT be changed.</strong></h4></div>");

                        stringBuilder.Append("<div class='row'><div class='col-md-12'><p></p></div><div class='col-md-offset-1 col-md-11'>An exception has been caught while attempting to change a user password. Please review an exception log for more details about the exception.</div></div>");

                        userConfirmationViewModel = new UserConfirmationViewModel(stringBuilder.ToString());

                        return(PartialView("_FailureConfirmation", userConfirmationViewModel));
                    }
                }
            }

            stringBuilder.Append("<div class='text-center'><h4><strong>Password could NOT be changed.</strong></h4></div>");

            stringBuilder.Append("<div class='row'><div class='col-md-12'><p></p></div><div class='col-md-offset-1 col-md-11'>ModelState is not valid for current instance of the request. Please try again in a moment.</div>");

            stringBuilder.Append("<div class='col-md-12'><p></p></div><div class='col-md-offset-1 col-md-11'><strong>NOTE:</strong> If you encounter this issue again in the future, please contact Technical Support with exact steps to reproduce this issue.</div></div>");

            userConfirmationViewModel = new UserConfirmationViewModel(stringBuilder.ToString());

            return(PartialView("_FailureConfirmation", userConfirmationViewModel));
        }
Ejemplo n.º 3
0
        public async Task <PartialViewResult> EditUserProfile(UserEditProfileViewModel model)
        {
            var response = new HttpResponseMessage();

            if (!String.IsNullOrEmpty(model.Email))
            {
                if (model.CurrentEmail != model.Email)
                {
                    // If email is taken, add model error with following error message "The Email is unavalilable."
                    if (this.GetUserManager.FindByEmail(model.Email) != null)
                    {
                        ModelState.AddModelError("Email", "The Email is unavalilable.");
                    }
                }
            }

            if (ModelState.IsValid)
            {
                try
                {
                    // Attempt to update an email and a user name
                    var user = await this.GetUserManager.FindByNameAsync(model.UserName);

                    user.Email = model.Email;

                    var result = await this.GetUserManager.UpdateAsync(user);

                    if (result.Succeeded)
                    {
                        // If user name and email successfully updated, attempt to update FullName user claim
                        result = await this.GetUserManager.RemoveClaimAsync(user.Id, new Claim("FullName", model.CurrentFullName));

                        if (!result.Succeeded)
                        {
                            var message = String.Format("FullName claim could not be removed from user {0}.", model.UserName);

                            if (!Utilities.IsEmpty(result.Errors))
                            {
                                foreach (var error in result.Errors)
                                {
                                    message += " " + error;
                                }
                            }

                            throw new ClaimsAssignmentException(message);
                        }

                        result = await this.GetUserManager.AddClaimAsync(user.Id, new Claim("FullName", model.FirstName + " " + model.LastName));

                        if (!result.Succeeded)
                        {
                            var message = "FullName claim could not be assigned to user " + user.UserName + ".";

                            if (!Utilities.IsEmpty(result.Errors))
                            {
                                foreach (var error in result.Errors)
                                {
                                    message += " " + error;
                                }
                            }

                            throw new ClaimsAssignmentException(message);
                        }

                        if (model.UserType == UserType.Agent)
                        {
                            // If FullName user claim successfully updated, attempt to update employee profile
                            var agent = Mapper.Map <UserEditProfileViewModel, Agent>(model);

                            response = await this.GetHttpClient().PutAsJsonAsync("Agent", agent);

                            if (response.IsSuccessStatusCode)
                            {
                                agent = await response.Content.ReadAsAsync <Agent>();

                                if (agent != null)
                                {
                                    var editUserConfirmationViewModel = Mapper.Map <Agent, EditUserConfirmationViewModel>(agent);

                                    return(PartialView("_EditUserConfirmation", editUserConfirmationViewModel));
                                }
                                else
                                {
                                    throw new EmployeeUpdateException("Null is returned after updating an employee. Status Code: " + response.StatusCode);
                                }
                            }
                            else
                            {
                                throw new EmployeeUpdateException("Employee profile could not be updated. Status Code: " + response.StatusCode);
                            }
                        }
                        else if (model.UserType == UserType.Client)
                        {
                            // If FullName user claim successfully updated, attempt to update employee profile
                            var client = Mapper.Map <UserEditProfileViewModel, Client>(model);

                            response = await this.GetHttpClient().PutAsJsonAsync("Client", client);

                            if (response.IsSuccessStatusCode)
                            {
                                client = await response.Content.ReadAsAsync <Client>();

                                if (client != null)
                                {
                                    var editUserConfirmationViewModel = Mapper.Map <Client, EditUserConfirmationViewModel>(client);

                                    return(PartialView("_EditUserConfirmation", editUserConfirmationViewModel));
                                }
                                else
                                {
                                    throw new EmployeeUpdateException("Null is returned after updating an employee. Status Code: " + response.StatusCode);
                                }
                            }
                            else
                            {
                                throw new EmployeeUpdateException("Employee profile could not be updated. Status Code: " + response.StatusCode);
                            }
                        }
                        else if (model.UserType == UserType.Admin)
                        {
                            // If FullName user claim successfully updated, attempt to update employee profile
                            var admin = Mapper.Map <UserEditProfileViewModel, Admin>(model);

                            response = await this.GetHttpClient().PutAsJsonAsync("Admin", admin);

                            if (response.IsSuccessStatusCode)
                            {
                                admin = await response.Content.ReadAsAsync <Admin>();

                                if (admin != null)
                                {
                                    var editUserConfirmationViewModel = Mapper.Map <Admin, EditUserConfirmationViewModel>(admin);

                                    return(PartialView("_EditUserConfirmation", editUserConfirmationViewModel));
                                }
                                else
                                {
                                    throw new EmployeeUpdateException("Null is returned after updating an employee. Status Code: " + response.StatusCode);
                                }
                            }
                            else
                            {
                                throw new EmployeeUpdateException("Employee profile could not be updated. Status Code: " + response.StatusCode);
                            }
                        }
                    }
                    else
                    {
                        var message = "User could not be updated.";

                        if (!Utilities.IsEmpty(result.Errors))
                        {
                            foreach (var error in result.Errors)
                            {
                                message += " " + error;
                            }
                        }

                        throw new UserUpdateException(message);
                    }
                }
                catch (UserUpdateException ex)
                {
                    // Log exception
                    ErrorHandlingUtilities.LogException(ErrorHandlingUtilities.GetExceptionDetails(ex));

                    var stringBuilder = new StringBuilder();

                    stringBuilder.Append("<div class='text-center'><h4><strong>User could NOT be updated.</strong></h4></div>");

                    stringBuilder.Append("<div class='row'><div class='col-md-12'><p></p></div><div class='col-md-offset-1 col-md-11'>An exception has been caught while attempting to update a user profile. Please review an exception log for more details about the exception.</div></div>");

                    var userConfirmationViewModel = new UserConfirmationViewModel(stringBuilder.ToString());

                    return(PartialView("_Error", userConfirmationViewModel));
                }
                catch (ClaimsAssignmentException ex)
                {
                    // Log exception
                    ErrorHandlingUtilities.LogException(ErrorHandlingUtilities.GetExceptionDetails(ex));

                    var stringBuilder = new StringBuilder();

                    stringBuilder.Append("<div class='text-center'><h4><strong>Claim could NOT be assigned to the user.</strong></h4></div>");

                    stringBuilder.Append("<div class='row'><div class='col-md-12'><p></p></div><div class='col-md-offset-1 col-md-11'>An exception has been caught while attempting to assign a user claim. Please review an exception log for more details about the exception.</div></div>");

                    var userConfirmationViewModel = new UserConfirmationViewModel(stringBuilder.ToString());

                    return(PartialView("_Error", userConfirmationViewModel));
                }
                catch (EmployeeUpdateException ex)
                {
                    // Log exception
                    ErrorHandlingUtilities.LogException(ErrorHandlingUtilities.GetExceptionDetails(ex));

                    var stringBuilder = new StringBuilder();

                    stringBuilder.Append("<div class='text-center'><h4><strong>Employee could NOT be updated.</strong></h4></div>");

                    stringBuilder.Append("<div class='row'><div class='col-md-12'><p></p></div><div class='col-md-offset-1 col-md-11'>An exception has been caught while attempting to update an employee profile. Please review an exception log for more details about the exception.</div></div>");

                    var userConfirmationViewModel = new UserConfirmationViewModel(stringBuilder.ToString());

                    return(PartialView("_Error", userConfirmationViewModel));
                }
            }

            return(PartialView("_EditUserProfile", model));
        }