Additional properties added to Route model to improve abstraction, modified Site Settings to support settings moved to the server.

This commit is contained in:
Shaun Walker 2021-12-02 16:33:16 -05:00
parent 43d166fb7d
commit a216e2b92e
3 changed files with 253 additions and 221 deletions

View File

@ -240,243 +240,249 @@
} }
@code { @code {
private ElementReference form; private ElementReference form;
private bool validated = false; private bool validated = false;
private bool _initialized = false; private bool _initialized = false;
private List<Theme> _themeList; private List<Theme> _themeList;
private List<ThemeControl> _themes = new List<ThemeControl>(); private List<ThemeControl> _themes = new List<ThemeControl>();
private List<ThemeControl> _containers = new List<ThemeControl>(); private List<ThemeControl> _containers = new List<ThemeControl>();
private string _name = string.Empty; private string _name = string.Empty;
private List<Alias> _aliasList; private List<Alias> _aliasList;
private string _urls = string.Empty; private string _urls = string.Empty;
private string _runtime = ""; private string _runtime = "";
private string _prerender = ""; private string _prerender = "";
private int _logofileid = -1; private int _logofileid = -1;
private FileManager _logofilemanager; private FileManager _logofilemanager;
private int _faviconfileid = -1; private int _faviconfileid = -1;
private FileManager _faviconfilemanager; private FileManager _faviconfilemanager;
private string _themetype = "-"; private string _themetype = "-";
private string _containertype = "-"; private string _containertype = "-";
private string _admincontainertype = "-"; private string _admincontainertype = "-";
private string _allowregistration; private string _allowregistration;
private string _smtphost = string.Empty; private string _smtphost = string.Empty;
private string _smtpport = string.Empty; private string _smtpport = string.Empty;
private string _smtpssl = "False"; private string _smtpssl = "False";
private string _smtpusername = string.Empty; private string _smtpusername = string.Empty;
private string _smtppassword = string.Empty; private string _smtppassword = string.Empty;
private string _smtpsender = string.Empty; private string _smtpsender = string.Empty;
private string _pwaisenabled; private string _pwaisenabled;
private int _pwaappiconfileid = -1; private int _pwaappiconfileid = -1;
private FileManager _pwaappiconfilemanager; private FileManager _pwaappiconfilemanager;
private int _pwasplashiconfileid = -1; private int _pwasplashiconfileid = -1;
private FileManager _pwasplashiconfilemanager; private FileManager _pwasplashiconfilemanager;
private string _tenant = string.Empty; private string _tenant = string.Empty;
private string _database = string.Empty; private string _database = string.Empty;
private string _connectionstring = string.Empty; private string _connectionstring = string.Empty;
private string _createdby; private string _createdby;
private DateTime _createdon; private DateTime _createdon;
private string _modifiedby; private string _modifiedby;
private DateTime _modifiedon; private DateTime _modifiedon;
private string _deletedby; private string _deletedby;
private DateTime? _deletedon; private DateTime? _deletedon;
private string _isdeleted; private string _isdeleted;
public override SecurityAccessLevel SecurityAccessLevel => SecurityAccessLevel.Admin; public override SecurityAccessLevel SecurityAccessLevel => SecurityAccessLevel.Admin;
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
{ {
try try
{ {
_themeList = await ThemeService.GetThemesAsync(); _themeList = await ThemeService.GetThemesAsync();
Site site = await SiteService.GetSiteAsync(PageState.Site.SiteId); Site site = await SiteService.GetSiteAsync(PageState.Site.SiteId);
if (site != null) if (site != null)
{ {
_name = site.Name; _name = site.Name;
_runtime = site.Runtime; _runtime = site.Runtime;
_prerender = site.RenderMode.Replace(_runtime, ""); _prerender = site.RenderMode.Replace(_runtime, "");
_allowregistration = site.AllowRegistration.ToString(); _allowregistration = site.AllowRegistration.ToString();
_isdeleted = site.IsDeleted.ToString(); _isdeleted = site.IsDeleted.ToString();
if (UserSecurity.IsAuthorized(PageState.User, RoleNames.Host)) if (UserSecurity.IsAuthorized(PageState.User, RoleNames.Host))
{ {
_aliasList = await AliasService.GetAliasesAsync(); _aliasList = await AliasService.GetAliasesAsync();
foreach (Alias alias in _aliasList.Where(item => item.SiteId == site.SiteId && item.TenantId == site.TenantId).ToList()) foreach (Alias alias in _aliasList.Where(item => item.SiteId == site.SiteId && item.TenantId == site.TenantId).ToList())
{ {
_urls += alias.Name + ","; _urls += alias.Name + ",";
} }
_urls = _urls.Substring(0, _urls.Length - 1); _urls = _urls.Substring(0, _urls.Length - 1);
} }
if (site.LogoFileId != null) if (site.LogoFileId != null)
{ {
_logofileid = site.LogoFileId.Value; _logofileid = site.LogoFileId.Value;
} }
if (site.FaviconFileId != null) if (site.FaviconFileId != null)
{ {
_faviconfileid = site.FaviconFileId.Value; _faviconfileid = site.FaviconFileId.Value;
} }
_themes = ThemeService.GetThemeControls(_themeList); _themes = ThemeService.GetThemeControls(_themeList);
_themetype = (!string.IsNullOrEmpty(site.DefaultThemeType)) ? site.DefaultThemeType : Constants.DefaultTheme; _themetype = (!string.IsNullOrEmpty(site.DefaultThemeType)) ? site.DefaultThemeType : Constants.DefaultTheme;
_containers = ThemeService.GetContainerControls(_themeList, _themetype); _containers = ThemeService.GetContainerControls(_themeList, _themetype);
_containertype = (!string.IsNullOrEmpty(site.DefaultContainerType)) ? site.DefaultContainerType : Constants.DefaultContainer; _containertype = (!string.IsNullOrEmpty(site.DefaultContainerType)) ? site.DefaultContainerType : Constants.DefaultContainer;
_admincontainertype = (!string.IsNullOrEmpty(site.AdminContainerType)) ? site.AdminContainerType : Constants.DefaultAdminContainer; _admincontainertype = (!string.IsNullOrEmpty(site.AdminContainerType)) ? site.AdminContainerType : Constants.DefaultAdminContainer;
var settings = await SettingService.GetSiteSettingsAsync(site.SiteId); _pwaisenabled = site.PwaIsEnabled.ToString();
_smtphost = SettingService.GetSetting(settings, "SMTPHost", string.Empty); if (site.PwaAppIconFileId != null)
_smtpport = SettingService.GetSetting(settings, "SMTPPort", string.Empty); {
_smtpssl = SettingService.GetSetting(settings, "SMTPSSL", "False"); _pwaappiconfileid = site.PwaAppIconFileId.Value;
_smtpusername = SettingService.GetSetting(settings, "SMTPUsername", string.Empty); }
_smtppassword = SettingService.GetSetting(settings, "SMTPPassword", string.Empty); if (site.PwaSplashIconFileId != null)
_smtpsender = SettingService.GetSetting(settings, "SMTPSender", string.Empty); {
_pwasplashiconfileid = site.PwaSplashIconFileId.Value;
}
_pwaisenabled = site.PwaIsEnabled.ToString(); var settings = await SettingService.GetSiteSettingsAsync(site.SiteId);
_smtphost = SettingService.GetSetting(settings, "SMTPHost", string.Empty);
_smtpport = SettingService.GetSetting(settings, "SMTPPort", string.Empty);
_smtpssl = SettingService.GetSetting(settings, "SMTPSSL", "False");
_smtpusername = SettingService.GetSetting(settings, "SMTPUsername", string.Empty);
_smtppassword = SettingService.GetSetting(settings, "SMTPPassword", string.Empty);
_smtpsender = SettingService.GetSetting(settings, "SMTPSender", string.Empty);
if (site.PwaAppIconFileId != null) if (UserSecurity.IsAuthorized(PageState.User, RoleNames.Host))
{ {
_pwaappiconfileid = site.PwaAppIconFileId.Value; var tenants = await TenantService.GetTenantsAsync();
} var _databases = await DatabaseService.GetDatabasesAsync();
var tenant = tenants.Find(item => item.TenantId == site.TenantId);
if (tenant != null)
{
_tenant = tenant.Name;
_database = _databases.Find(item => item.DBType == tenant.DBType)?.Name;
_connectionstring = tenant.DBConnectionString;
}
}
if (site.PwaSplashIconFileId != null) _createdby = site.CreatedBy;
{ _createdon = site.CreatedOn;
_pwasplashiconfileid = site.PwaSplashIconFileId.Value; _modifiedby = site.ModifiedBy;
} _modifiedon = site.ModifiedOn;
_deletedby = site.DeletedBy;
_deletedon = site.DeletedOn;
_pwaisenabled = site.PwaIsEnabled.ToString(); _initialized = true;
if (site.PwaAppIconFileId != null) }
{ }
_pwaappiconfileid = site.PwaAppIconFileId.Value; catch (Exception ex)
} {
if (site.PwaSplashIconFileId != null) await logger.LogError(ex, "Error Loading Site {SiteId} {Error}", PageState.Site.SiteId, ex.Message);
{ AddModuleMessage(ex.Message, MessageType.Error);
_pwasplashiconfileid = site.PwaSplashIconFileId.Value; }
} }
if (UserSecurity.IsAuthorized(PageState.User, RoleNames.Host)) private async void ThemeChanged(ChangeEventArgs e)
{ {
var tenants = await TenantService.GetTenantsAsync(); try
var _databases = await DatabaseService.GetDatabasesAsync(); {
var tenant = tenants.Find(item => item.TenantId == site.TenantId); _themetype = (string)e.Value;
if (tenant != null) if (_themetype != "-")
{ {
_tenant = tenant.Name; _containers = ThemeService.GetContainerControls(_themeList, _themetype);
_database = _databases.Find(item => item.DBType == tenant.DBType)?.Name; }
_connectionstring = tenant.DBConnectionString; else
} {
} _containers = new List<ThemeControl>();
}
_containertype = "-";
_admincontainertype = Constants.DefaultAdminContainer;
StateHasChanged();
}
catch (Exception ex)
{
await logger.LogError(ex, "Error Loading Pane Layouts For Theme {ThemeType} {Error}", _themetype, ex.Message);
AddModuleMessage(Localizer["Error.Theme.LoadPane"], MessageType.Error);
}
}
_createdby = site.CreatedBy; private async Task SaveSite()
_createdon = site.CreatedOn; {
_modifiedby = site.ModifiedBy; validated = true;
_modifiedon = site.ModifiedOn; var interop = new Interop(JSRuntime);
_deletedby = site.DeletedBy; if (await interop.FormValid(form))
_deletedon = site.DeletedOn; {
try
{
if (_name != string.Empty && _urls != string.Empty && _themetype != "-" && _containertype != "-")
{
var unique = true;
if (UserSecurity.IsAuthorized(PageState.User, RoleNames.Host))
{
foreach (string name in _urls.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
if (_aliasList.Exists(item => item.Name == name && item.SiteId != PageState.Alias.SiteId && item.TenantId != PageState.Alias.TenantId))
{
unique = false;
}
}
}
_initialized = true; if (unique)
} {
} var site = await SiteService.GetSiteAsync(PageState.Site.SiteId);
catch (Exception ex) if (site != null)
{ {
await logger.LogError(ex, "Error Loading Site {SiteId} {Error}", PageState.Site.SiteId, ex.Message); bool refresh = false;
AddModuleMessage(ex.Message, MessageType.Error); bool reload = false;
}
}
private async void ThemeChanged(ChangeEventArgs e) site.Name = _name;
{ if (UserSecurity.IsAuthorized(PageState.User, RoleNames.Host))
try {
{ if (site.Runtime != _runtime || site.RenderMode != _runtime + _prerender)
_themetype = (string)e.Value; {
if (_themetype != "-") site.Runtime = _runtime;
{ site.RenderMode = _runtime + _prerender;
_containers = ThemeService.GetContainerControls(_themeList, _themetype); refresh = true;
} reload = true; // needs to be reloaded on server
else }
{ }
_containers = new List<ThemeControl>(); site.AllowRegistration = (_allowregistration == null ? true : Boolean.Parse(_allowregistration));
} site.IsDeleted = (_isdeleted == null ? true : Boolean.Parse(_isdeleted));
_containertype = "-";
_admincontainertype = Constants.DefaultAdminContainer;
StateHasChanged();
}
catch (Exception ex)
{
await logger.LogError(ex, "Error Loading Pane Layouts For Theme {ThemeType} {Error}", _themetype, ex.Message);
AddModuleMessage(Localizer["Error.Theme.LoadPane"], MessageType.Error);
}
}
private async Task SaveSite() site.LogoFileId = null;
{ var logofileid = _logofilemanager.GetFileId();
validated = true; if (logofileid != -1)
var interop = new Interop(JSRuntime); {
if (await interop.FormValid(form)) site.LogoFileId = logofileid;
{ }
try int? faviconFieldId = _faviconfilemanager.GetFileId();
{ if (faviconFieldId == -1) faviconFieldId = null;
if (_name != string.Empty && _urls != string.Empty && _themetype != "-" && _containertype != "-") if (site.FaviconFileId != faviconFieldId)
{ {
var unique = true; site.FaviconFileId = faviconFieldId;
if (UserSecurity.IsAuthorized(PageState.User, RoleNames.Host)) reload = true; // needs to be reloaded on server
{ }
foreach (string name in _urls.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) if (site.DefaultThemeType != _themetype)
{ {
if (_aliasList.Exists(item => item.Name == name && item.SiteId != PageState.Alias.SiteId && item.TenantId != PageState.Alias.TenantId)) site.DefaultThemeType = _themetype;
{ refresh = true; // needs to be refreshed on client
unique = false; }
} if (site.DefaultContainerType != _containertype)
} {
} site.DefaultContainerType = _containertype;
refresh = true; // needs to be refreshed on client
}
site.AdminContainerType = _admincontainertype;
if (unique) if (site.PwaIsEnabled.ToString() != _pwaisenabled)
{ {
var site = await SiteService.GetSiteAsync(PageState.Site.SiteId); site.PwaIsEnabled = Boolean.Parse(_pwaisenabled);
if (site != null) reload = true; // needs to be reloaded on server
{ }
bool reload = false; int? pwaappiconfileid = _pwaappiconfilemanager.GetFileId();
bool refresh = (site.DefaultThemeType != _themetype || site.DefaultContainerType != _containertype); if (pwaappiconfileid == -1) pwaappiconfileid = null;
if (site.PwaAppIconFileId != pwaappiconfileid)
site.Name = _name;
if (UserSecurity.IsAuthorized(PageState.User, RoleNames.Host))
{
if (site.Runtime != _runtime || site.RenderMode != _runtime + _prerender)
{
site.Runtime = _runtime;
site.RenderMode = _runtime + _prerender;
refresh = true;
reload = true;
}
}
site.AllowRegistration = (_allowregistration == null ? true : Boolean.Parse(_allowregistration));
site.IsDeleted = (_isdeleted == null ? true : Boolean.Parse(_isdeleted));
site.LogoFileId = null;
var logofileid = _logofilemanager.GetFileId();
if (logofileid != -1)
{
site.LogoFileId = logofileid;
}
var faviconFieldId = _faviconfilemanager.GetFileId();
if (faviconFieldId != -1)
{
site.FaviconFileId = faviconFieldId;
}
site.DefaultThemeType = _themetype;
site.DefaultContainerType = _containertype;
site.AdminContainerType = _admincontainertype;
site.PwaIsEnabled = (_pwaisenabled == null ? true : Boolean.Parse(_pwaisenabled));
var pwaappiconfileid = _pwaappiconfilemanager.GetFileId();
if (pwaappiconfileid != -1)
{ {
site.PwaAppIconFileId = pwaappiconfileid; site.PwaAppIconFileId = pwaappiconfileid;
reload = true; // needs to be reloaded on server
} }
var pwasplashiconfileid = _pwasplashiconfilemanager.GetFileId(); int? pwasplashiconfileid = _pwasplashiconfilemanager.GetFileId();
if (pwasplashiconfileid != -1) if (pwasplashiconfileid == -1) pwasplashiconfileid = null;
if (site.PwaSplashIconFileId != pwasplashiconfileid)
{ {
site.PwaSplashIconFileId = pwasplashiconfileid; site.PwaSplashIconFileId = pwasplashiconfileid;
reload = true; // needs to be reloaded on server
} }
site = await SiteService.UpdateSiteAsync(site); site = await SiteService.UpdateSiteAsync(site);
@ -518,7 +524,7 @@
if (refresh) if (refresh)
{ {
NavigationManager.NavigateTo(NavigateUrl(), reload); // refresh to show new theme or container NavigationManager.NavigateTo(NavigateUrl(true), reload); // refresh/reload
} }
else else
{ {

View File

@ -149,16 +149,16 @@ namespace Oqtane.Pages
"var manifest = { " + "var manifest = { " +
"\"name\": \"" + site.Name + "\", " + "\"name\": \"" + site.Name + "\", " +
"\"short_name\": \"" + site.Name + "\", " + "\"short_name\": \"" + site.Name + "\", " +
"\"start_url\": \"" + route.Scheme + "://" + route.Authority + "/\", " + "\"start_url\": \"" + route.SiteUrl + "/\", " +
"\"display\": \"standalone\", " + "\"display\": \"standalone\", " +
"\"background_color\": \"#fff\", " + "\"background_color\": \"#fff\", " +
"\"description\": \"" + site.Name + "\", " + "\"description\": \"" + site.Name + "\", " +
"\"icons\": [{ " + "\"icons\": [{ " +
"\"src\": \"" + route.Scheme + "://" + route.Authority + Utilities.ContentUrl(alias, site.PwaAppIconFileId.Value) + "\", " + "\"src\": \"" + route.RootUrl + Utilities.ContentUrl(alias, site.PwaAppIconFileId.Value) + "\", " +
"\"sizes\": \"192x192\", " + "\"sizes\": \"192x192\", " +
"\"type\": \"image/png\" " + "\"type\": \"image/png\" " +
"}, { " + "}, { " +
"\"src\": \"" + route.Scheme + "://" + route.Authority + Utilities.ContentUrl(alias, site.PwaSplashIconFileId.Value) + "\", " + "\"src\": \"" + route.RootUrl + Utilities.ContentUrl(alias, site.PwaSplashIconFileId.Value) + "\", " +
"\"sizes\": \"512x512\", " + "\"sizes\": \"512x512\", " +
"\"type\": \"image/png\" " + "\"type\": \"image/png\" " +
"}] " + "}] " +

View File

@ -4,12 +4,16 @@ using Oqtane.Shared;
namespace Oqtane.Models namespace Oqtane.Models
{ {
/// <summary> /// <summary>
/// A route is comprised of multiple components: /// A route is comprised of multiple components ( some optional depending on context )
/// {scheme}://{hostname}/{aliaspath}/{pagepath}/*/{moduleid}/{action}/!/{urlparameters}?{query}#{fragment} /// {scheme}://{hostname}/{aliaspath}/{pagepath}/*/{moduleid}/{action}/!/{urlparameters}?{query}#{fragment}
/// </summary> /// </summary>
public class Route public class Route
{ {
// default constructor accepts an absolute route url and alias /// <summary>
/// default constructor
/// the route parameter can be obtained from NavigationManager.Uri on client or HttpContext.Request.GetEncodedUrl() on server
/// the aliaspath parameter can be obtained from PageState.Alias.Path on client or TenantManager.GetAlias().Path on server
/// </summary>
public Route(string route, string aliaspath) public Route(string route, string aliaspath)
{ {
Uri uri = new Uri(route); Uri uri = new Uri(route);
@ -120,5 +124,27 @@ namespace Oqtane.Models
/// A route may contain a fragment located after the # delimiter /// A route may contain a fragment located after the # delimiter
/// </summary> /// </summary>
public string Fragment { get; set; } public string Fragment { get; set; }
/// <summary>
/// The root url contains the resource identifier for the root of an Oqtane installation ( including scheme )
/// </summary>
public string RootUrl
{
get
{
return Scheme + "://" + Authority;
}
}
/// <summary>
/// The site url contains the resource identifier for the home page of a specific Oqtane site ( including scheme and possibly an alias path )
/// </summary>
public string SiteUrl
{
get
{
return Scheme + "://" + Authority + ((!string.IsNullOrEmpty(AliasPath)) ? "/" + AliasPath : "");
}
}
} }
} }