Beispiel #1
0
        public ActionResult IndexPOST(SetupViewModel model)
        {
            // Sets the setup request timeout to a configurable amount of seconds to give enough time to execute custom recipes.
            HttpContext.Server.ScriptTimeout = RecipeExecutionTimeout;

            var recipes = _setupService.Recipes().ToList();

            // If no builtin provider, a connection string is mandatory.
            if (model.DatabaseProvider != SetupDatabaseType.Builtin && string.IsNullOrEmpty(model.DatabaseConnectionString))
            {
                ModelState.AddModelError("DatabaseConnectionString", T("A connection string is required.").Text);
            }

            if (!String.IsNullOrWhiteSpace(model.ConfirmPassword) && model.AdminPassword != model.ConfirmPassword)
            {
                ModelState.AddModelError("ConfirmPassword", T("Password confirmation must match.").Text);
            }

            if (model.DatabaseProvider != SetupDatabaseType.Builtin && !string.IsNullOrWhiteSpace(model.DatabaseTablePrefix))
            {
                model.DatabaseTablePrefix = model.DatabaseTablePrefix.Trim();
                if (!Char.IsLetter(model.DatabaseTablePrefix[0]))
                {
                    ModelState.AddModelError("DatabaseTablePrefix", T("The table prefix must begin with a letter.").Text);
                }

                if (model.DatabaseTablePrefix.Any(x => !Char.IsLetterOrDigit(x)))
                {
                    ModelState.AddModelError("DatabaseTablePrefix", T("The table prefix must contain letters or digits.").Text);
                }
            }
            if (model.Recipe == null)
            {
                if (!(recipes.Select(r => r.Name).Contains(DefaultRecipe)))
                {
                    ModelState.AddModelError("Recipe", T("No recipes were found.").Text);
                }
                else
                {
                    model.Recipe = DefaultRecipe;
                }
            }
            if (!ModelState.IsValid)
            {
                model.Recipes = recipes;
                foreach (var recipe in recipes.Where(recipe => recipe.Name == model.Recipe))
                {
                    model.RecipeDescription = recipe.Description;
                }
                model.DatabaseIsPreconfigured = !String.IsNullOrEmpty(_setupService.Prime().DataProvider);

                return(IndexViewResult(model));
            }

            try {
                string providerName = null;

                switch (model.DatabaseProvider)
                {
                case SetupDatabaseType.Builtin:
                    providerName = "SqlCe";
                    break;

                case SetupDatabaseType.SqlServer:
                    providerName = "SqlServer";
                    break;

                case SetupDatabaseType.MySql:
                    providerName = "MySql";
                    break;

                case SetupDatabaseType.PostgreSql:
                    providerName = "PostgreSql";
                    break;

                default:
                    throw new ApplicationException("Unknown database type: " + model.DatabaseProvider);
                }

                var recipe       = recipes.GetRecipeByName(model.Recipe);
                var setupContext = new SetupContext {
                    SiteName                 = model.SiteName,
                    AdminUsername            = model.AdminUsername,
                    AdminPassword            = model.AdminPassword,
                    DatabaseProvider         = providerName,
                    DatabaseConnectionString = model.DatabaseConnectionString,
                    DatabaseTablePrefix      = model.DatabaseTablePrefix,
                    EnabledFeatures          = null, // Default list
                    Recipe = recipe
                };

                var executionId = _setupService.Setup(setupContext);

                // First time installation if finally done. Tell the background views compilation
                // process to stop, so that it doesn't interfere with the user (asp.net compilation
                // uses a "single lock" mechanism for compiling views).
                _viewsBackgroundCompilation.Stop();

                // Redirect to the welcome page.
                return(Redirect("~/" + _shellSettings.RequestUrlPrefix));
            }
            catch (Exception ex) {
                Logger.Error(ex, "Setup failed");
                _notifier.Error(T("Setup failed: {0}", ex.Message));

                model.Recipes = recipes;
                foreach (var recipe in recipes.Where(recipe => recipe.Name == model.Recipe))
                {
                    model.RecipeDescription = recipe.Description;
                }
                model.DatabaseIsPreconfigured = !String.IsNullOrEmpty(_setupService.Prime().DataProvider);

                return(IndexViewResult(model));
            }
        }
