269 lines
10 KiB
C#
269 lines
10 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using Sarand.BusinessMgr.Net.Standard.Base.App;
|
|
using Sarand.Net.Standard.Base.Utils;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
|
|
namespace Sarand.BusinessMgr.Net.Core.Web.App
|
|
{
|
|
//=======================================================================
|
|
// BusinessMgr Application Configuration.
|
|
//=======================================================================
|
|
public class SbmAppConfig
|
|
{
|
|
//---------------------------------------------------------------
|
|
// Data members.
|
|
private readonly List<String> environments = new List<string>();
|
|
|
|
//---------------------------------------------------------------
|
|
// Construction.
|
|
public SbmAppConfig(String context, ConfigurationSection configSection, SbmAppConfig defaultConfig)
|
|
{
|
|
Context = context;
|
|
PopulateDefaults(defaultConfig);
|
|
PopulateConfig(configSection);
|
|
}
|
|
|
|
//---------------------------------------------------------------
|
|
// Properties.
|
|
public String Context { get; }
|
|
public List<String> Environments { get { return new List<String>(environments); } }
|
|
public String Instance { get; set; } = "";
|
|
// URLs.
|
|
public class URLConfig
|
|
{
|
|
public class PortalConfig
|
|
{
|
|
public String Root { get; set; }
|
|
}
|
|
public PortalConfig Portal { get; } = new PortalConfig();
|
|
}
|
|
public URLConfig URL { get; } = new URLConfig();
|
|
// Security.
|
|
public class SecurityConfig
|
|
{
|
|
public class PasswordConfig
|
|
{
|
|
public int MinLength { get; set; }
|
|
}
|
|
public PasswordConfig Password { get; } = new PasswordConfig();
|
|
public class SignUpConfig
|
|
{
|
|
public class ActivationConfig
|
|
{
|
|
public int EmailLinkExpiresMins = Sbm.Config.Security.Activation.EmailLinkExpiresMins_Default;
|
|
}
|
|
public ActivationConfig Activation { get; } = new ActivationConfig();
|
|
}
|
|
public SignUpConfig SignUp { get; } = new SignUpConfig();
|
|
public class SignInConfig
|
|
{
|
|
public class PasswordRequestConfig
|
|
{
|
|
public int EmailLinkExpiresMins = Sbm.Config.Security.Activation.EmailLinkExpiresMins_Default;
|
|
}
|
|
public PasswordRequestConfig PasswordRequest { get; } = new PasswordRequestConfig();
|
|
public class KeepLoggedInConfig
|
|
{
|
|
public bool Allowed { get; set; } = Sbm.Config.Security.SignIn.KeepLoggedIn.Allowed_Default;
|
|
public int CookieExpiresDays { get; set; } = Sbm.Config.Security.SignIn.KeepLoggedIn.CookieExpiresDays_Default;
|
|
}
|
|
public KeepLoggedInConfig KeepLoggedIn { get; } = new KeepLoggedInConfig();
|
|
}
|
|
public SignInConfig SignIn { get; } = new SignInConfig();
|
|
}
|
|
public SecurityConfig Security { get; } = new SecurityConfig();
|
|
// Email.
|
|
public class EmailConfig
|
|
{
|
|
public class SpecificEmailConfig
|
|
{
|
|
public List<String> AddressesTo { get; } = new List<string>();
|
|
public List<String> AddressesCc { get; } = new List<string>();
|
|
public List<String> AddressesBcc { get; } = new List<string>();
|
|
}
|
|
public SpecificEmailConfig ContactUs { get; } = new SpecificEmailConfig();
|
|
public SpecificEmailConfig Signup_Activate { get; } = new SpecificEmailConfig();
|
|
public SpecificEmailConfig Signup_Welcome { get; } = new SpecificEmailConfig();
|
|
public String Context { get; internal set; }
|
|
}
|
|
public EmailConfig Email { get; } = new EmailConfig();
|
|
// Databases.
|
|
public class DatabaseConfig
|
|
{
|
|
public List<String> Identifiers { get; } = new List<string>();
|
|
public String Location { get; internal set; }
|
|
public String User { get; internal set; }
|
|
public String Password { get; internal set; }
|
|
}
|
|
public DatabaseConfig ControlDatabase { get; private set; }
|
|
public DatabaseConfig TemplateDatabase { get; private set; }
|
|
public List<DatabaseConfig> BusMgrDatabases { get; } = new List<DatabaseConfig>();
|
|
// Documents.
|
|
public class DocumentsConfig
|
|
{
|
|
public String RootFolder { get; internal set; }
|
|
}
|
|
public DocumentsConfig Documents { get; } = new DocumentsConfig();
|
|
// Logging.
|
|
public class LoggingConfig
|
|
{
|
|
public String RootFolder { get; internal set; }
|
|
}
|
|
public LoggingConfig Logging { get; } = new LoggingConfig();
|
|
|
|
//---------------------------------------------------------------
|
|
private void PopulateDefaults(SbmAppConfig defaultConfig)
|
|
{
|
|
if (defaultConfig == null) return;
|
|
Security.Password.MinLength = defaultConfig.Security.Password.MinLength;
|
|
Security.SignIn.KeepLoggedIn.Allowed = defaultConfig.Security.SignIn.KeepLoggedIn.Allowed;
|
|
Security.SignIn.KeepLoggedIn.CookieExpiresDays = defaultConfig.Security.SignIn.KeepLoggedIn.CookieExpiresDays;
|
|
}
|
|
|
|
//---------------------------------------------------------------
|
|
private void PopulateConfig(ConfigurationSection configSection)
|
|
{
|
|
if (configSection != null)
|
|
{
|
|
// Environments.
|
|
foreach (ConfigurationSection environmentSection in configSection.GetSection("Environments").GetChildren())
|
|
environments.Add(StringUtil.TrimAll(environmentSection.Value));
|
|
Instance = StringUtil.OnEmpty(configSection["Instance"], Instance);
|
|
|
|
// URLs.
|
|
IConfigurationSection urls = configSection.GetSection("URLs");
|
|
if (urls.Exists())
|
|
{
|
|
IConfigurationSection portalUrls = urls.GetSection("Portal");
|
|
if (portalUrls.Exists())
|
|
{
|
|
URL.Portal.Root = portalUrls["Root"];
|
|
}
|
|
}
|
|
|
|
// Security.
|
|
IConfigurationSection securitySection = configSection.GetSection("Security");
|
|
if (securitySection.Exists())
|
|
{
|
|
// Security - Password.
|
|
IConfigurationSection securityPasswordSection = securitySection.GetSection("Password");
|
|
if (securityPasswordSection.Exists())
|
|
{
|
|
Security.Password.MinLength = StringUtil.ToInt(securityPasswordSection["MinLength"], Security.Password.MinLength);
|
|
}
|
|
// Security - Sign-Up.
|
|
IConfigurationSection securitySignUpSection = securitySection.GetSection("SignUp");
|
|
if (securitySignUpSection.Exists())
|
|
{
|
|
}
|
|
// Security - Sign-In.
|
|
IConfigurationSection securitySignInSection = securitySection.GetSection("SignIn");
|
|
if (securitySignInSection.Exists())
|
|
{
|
|
IConfigurationSection keepLoggedInSection = securitySignInSection.GetSection("SignIn");
|
|
if (keepLoggedInSection.Exists())
|
|
{
|
|
Security.SignIn.KeepLoggedIn.Allowed = StringUtil.ToBool(StringUtil.OnEmpty(keepLoggedInSection["Allowed"], Security.SignIn.KeepLoggedIn.Allowed.ToString()));
|
|
Security.SignIn.KeepLoggedIn.CookieExpiresDays = StringUtil.ToInt(keepLoggedInSection["CookieExpiresDays"], Security.SignIn.KeepLoggedIn.CookieExpiresDays);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Email.
|
|
IConfigurationSection emailSection = configSection.GetSection("Email");
|
|
if (emailSection.Exists())
|
|
{
|
|
Email.Context = emailSection["Context"];
|
|
PopulateEmailConfig(Email.ContactUs, emailSection.GetSection("ContactUs"));
|
|
PopulateEmailConfig(Email.Signup_Activate, emailSection.GetSection("Signup").GetSection("Activate"));
|
|
PopulateEmailConfig(Email.Signup_Welcome, emailSection.GetSection("Signup").GetSection("Welcome"));
|
|
}
|
|
|
|
// Documents.
|
|
IConfigurationSection documentsSection = configSection.GetSection("Documents");
|
|
if (documentsSection.Exists())
|
|
{
|
|
Documents.RootFolder = StringUtil.OnEmpty(documentsSection["RootFolder"], Documents.RootFolder);
|
|
}
|
|
|
|
// Logging.
|
|
IConfigurationSection loggingSection = configSection.GetSection("Logging");
|
|
if (loggingSection.Exists())
|
|
{
|
|
Logging.RootFolder = StringUtil.OnEmpty(loggingSection["RootFolder"], Logging.RootFolder);
|
|
}
|
|
|
|
// Control database.
|
|
IConfigurationSection controlDatabaseSection = configSection.GetSection("ControlDatabase");
|
|
if (controlDatabaseSection.Exists())
|
|
{
|
|
ControlDatabase = new DatabaseConfig();
|
|
ExtractDatabaseConfig(ControlDatabase, controlDatabaseSection);
|
|
}
|
|
|
|
// Template database.
|
|
IConfigurationSection templateDatabaseSection = configSection.GetSection("TemplateDatabase");
|
|
if (templateDatabaseSection.Exists())
|
|
{
|
|
TemplateDatabase = new DatabaseConfig();
|
|
ExtractDatabaseConfig(TemplateDatabase, templateDatabaseSection);
|
|
}
|
|
|
|
// BusinessMgr databases.
|
|
IConfigurationSection busMgrDatabasesSection = configSection.GetSection("BusMgrDatabases");
|
|
if (busMgrDatabasesSection.Exists())
|
|
{
|
|
foreach (IConfigurationSection busMgrDbSection in busMgrDatabasesSection.GetChildren())
|
|
{
|
|
SbmAppConfig.DatabaseConfig dbConfig = new SbmAppConfig.DatabaseConfig();
|
|
ExtractDatabaseConfig(dbConfig, busMgrDbSection);
|
|
BusMgrDatabases.Add(dbConfig);
|
|
}
|
|
}
|
|
|
|
// Variants.
|
|
foreach (ConfigurationSection variantSection in configSection.GetSection("Variants").GetChildren())
|
|
{
|
|
// Variants override selected values but must match the application root folder to identify a specific instance.
|
|
String applicationRootPath = variantSection["ApplicationRootFolder"];
|
|
if (StringUtil.IsEmpty(applicationRootPath)) continue;
|
|
if (!SbmSystem.ApplicationFolder.EqualsCI(applicationRootPath)) continue;
|
|
PopulateConfig(variantSection);
|
|
}
|
|
}
|
|
}
|
|
|
|
//---------------------------------------------------------------
|
|
private void PopulateEmailConfig(EmailConfig.SpecificEmailConfig config, IConfigurationSection emailSection)
|
|
{
|
|
if (!emailSection.Exists()) return;
|
|
foreach (var addressTo in emailSection.GetSection("AddressesTo").GetChildren())
|
|
config.AddressesTo.Add(StringUtil.TrimAll(addressTo.Value));
|
|
foreach (var addressCc in emailSection.GetSection("AddressesCc").GetChildren())
|
|
config.AddressesCc.Add(StringUtil.TrimAll(addressCc.Value));
|
|
foreach (var addressBcc in emailSection.GetSection("AddressesBcc").GetChildren())
|
|
config.AddressesBcc.Add(StringUtil.TrimAll(addressBcc.Value));
|
|
}
|
|
|
|
//---------------------------------------------------------------
|
|
private void ExtractDatabaseConfig(DatabaseConfig database, IConfigurationSection databaseSection)
|
|
{
|
|
if (!databaseSection.Exists()) return;
|
|
IConfigurationSection identifiersSection = databaseSection.GetSection("Identifiers");
|
|
if (identifiersSection.Exists())
|
|
{
|
|
foreach (var identifier in identifiersSection.GetChildren())
|
|
database.Identifiers.Add(StringUtil.TrimAll(identifier.Value));
|
|
}
|
|
database.Location = StringUtil.OnEmpty(databaseSection["Location"], database.Location);
|
|
database.User = StringUtil.OnEmpty(databaseSection["User"], database.User);
|
|
database.Password = StringUtil.OnEmpty(databaseSection["Password"], database.Password);
|
|
}
|
|
|
|
//---------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|