private DataSettings ParseSettings(string json) { if (string.IsNullOrEmpty(json)) return null; var rawSettings = JsonConvert.DeserializeObject<Dictionary<string, object>>(json); var settings = new DataSettings(); foreach (var setting in rawSettings) { switch (setting.Key) { case "ConnectionString": if (setting.Value != null) settings.ConnectionString = setting.Value.ToString(); break; case "InstallComplete": if (setting.Value != null) settings.InstallComplete = bool.Parse(setting.Value.ToString()); break; case "RawSettings": break; case "SiteOwner": if (setting.Value != null) settings.SiteOwner = JsonConvert.DeserializeObject<User>(setting.Value.ToString()); break; default: settings.RawSettings.Add(setting.Key, setting.Value); break; } } return settings; }
public void SaveSettings(DataSettings settings) { string directoryPath = HostingEnvironment.MapPath("~/App_Data"); if (!string.IsNullOrEmpty(directoryPath)) { string filePath = Path.Combine(directoryPath, SETTINGS_FILE_NAME); var json = JsonConvert.SerializeObject(settings); File.WriteAllText(filePath, json); } }
public ActionResult StepOne(StepOneModel model) { var manager = new DataSettingsManager(); model = PrepareStepOneModel(model); // Make sure if SQL Server authenticaton is selected, we have been given a username/password if (model.DatabaseAuthenticationMethod == 10) { if (string.IsNullOrEmpty(model.DatabaseUsername)) ModelState.AddModelError("DatabaseUsername", "Please enter your SQL username."); if (string.IsNullOrEmpty(model.DatabasePassword)) ModelState.AddModelError("DatabasePassword", "Please enter your SQL password."); } // Make sure we have the required folder permissions string root = Server.MapPath("~/"); var directories = new List<string> { Path.Combine(root, "robots.txt"), Path.Combine(root, "App_Data"), Path.Combine(root, "Uploads"), Path.Combine(root, "Uploads\\Media"), Path.Combine(root, "Uploads\\Profile") }; // Check each directory, flagging a modelstate error if the permissions aren't correct foreach (var directory in directories.Where(directory => !CheckPermissions(directory, 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, directory)); // Ensure the rest of the form is valid if (ModelState.IsValid) { try { // Build the connection string string connectionString = GenerateConnectionString(model.DatabaseAuthenticationMethod == 20, model.DatabaseServerName, model.DatabaseName, model.DatabaseUsername, model.DatabasePassword); // If the database doesn't exist, create it if (!DatabaseExists(connectionString)) { string databaseCreationError = CreateDatabase(connectionString); if (!string.IsNullOrEmpty(databaseCreationError)) throw new Exception(databaseCreationError); Thread.Sleep(3000); } // Create settings and save var settings = new DataSettings {ConnectionString = connectionString}; manager.SaveSettings(settings); // Initialise the database var dataProviderInstance = new EfDataProvider(); dataProviderInstance.InitDatabase(); // Install the core data var installService = EngineContext.Current.Resolve<IInstallService>(); bool coreDataInstalled = installService.InstallCoreData(); // Make sure the core data installed correctly before running on ahead if (!coreDataInstalled) ModelState.AddModelError("", "An error occurred during installation, please try again."); else return RedirectToAction("steptwo", "install"); } catch (Exception ex) { ModelState.AddModelError("", "Installation failed: " + ex); } } return View(model); }