Beispiel #2
0
        public ActionResult IndexPOST(SetupViewModel model)
        {
            // Sets the setup request timeout to a configurable amount of seconds to give enough time to execute custom recipes.
            HttpContext.Server.ScriptTimeout = RecipeExecutionTimeout;

            var recipes = _setupService.Recipes().ToList();

            // If no builtin provider, a connection string is mandatory.
            if (model.DatabaseProvider != SetupDatabaseType.Builtin && string.IsNullOrEmpty(model.DatabaseConnectionString))
            {
                return(Json(new { State = 0, Msg = T("数据库连接字符串未填.").Text }));
            }

            if (!String.IsNullOrWhiteSpace(model.ConfirmPassword) && model.AdminPassword != model.ConfirmPassword)
            {
                return(Json(new { State = 0, Msg = T("密码不一致.").Text }));
            }

            if (model.DatabaseProvider != SetupDatabaseType.Builtin && !string.IsNullOrWhiteSpace(model.DatabaseTablePrefix))
            {
                model.DatabaseTablePrefix = model.DatabaseTablePrefix.Trim();
                if (!Char.IsLetter(model.DatabaseTablePrefix[0]))
                {
                    return(Json(new { State = 0, Msg = T("表前缀必须以字母开头.").Text }));
                }

                if (model.DatabaseTablePrefix.Any(x => !Char.IsLetterOrDigit(x)))
                {
                    return(Json(new { State = 0, Msg = T("表前缀必须包含字母或数字.").Text }));
                }
            }
            if (model.Recipe == null)
            {
                if (!(recipes.Select(r => r.Name).Contains(DefaultRecipe)))
                {
                    return(Json(new { State = 0, Msg = T("安装模式不存在.").Text }));
                }
                else
                {
                    model.Recipe = DefaultRecipe;
                }
            }
            if (!ModelState.IsValid)
            {
                return(Json(new { State = 0, Msg = T("验证失败.").Text }));
            }

            try
            {
                string providerName = null;

                switch (model.DatabaseProvider)
                {
                case SetupDatabaseType.Builtin:
                    providerName = "SqlCe";
                    break;

                case SetupDatabaseType.SqlServer:
                    providerName = "SqlServer";
                    break;

                case SetupDatabaseType.MySql:
                    providerName = "MySql";
                    break;

                case SetupDatabaseType.PostgreSql:
                    providerName = "PostgreSql";
                    break;

                default:
                    throw new ApplicationException("Unknown database type: " + model.DatabaseProvider);
                }

                var recipe       = recipes.GetRecipeByName(model.Recipe);
                var setupContext = new SetupContext
                {
                    SiteName                 = model.SiteName,
                    AdminUsername            = model.AdminUsername,
                    AdminPassword            = model.AdminPassword,
                    DatabaseProvider         = providerName,
                    DatabaseConnectionString = model.DatabaseConnectionString,
                    DatabaseTablePrefix      = model.DatabaseTablePrefix,
                    EnabledFeatures          = null, // Default list
                    Recipe = recipe
                };

                var executionId = _setupService.Setup(setupContext);

                // First time installation if finally done. Tell the background views compilation
                // process to stop, so that it doesn't interfere with the user (asp.net compilation
                // uses a "single lock" mechanism for compiling views).
                _viewsBackgroundCompilation.Stop();

                // Redirect to the welcome page.
                return(Json(new { State = 1, Msg = "/" + _shellSettings.RequestUrlPrefix }));
            }
            catch (Exception ex)
            {
                Logger.Error(ex, "Setup failed");
                //_notifier.Error(T("安装失败: {0}", ex.Message));
                return(Json(new { State = 0, Msg = T("安装失败:" + ex.Message).Text }));
            }
        }
        public ActionResult IndexPOST(SetupViewModel model)
        {
            var recipes = OrderRecipes(_setupService.Recipes());

            //TODO: Couldn't get a custom ValidationAttribute to validate two properties
            if (!model.DatabaseOptions && string.IsNullOrEmpty(model.DatabaseConnectionString))
            {
                ModelState.AddModelError("DatabaseConnectionString", T("A SQL connection string is required").Text);
            }

            if (!String.IsNullOrWhiteSpace(model.ConfirmPassword) && model.AdminPassword != model.ConfirmPassword)
            {
                ModelState.AddModelError("ConfirmPassword", T("Password confirmation must match").Text);
            }

            if (!model.DatabaseOptions && !String.IsNullOrWhiteSpace(model.DatabaseTablePrefix))
            {
                model.DatabaseTablePrefix = model.DatabaseTablePrefix.Trim();
                if (!Char.IsLetter(model.DatabaseTablePrefix[0]))
                {
                    ModelState.AddModelError("DatabaseTablePrefix", T("The table prefix must begin with a letter").Text);
                }
            }
            if (model.Recipe == null)
            {
                if (!(recipes.Select(r => r.Name).Contains(DefaultRecipe)))
                {
                    ModelState.AddModelError("Recipe", T("No recipes were found in the Setup module").Text);
                }
                else
                {
                    model.Recipe = DefaultRecipe;
                }
            }
            if (!ModelState.IsValid)
            {
                model.Recipes = recipes;
                foreach (var recipe in recipes.Where(recipe => recipe.Name == model.Recipe))
                {
                    model.RecipeDescription = recipe.Description;
                }
                model.DatabaseIsPreconfigured = !string.IsNullOrEmpty(_setupService.Prime().DataProvider);

                return(IndexViewResult(model));
            }

            try {
                string providerName = null;

                if (model.DatabaseOptions)
                {
                    providerName = "SqlCe";
                }
                else
                {
                    switch (model.DatabaseType)
                    {
                    case SetupDatabaseType.SqlServer:
                        providerName = "SqlServer";
                        break;

                    case SetupDatabaseType.MySQL:
                        providerName = "MySql";
                        break;

                    default:
                        throw new ApplicationException("Unknown database type: " + model.DatabaseType);
                    }
                }

                var setupContext = new SetupContext {
                    SiteName                 = model.SiteName,
                    AdminUsername            = model.AdminUsername,
                    AdminPassword            = model.AdminPassword,
                    DatabaseProvider         = providerName,
                    DatabaseConnectionString = model.DatabaseConnectionString,
                    DatabaseTablePrefix      = model.DatabaseTablePrefix,
                    EnabledFeatures          = null, // default list
                    Recipe = model.Recipe
                };

                string executionId = _setupService.Setup(setupContext);

                // First time installation if finally done. Tell the background views compilation
                // process to stop, so that it doesn't interfere with the user (asp.net compilation
                // uses a "single lock" mechanism for compiling views).
                _viewsBackgroundCompilation.Stop();

                // redirect to the welcome page.
                return(Redirect("~/"));
            } catch (Exception ex) {
                Logger.Error(ex, "Setup failed");
                _notifier.Error(T("Setup failed: {0}", ex.Message));

                model.Recipes = recipes;
                foreach (var recipe in recipes.Where(recipe => recipe.Name == model.Recipe))
                {
                    model.RecipeDescription = recipe.Description;
                }
                model.DatabaseIsPreconfigured = !string.IsNullOrEmpty(_setupService.Prime().DataProvider);

                return(IndexViewResult(model));
            }
        }