// Load config data from EF DB. public override void Load() { if (_connectionString.IsNullOrEmpty() || _connectionString == "...") { var seedData = ConfigurationSeed.Data; Data = seedData.ToDictionary( item => item.Id, item => item.Value); } else { var contextFactory = new BaseDbContextFactory(); using (var dbContext = contextFactory.CreateDbContext(new[] { _connectionString })) { if (dbContext.Database.GetPendingMigrations().Any()) { Log.LogEvent("Migrating Angular DB"); dbContext.Database.Migrate(); } Data = dbContext.ConfigurationValues .AsNoTracking() .ToDictionary(c => c.Id, c => c.Value); } } }
private ICollection <PluginPermissionModel> GetPluginsPermissions() { var permissions = new List <PluginPermissionModel>(); if (Configuration.MyConnectionString() != "...") { var contextFactory = new BaseDbContextFactory(); using (var dbContext = contextFactory.CreateDbContext(new[] { Configuration.MyConnectionString() })) { foreach (var eformPlugin in dbContext.EformPlugins .AsNoTracking() .Where(x => x.ConnectionString != "...")) { var plugin = Program.EnabledPlugins.FirstOrDefault(p => p.PluginId == eformPlugin.PluginId); if (plugin != null) { var permissionsManager = plugin.GetPermissionsManager(eformPlugin.ConnectionString); permissions.AddRange(permissionsManager.GetPluginPermissions().Result); } } } } return(permissions); }
public static void AddEFormPluginsDbContext(this IServiceCollection services, IConfiguration configuration, List <IEformPlugin> plugins) { var connectionString = configuration.MyConnectionString(); if (!connectionString.IsNullOrEmpty() && connectionString != "...") { List <EformPlugin> eformPlugins; var contextFactory = new BaseDbContextFactory(); using (var dbContext = contextFactory.CreateDbContext(new[] { configuration.MyConnectionString() })) { eformPlugins = dbContext.EformPlugins .AsNoTracking() .Where(x => x.ConnectionString != "...") .ToList(); } foreach (var plugin in plugins) { var eformPlugin = eformPlugins.FirstOrDefault(x => x.PluginId == plugin.PluginId); if (eformPlugin?.ConnectionString != null) { plugin.ConfigureDbContext(services, eformPlugin.ConnectionString); SetAdminGroupPluginPermissions(plugin, eformPlugin.ConnectionString); } } } }
private static IWebHost BuildWebHost(string[] args) { var defaultConfig = new ConfigurationBuilder() .AddCommandLine(args) .AddEnvironmentVariables(prefix: "ASPNETCORE_") .Build(); var port = defaultConfig.GetValue("port", 5000); var connectionString = defaultConfig.GetValue("ConnectionString", ""); return(WebHost.CreateDefaultBuilder(args) .ConfigureKestrel(serverOptions => { serverOptions.Limits.MaxRequestBodySize = 100 * 1024 * 1024; // 100Mb }) .UseUrls($"http://0.0.0.0:{port}") // .UseIISIntegration() .ConfigureAppConfiguration((hostContext, config) => { Log.LogEvent("Delete all default configuration providers"); // delete all default configuration providers config.Sources.Clear(); config.SetBasePath(hostContext.HostingEnvironment.ContentRootPath); var filePath = Path.Combine(hostContext.HostingEnvironment.ContentRootPath, "connection.json"); if (!File.Exists(filePath)) { ConnectionStringManager.CreateDefault(filePath); } if (!string.IsNullOrEmpty(connectionString)) { ConnectionStringManager.CreateWithConnectionString(filePath, connectionString); } config.AddJsonFile("connection.json", optional: true, reloadOnChange: true); var mainSettings = ConnectionStringManager.Read(filePath); _defaultConnectionString = mainSettings?.ConnectionStrings?.DefaultConnection; config.AddEfConfiguration(_defaultConnectionString); EnabledPlugins = PluginHelper.GetPlugins(_defaultConnectionString); DisabledPlugins = PluginHelper.GetDisablePlugins(_defaultConnectionString); var contextFactory = new BaseDbContextFactory(); if (_defaultConnectionString != "...") { using var dbContext = contextFactory.CreateDbContext(new[] { _defaultConnectionString }); foreach (var plugin in EnabledPlugins) { var pluginEntity = dbContext.EformPlugins .FirstOrDefault(x => x.PluginId == plugin.PluginId); if (pluginEntity != null && !string.IsNullOrEmpty(pluginEntity.ConnectionString)) { plugin.AddPluginConfig(config, pluginEntity.ConnectionString); } } } config.AddEnvironmentVariables(); }) .UseStartup <Startup>() .Build()); }
private static async Task InitializeSettings(IWebHost webHost, string[] args) { using var scope = webHost.Services.GetService <IServiceScopeFactory>().CreateScope(); var settingsService = scope.ServiceProvider.GetRequiredService <ISettingsService>(); var existsResult = settingsService.ConnectionStringExist(); if (!existsResult.Success)// do need to initialize database { // Find file var filePath = Path.Combine(Directory.GetCurrentDirectory(), "init.json"); if (File.Exists(filePath)) { Log.LogEvent($"Try initialize from {filePath}"); // Get content var startupContent = await File.ReadAllTextAsync(filePath); var startup = JsonConvert.DeserializeObject <StartupInitializeModel>(startupContent); // Apply settings var updateConnectionResult = await settingsService.UpdateConnectionString(startup.InitialSettings); if (!updateConnectionResult.Success) { throw new Exception("Init error: " + updateConnectionResult.Message); } var adminSettingsUpdateModel = new AdminSettingsModel { S3SettingsModel = startup.S3SettingsModel, SMTPSettingsModel = startup.SMTPSettingsModel, SdkSettingsModel = startup.SdkSettingsModel, SendGridSettingsModel = startup.SendGridSettingsModel, SwiftSettingsModel = startup.SwiftSettingsModel, }; var updateAdminSettingsResult = await settingsService.UpdateAdminSettings(adminSettingsUpdateModel); if (!updateAdminSettingsResult.Success) { throw new Exception("Init error: " + updateAdminSettingsResult.Message); } EnabledPlugins = PluginHelper.GetPlugins(_defaultConnectionString); DisabledPlugins = PluginHelper.GetDisablePlugins(_defaultConnectionString); // Enable plugins foreach (var pluginId in startup.PluginsList) { var pluginObject = DisabledPlugins.FirstOrDefault(x => x.PluginId == pluginId); if (pluginObject != null) { var contextFactory = new BaseDbContextFactory(); await using var dbContext = contextFactory.CreateDbContext(new[] { _defaultConnectionString }); var eformPlugin = await dbContext.EformPlugins .Where(x => x.Status == (int)PluginStatus.Disabled) .FirstOrDefaultAsync(x => x.PluginId == pluginObject.PluginId); if (eformPlugin != null) { eformPlugin.Status = (int)PluginStatus.Enabled; dbContext.EformPlugins.Update(eformPlugin); await dbContext.SaveChangesAsync(); var pluginMenu = pluginObject.GetNavigationMenu(scope.ServiceProvider); // Load to database all navigation menu from plugin by id var pluginMenuItemsLoader = new PluginMenuItemsLoader(dbContext, pluginId); pluginMenuItemsLoader.Load(pluginMenu); } } } // not need because settingsService.UpdateAdminSettings call restart // Restart(); // restart IF some plugins has been enabled } else if (args.Any()) { Log.LogEvent("Try initialize from args"); var defaultConfig = new ConfigurationBuilder() .AddCommandLine(args) .AddEnvironmentVariables(prefix: "ASPNETCORE_") .Build(); var firstName = defaultConfig.GetValue("FirstName", ""); var lastName = defaultConfig.GetValue("LastName", ""); var email = defaultConfig.GetValue("Email", ""); var password = defaultConfig.GetValue("Password", ""); var token = defaultConfig.GetValue("Token", ""); if (!string.IsNullOrEmpty(token) && !string.IsNullOrEmpty(firstName) && !string.IsNullOrEmpty(lastName) && !string.IsNullOrEmpty(email) && !string.IsNullOrEmpty(password)) { var sdkConnectionString = _defaultConnectionString.Replace("_Angular", "_SDK"); // get customer number const RegexOptions options = RegexOptions.Multiline | RegexOptions.CultureInvariant; const string pattern = @"[D|d]atabase=(\D*)(\d*)_Angular"; if (int.TryParse(Regex.Match(_defaultConnectionString, pattern, options).Groups[^ 1].Value, out var customerNumber)) { var adminTools = new AdminTools(sdkConnectionString); // Setup SDK DB await adminTools.DbSetup(token); var core = new Core(); await core.StartSqlOnly(sdkConnectionString); await core.SetSdkSetting(Settings.customerNo, customerNumber.ToString()); // setup admin var adminSetupModel = new AdminSetupModel() { DarkTheme = false, FirstName = firstName, LastName = lastName, Email = email, Password = password, }; var contextFactory = new BaseDbContextFactory(); await using var dbContext = contextFactory.CreateDbContext(new[] { _defaultConnectionString }); var connectionStringsSdk = scope.ServiceProvider.GetRequiredService <IDbOptions <ConnectionStringsSdk> >(); await connectionStringsSdk.UpdateDb( options => { options.SdkConnection = sdkConnectionString; }, dbContext); await SeedAdminHelper.SeedAdmin(adminSetupModel, "", dbContext); Restart(); } } } } }
public static List <IEformPlugin> GetPlugins(string connectionString) { Log.LogEvent($"PluginHelper.GetPlugins with connectionString {connectionString}"); // Load info from database List <EformPlugin> eformPlugins = null; var plugins = new List <IEformPlugin>(); var contextFactory = new BaseDbContextFactory(); if (connectionString != "...") { using (var dbContext = contextFactory.CreateDbContext(new[] { connectionString })) { try { eformPlugins = dbContext.EformPlugins .AsNoTracking() .ToList(); } catch { // ignored } } // create plugin loaders if (eformPlugins != null) { using (var dbContext = contextFactory.CreateDbContext(new[] { connectionString })) { var dbNameSection = Regex.Match(connectionString, @"([D|d]atabase=\w*;)").Groups[0].Value; var dbPrefix = Regex.Match(connectionString, @"[D|d]atabase=(\d*)_").Groups[1].Value; foreach (var plugin in GetAllPlugins()) { var eformPlugin = eformPlugins.FirstOrDefault(x => x.PluginId == plugin.PluginId); if (eformPlugin != null) { if (!eformPlugin.ConnectionString.Contains("PersistSecurityInfo=true;")) { var aPlugin = dbContext.EformPlugins.SingleOrDefault(x => x.PluginId == plugin.PluginId); if (aPlugin != null) { aPlugin.ConnectionString += "PersistSecurityInfo=true;"; } dbContext.SaveChanges(); } if (eformPlugin.Status == (int)PluginStatus.Enabled) { plugins.Add(plugin); } } else { var pluginDbName = $"Database={dbPrefix}_{plugin.PluginId};"; var pluginConnectionString = connectionString.Replace(dbNameSection, pluginDbName) + "PersistSecurityInfo=true;"; var newPlugin = new EformPlugin { PluginId = plugin.PluginId, ConnectionString = pluginConnectionString, Status = (int)PluginStatus.Disabled }; dbContext.EformPlugins.Add(newPlugin); dbContext.SaveChanges(); } } } } } return(plugins); }