public ActionResult ActualResetPassword(DataModel model)
        {
            using (var e = new EntityContext())
            {
                var data = ActionData.GetAction <DataModel>(Guid.Parse(model.Guid), e);
                model.Email = data.Item1.Email;
                var actionRow = data.Item2;
                if (model == null || actionRow == null || actionRow.Investigator_Name == null)
                {
                    return(View(ResetPasswordErrorView));
                }

                // clears the errors from the model
                model.ClearToaster();
                // check for simple warnings
                var isValid = true;
                // makes sure we don't have any empty fields
                if (String.IsNullOrEmpty(model.Password) || String.IsNullOrEmpty(model.Email))
                {
                    model.AddError(GlobalErrors.EmptyFields);
                    isValid = false;
                }
                if (!CredentialsHelper.IsPasswordValid(model.Password)) // check password is valid
                {
                    model.AddError(RegistrationErrors.InvalidPassword);
                    isValid = false;
                }
                else // if password is valid get warnings
                {
                    model.AddWarnings(CredentialsHelper.GetPasswordWarnings(model.Password));
                }


                if (isValid)                                 // check for more serious warnings
                {
                    using (var e2 = new EntityContext())     // db context
                    {
                        if (isValid && !model.HasWarnings()) // we have checked everything we need to check
                        {
                            var success = Authorize.ResetPassword(model.Email, model.Password, e2);

                            e.Web_Action_Data.Remove(actionRow);
                            e.SaveChanges();
                            if (!success)
                            {
                                return(View(ResetPasswordErrorView));
                            }
                            else
                            {
                                return(View(ResetPasswordSuccessView));
                            }
                        }
                    }
                }
            }
            // if we got here there was an error
            return(View(ReceivedView, model));
        }
Ejemplo n.º 2
0
        public ActionResult LoginResetPassword(LoginResetPasswordModel model)
        {
            // clears the errors from the model
            model.ClearToaster();
            // check for simple warnings
            var isValid = true;

            // makes sure we don't have any empty fields
            if (String.IsNullOrEmpty(model.Password))
            {
                model.AddError(GlobalErrors.EmptyFields);
                isValid = false;
            }
            if (!CredentialsHelper.IsPasswordValid(model.Password)) // check password is valid
            {
                model.AddError(RegistrationErrors.InvalidPassword);
                isValid = false;
            }
            else // if password is valid get warnings
            {
                model.AddWarnings(CredentialsHelper.GetPasswordWarnings(model.Password));
            }


            if (isValid && !model.HasWarnings())
            {
                using (var e2 = new EntityContext()) // db context
                {
                    var currentUser = SessionHelper.GetSessionUser();
                    if (currentUser == null)
                    {
                        model.AddError(GlobalErrors.ServerError);
                        return(View(model));
                    }
                    var success = Authorize.ResetPassword(currentUser.Email, model.Password, e2);
                    var newUser = Authorize.CredentialsByEmail(currentUser.Email, model.Password, e2);
                    if (!success || newUser == null)
                    {
                        model.AddError(GlobalErrors.ServerError);
                        return(View(model));
                    }
                    else
                    {
                        //if username and password is correct, create session and return Success
                        SessionHelper.SetSessionUser(newUser);
                        FormsAuthentication.SetAuthCookie(newUser.Username, true);
                        model = new LoginResetPasswordModel();
                        model.AddSuccess(ResetPasswordSuccessEnum.PasswordReset);
                        return(View(model));
                    }
                }
            }
            // if we got here there was an error
            return(View(model));
        }
Ejemplo n.º 3
0
        public ActionResult Register(RegisterModel model)
        {
            // clears the errors from the model
            model.ClearToaster();
            // check for simple warnings
            var isValid = true;

            // makes sure we don't have any empty fields
            if (String.IsNullOrEmpty(model.Username) || String.IsNullOrEmpty(model.Password) || String.IsNullOrEmpty(model.Email))
            {
                model.AddError(GlobalErrors.EmptyFields);
                isValid = false;
            }
            if (!CredentialsHelper.IsEmailValid(model.Email)) // check email is valid
            {
                model.AddError(RegistrationErrors.InvalidEmail);
                isValid = false;
            }
            if (!CredentialsHelper.IsPasswordValid(model.Password)) // check password is valid
            {
                model.AddError(RegistrationErrors.InvalidPassword);
                isValid = false;
            }
            else // if password is valid get warnings
            {
                model.AddWarnings(CredentialsHelper.GetPasswordWarnings(model.Password));
            }

            if (!CredentialsHelper.IsUsernameValid(model.Username)) // check if username is valid
            {
                model.AddError(RegistrationErrors.InvalidUsername);
                isValid = false;
            }

            if (isValid)                            // check for more serious warnings
            {
                using (var e = new EntityContext()) // db context
                {
                    // check if email exists in the database, we need the email to register
                    if (!Authorize.EmailExists(model.Email, e))
                    {
                        model.AddError(RegistrationErrors.EmailNotAssociatedWithUser);
                        isValid = false;
                    }
                    else if (Authorize.EmailIsRegistered(model.Email, e)) // if it does check if it is already registered
                    {
                        model.AddError(RegistrationErrors.EmailAlreadyExists);
                        isValid = false;
                    }
                    else if (Authorize.UsernameIsRegistered(model.Username, e)) // check if the username is already registered
                    {
                        model.AddError(RegistrationErrors.UsernameAlreadyExists);
                        isValid = false;
                    }

                    if (isValid && !model.HasWarnings()) // we have checked everything we need to check
                    {
                        CachedUser cachedUser = Account.MakeNewUserLogin(model.Username, model.Email, model.Password, e);
                        if (cachedUser == null)
                        {
                            model.AddError(RegistrationErrors.UnknowError);
                        }
                        else
                        {
                            return(RedirectToAction("Send", "CompleteRegistration", new {
                                email = cachedUser.Email,
                                username = cachedUser.Username,
                                investigatorName = cachedUser.InvestigatorName
                            }));
                        }
                    }
                }
            }
            // if we got here there was an error
            return(View(model));
        }