/// <summary>
 /// Show ChangePassword screen
 /// </summary>
 /// <returns></returns>
 public ActionResult ChangePassword()
 {
     ChangePasswordModel model = new ChangePasswordModel();
     int userType = int.Parse(AuthorizationBusiness.GetUserParameter(UserName, "UserType"));
     model.UserName = UserName;
     model.UserType = userType;
     return View(model);
 }
        public ActionResult SaveChangePassword(ChangePasswordModel model)
        {
            if (string.IsNullOrEmpty(model.OldPassword))
                ModelState.AddModelError("OldPassRequired", Resources.Administration.OldPassRequired);
            if (model.OldPassword.Length < AuthorizationBusiness.Instance.MinPasswordLength)
                ModelState.AddModelError("", Resources.Administration.MinLegthPassword);

            if (string.IsNullOrEmpty(model.NewPassword))
                ModelState.AddModelError("NewPassRequired", Resources.Administration.NewPassRequired);
            if (model.NewPassword.Length < AuthorizationBusiness.Instance.MinPasswordLength)
                ModelState.AddModelError("", Resources.Administration.MinLegthPassword);

            if (string.IsNullOrEmpty(model.ReNewPassword))
                ModelState.AddModelError("ReNewPassWordRequired", Resources.Administration.ReNewPassWordRequired);
            if (model.ReNewPassword.Length < AuthorizationBusiness.Instance.MinPasswordLength)
                ModelState.AddModelError("", Resources.Administration.MinLegthPassword);

            if (model.NewPassword != model.ReNewPassword)
                ModelState.AddModelError("", Resources.Administration.NotEqualPassword);

            if (ModelState.IsValid)
            {
                bool result = AuthorizationBusiness.Instance.ChangePassword(model.UserName, model.OldPassword, model.ReNewPassword);

                if (result)
                {
                    List<BusinessApplicationByUser> businessAplicationsByUser = Session["BusinessAplicationsByUser"] as List<BusinessApplicationByUser>;
                    BusinessApplicationByUser applicationByUser = businessAplicationsByUser.FirstOrDefault();
                    if (applicationByUser != null)
                    {
                        int numRoles = Roles.GetRolesForUser(User.Identity.Name).Where(rol => rol.Contains("_" + applicationByUser.Prefix)).ToList().Count;
                        if (numRoles == 1 && (User.IsInRole("GlobalAdministrator") || (User.IsInRole("ApplicationAdministrator_" + applicationByUser.Prefix))))
                        {
                            return RedirectToAction("Index", "Catalogue");
                        }
                        else
                        {
                            return RedirectToAction("Index", "ServiceOrder");
                        }
                    }
                    else
                    {
                        return RedirectToAction("Index", "Catalogue");
                    }
                }
                else
                {
                    ModelState.AddModelError("", Resources.Administration.ChangePasswordError);
                    return View("ChangePassword", model);
                }

            }
            else
            {
                return View("ChangePassword", model);
            }
        }
        public ActionResult ChangePassword(string zi)
        {
            DateTime now = DateTime.Now;
            DateTime sentTime;
            string userName = "";
            try
            {
                //read the parameters from url
                string dec = EncryptionHelper.DecryptAes(zi);
                string[] separator = { "&&" };
                string[] data = dec.Split(separator, StringSplitOptions.RemoveEmptyEntries);
                //get the information
                userName = data[0];
                sentTime = new DateTime(Convert.ToInt32(data[1]), Convert.ToInt32(data[2]), Convert.ToInt32(data[3]), Convert.ToInt32(data[4]), Convert.ToInt32(data[5]), 0);

                //validate if is valid
                TimeSpan t = now - sentTime;
                if (t.TotalDays > 3)
                    return View("Expired");

                if (AuthorizationBusiness.VerifyResetPassword(sentTime, userName))
                    return View("Expired");

                //if all is ok, the system shows change password view
                ChangePasswordModel model = new ChangePasswordModel();
                model.UserName = userName;

                return View("ChangePassword", model);

            }
            catch (Exception)
            {
                return View("Unavailable");
            }
        }
        public ActionResult SaveChangePassword(ChangePasswordModel model)
        {
            //the system validates old password
            if (string.IsNullOrEmpty(model.OldPassword))
                ModelState.AddModelError("OldPassRequired", Resources.Administration.OldPassRequired);
            if (model.OldPassword.Length < AuthorizationBusiness.Instance.MinPasswordLength)
                ModelState.AddModelError("", Resources.Administration.MinLegthPassword);
            //The system validates new password
            if (string.IsNullOrEmpty(model.NewPassword))
                ModelState.AddModelError("NewPassRequired", Resources.Administration.NewPassRequired);
            if (model.NewPassword.Length < AuthorizationBusiness.Instance.MinPasswordLength)
                ModelState.AddModelError("", Resources.Administration.MinLegthPassword);
            //the system validates new password
            if (string.IsNullOrEmpty(model.ReNewPassword))
                ModelState.AddModelError("ReNewPassWordRequired", Resources.Administration.ReNewPassWordRequired);
            if (model.ReNewPassword.Length < AuthorizationBusiness.Instance.MinPasswordLength)
                ModelState.AddModelError("", Resources.Administration.MinLegthPassword);
            //the system validates if the new password and the ReNewPassword are equals
            if (model.NewPassword != model.ReNewPassword)
                ModelState.AddModelError("", Resources.Administration.NotEqualPassword);
            //validate the temporary password
            if (!AuthorizationBusiness.Instance.CompareTemporalPassword(model.UserName, model.OldPassword))
                ModelState.AddModelError("", "The temporary password is not correct");

            //if there are no errors, the system will continue with the process
            if (ModelState.IsValid)
            {
                //change the password.
                if (AuthorizationBusiness.Instance.ResetPassword(model.UserName, model.ReNewPassword))
                {
                    string userEmail = model.UserName;

                    //the system reads the template
                    string messageBody = System.IO.File.ReadAllText(Server.MapPath("~/Templates/PasswordChangedConfirmationTemplate.htm"));

                    //the system send an email to the user, with the necessary information for reset the password
                    EmailBusiness.SendEmail(userEmail, messageBody, Resources.Administration.ForgetPasswordSubjectConfirEmail, Settings.Default.EmailSupport, Settings.Default.NameEmailSupport);

                    return View("ChangePasswordConfirmation");
                }
                else
                {
                    ModelState.AddModelError("", Resources.Administration.ForgetPasswordGeneralError);
                    return View("ChangePassword", model);
                }
            }
            else
            {
                //if exist errors, the system will display the errors.
                return View("ChangePassword", model);
            }
        }