oqtane.framework/Oqtane.Client/Modules/Admin/Sites/Add.razor
Pavel Vesely 940cdcb349 Database Manager
done:
+ master.sql as resource
+ implemented incremental database changes also for Master
+ dbUp sql script variables implemented
+ improved database handling and creation code
+ simpified database creation
+ almost all Database and Tenant creation moved to DatabaseManager.cs (rest code marked with TODO)
+ Unattended install of master can be performed by settings in appsettings.json
+ Improved IsInstalled checking
+ Removed DBSchema field from Tenant
+ Default database and site creation moved to Program.Main
2020-03-29 14:45:02 +02:00

306 lines
11 KiB
Plaintext

@namespace Oqtane.Modules.Admin.Sites
@inherits ModuleBase
@inject NavigationManager NavigationManager
@inject ITenantService TenantService
@inject IAliasService AliasService
@inject ISiteService SiteService
@inject IThemeService ThemeService
@inject ISiteTemplateService SiteTemplateService
@inject IUserService UserService
@if (_tenants == null)
{
<p><em>Loading...</em></p>
}
else
{
<table class="table table-borderless">
<tr>
<td>
<label class="control-label">Tenant: </label>
</td>
<td>
<select class="form-control" @onchange="(e => TenantChanged(e))">
<option value="-1">&lt;Select Tenant&gt;</option>
@foreach (Tenant tenant in _tenants)
{
<option value="@tenant.TenantId">@tenant.Name</option>
}
</select>
</td>
</tr>
<tr>
<td>
<label class="control-label">Name: </label>
</td>
<td>
<input class="form-control" @bind="@_name" />
</td>
</tr>
<tr>
<td>
<label class="control-label">Aliases: </label>
</td>
<td>
<textarea class="form-control" @bind="@_urls" rows="3"></textarea>
</td>
</tr>
<tr>
<td>
<label class="control-label">Default Theme: </label>
</td>
<td>
<select class="form-control" @onchange="(e => ThemeChanged(e))">
<option value="">&lt;Select Theme&gt;</option>
@foreach (KeyValuePair<string, string> item in _themes)
{
<option value="@item.Key">@item.Value</option>
}
</select>
</td>
</tr>
<tr>
<td>
<label class="control-label">Default Layout: </label>
</td>
<td>
<select class="form-control" @bind="@_layouttype">
<option value="">&lt;Select Layout&gt;</option>
@foreach (KeyValuePair<string, string> panelayout in _panelayouts)
{
<option value="@panelayout.Key">@panelayout.Value</option>
}
</select>
</td>
</tr>
<tr>
<td>
<label class="control-label">Default Container: </label>
</td>
<td>
<select class="form-control" @bind="@_containertype">
<option value="">&lt;Select Container&gt;</option>
@foreach (KeyValuePair<string, string> container in _containers)
{
<option value="@container.Key">@container.Value</option>
}
</select>
</td>
</tr>
<tr>
<td>
<label class="control-label">Site Template: </label>
</td>
<td>
<select class="form-control" @bind="@_sitetemplatetype">
<option value="">&lt;Select Site Template&gt;</option>
@foreach (SiteTemplate siteTemplate in _siteTemplates)
{
<option value="@siteTemplate.TypeName">@siteTemplate.Name</option>
}
</select>
</td>
</tr>
@if (!_isinitialized)
{
<tr>
<td>
<label class="control-label">Host Username:</label>
</td>
<td>
<input class="form-control" @bind="@_username" readonly />
</td>
</tr>
<tr>
<td>
<label class="control-label">Host Password:</label>
</td>
<td>
<input type="password" class="form-control" @bind="@_password" />
</td>
</tr>
}
</table>
<button type="button" class="btn btn-success" @onclick="SaveSite">Save</button>
<NavLink class="btn btn-secondary" href="@NavigateUrl()">Cancel</NavLink>
}
@code {
public override SecurityAccessLevel SecurityAccessLevel { get { return SecurityAccessLevel.Host; } }
Dictionary<string, string> _themes = new Dictionary<string, string>();
Dictionary<string, string> _panelayouts = new Dictionary<string, string>();
Dictionary<string, string> _containers = new Dictionary<string, string>();
List<SiteTemplate> _siteTemplates;
List<Theme> _themeList;
List<Tenant> _tenants;
string _tenantid = "-1";
string _name = "";
string _urls = "";
string _themetype = "";
string _layouttype = "";
string _containertype = "";
string _sitetemplatetype = "";
bool _isinitialized = true;
string _username = "";
string _password = "";
protected override async Task OnInitializedAsync()
{
_themeList = await ThemeService.GetThemesAsync();
_tenants = await TenantService.GetTenantsAsync();
_urls = PageState.Alias.Name;
_themes = ThemeService.GetThemeTypes(_themeList);
_containers = ThemeService.GetContainerTypes(_themeList);
_siteTemplates = await SiteTemplateService.GetSiteTemplatesAsync();
_username = Constants.HostUser;
}
private async void TenantChanged(ChangeEventArgs e)
{
try
{
_tenantid = (string)e.Value;
if (_tenantid != "-1")
{
Tenant tenant = _tenants.FirstOrDefault(item => item.TenantId == int.Parse(_tenantid));
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(_themeList, _themetype);
}
else
{
_panelayouts = new Dictionary<string, string>();
}
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) && !string.IsNullOrEmpty(_sitetemplatetype))
{
bool unique = true;
List<Alias> aliases = await AliasService.GetAliasesAsync();
foreach (string name in _urls.Split(new[] { ',' }, 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<Alias>();
_urls = _urls.Replace("\n", ",");
foreach (string name in _urls.Split(new[] { ',' }, 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.SiteTemplateType = _sitetemplatetype;
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.FirstOrDefault(item => item.TenantId == int.Parse(_tenantid));
if (tenant != null)
{
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, Default Theme/Container, And Site Template", MessageType.Warning);
}
}
}