예제 #1
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));
        }