/// <summary> /// The main entry point of the application - initializes database drivers, checks access rights, detects the current page type and does more setting accordingly. /// </summary> protected void Page_Init(object sender, EventArgs e) { DbServer = (DbServer)Enum.Parse(typeof(DbServer), System.Configuration.ConfigurationManager.AppSettings["ServerType"] as string); bool isFirstRun = System.Configuration.ConfigurationManager.AppSettings["FirstRun"] as string == "True"; // detect the site type based on the beginning of the URL string lp = Request.Url.LocalPath; if (lp.StartsWith("/architect")){ Common.Environment.GlobalState = GlobalState.Architect; } else if (lp.StartsWith("/admin")) { Common.Environment.GlobalState = GlobalState.Administer; } else if(lp == "/sys/users"){ Common.Environment.GlobalState = GlobalState.UsersManagement; } else if(lp == "/sys/projects"){ Common.Environment.GlobalState = GlobalState.ProjectsManagement; } else if(lp.StartsWith("/account")){ Common.Environment.GlobalState = GlobalState.Account; } else if(lp.StartsWith("/FirstRun")){ Common.Environment.GlobalState = GlobalState.FirstRun; } else Common.Environment.GlobalState = GlobalState.Error; bool firstRunMono = System.Configuration.ConfigurationManager.AppSettings["FirstRunMono"] == "True"; if(isFirstRun && Common.Environment.GlobalState != GlobalState.FirstRun && !firstRunMono) { Response.Redirect("~/FirstRun/FirstRun.aspx"); } if(!isFirstRun && Common.Environment.GlobalState == GlobalState.FirstRun) { Response.RedirectToRoute("DefaultRoute"); } // set the warning only for logged in users System.Configuration.ConfigurationManager.AppSettings["SessionWarning"] = (user is MembershipUser) ? (Session.Timeout - 5).ToString() : "-1"; if (isFirstRun) { return; } user = Membership.GetUser(); // session expiry means logout, even if the provider would keep the user logged in if ((Session.IsNewSession || user == null) && CE.GlobalState != GlobalState.Account && CE.GlobalState != GlobalState.Error) { FormsAuthentication.SignOut(); Response.RedirectToRoute("LockoutRoute", new { message = 7 }); } IBaseDriver systemBaseDriver = null; // initialize the system driver based on the server type read from the configuration switch (DbServer) { case DbServer.MySql: systemBaseDriver = new BaseDriverMySql(ConfigurationManager.ConnectionStrings["MySqlServer"].ConnectionString); break; case DbServer.MsSql: systemBaseDriver = new BaseDriverMsSql(ConfigurationManager.ConnectionStrings["MsSqlServer"].ConnectionString); break; default: break; } SysDriver = new SystemDriver(systemBaseDriver); if (firstRunMono && CE.GlobalState != GlobalState.FirstRun) { Response.Redirect("~/FirstRun/FirstRunMono.aspx"); } if (!firstRunMono && CE.GlobalState == GlobalState.FirstRun) { Response.RedirectToRoute("DefaultRoute"); } if (firstRunMono) { return; } // global service // is there a need for a reload of the project architecture? bool NewProjectLoad = false; // get current project and init drivers and architect if (Page.RouteData.Values.ContainsKey("projectName")) { ProjectName = Page.RouteData.Values["projectName"] as string; CE.Project actProject = SysDriver.GetProject(ProjectName); if (CE.project == null || actProject.Id != CE.project.Id || actProject.Version != CE.project.Version) { Session.Clear(); // may not be neccessary in all cases, but better be safe NewProjectLoad = true; } CE.project = SysDriver.GetProject(ProjectName); IBaseDriver statsBaseDriver = null; IBaseDriver webBaseDriver = null; switch (CE.project.ServerType) { case DbServer.MySql: statsBaseDriver = new BaseDriverMySql(CE.project.ConnstringIS); Stats = new StatsMySql((BaseDriverMySql)statsBaseDriver, CE.project.WebDbName); webBaseDriver = new BaseDriverMySql(CE._project.ConnstringWeb); break; case DbServer.MsSql: statsBaseDriver = new BaseDriverMsSql(CE.project.ConnstringIS); Stats = new StatsMsSql((BaseDriverMsSql)statsBaseDriver); webBaseDriver = new BaseDriverMsSql(CE._project.ConnstringWeb); break; default: break; } WebDriver = new WebDriver(webBaseDriver); Architect = new _min.Models.Architect(SysDriver, Stats); if ((!Page.IsPostBack || NewProjectLoad) && CE.GlobalState != GlobalState.Error) // new version or differnet page ~ othervise access must have remained // at least "allowable", if not allowed { LockingAccess(); // just check } // check whether there is something to load at all if (Page.RouteData.Route != RouteTable.Routes["ArchitectInitRoute"]) { if (!SysDriver.ProposalExists()) { if (CE.GlobalState == GlobalState.Architect) { Response.RedirectToRoute("ArchitectInitRoute", new { projectName = Page.RouteData.Values["projectName"] }); Response.End(); } else { // change to some kind of "Not found" page Response.RedirectToRoute("DefaultRoute", new { projectName = Page.RouteData.Values["projectName"] }); Response.End(); } } // get the current architecture - either extract from Session or directry from the DB, if project version has changed int actVersion = CE.project.Version; if (Session[CC.SESSION_ARCHITECTURE] is _min.Models.Panel && Session[CC.SESSION_ARCHITECTURE_VERSION] is int && (int)Session[CC.SESSION_ARCHITECTURE_VERSION] == actVersion) { SysDriver.SetArchitecture((MPanel)Session[CC.SESSION_ARCHITECTURE]); } else { SysDriver.FullProjectLoad(); Session[CC.SESSION_ARCHITECTURE] = SysDriver.MainPanel; Session[CC.SESSION_ARCHITECTURE_VERSION] = CE.project.Version; } } } // local issues if (!Page.IsPostBack) { if (user != null) { List<string> adminOf; List<string> architectOf; List<CE.Project> allProjects = SysDriver.GetProjectObjects(); List<string> allNames = (from CE.Project p in allProjects select p.Name).ToList<string>(); object userId = user.ProviderUserKey; int globalRights = SysDriver.GetUserRights(userId, null); // by default, fetch only the sites to which the access rights are set explicitly, // if global rights are sufficient, replace them with the complete lists SysDriver.UserMenuOptions(userId, out adminOf, out architectOf); if (globalRights % 100 >= 10) adminOf = allNames; if (globalRights % 1000 >= 100) architectOf = allNames; // decide on the upper menu content MenuItem administerItem = new MenuItem("Administer", "admin"); foreach (string site in adminOf) { administerItem.ChildItems.Add(new MenuItem(site, site, null, "/admin/" + site)); } if (adminOf.Count > 0) NavigationMenu.Items.AddAt(0, administerItem); // architect menu MenuItem architectItem = new MenuItem("Architect", "architect"); foreach (string site in architectOf) { architectItem.ChildItems.Add(new MenuItem(site, site, null, "/architect/show/" + Server.UrlEncode(site))); } if (architectOf.Count > 0) NavigationMenu.Items.AddAt(1, architectItem); // user & projects management NavigationMenu.Items.Add(new MenuItem("Manage users", "users", null, "/sys/users")); if (globalRights >= 10000) // this is the one and only project manager for this application instance NavigationMenu.Items.Add(new MenuItem("Manage projects", "projects", null, "/sys/projects")); // account settings for logged in users MenuItem accountItem = new MenuItem("Account", "account"); accountItem.ChildItems.Add(new MenuItem("Change password", null, null, "/account/change-password")); accountItem.ChildItems.Add(new MenuItem("Logout", null, null, "/account/logout")); NavigationMenu.Items.Add(accountItem); } else { MenuItem accountItem = new MenuItem("Account", "account"); accountItem.ChildItems.Add(new MenuItem("Login", null, null, "/account/login")); accountItem.ChildItems.Add(new MenuItem("Register", null, null, "/account/register")); NavigationMenu.Items.Add(accountItem); } NavigationMenu.RenderingMode = MenuRenderingMode.Table; } }
protected void SaveButton_Click(object sender, EventArgs e) { Errors.Items.Clear(); string serverType = ServerTypeDrop.SelectedValue; DbServer serverTypeParsed; if (!Enum.TryParse<DbServer>(serverType, out serverTypeParsed)) { Errors.Items.Add("Please, choose the type of database engine you wish to use."); return; } // initial testing of the database connection before we attempt to create the main schema IBaseDriver drv = null; switch (serverTypeParsed) { case DbServer.MySql: drv = new BaseDriverMySql(SystemConnstringTextBox.Text); break; case DbServer.MsSql: drv = new BaseDriverMsSql(SystemConnstringTextBox.Text); break; } try { drv.TestConnection(); drv.TestDatabaseIsEmpty(); } catch (Exception ex) { Errors.Items.Add(ex.Message); return; } if (UsernameTextBox.Text == "") { Errors.Items.Add("Please, insert the initial user's name"); return; } if (PasswordTextBox.Text.Length < 7) { Errors.Items.Add("The password must be at least 7 characters long."); return; } if (PasswordTextBox.Text != RetypePasswordTextBox.Text) { Errors.Items.Add("The passwords do not match."); return; } try { System.Net.Mail.MailAddress address = new System.Net.Mail.MailAddress(MailTextBox.Text); } catch (FormatException fe) { Errors.Items.Add(fe.Message); return; } // run the schema dump script switch (serverTypeParsed) { case DbServer.MySql: MySql.Data.MySqlClient.MySqlConnection connection = new MySql.Data.MySqlClient.MySqlConnection(SystemConnstringTextBox.Text); try { MySql.Data.MySqlClient.MySqlScript script = new MySql.Data.MySqlClient.MySqlScript(connection); string scriptText = File.ReadAllText(HttpContext.Current.Server.MapPath(CC.MYSQL_SCHEMA_FILE_PATH)); script.Query = scriptText; script.Query = scriptText; connection.Open(); script.Execute(); connection.Clone(); } catch (Exception esql1) { Errors.Items.Add(esql1.Message); connection.Close(); return; } break; case DbServer.MsSql: SqlConnection conn = new SqlConnection(SystemConnstringTextBox.Text); try { string query = File.ReadAllText(HttpContext.Current.Server.MapPath(CC.MSSQL_SCHEMA_FILE_PATH)); Microsoft.SqlServer.Management.Smo.Server sqlServer = new Server(new ServerConnection(conn)); conn.Open(); sqlServer.ConnectionContext.ExecuteNonQuery(query); conn.Close(); SqlMembershipProvider mssqlProvider = new SqlMembershipProvider(); } catch (Exception esql2) { Errors.Items.Add(esql2.Message); conn.Close(); return; } break; } var configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~"); var section = (ConnectionStringsSection)configuration.GetSection("connectionStrings"); System.Web.Security.MembershipProvider membership = null; string username = UsernameTextBox.Text; string password = PasswordTextBox.Text; string mail = MailTextBox.Text; MembershipCreateStatus status; // rewrite the connection in the database and reload the connstring section, also set the defaultProvidder for the membership tag switch (serverTypeParsed) { case DbServer.MySql: section.ConnectionStrings["MySqlServer"].ConnectionString = SystemConnstringTextBox.Text; configuration.AppSettings.Settings["ServerType"].Value = "MySql"; configuration.Save(); SetDefaultMembershipProvider("MySqlMembershipProvider"); // remove the readonly attribute of the connection string variable of the connfiguration var settingsMy = ConfigurationManager.ConnectionStrings["MsSqlServer"]; var fiMy = typeof( ConfigurationElement ).GetField( "_bReadOnly", BindingFlags.Instance | BindingFlags.NonPublic ); fiMy.SetValue(settingsMy, false); settingsMy.ConnectionString = SystemConnstringTextBox.Text; membership = Membership.Providers["MySqlMembershipProvider"]; membership.CreateUser(username, password, mail, "Dummy question", "Dummy answer", true, 1, out status); break; case DbServer.MsSql: section.ConnectionStrings["MsSqlServer"].ConnectionString = SystemConnstringTextBox.Text; configuration.AppSettings.Settings["ServerType"].Value = "MsSql"; configuration.Save(); SetDefaultMembershipProvider("MsSqlMembershipProvider"); // remove the readonly attribute of the connection string variable of the connfiguration var settings = ConfigurationManager.ConnectionStrings["MsSqlServer"]; var fi = typeof( ConfigurationElement ).GetField( "_bReadOnly", BindingFlags.Instance | BindingFlags.NonPublic ); fi.SetValue(settings, false); settings.ConnectionString = SystemConnstringTextBox.Text; membership = Membership.Providers["MsSqlMembershipProvider"]; // generate a ProviderUserKey Random rand = new Random(); Guid key = new Guid(rand.Next(), 2, 3, new byte[] { 10, 20, 30, 40, 50, 60, 70, 80 }); ((SqlMembershipProvider)membership).CreateUser(username, password, mail, "Dummy question", "Dummy answer", true, key, out status); break; } int totalUsers; MembershipUser user = membership.FindUsersByName(username, 0, 1, out totalUsers)[username]; SystemDriver sysDriver = new SystemDriver(drv); sysDriver.SetUserRights((user.ProviderUserKey), null, 11110); // Set FirstRun to false. This cannot be done by the first configuration object - it wil // not like the configuration file since it has been modified by SetDefaultMembershipProvider // in the meantime. var config2 = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~"); config2.AppSettings.Settings["FirstRun"].Value = "False"; System.Web.Configuration.WebConfigurationManager.AppSettings["FirstRun"] = "False"; config2.Save(); Errors.Items.Add("Done."); Response.RedirectToRoute("DefaultRoute"); }