public ActionResult Index() { if (DataSettingsHelper.DatabaseIsInstalled()) return RedirectToAction("Index", "Home"); //set page timeout to 5 minutes this.Server.ScriptTimeout = 300; var model = new InstallModel() { AdminEmail = "*****@*****.**", //AdminPassword = "******", //ConfirmPassword = "******", InstallSampleData = true, DatabaseConnectionString = "", DataProvider = "sqlserver", SqlAuthenticationType = "sqlauthentication", SqlConnectionInfo = "sqlconnectioninfo_values", SqlServerCreateDatabase = false, }; return View(model); }
public ActionResult Index(InstallModel model) { if (DataSettingsHelper.DatabaseIsInstalled()) return RedirectToAction("Index", "Home"); //set page timeout to 5 minutes this.Server.ScriptTimeout = 300; if (model.DatabaseConnectionString != null) model.DatabaseConnectionString = model.DatabaseConnectionString.Trim(); //SQL Server if (model.DataProvider.Equals("sqlserver", StringComparison.InvariantCultureIgnoreCase)) { if (model.SqlConnectionInfo.Equals("sqlconnectioninfo_raw", StringComparison.InvariantCultureIgnoreCase)) { //raw connection string if (string.IsNullOrEmpty(model.DatabaseConnectionString)) ModelState.AddModelError("", "A SQL connection string is required"); try { //try to create connection string new SqlConnectionStringBuilder(model.DatabaseConnectionString); } catch { ModelState.AddModelError("", "Wrong SQL connection string format"); } } else { //values if (string.IsNullOrEmpty(model.SqlServerName)) ModelState.AddModelError("", "SQL Server name is required"); if (string.IsNullOrEmpty(model.SqlDatabaseName)) ModelState.AddModelError("", "Database name is required"); //authentication type if (model.SqlAuthenticationType.Equals("sqlauthentication", StringComparison.InvariantCultureIgnoreCase)) { //SQL authentication if (string.IsNullOrEmpty(model.SqlServerUsername)) ModelState.AddModelError("", "SQL Username is required"); if (string.IsNullOrEmpty(model.SqlServerPassword)) ModelState.AddModelError("", "SQL Password is required"); } } } //Consider granting access rights to the resource to the ASP.NET request identity. //ASP.NET has a base process identity //(typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6 and IIS 7, //and the configured application pool identity on IIS 7.5) that is used if the application is not impersonating. //If the application is impersonating via <identity impersonate="true"/>, //the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user. //validate permissions string rootDir = Server.MapPath("~/"); var dirsToCheck = new List<string>(); //dirsToCheck.Add(rootDir); dirsToCheck.Add(rootDir + "App_Data"); dirsToCheck.Add(rootDir + "bin"); dirsToCheck.Add(rootDir + "content"); dirsToCheck.Add(rootDir + "content\\images"); dirsToCheck.Add(rootDir + "content\\images\\thumbs"); dirsToCheck.Add(rootDir + "content\\files\\exportimport"); dirsToCheck.Add(rootDir + "plugins"); dirsToCheck.Add(rootDir + "plugins\\bin"); foreach (string dir in dirsToCheck) if (!checkPermissions(dir, false, true, true, true)) ModelState.AddModelError("", string.Format("The '{0}' account is not granted with Modify permission on folder '{1}'. Please configure these permissions.", WindowsIdentity.GetCurrent().Name, dir)); var filesToCheck = new List<string>(); filesToCheck.Add(rootDir + "Global.asax"); filesToCheck.Add(rootDir + "web.config"); filesToCheck.Add(rootDir + "App_Data\\InstalledPlugins.txt"); filesToCheck.Add(rootDir + "App_Data\\Settings.txt"); foreach (string file in filesToCheck) if (!checkPermissions(file, false, true, true, true)) ModelState.AddModelError("", string.Format("The '{0}' account is not granted with Modify permission on file '{1}'. Please configure these permissions.", WindowsIdentity.GetCurrent().Name, file)); if (ModelState.IsValid) { var settingsManager = new DataSettingsManager(); try { string connectionString = null; if (model.DataProvider.Equals("sqlserver", StringComparison.InvariantCultureIgnoreCase)) { //SQL Server if (model.SqlConnectionInfo.Equals("sqlconnectioninfo_raw", StringComparison.InvariantCultureIgnoreCase)) { //raw connection string connectionString = model.DatabaseConnectionString; } else { //values connectionString = createConnectionString(model.SqlAuthenticationType == "windowsauthentication", model.SqlServerName, model.SqlDatabaseName, model.SqlServerUsername, model.SqlServerPassword); } if (model.SqlServerCreateDatabase) { if (!sqlServerDatabaseExists(connectionString)) { //create database var errorCreatingDatabase = createDatabase(connectionString); if (!String.IsNullOrEmpty(errorCreatingDatabase)) throw new Exception(errorCreatingDatabase); else { //Database cannot be created sometimes. Weird! Seems to be Entity Framework issue //that's just wait 3 seconds Thread.Sleep(3000); } } } else { //check whether database exists if (!sqlServerDatabaseExists(connectionString)) throw new Exception("Database does not exist or you don't have permissions to connect to it"); } } else { //SQL CE string databaseFileName = "AgileEAP.Db.sdf"; string databasePath = @"|DataDirectory|\" + databaseFileName; connectionString = "Data Source=" + databasePath + ";Persist Security Info=False"; //drop database if exists string databaseFullPath = HostingEnvironment.MapPath("~/App_Data/") + databaseFileName; if (System.IO.File.Exists(databaseFullPath)) { System.IO.File.Delete(databaseFullPath); } } //save settings var dataProvider = model.DataProvider; var settings = new DataSettings() { DataProvider = dataProvider, DataConnectionString = connectionString }; settingsManager.SaveSettings(settings); //init data provider var dataProviderInstance = EngineContext.Current.Resolve<BaseDataProviderManager>().LoadDataProvider(); dataProviderInstance.InitDatabase(); //now resolve installation service //var installationService = EngineContext.Current.Resolve<IInstallationService>(); //installationService.InstallData(model.AdminEmail, model.AdminPassword, model.InstallSampleData); //reset cache DataSettingsHelper.ResetCache(); //install plugins PluginManager.MarkAllPluginsAsUninstalled(); var pluginFinder = EngineContext.Current.Resolve<IPluginFinder>(); var plugins = pluginFinder.GetPlugins<IPluginInstaller>(false) .ToList() .OrderBy(x => x.PluginDescriptor.Group) .ThenBy(x => x.PluginDescriptor.DisplayOrder) .ToList(); foreach (var plugin in plugins) { plugin.Install(); } //register default permissions //var permissionProviders = EngineContext.Current.Resolve<ITypeFinder>().FindClassesOfType<IPermissionProvider>(); var permissionProviders = new List<Type>(); //permissionProviders.Add(typeof(StandardPermissionProvider)); //foreach (var providerType in permissionProviders) //{ // dynamic provider = Activator.CreateInstance(providerType); // EngineContext.Current.Resolve<IPermissionService>().InstallPermissions(provider); //} //restart application var webHelper = EngineContext.Current.Resolve<IWebHelper>(); webHelper.RestartAppDomain(); //Redirect to home page return RedirectToAction("Index", "Home"); } catch (Exception exception) { //reset cache DataSettingsHelper.ResetCache(); //clear provider settings if something got wrong settingsManager.SaveSettings(new DataSettings { DataProvider = null, DataConnectionString = null }); ModelState.AddModelError("", "Setup failed: " + exception); } } return View(model); }