resolved a number of issues with site creation #224
This commit is contained in:
		@ -1,13 +1,14 @@
 | 
			
		||||
@namespace Oqtane.Modules.Admin.Sites
 | 
			
		||||
@namespace Oqtane.Modules.Admin.Site
 | 
			
		||||
@inherits ModuleBase
 | 
			
		||||
@inject NavigationManager NavigationManager
 | 
			
		||||
@inject ISiteService SiteService
 | 
			
		||||
@inject ITenantService TenantService
 | 
			
		||||
@inject IAliasService AliasService
 | 
			
		||||
@inject IThemeService  ThemeService
 | 
			
		||||
@inject ISettingService  SettingService
 | 
			
		||||
 | 
			
		||||
@if (themes != null)
 | 
			
		||||
{ 
 | 
			
		||||
{
 | 
			
		||||
    <table class="table table-borderless">
 | 
			
		||||
        <tr>
 | 
			
		||||
            <td>
 | 
			
		||||
@ -17,6 +18,14 @@
 | 
			
		||||
                <input class="form-control" @bind="@name" />
 | 
			
		||||
            </td>
 | 
			
		||||
        </tr>
 | 
			
		||||
        <tr>
 | 
			
		||||
            <td>
 | 
			
		||||
                <label for="Name" class="control-label">Tenant: </label>
 | 
			
		||||
            </td>
 | 
			
		||||
            <td>
 | 
			
		||||
                <input class="form-control" @bind="@tenant" readonly />
 | 
			
		||||
            </td>
 | 
			
		||||
        </tr>
 | 
			
		||||
        <tr>
 | 
			
		||||
            <td>
 | 
			
		||||
                <label for="Name" class="control-label">Aliases: </label>
 | 
			
		||||
@ -96,7 +105,7 @@
 | 
			
		||||
    </table>
 | 
			
		||||
 | 
			
		||||
    <a data-toggle="collapse" class="app-link-unstyled" href="#SMTP" aria-expanded="false" aria-controls="SMTP">
 | 
			
		||||
        <h5>SMTP Settings</h5><hr class="app-rule" />
 | 
			
		||||
        <h5><i class="oi oi-chevron-bottom"></i> SMTP Settings</h5><hr class="app-rule" />
 | 
			
		||||
    </a>
 | 
			
		||||
    <div class="collapse" id="SMTP">
 | 
			
		||||
        <table class="table table-borderless">
 | 
			
		||||
@ -142,25 +151,29 @@
 | 
			
		||||
            </tr>
 | 
			
		||||
        </table>
 | 
			
		||||
    </div>
 | 
			
		||||
 | 
			
		||||
    <br />
 | 
			
		||||
    <button type="button" class="btn btn-success" @onclick="SaveSite">Save</button>
 | 
			
		||||
    <NavLink class="btn btn-secondary" href="@NavigateUrl()">Cancel</NavLink>
 | 
			
		||||
    @if (UserSecurity.IsAuthorized(PageState.User, Constants.HostRole))
 | 
			
		||||
    {
 | 
			
		||||
        <ActionDialog Header="Delete Site" Message="@("Are You Sure You Wish To Delete This Site?")" Action="Delete" Security="SecurityAccessLevel.Host" Class="btn btn-danger" OnClick="@(async () => await DeleteSite())" />
 | 
			
		||||
    }
 | 
			
		||||
    <br />
 | 
			
		||||
    <br />
 | 
			
		||||
    <AuditInfo CreatedBy="@createdby" CreatedOn="@createdon" ModifiedBy="@modifiedby" ModifiedOn="@modifiedon" DeletedBy="@deletedby" DeletedOn="@deletedon"></AuditInfo>
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@code {
 | 
			
		||||
    public override SecurityAccessLevel SecurityAccessLevel { get { return SecurityAccessLevel.Host; } }
 | 
			
		||||
    public override SecurityAccessLevel SecurityAccessLevel { get { return SecurityAccessLevel.Admin; } }
 | 
			
		||||
 | 
			
		||||
    Dictionary<string, string> themes;
 | 
			
		||||
    Dictionary<string, string> panelayouts;
 | 
			
		||||
    Dictionary<string, string> containers;
 | 
			
		||||
 | 
			
		||||
    List<Theme> Themes;
 | 
			
		||||
    Alias Alias;
 | 
			
		||||
    int siteid;
 | 
			
		||||
    string name = "";
 | 
			
		||||
    List<Tenant> tenants;
 | 
			
		||||
    string tenant = "";
 | 
			
		||||
    List<Alias> aliases;
 | 
			
		||||
    string urls = "";
 | 
			
		||||
    int logofileid = -1;
 | 
			
		||||
@ -189,12 +202,12 @@
 | 
			
		||||
        {
 | 
			
		||||
            Themes = await ThemeService.GetThemesAsync();
 | 
			
		||||
            aliases = await AliasService.GetAliasesAsync();
 | 
			
		||||
            Alias = aliases.Where(item => item.AliasId == int.Parse(PageState.QueryString["id"])).FirstOrDefault();
 | 
			
		||||
            siteid = Alias.SiteId;
 | 
			
		||||
            Site site = await SiteService.GetSiteAsync(siteid, Alias);
 | 
			
		||||
            Site site = await SiteService.GetSiteAsync(PageState.Site.SiteId);
 | 
			
		||||
            if (site != null)
 | 
			
		||||
            {
 | 
			
		||||
                name = site.Name;
 | 
			
		||||
                tenants = await TenantService.GetTenantsAsync();
 | 
			
		||||
                tenant = tenants.Find(item => item.TenantId == site.TenantId).Name;
 | 
			
		||||
                foreach (Alias alias in aliases.Where(item => item.SiteId == site.SiteId && item.TenantId == site.TenantId).ToList())
 | 
			
		||||
                {
 | 
			
		||||
                    urls += alias.Name + "\n";
 | 
			
		||||
@ -229,7 +242,7 @@
 | 
			
		||||
        }
 | 
			
		||||
        catch (Exception ex)
 | 
			
		||||
        {
 | 
			
		||||
            await logger.LogError(ex, "Error Loading Site {SiteId} {Error}", siteid, ex.Message);
 | 
			
		||||
            await logger.LogError(ex, "Error Loading Site {SiteId} {Error}", PageState.Site.SiteId, ex.Message);
 | 
			
		||||
            AddModuleMessage(ex.Message, MessageType.Error);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
@ -262,7 +275,7 @@
 | 
			
		||||
        {
 | 
			
		||||
            if (name != "" && urls != "" && !string.IsNullOrEmpty(themetype) && (panelayouts.Count == 0 || !string.IsNullOrEmpty(layouttype)) && !string.IsNullOrEmpty(containertype))
 | 
			
		||||
            {
 | 
			
		||||
                Site site = await SiteService.GetSiteAsync(siteid, Alias);
 | 
			
		||||
                Site site = await SiteService.GetSiteAsync(PageState.Site.SiteId);
 | 
			
		||||
                if (site != null)
 | 
			
		||||
                {
 | 
			
		||||
                    site.Name = name;
 | 
			
		||||
@ -277,7 +290,7 @@
 | 
			
		||||
                    site.DefaultContainerType = containertype;
 | 
			
		||||
                    site.IsDeleted = (isdeleted == null ? true : Boolean.Parse(isdeleted));
 | 
			
		||||
 | 
			
		||||
                    site = await SiteService.UpdateSiteAsync(site, Alias);
 | 
			
		||||
                    site = await SiteService.UpdateSiteAsync(site);
 | 
			
		||||
 | 
			
		||||
                    urls = urls.Replace("\n", ",");
 | 
			
		||||
                    string[] names = urls.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
 | 
			
		||||
@ -320,8 +333,23 @@
 | 
			
		||||
        }
 | 
			
		||||
        catch (Exception ex)
 | 
			
		||||
        {
 | 
			
		||||
            await logger.LogError(ex, "Error Saving Site {SiteId} {Error}", siteid, ex.Message);
 | 
			
		||||
            await logger.LogError(ex, "Error Saving Site {SiteId} {Error}", PageState.Site.SiteId, ex.Message);
 | 
			
		||||
            AddModuleMessage("Error Saving Site", MessageType.Error);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private async Task DeleteSite()
 | 
			
		||||
    {
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            await SiteService.DeleteSiteAsync(PageState.Site.SiteId);
 | 
			
		||||
            await logger.LogInformation("Sited Deleted {SiteId}", PageState.Site.SiteId);
 | 
			
		||||
            StateHasChanged();
 | 
			
		||||
        }
 | 
			
		||||
        catch (Exception ex)
 | 
			
		||||
        {
 | 
			
		||||
            await logger.LogError(ex, "Error Deleting Site {SiteId} {Error}", PageState.Site.SiteId, ex.Message);
 | 
			
		||||
            AddModuleMessage("Error Deleting Site", MessageType.Error);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -44,14 +44,6 @@ else
 | 
			
		||||
            <textarea class="form-control" @bind="@urls" rows="3" />
 | 
			
		||||
        </td>
 | 
			
		||||
    </tr>
 | 
			
		||||
    <tr>
 | 
			
		||||
        <td>
 | 
			
		||||
            <label for="Name" class="control-label">Logo: </label>
 | 
			
		||||
        </td>
 | 
			
		||||
        <td>
 | 
			
		||||
            <FileManager @ref="filemanager" />
 | 
			
		||||
        </td>
 | 
			
		||||
    </tr>
 | 
			
		||||
    <tr>
 | 
			
		||||
        <td>
 | 
			
		||||
            <label for="Name" class="control-label">Default Theme: </label>
 | 
			
		||||
@ -130,7 +122,6 @@ else
 | 
			
		||||
    string tenantid = "-1";
 | 
			
		||||
    string name = "";
 | 
			
		||||
    string urls = "";
 | 
			
		||||
    FileManager filemanager;
 | 
			
		||||
    string themetype = "";
 | 
			
		||||
    string layouttype = "";
 | 
			
		||||
    string containertype = "";
 | 
			
		||||
@ -210,6 +201,8 @@ else
 | 
			
		||||
 | 
			
		||||
            if (isvalid)
 | 
			
		||||
            {
 | 
			
		||||
                ShowProgressIndicator();
 | 
			
		||||
 | 
			
		||||
                List<Alias> aliases = new List<Alias>();
 | 
			
		||||
                urls = urls.Replace("\n", ",");
 | 
			
		||||
                foreach (string name in urls.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
 | 
			
		||||
@ -226,11 +219,6 @@ else
 | 
			
		||||
                site.TenantId = int.Parse(tenantid);
 | 
			
		||||
                site.Name = name;
 | 
			
		||||
                site.LogoFileId = null;
 | 
			
		||||
                int logofileid = filemanager.GetFileId();
 | 
			
		||||
                if (logofileid != -1)
 | 
			
		||||
                {
 | 
			
		||||
                    site.LogoFileId = logofileid;
 | 
			
		||||
                }
 | 
			
		||||
                site.DefaultThemeType = themetype;
 | 
			
		||||
                site.DefaultLayoutType = (layouttype == null ? "" : layouttype);
 | 
			
		||||
                site.DefaultContainerType = containertype;
 | 
			
		||||
 | 
			
		||||
@ -19,8 +19,8 @@ else
 | 
			
		||||
            <th>Name</th>
 | 
			
		||||
        </Header>
 | 
			
		||||
        <Row>
 | 
			
		||||
            <td><ActionLink Action="Edit" Parameters="@($"id=" + context.AliasId.ToString())" /></td>
 | 
			
		||||
            <td><ActionDialog Header="Delete Site" Message="@("Are You Sure You Wish To Delete The " + context.Name + " Site?")" Action="Delete" Security="SecurityAccessLevel.Host" Class="btn btn-danger" OnClick="@(async () => await DeleteSite(context))" Disabled="sites.Count == 1" /></td>
 | 
			
		||||
            <td><NavLink class="btn btn-primary" href="@(scheme + context.Name + "/admin/site")">Edit</NavLink></td>
 | 
			
		||||
            <td><NavLink class="btn btn-danger" href="@(scheme + context.Name + "/admin/site")">Delete</NavLink></td>
 | 
			
		||||
            <td><a href="@(scheme + context.Name)">@context.Name</a></td>
 | 
			
		||||
        </Row>
 | 
			
		||||
    </Pager>
 | 
			
		||||
@ -47,19 +47,4 @@ else
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private async Task DeleteSite(Alias Alias)
 | 
			
		||||
    {
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            await SiteService.DeleteSiteAsync(Alias.SiteId, Alias);
 | 
			
		||||
            await logger.LogInformation("Sited Deleted {Alias}", Alias);
 | 
			
		||||
            StateHasChanged();
 | 
			
		||||
        }
 | 
			
		||||
        catch (Exception ex)
 | 
			
		||||
        {
 | 
			
		||||
            await logger.LogError(ex, "Error Deleting Site {Error}", ex.Message);
 | 
			
		||||
            AddModuleMessage("Error Deleting Site", MessageType.Error);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -4,32 +4,78 @@
 | 
			
		||||
@inject ITenantService TenantService
 | 
			
		||||
@inject IInstallationService InstallationService
 | 
			
		||||
 | 
			
		||||
<table class="table table-borderless">
 | 
			
		||||
    <tr>
 | 
			
		||||
        <td>
 | 
			
		||||
            <label for="Name" class="control-label">Name: </label>
 | 
			
		||||
        </td>
 | 
			
		||||
        <td>
 | 
			
		||||
            <input class="form-control" @bind="@name" />
 | 
			
		||||
        </td>
 | 
			
		||||
    </tr>
 | 
			
		||||
    <tr>
 | 
			
		||||
        <td>
 | 
			
		||||
            <label for="Name" class="control-label">Connection String: </label>
 | 
			
		||||
        </td>
 | 
			
		||||
        <td>
 | 
			
		||||
            <input class="form-control" @bind="@connectionstring" />
 | 
			
		||||
        </td>
 | 
			
		||||
    </tr>
 | 
			
		||||
    <tr>
 | 
			
		||||
        <td>
 | 
			
		||||
            <label for="Name" class="control-label">Schema: </label>
 | 
			
		||||
        </td>
 | 
			
		||||
        <td>
 | 
			
		||||
            <input class="form-control" @bind="@schema" />
 | 
			
		||||
        </td>
 | 
			
		||||
    </tr>
 | 
			
		||||
</table>
 | 
			
		||||
    <table class="table table-borderless">
 | 
			
		||||
        <tr>
 | 
			
		||||
            <td>
 | 
			
		||||
                <label for="Name" class="control-label">Name: </label>
 | 
			
		||||
            </td>
 | 
			
		||||
            <td>
 | 
			
		||||
                <input class="form-control" @bind="@name" />
 | 
			
		||||
            </td>
 | 
			
		||||
        </tr>
 | 
			
		||||
        <tr>
 | 
			
		||||
            <td>
 | 
			
		||||
                <label for="Title" class="control-label">Database Type: </label>
 | 
			
		||||
            </td>
 | 
			
		||||
            <td>
 | 
			
		||||
                <select class="custom-select" @bind="@type">
 | 
			
		||||
                    <option value="LocalDB">Local Database</option>
 | 
			
		||||
                    <option value="SQLServer">SQL Server</option>
 | 
			
		||||
                </select>
 | 
			
		||||
            </td>
 | 
			
		||||
        </tr>
 | 
			
		||||
        <tr>
 | 
			
		||||
            <td>
 | 
			
		||||
                <label for="Title" class="control-label">Server: </label>
 | 
			
		||||
            </td>
 | 
			
		||||
            <td>
 | 
			
		||||
                <input type="text" class="form-control" @bind="@server" />
 | 
			
		||||
            </td>
 | 
			
		||||
        </tr>
 | 
			
		||||
        <tr>
 | 
			
		||||
            <td>
 | 
			
		||||
                <label for="Title" class="control-label">Database: </label>
 | 
			
		||||
            </td>
 | 
			
		||||
            <td>
 | 
			
		||||
                <input type="text" class="form-control" @bind="@database" />
 | 
			
		||||
            </td>
 | 
			
		||||
        </tr>
 | 
			
		||||
        <tr>
 | 
			
		||||
            <td>
 | 
			
		||||
                <label for="Title" class="control-label">Integrated Security: </label>
 | 
			
		||||
            </td>
 | 
			
		||||
            <td>
 | 
			
		||||
                <select class="custom-select" @onchange="SetIntegratedSecurity">
 | 
			
		||||
                    <option value="true" selected>True</option>
 | 
			
		||||
                    <option value="false">False</option>
 | 
			
		||||
                </select>
 | 
			
		||||
            </td>
 | 
			
		||||
        </tr>
 | 
			
		||||
        <tr style="@integratedsecurity">
 | 
			
		||||
            <td>
 | 
			
		||||
                <label for="Title" class="control-label">Username: </label>
 | 
			
		||||
            </td>
 | 
			
		||||
            <td>
 | 
			
		||||
                <input type="text" class="form-control" @bind="@username" />
 | 
			
		||||
            </td>
 | 
			
		||||
        </tr>
 | 
			
		||||
        <tr style="@integratedsecurity">
 | 
			
		||||
            <td>
 | 
			
		||||
                <label for="Title" class="control-label">Password: </label>
 | 
			
		||||
            </td>
 | 
			
		||||
            <td>
 | 
			
		||||
                <input type="password" class="form-control" @bind="@password" />
 | 
			
		||||
            </td>
 | 
			
		||||
        </tr>
 | 
			
		||||
        <tr>
 | 
			
		||||
            <td>
 | 
			
		||||
                <label for="Name" class="control-label">Schema: </label>
 | 
			
		||||
            </td>
 | 
			
		||||
            <td>
 | 
			
		||||
                <input class="form-control" @bind="@schema" />
 | 
			
		||||
            </td>
 | 
			
		||||
        </tr>
 | 
			
		||||
    </table>
 | 
			
		||||
<button type="button" class="btn btn-success" @onclick="SaveTenant">Save</button>
 | 
			
		||||
<NavLink class="btn btn-secondary" href="@NavigateUrl()">Cancel</NavLink>
 | 
			
		||||
 | 
			
		||||
@ -37,46 +83,72 @@
 | 
			
		||||
    public override SecurityAccessLevel SecurityAccessLevel { get { return SecurityAccessLevel.Host; } }
 | 
			
		||||
 | 
			
		||||
    string name = "";
 | 
			
		||||
    string connectionstring = "";
 | 
			
		||||
    string type = "LocalDB";
 | 
			
		||||
    string server = "(LocalDb)\\MSSQLLocalDB";
 | 
			
		||||
    string database = "Oqtane-" + DateTime.Now.ToString("yyyyMMddHHmm");
 | 
			
		||||
    string username = "";
 | 
			
		||||
    string password = "";
 | 
			
		||||
    string schema = "";
 | 
			
		||||
    string integratedsecurity = "display: none;";
 | 
			
		||||
 | 
			
		||||
    protected override async Task OnInitializedAsync()
 | 
			
		||||
    private void SetIntegratedSecurity(ChangeEventArgs e)
 | 
			
		||||
    {
 | 
			
		||||
        try
 | 
			
		||||
        if (Convert.ToBoolean((string)e.Value))
 | 
			
		||||
        {
 | 
			
		||||
            List<Tenant> tenants = await TenantService.GetTenantsAsync();
 | 
			
		||||
            connectionstring = tenants.FirstOrDefault().DBConnectionString;
 | 
			
		||||
            schema = tenants.FirstOrDefault().DBSchema;
 | 
			
		||||
            integratedsecurity = "display: none;";
 | 
			
		||||
        }
 | 
			
		||||
        catch (Exception ex)
 | 
			
		||||
        else
 | 
			
		||||
        {
 | 
			
		||||
            await logger.LogError(ex, "Error Loading Tenants {Error}", ex.Message);
 | 
			
		||||
            AddModuleMessage("Error Loading Tenants", MessageType.Error);
 | 
			
		||||
            integratedsecurity = "";
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private async Task SaveTenant()
 | 
			
		||||
    {
 | 
			
		||||
        ShowProgressIndicator();
 | 
			
		||||
 | 
			
		||||
        connectionstring = connectionstring.Replace("\\\\", "\\");
 | 
			
		||||
        GenericResponse response = await InstallationService.Install(connectionstring);
 | 
			
		||||
        if (response.Success)
 | 
			
		||||
        if (!string.IsNullOrEmpty(name))
 | 
			
		||||
        {
 | 
			
		||||
            Tenant tenant = new Tenant();
 | 
			
		||||
            tenant.Name = name;
 | 
			
		||||
            tenant.DBConnectionString = connectionstring;
 | 
			
		||||
            tenant.DBSchema = schema;
 | 
			
		||||
            tenant.IsInitialized = false;
 | 
			
		||||
            await TenantService.AddTenantAsync(tenant);
 | 
			
		||||
            await logger.LogInformation("Tenant Created {Tenant}", tenant);
 | 
			
		||||
            ShowProgressIndicator();
 | 
			
		||||
 | 
			
		||||
            NavigationManager.NavigateTo(NavigateUrl());
 | 
			
		||||
            string connectionstring = "";
 | 
			
		||||
            if (type == "LocalDB")
 | 
			
		||||
            {
 | 
			
		||||
                connectionstring = "Data Source=" + server + ";AttachDbFilename=|DataDirectory|\\" + database + ".mdf;Initial Catalog=" + database + ";Integrated Security=SSPI;";
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
                connectionstring = "Data Source=" + server + ";Initial Catalog=" + database + ";";
 | 
			
		||||
                if (integratedsecurity == "display: none;")
 | 
			
		||||
                {
 | 
			
		||||
                    connectionstring += "Integrated Security=SSPI;";
 | 
			
		||||
                }
 | 
			
		||||
                else
 | 
			
		||||
                {
 | 
			
		||||
                    connectionstring += "User ID=" + username + ";Password=" + password;
 | 
			
		||||
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            GenericResponse response = await InstallationService.Install(connectionstring);
 | 
			
		||||
            if (response.Success)
 | 
			
		||||
            {
 | 
			
		||||
                Tenant tenant = new Tenant();
 | 
			
		||||
                tenant.Name = name;
 | 
			
		||||
                tenant.DBConnectionString = connectionstring;
 | 
			
		||||
                tenant.DBSchema = schema;
 | 
			
		||||
                tenant.IsInitialized = false;
 | 
			
		||||
                await TenantService.AddTenantAsync(tenant);
 | 
			
		||||
                await logger.LogInformation("Tenant Created {Tenant}", tenant);
 | 
			
		||||
 | 
			
		||||
                NavigationManager.NavigateTo(NavigateUrl());
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
                await logger.LogError("Error Creating Tenant {Error}", response.Message);
 | 
			
		||||
                AddModuleMessage(response.Message, MessageType.Error);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
        {
 | 
			
		||||
            await logger.LogError("Error Creating Tenant {Error}", response.Message);
 | 
			
		||||
            AddModuleMessage(response.Message, MessageType.Error);
 | 
			
		||||
            AddModuleMessage("You Must Provide A Name For The Tenant", MessageType.Warning);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -93,7 +93,6 @@
 | 
			
		||||
    [Parameter]
 | 
			
		||||
    public string UploadMultiple { get; set; } // optional - enable multiple file uploads - default false
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    string id;
 | 
			
		||||
    List<Folder> folders;
 | 
			
		||||
    int folderid = -1;
 | 
			
		||||
 | 
			
		||||
@ -9,15 +9,11 @@ namespace Oqtane.Services
 | 
			
		||||
        Task<List<Site>> GetSitesAsync();
 | 
			
		||||
 | 
			
		||||
        Task<Site> GetSiteAsync(int SiteId);
 | 
			
		||||
        Task<Site> GetSiteAsync(int SiteId, Alias Alias);
 | 
			
		||||
 | 
			
		||||
        Task<Site> AddSiteAsync(Site Site);
 | 
			
		||||
        Task<Site> AddSiteAsync(Site Site, Alias Alias);
 | 
			
		||||
 | 
			
		||||
        Task<Site> UpdateSiteAsync(Site Site);
 | 
			
		||||
        Task<Site> UpdateSiteAsync(Site Site, Alias Alias);
 | 
			
		||||
 | 
			
		||||
        Task DeleteSiteAsync(int SiteId);
 | 
			
		||||
        Task DeleteSiteAsync(int SiteId, Alias Alias);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -12,7 +12,7 @@ namespace Oqtane.Services
 | 
			
		||||
 | 
			
		||||
        Task<User> AddUserAsync(User User);
 | 
			
		||||
 | 
			
		||||
        Task<User> AddUserAsync(User User, Alias alias);
 | 
			
		||||
        Task<User> AddUserAsync(User User, Alias Alias);
 | 
			
		||||
 | 
			
		||||
        Task<User> UpdateUserAsync(User User);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -36,37 +36,27 @@ namespace Oqtane.Services
 | 
			
		||||
        {
 | 
			
		||||
            return await http.GetJsonAsync<Site>(apiurl + "/" + SiteId.ToString());
 | 
			
		||||
        }
 | 
			
		||||
        public async Task<Site> GetSiteAsync(int SiteId, Alias Alias)
 | 
			
		||||
        {
 | 
			
		||||
            return await http.GetJsonAsync<Site>(CreateApiUrl(Alias, NavigationManager.Uri, "Site") + "/" + SiteId.ToString());
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task<Site> AddSiteAsync(Site Site)
 | 
			
		||||
        {
 | 
			
		||||
            return await http.PostJsonAsync<Site>(apiurl, Site);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task<Site> AddSiteAsync(Site Site, Alias Alias)
 | 
			
		||||
        {
 | 
			
		||||
            return await http.PostJsonAsync<Site>(CreateApiUrl(Alias, NavigationManager.Uri, "Site"), Site);
 | 
			
		||||
            if (Alias == null)
 | 
			
		||||
            {
 | 
			
		||||
                return await http.PostJsonAsync<Site>(apiurl, Site);
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
                return await http.PostJsonAsync<Site>(CreateApiUrl(Alias, NavigationManager.Uri, "Site"), Site);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task<Site> UpdateSiteAsync(Site Site)
 | 
			
		||||
        {
 | 
			
		||||
            return await http.PutJsonAsync<Site>(apiurl + "/" + Site.SiteId.ToString(), Site);
 | 
			
		||||
        }
 | 
			
		||||
        public async Task<Site> UpdateSiteAsync(Site Site, Alias Alias)
 | 
			
		||||
        {
 | 
			
		||||
            return await http.PutJsonAsync<Site>(CreateApiUrl(Alias, NavigationManager.Uri, "Site") + "/" + Site.SiteId.ToString(), Site);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task DeleteSiteAsync(int SiteId)
 | 
			
		||||
        {
 | 
			
		||||
            await http.DeleteAsync(apiurl + "/" + SiteId.ToString());
 | 
			
		||||
        }
 | 
			
		||||
        public async Task DeleteSiteAsync(int SiteId, Alias Alias)
 | 
			
		||||
        {
 | 
			
		||||
            await http.DeleteAsync(CreateApiUrl(Alias, NavigationManager.Uri, "Site") + "/" + SiteId.ToString());
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -187,7 +187,7 @@
 | 
			
		||||
                site.DefaultThemeType = Constants.DefaultTheme;
 | 
			
		||||
                site.DefaultLayoutType = Constants.DefaultLayout;
 | 
			
		||||
                site.DefaultContainerType = Constants.DefaultContainer;
 | 
			
		||||
                site = await SiteService.AddSiteAsync(site);
 | 
			
		||||
                site = await SiteService.AddSiteAsync(site, null);
 | 
			
		||||
 | 
			
		||||
                User user = new User();
 | 
			
		||||
                user.SiteId = site.SiteId;
 | 
			
		||||
 | 
			
		||||
@ -7,6 +7,7 @@ using Oqtane.Shared;
 | 
			
		||||
using Oqtane.Infrastructure;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using System;
 | 
			
		||||
using System.Net;
 | 
			
		||||
 | 
			
		||||
namespace Oqtane.Controllers
 | 
			
		||||
{
 | 
			
		||||
@ -42,6 +43,7 @@ namespace Oqtane.Controllers
 | 
			
		||||
        [HttpGet("name/{name}")]
 | 
			
		||||
        public Alias Get(string name)
 | 
			
		||||
        {
 | 
			
		||||
            name = WebUtility.UrlDecode(name);
 | 
			
		||||
            List<Alias> aliases = Aliases.GetAliases().ToList();
 | 
			
		||||
            Alias alias = null;
 | 
			
		||||
            alias = aliases.Where(item => item.Name == name).FirstOrDefault();
 | 
			
		||||
 | 
			
		||||
@ -63,7 +63,7 @@ namespace Oqtane.Controllers
 | 
			
		||||
                if (authorized)
 | 
			
		||||
                {
 | 
			
		||||
                    Site = Sites.AddSite(Site);
 | 
			
		||||
                    logger.Log(LogLevel.Information, this, LogFunction.Create, "Site Added {Site}", Site);
 | 
			
		||||
                    logger.Log(Site.SiteId, LogLevel.Information, this, LogFunction.Create, "Site Added {Site}", Site);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            return Site;
 | 
			
		||||
@ -77,7 +77,7 @@ namespace Oqtane.Controllers
 | 
			
		||||
            if (ModelState.IsValid)
 | 
			
		||||
            {
 | 
			
		||||
                Site = Sites.UpdateSite(Site);
 | 
			
		||||
                logger.Log(LogLevel.Information, this, LogFunction.Update, "Site Updated {Site}", Site);
 | 
			
		||||
                logger.Log(Site.SiteId, LogLevel.Information, this, LogFunction.Update, "Site Updated {Site}", Site);
 | 
			
		||||
            }
 | 
			
		||||
            return Site;
 | 
			
		||||
        }
 | 
			
		||||
@ -87,14 +87,15 @@ namespace Oqtane.Controllers
 | 
			
		||||
        [Authorize(Roles = Constants.HostRole)]
 | 
			
		||||
        public void Delete(int id)
 | 
			
		||||
        {
 | 
			
		||||
            Site Site = Sites.GetSite(id);
 | 
			
		||||
            if (Sites.GetSites().Count() > 1)
 | 
			
		||||
            {
 | 
			
		||||
                Sites.DeleteSite(id);
 | 
			
		||||
                logger.Log(LogLevel.Information, this, LogFunction.Delete, "Site Deleted {SiteId}", id);
 | 
			
		||||
                logger.Log(Site.SiteId, LogLevel.Information, this, LogFunction.Delete, "Site Deleted {SiteId}", id);
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
                logger.Log(LogLevel.Warning, this, LogFunction.Delete, "Unable to delete the root site.");
 | 
			
		||||
                logger.Log(Site.SiteId, LogLevel.Warning, this, LogFunction.Delete, "Unable to delete the root site.");
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -160,7 +160,7 @@ namespace Oqtane.Controllers
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
                user.Password = ""; // remove sensitive information
 | 
			
		||||
                logger.Log(LogLevel.Information, this, LogFunction.Create, "User Added {User}", user);
 | 
			
		||||
                logger.Log(User.SiteId, LogLevel.Information, this, LogFunction.Create, "User Added {User}", user);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            return user;
 | 
			
		||||
 | 
			
		||||
@ -8,6 +8,8 @@ namespace Oqtane.Infrastructure
 | 
			
		||||
    {
 | 
			
		||||
        void Log(LogLevel Level, object Class, LogFunction Function, string Message, params object[] Args);
 | 
			
		||||
        void Log(LogLevel Level, object Class, LogFunction Function, Exception Exception, string Message, params object[] Args);
 | 
			
		||||
        void Log(int SiteId, LogLevel Level, object Class, LogFunction Function, string Message, params object[] Args);
 | 
			
		||||
        void Log(int SiteId, LogLevel Level, object Class, LogFunction Function, Exception Exception, string Message, params object[] Args);
 | 
			
		||||
        void Log(Log Log);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -29,14 +29,35 @@ namespace Oqtane.Infrastructure
 | 
			
		||||
 | 
			
		||||
        public void Log(LogLevel Level, object Class, LogFunction Function, string Message, params object[] Args)
 | 
			
		||||
        {
 | 
			
		||||
            Log(Level, Class.GetType().AssemblyQualifiedName, Function, null, Message, Args);
 | 
			
		||||
            Log(-1, Level, Class.GetType().AssemblyQualifiedName, Function, null, Message, Args);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void Log(LogLevel Level, object Class, LogFunction Function, Exception Exception, string Message, params object[] Args)
 | 
			
		||||
        {
 | 
			
		||||
            Alias alias = TenantResolver.GetAlias();
 | 
			
		||||
            Log(-1, Level, Class.GetType().AssemblyQualifiedName, Function, Exception, Message, Args);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void Log(int SiteId, LogLevel Level, object Class, LogFunction Function, string Message, params object[] Args)
 | 
			
		||||
        {
 | 
			
		||||
            Log(SiteId, Level, Class.GetType().AssemblyQualifiedName, Function, null, Message, Args);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void Log(int SiteId, LogLevel Level, object Class, LogFunction Function, Exception Exception, string Message, params object[] Args)
 | 
			
		||||
        {
 | 
			
		||||
            Log log = new Log();
 | 
			
		||||
            log.SiteId = alias.SiteId;
 | 
			
		||||
            if (SiteId == -1)
 | 
			
		||||
            {
 | 
			
		||||
                log.SiteId = null;
 | 
			
		||||
                Alias alias = TenantResolver.GetAlias();
 | 
			
		||||
                if (alias != null)
 | 
			
		||||
                {
 | 
			
		||||
                    log.SiteId = alias.SiteId;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
                log.SiteId = SiteId;
 | 
			
		||||
            }
 | 
			
		||||
            log.PageId = null;
 | 
			
		||||
            log.ModuleId = null;
 | 
			
		||||
            log.UserId = null;
 | 
			
		||||
@ -96,56 +117,70 @@ namespace Oqtane.Infrastructure
 | 
			
		||||
                Log.Server = Environment.MachineName;
 | 
			
		||||
                Log.MessageTemplate = Log.Message;
 | 
			
		||||
                Log = ProcessStructuredLog(Log);
 | 
			
		||||
                Logs.AddLog(Log);
 | 
			
		||||
                try
 | 
			
		||||
                {
 | 
			
		||||
                    Logs.AddLog(Log);
 | 
			
		||||
                }
 | 
			
		||||
                catch
 | 
			
		||||
                {
 | 
			
		||||
                    // an error occurred writing to the database
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private Log ProcessStructuredLog(Log Log)
 | 
			
		||||
        {
 | 
			
		||||
            string message = Log.Message;
 | 
			
		||||
            string properties = "";
 | 
			
		||||
            if (!string.IsNullOrEmpty(message) && message.Contains("{") && message.Contains("}") && !string.IsNullOrEmpty(Log.Properties))
 | 
			
		||||
            try
 | 
			
		||||
            {
 | 
			
		||||
                // get the named holes in the message and replace values
 | 
			
		||||
                object[] values = JsonSerializer.Deserialize<object[]>(Log.Properties);
 | 
			
		||||
                List<string> names = new List<string>();
 | 
			
		||||
                int index = message.IndexOf("{");
 | 
			
		||||
                while (index != -1)
 | 
			
		||||
                string message = Log.Message;
 | 
			
		||||
                string properties = "";
 | 
			
		||||
                if (!string.IsNullOrEmpty(message) && message.Contains("{") && message.Contains("}") && !string.IsNullOrEmpty(Log.Properties))
 | 
			
		||||
                {
 | 
			
		||||
                    if (message.IndexOf("}", index) != -1)
 | 
			
		||||
                    // get the named holes in the message and replace values
 | 
			
		||||
                    object[] values = JsonSerializer.Deserialize<object[]>(Log.Properties);
 | 
			
		||||
                    List<string> names = new List<string>();
 | 
			
		||||
                    int index = message.IndexOf("{");
 | 
			
		||||
                    while (index != -1)
 | 
			
		||||
                    {
 | 
			
		||||
                        names.Add(message.Substring(index + 1, message.IndexOf("}", index) - index - 1));
 | 
			
		||||
                        if (values.Length > (names.Count - 1))
 | 
			
		||||
                        if (message.IndexOf("}", index) != -1)
 | 
			
		||||
                        {
 | 
			
		||||
                            if (values[names.Count - 1] == null)
 | 
			
		||||
                            names.Add(message.Substring(index + 1, message.IndexOf("}", index) - index - 1));
 | 
			
		||||
                            if (values.Length > (names.Count - 1))
 | 
			
		||||
                            {
 | 
			
		||||
                                message = message.Replace("{" + names[names.Count - 1] + "}", "null");
 | 
			
		||||
                            }
 | 
			
		||||
                            else
 | 
			
		||||
                            {
 | 
			
		||||
                                message = message.Replace("{" + names[names.Count - 1] + "}", values[names.Count - 1].ToString());
 | 
			
		||||
                                if (values[names.Count - 1] == null)
 | 
			
		||||
                                {
 | 
			
		||||
                                    message = message.Replace("{" + names[names.Count - 1] + "}", "null");
 | 
			
		||||
                                }
 | 
			
		||||
                                else
 | 
			
		||||
                                {
 | 
			
		||||
                                    message = message.Replace("{" + names[names.Count - 1] + "}", values[names.Count - 1].ToString());
 | 
			
		||||
                                }
 | 
			
		||||
                            }
 | 
			
		||||
                        }
 | 
			
		||||
                        index = message.IndexOf("{", index + 1);
 | 
			
		||||
                    }
 | 
			
		||||
                    index = message.IndexOf("{", index + 1);
 | 
			
		||||
                }
 | 
			
		||||
                // rebuild properties into dictionary
 | 
			
		||||
                Dictionary<string, object> propertydictionary = new Dictionary<string, object>();
 | 
			
		||||
                for (int i = 0; i < values.Length; i++)
 | 
			
		||||
                {
 | 
			
		||||
                    if (i < names.Count)
 | 
			
		||||
                    // rebuild properties into dictionary
 | 
			
		||||
                    Dictionary<string, object> propertydictionary = new Dictionary<string, object>();
 | 
			
		||||
                    for (int i = 0; i < values.Length; i++)
 | 
			
		||||
                    {
 | 
			
		||||
                        propertydictionary.Add(names[i], values[i]);
 | 
			
		||||
                    }
 | 
			
		||||
                    else
 | 
			
		||||
                    {
 | 
			
		||||
                        propertydictionary.Add("Property" + i.ToString(), values[i]);
 | 
			
		||||
                        if (i < names.Count)
 | 
			
		||||
                        {
 | 
			
		||||
                            propertydictionary.Add(names[i], values[i]);
 | 
			
		||||
                        }
 | 
			
		||||
                        else
 | 
			
		||||
                        {
 | 
			
		||||
                            propertydictionary.Add("Property" + i.ToString(), values[i]);
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                    properties = JsonSerializer.Serialize(propertydictionary);
 | 
			
		||||
                }
 | 
			
		||||
                properties = JsonSerializer.Serialize(propertydictionary);
 | 
			
		||||
                Log.Message = message;
 | 
			
		||||
                Log.Properties = properties;
 | 
			
		||||
            }
 | 
			
		||||
            catch
 | 
			
		||||
            {
 | 
			
		||||
                Log.Properties = "";
 | 
			
		||||
            }
 | 
			
		||||
            Log.Message = message;
 | 
			
		||||
            Log.Properties = properties;
 | 
			
		||||
            return Log;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -66,6 +66,9 @@ namespace Oqtane.Repository
 | 
			
		||||
            SiteTemplate.Add(new PageTemplate { Name = "Site Management", Parent = "Admin", Path = "admin/sites", Icon = "globe", IsNavigation = false, IsPersonalizable = false, EditMode = true, PagePermissions = "[{\"PermissionName\":\"View\",\"Permissions\":\"Administrators\"},{\"PermissionName\":\"Edit\",\"Permissions\":\"Administrators\"}]", PageTemplateModules = new List<PageTemplateModule> {
 | 
			
		||||
                new PageTemplateModule { ModuleDefinitionName = "Oqtane.Modules.Admin.Sites, Oqtane.Client", Title = "Site Management", Pane = "Content", ModulePermissions = "[{\"PermissionName\":\"View\",\"Permissions\":\"Administrators\"},{\"PermissionName\":\"Edit\",\"Permissions\":\"Administrators\"}]", Content = "" }
 | 
			
		||||
            }});
 | 
			
		||||
            SiteTemplate.Add(new PageTemplate { Name = "Site Settings", Parent = "Admin", Path = "admin/site", Icon = "home", IsNavigation = false, IsPersonalizable = false, EditMode = true, PagePermissions = "[{\"PermissionName\":\"View\",\"Permissions\":\"Administrators\"},{\"PermissionName\":\"Edit\",\"Permissions\":\"Administrators\"}]", PageTemplateModules = new List<PageTemplateModule> {
 | 
			
		||||
                new PageTemplateModule { ModuleDefinitionName = "Oqtane.Modules.Admin.Site, Oqtane.Client", Title = "Site Settings", Pane = "Content", ModulePermissions = "[{\"PermissionName\":\"View\",\"Permissions\":\"Administrators\"},{\"PermissionName\":\"Edit\",\"Permissions\":\"Administrators\"}]", Content = "" }
 | 
			
		||||
            }});
 | 
			
		||||
            SiteTemplate.Add(new PageTemplate { Name = "Page Management", Parent = "Admin", Path = "admin/pages", Icon = "layers", IsNavigation = false, IsPersonalizable = false, EditMode = true, PagePermissions = "[{\"PermissionName\":\"View\",\"Permissions\":\"Administrators\"},{\"PermissionName\":\"Edit\",\"Permissions\":\"Administrators\"}]", PageTemplateModules = new List<PageTemplateModule> {
 | 
			
		||||
                new PageTemplateModule { ModuleDefinitionName = "Oqtane.Modules.Admin.Pages, Oqtane.Client", Title = "Page Management", Pane = "Content", ModulePermissions = "[{\"PermissionName\":\"View\",\"Permissions\":\"Administrators\"},{\"PermissionName\":\"Edit\",\"Permissions\":\"Administrators\"}]", Content = "" }
 | 
			
		||||
            }});
 | 
			
		||||
 | 
			
		||||
@ -212,7 +212,7 @@ GO
 | 
			
		||||
CREATE TABLE [dbo].[Log] (
 | 
			
		||||
 | 
			
		||||
   [LogId] [int] IDENTITY(1,1) NOT NULL,
 | 
			
		||||
   [SiteId] [int] NOT NULL,
 | 
			
		||||
   [SiteId] [int] NULL,
 | 
			
		||||
   [LogDate] [datetime] NOT NULL,
 | 
			
		||||
   [PageId] [int] NULL,
 | 
			
		||||
   [ModuleId] [int] NULL,
 | 
			
		||||
 | 
			
		||||
@ -6,7 +6,7 @@ namespace Oqtane.Models
 | 
			
		||||
    public class Log
 | 
			
		||||
    {
 | 
			
		||||
        public int LogId { get; set; }
 | 
			
		||||
        public int SiteId { get; set; }
 | 
			
		||||
        public int? SiteId { get; set; }
 | 
			
		||||
        public DateTime LogDate { get; set; }
 | 
			
		||||
        public int? PageId { get; set; }
 | 
			
		||||
        public int? ModuleId { get; set; }
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user