Exemplo n.º 1
0
        public ActionResult ForgotPassword(ForgotPasswordUser forgotPasswordUser)
        {
            var appUserContactEmail = forgotPasswordUser.ContactEmail;
            var _userBusinessLogic  = new UserBusinessLogic();
            var invitationManager   = new InvitationManager();

            if (!string.IsNullOrEmpty(appUserContactEmail))
            {
                try
                {
                    var valid = Regex.IsMatch(appUserContactEmail, @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z", RegexOptions.IgnoreCase);
                    if (!valid)
                    {
                        TempData.Remove("EmailAddressError");
                        TempData.Add("EmailAddressError", "Please use a valid email address.");
                        return(View("ForgotPassword"));
                    }

                    var  currentUrl = Request.Url;
                    var  user       = _userBusinessLogic.GetByEmail(appUserContactEmail);
                    bool sent       = false;

                    if (user != null)
                    {
                        //Session["appUser"] = user.AppUser;
                        var applicationUser = user.AppUser;
                        sent = _userBusinessLogic.SendResetPasswordEmail(
                            applicationUser.ContactEmail, applicationUser.PrincipalId, currentUrl, applicationUser.Tenancy);
                        return(RedirectToAction(sent ? "ForgotEmailSendConfirmation" : "ForgotPassword"));
                    }

                    var systemOwner = _userBusinessLogic.GetSuperAdmin(appUserContactEmail);
                    if (systemOwner != null)
                    {
                        //Session["appUser"] = systemOwner;
                        sent = _userBusinessLogic.SendResetPasswordEmail(
                            systemOwner.ContactEmail, systemOwner.PrincipalId, currentUrl, systemOwner.Tenancy);
                        return(RedirectToAction(sent ? "ForgotEmailSendConfirmation" : "ForgotPassword"));
                    }
                }
                catch (Exception ex)
                {
                    TempData.Add("EmailAddressError", "The email was not sent, please contact the system administrator.");
                    var message = ExceptionHandler.Manage(ex, this, Layer.UILogic) ? ex.Message : "An unexpected error has ocurred.";
                    ModelState.AddModelError("ContactEmail", message);
                    return(View("ForgotPassword"));
                }
                TempData.Remove("EmailAddressError");
                TempData.Add("EmailAddressError", "The email address does not exist!.");
                ModelState.AddModelError("ContactEmail", "The email address does not exist!");
                return(View("ForgotPassword"));
            }
            TempData.Remove("EmailAddressError");
            TempData.Add("EmailAddressError", "Please the email address is required.");
            ModelState.AddModelError("ContactEmail", "Please the email address is required");
            return(View("ForgotPassword"));
        }