optimized cross tenant logic and fixed bug related to reordering modules in panes
This commit is contained in:
		@ -1,6 +1,5 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Net;
 | 
			
		||||
using System.Net.Http;
 | 
			
		||||
using System.Text.Json;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
@ -69,14 +68,7 @@ namespace Oqtane.Services
 | 
			
		||||
            log.Message = message;
 | 
			
		||||
            log.MessageTemplate = "";
 | 
			
		||||
            log.Properties = JsonSerializer.Serialize(args);
 | 
			
		||||
            if (Alias == null)
 | 
			
		||||
            {
 | 
			
		||||
                await http.PostJsonAsync(apiurl, log);
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
                await http.PostJsonAsync(apiurl + "?alias=" + WebUtility.UrlEncode(Alias.Name), log);
 | 
			
		||||
            }
 | 
			
		||||
            await http.PostJsonAsync(CreateCrossTenantUrl(apiurl, Alias), log);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -1,7 +1,5 @@
 | 
			
		||||
using System;
 | 
			
		||||
using Microsoft.AspNetCore.Components;
 | 
			
		||||
using Oqtane.Models;
 | 
			
		||||
using Oqtane.Shared;
 | 
			
		||||
 | 
			
		||||
namespace Oqtane.Services
 | 
			
		||||
{
 | 
			
		||||
@ -30,5 +28,15 @@ namespace Oqtane.Services
 | 
			
		||||
            apiurl += "api/" + serviceName;
 | 
			
		||||
            return apiurl;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public static string CreateCrossTenantUrl(string url, Alias alias)
 | 
			
		||||
        {
 | 
			
		||||
            if (alias != null)
 | 
			
		||||
            {
 | 
			
		||||
                url += (url.Contains("?")) ? "&" : "?";
 | 
			
		||||
                url += "aliasid=" + alias.AliasId.ToString();
 | 
			
		||||
            }
 | 
			
		||||
            return url;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -5,7 +5,6 @@ using System.Linq;
 | 
			
		||||
using Microsoft.AspNetCore.Components;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using Oqtane.Shared;
 | 
			
		||||
using System.Net;
 | 
			
		||||
 | 
			
		||||
namespace Oqtane.Services
 | 
			
		||||
{
 | 
			
		||||
@ -27,40 +26,30 @@ namespace Oqtane.Services
 | 
			
		||||
            get { return CreateApiUrl(sitestate.Alias, NavigationManager.Uri, "Site"); }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private string urlsuffix(Alias Alias)
 | 
			
		||||
        {
 | 
			
		||||
            string querystring = "";
 | 
			
		||||
            if (Alias != null)
 | 
			
		||||
            {
 | 
			
		||||
                querystring = "?alias=" + WebUtility.UrlEncode(Alias.Name);
 | 
			
		||||
            }
 | 
			
		||||
            return querystring;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task<List<Site>> GetSitesAsync(Alias Alias)
 | 
			
		||||
        {
 | 
			
		||||
            List<Site> sites = await http.GetJsonAsync<List<Site>>(apiurl + urlsuffix(Alias));
 | 
			
		||||
            List<Site> sites = await http.GetJsonAsync<List<Site>>(CreateCrossTenantUrl(apiurl, Alias));
 | 
			
		||||
            return sites.OrderBy(item => item.Name).ToList();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task<Site> GetSiteAsync(int SiteId, Alias Alias)
 | 
			
		||||
        {
 | 
			
		||||
            return await http.GetJsonAsync<Site>(apiurl + "/" + SiteId.ToString() + urlsuffix(Alias));
 | 
			
		||||
            return await http.GetJsonAsync<Site>(CreateCrossTenantUrl(apiurl + "/" + SiteId.ToString(), Alias));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task<Site> AddSiteAsync(Site Site, Alias Alias)
 | 
			
		||||
        {
 | 
			
		||||
            return await http.PostJsonAsync<Site>(apiurl + urlsuffix(Alias), Site);
 | 
			
		||||
            return await http.PostJsonAsync<Site>(CreateCrossTenantUrl(apiurl, Alias), Site);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task<Site> UpdateSiteAsync(Site Site, Alias Alias)
 | 
			
		||||
        {
 | 
			
		||||
            return await http.PutJsonAsync<Site>(apiurl + "/" + Site.SiteId.ToString() + urlsuffix(Alias), Site);
 | 
			
		||||
            return await http.PutJsonAsync<Site>(CreateCrossTenantUrl(apiurl + "/" + Site.SiteId.ToString(), Alias), Site);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task DeleteSiteAsync(int SiteId, Alias Alias)
 | 
			
		||||
        {
 | 
			
		||||
            await http.DeleteAsync(apiurl + "/" + SiteId.ToString() + urlsuffix(Alias));
 | 
			
		||||
            await http.DeleteAsync(CreateCrossTenantUrl(apiurl + "/" + SiteId.ToString(), Alias));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -1,12 +1,8 @@
 | 
			
		||||
using Oqtane.Shared;
 | 
			
		||||
using Oqtane.Models;
 | 
			
		||||
using System;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using System.Net.Http;
 | 
			
		||||
using Microsoft.AspNetCore.Components;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Net;
 | 
			
		||||
 | 
			
		||||
namespace Oqtane.Services
 | 
			
		||||
{
 | 
			
		||||
@ -54,7 +50,7 @@ namespace Oqtane.Services
 | 
			
		||||
        {
 | 
			
		||||
            try
 | 
			
		||||
            {
 | 
			
		||||
                return await http.PostJsonAsync<User>(apiurl + "?alias=" + WebUtility.UrlEncode(Alias.Name), User);
 | 
			
		||||
                return await http.PostJsonAsync<User>(CreateCrossTenantUrl(apiurl, Alias), User);
 | 
			
		||||
            }
 | 
			
		||||
            catch
 | 
			
		||||
            {
 | 
			
		||||
 | 
			
		||||
@ -106,8 +106,8 @@ namespace Oqtane.Controllers
 | 
			
		||||
            if (UserPermissions.IsAuthorized(User, "Page", pageid, "Edit"))
 | 
			
		||||
            {
 | 
			
		||||
                int order = 1;
 | 
			
		||||
                List<PageModule> pagemodules = PageModules.GetPageModules(pageid).ToList();
 | 
			
		||||
                foreach (PageModule pagemodule in pagemodules.Where(item => item.Pane == pane).OrderBy(item => item.Order))
 | 
			
		||||
                List<PageModule> pagemodules = PageModules.GetPageModules(pageid, pane).OrderBy(item => item.Order).ToList();
 | 
			
		||||
                foreach (PageModule pagemodule in pagemodules)
 | 
			
		||||
                {
 | 
			
		||||
                    if (pagemodule.Order != order)
 | 
			
		||||
                    {
 | 
			
		||||
 | 
			
		||||
@ -6,6 +6,7 @@ namespace Oqtane.Repository
 | 
			
		||||
    public interface IPageModuleRepository
 | 
			
		||||
    {
 | 
			
		||||
        IEnumerable<PageModule> GetPageModules(int SiteId);
 | 
			
		||||
        IEnumerable<PageModule> GetPageModules(int PageId, string Pane);
 | 
			
		||||
        PageModule AddPageModule(PageModule PageModule);
 | 
			
		||||
        PageModule UpdatePageModule(PageModule PageModule);
 | 
			
		||||
        PageModule GetPageModule(int PageModuleId);
 | 
			
		||||
 | 
			
		||||
@ -32,6 +32,26 @@ namespace Oqtane.Repository
 | 
			
		||||
            return pagemodules;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public IEnumerable<PageModule> GetPageModules(int PageId, string Pane)
 | 
			
		||||
        {
 | 
			
		||||
            IEnumerable<PageModule> pagemodules = db.PageModule
 | 
			
		||||
                .Include(item => item.Module) // eager load modules
 | 
			
		||||
                .Where(item => item.PageId == PageId);
 | 
			
		||||
            if (Pane != "" && pagemodules != null && pagemodules.Any())
 | 
			
		||||
            {
 | 
			
		||||
                pagemodules = pagemodules.Where(item => item.Pane == Pane);
 | 
			
		||||
            }
 | 
			
		||||
            if (pagemodules != null && pagemodules.Any())
 | 
			
		||||
            {
 | 
			
		||||
                IEnumerable<Permission> permissions = Permissions.GetPermissions(pagemodules.FirstOrDefault().Module.SiteId, "Module").ToList();
 | 
			
		||||
                foreach (PageModule pagemodule in pagemodules)
 | 
			
		||||
                {
 | 
			
		||||
                    pagemodule.Module.Permissions = Permissions.EncodePermissions(pagemodule.ModuleId, permissions);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            return pagemodules;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public PageModule AddPageModule(PageModule PageModule)
 | 
			
		||||
        {
 | 
			
		||||
            db.PageModule.Add(PageModule);
 | 
			
		||||
 | 
			
		||||
@ -9,32 +9,26 @@ namespace Oqtane.Repository
 | 
			
		||||
{
 | 
			
		||||
    public class TenantResolver : ITenantResolver
 | 
			
		||||
    {
 | 
			
		||||
        private MasterDBContext db;
 | 
			
		||||
        private readonly string aliasname;
 | 
			
		||||
        private readonly IAliasRepository Aliases;
 | 
			
		||||
        private readonly ITenantRepository Tenants;
 | 
			
		||||
        private readonly SiteState sitestate;
 | 
			
		||||
        private readonly Alias alias = null;
 | 
			
		||||
        private readonly Tenant tenant = null;
 | 
			
		||||
 | 
			
		||||
        public TenantResolver(MasterDBContext context, IHttpContextAccessor accessor, IAliasRepository Aliases, ITenantRepository Tenants, SiteState sitestate)
 | 
			
		||||
        public TenantResolver(IHttpContextAccessor Accessor, IAliasRepository Aliases, ITenantRepository Tenants, SiteState SiteState)
 | 
			
		||||
        {
 | 
			
		||||
            db = context;
 | 
			
		||||
            this.Aliases = Aliases;
 | 
			
		||||
            this.Tenants = Tenants;
 | 
			
		||||
            this.sitestate = sitestate;
 | 
			
		||||
            aliasname = "";
 | 
			
		||||
            int aliasid = -1;
 | 
			
		||||
            string aliasname = "";
 | 
			
		||||
 | 
			
		||||
            // get alias based on request context
 | 
			
		||||
            if (accessor.HttpContext != null)
 | 
			
		||||
            // get alias identifier based on request context
 | 
			
		||||
            if (Accessor.HttpContext != null)
 | 
			
		||||
            {
 | 
			
		||||
                // check if an alias is passed as a querystring parameter
 | 
			
		||||
                if (accessor.HttpContext.Request.Query.ContainsKey("alias"))
 | 
			
		||||
                // check if an alias is passed as a querystring parameter ( for cross tenant access )
 | 
			
		||||
                if (Accessor.HttpContext.Request.Query.ContainsKey("aliasid"))
 | 
			
		||||
                {
 | 
			
		||||
                    aliasname = accessor.HttpContext.Request.Query["alias"];
 | 
			
		||||
                    aliasid = int.Parse(Accessor.HttpContext.Request.Query["aliasid"]);
 | 
			
		||||
                }
 | 
			
		||||
                else // get the alias from the request url
 | 
			
		||||
                {
 | 
			
		||||
                    aliasname = accessor.HttpContext.Request.Host.Value;
 | 
			
		||||
                    string path = accessor.HttpContext.Request.Path.Value;
 | 
			
		||||
                    aliasname = Accessor.HttpContext.Request.Host.Value;
 | 
			
		||||
                    string path = Accessor.HttpContext.Request.Path.Value;
 | 
			
		||||
                    string[] segments = path.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
 | 
			
		||||
                    if (segments.Length > 1 && segments[1] == "api" && segments[0] != "~")
 | 
			
		||||
                    {
 | 
			
		||||
@ -48,28 +42,40 @@ namespace Oqtane.Repository
 | 
			
		||||
            }
 | 
			
		||||
            else  // background processes can pass in an alias using the SiteState service
 | 
			
		||||
            {
 | 
			
		||||
                if (sitestate != null)
 | 
			
		||||
                if (SiteState != null)
 | 
			
		||||
                {
 | 
			
		||||
                    aliasname = sitestate.Alias.Name;
 | 
			
		||||
                    aliasid = SiteState.Alias.AliasId;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            // get the alias and tenant
 | 
			
		||||
            if (aliasid != -1 || aliasname != "")
 | 
			
		||||
            {
 | 
			
		||||
                IEnumerable<Alias> aliases = Aliases.GetAliases(); // cached
 | 
			
		||||
                IEnumerable<Tenant> tenants = Tenants.GetTenants(); // cached
 | 
			
		||||
 | 
			
		||||
                if (aliasid != -1)
 | 
			
		||||
                {
 | 
			
		||||
                    alias = aliases.Where(item => item.AliasId == aliasid).FirstOrDefault();
 | 
			
		||||
                }
 | 
			
		||||
                else
 | 
			
		||||
                {
 | 
			
		||||
                    alias = aliases.Where(item => item.Name == aliasname).FirstOrDefault();
 | 
			
		||||
                }
 | 
			
		||||
                if (alias != null)
 | 
			
		||||
                {
 | 
			
		||||
                    tenant = tenants.Where(item => item.TenantId == alias.TenantId).FirstOrDefault();
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public Alias GetAlias()
 | 
			
		||||
        {
 | 
			
		||||
            IEnumerable<Alias> aliases = Aliases.GetAliases(); // cached
 | 
			
		||||
            return aliases.Where(item => item.Name == aliasname).FirstOrDefault();
 | 
			
		||||
            return alias;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public Tenant GetTenant()
 | 
			
		||||
        {
 | 
			
		||||
            Tenant tenant = null;
 | 
			
		||||
            Alias alias = GetAlias();
 | 
			
		||||
            if (alias != null)
 | 
			
		||||
            {
 | 
			
		||||
                IEnumerable<Tenant> tenants = Tenants.GetTenants(); // cached
 | 
			
		||||
                tenant = tenants.Where(item => item.TenantId == alias.TenantId).FirstOrDefault();
 | 
			
		||||
            }
 | 
			
		||||
            return tenant;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user