@namespace Oqtane.Modules.Admin.Sites
@inherits ModuleBase
@inject NavigationManager NavigationManager
@inject ITenantService TenantService
@inject IAliasService AliasService
@inject ISiteService SiteService
@inject IThemeService ThemeService
@inject IUserService UserService
@if (tenants == null)
{
Loading...
}
else
{
Cancel
}
@code {
public override SecurityAccessLevel SecurityAccessLevel { get { return SecurityAccessLevel.Host; } }
Dictionary themes = new Dictionary();
Dictionary panelayouts = new Dictionary();
Dictionary containers = new Dictionary();
List Themes;
List tenants;
string tenantid = "-1";
string name = "";
string urls = "";
string themetype = "";
string layouttype = "";
string containertype = "";
bool isinitialized = true;
string username = "";
string password = "";
protected override async Task OnInitializedAsync()
{
Themes = await ThemeService.GetThemesAsync();
tenants = await TenantService.GetTenantsAsync();
urls = PageState.Alias.Name;
themes = ThemeService.GetThemeTypes(Themes);
containers = ThemeService.GetContainerTypes(Themes);
username = Constants.HostUser;
}
private async void TenantChanged(ChangeEventArgs e)
{
try
{
tenantid = (string)e.Value;
if (tenantid != "-1")
{
Tenant tenant = tenants.Where(item => item.TenantId == int.Parse(tenantid)).FirstOrDefault();
if (tenant != null)
{
isinitialized = tenant.IsInitialized;
StateHasChanged();
}
}
}
catch (Exception ex)
{
await logger.LogError(ex, "Error Loading Tenant {TenantId} {Error}", tenantid, ex.Message);
AddModuleMessage("Error Loading Tenant", MessageType.Error);
}
}
private async void ThemeChanged(ChangeEventArgs e)
{
try
{
themetype = (string)e.Value;
if (themetype != "")
{
panelayouts = ThemeService.GetPaneLayoutTypes(Themes, themetype);
}
else
{
panelayouts = new Dictionary();
}
StateHasChanged();
}
catch (Exception ex)
{
await logger.LogError(ex, "Error Loading Pane Layouts For Theme {ThemeType} {Error}", themetype, ex.Message);
AddModuleMessage("Error Loading Pane Layouts For Theme", MessageType.Error);
}
}
private async Task SaveSite()
{
if (tenantid != "-1" && name != "" && urls != "" && !string.IsNullOrEmpty(themetype) && (panelayouts.Count == 0 || !string.IsNullOrEmpty(layouttype)) && !string.IsNullOrEmpty(containertype))
{
bool unique = true;
List aliases = await AliasService.GetAliasesAsync();
foreach (string name in urls.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
if (aliases.Exists(item => item.Name == name))
{
unique = false;
}
}
if (unique)
{
bool isvalid = true;
if (!isinitialized)
{
User user = new User();
user.SiteId = PageState.Site.SiteId;
user.Username = username;
user.Password = password;
user = await UserService.LoginUserAsync(user, false, false);
isvalid = user.IsAuthenticated;
}
if (isvalid)
{
ShowProgressIndicator();
aliases = new List();
urls = urls.Replace("\n", ",");
foreach (string name in urls.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
Alias alias = new Alias();
alias.Name = name;
alias.TenantId = int.Parse(tenantid);
alias.SiteId = -1;
alias = await AliasService.AddAliasAsync(alias);
aliases.Add(alias);
}
Site site = new Site();
site.TenantId = int.Parse(tenantid);
site.Name = name;
site.LogoFileId = null;
site.DefaultThemeType = themetype;
site.DefaultLayoutType = (layouttype == null ? "" : layouttype);
site.DefaultContainerType = containertype;
site = await SiteService.AddSiteAsync(site, aliases[0]);
foreach (Alias alias in aliases)
{
alias.SiteId = site.SiteId;
await AliasService.UpdateAliasAsync(alias);
}
if (!isinitialized)
{
User user = new User();
user.SiteId = site.SiteId;
user.Username = username;
user.Password = password;
user.Email = PageState.User.Email;
user.DisplayName = PageState.User.DisplayName;
user = await UserService.AddUserAsync(user, aliases[0]);
if (user != null)
{
Tenant tenant = tenants.Where(item => item.TenantId == int.Parse(tenantid)).FirstOrDefault();
tenant.IsInitialized = true;
await TenantService.UpdateTenantAsync(tenant);
}
}
await Log(aliases[0], LogLevel.Information, "", null, "Site Created {Site}", site);
Uri uri = new Uri(NavigationManager.Uri);
NavigationManager.NavigateTo(uri.Scheme + "://" + aliases[0].Name, true);
}
else
{
await logger.LogError("Invalid Password Entered For Host {Username}", username);
AddModuleMessage("Invalid Host Password", MessageType.Error);
}
}
else
{
AddModuleMessage("An Alias Specified Has Already Been Used For Another Site", MessageType.Warning);
}
}
else
{
AddModuleMessage("You Must Provide A Tenant, Site Name, Alias, And Default Theme/Container", MessageType.Warning);
}
}
}