improve performance of alias handling and allow aliases to be an unlimited number of subfolders in depth
This commit is contained in:
@ -10,6 +10,7 @@ using System.Globalization;
|
||||
using Oqtane.Enums;
|
||||
using Oqtane.Infrastructure;
|
||||
using Oqtane.Repository;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace Oqtane.Controllers
|
||||
{
|
||||
@ -17,12 +18,14 @@ namespace Oqtane.Controllers
|
||||
public class AliasController : Controller
|
||||
{
|
||||
private readonly IAliasRepository _aliases;
|
||||
private readonly IHttpContextAccessor _accessor;
|
||||
private readonly ISyncManager _syncManager;
|
||||
private readonly ILogManager _logger;
|
||||
|
||||
public AliasController(IAliasRepository aliases, ISyncManager syncManager, ILogManager logger)
|
||||
public AliasController(IAliasRepository aliases, IHttpContextAccessor accessor, ISyncManager syncManager, ILogManager logger)
|
||||
{
|
||||
_aliases = aliases;
|
||||
_accessor = accessor;
|
||||
_syncManager = syncManager;
|
||||
_logger = logger;
|
||||
}
|
||||
@ -43,20 +46,30 @@ namespace Oqtane.Controllers
|
||||
return _aliases.GetAlias(id);
|
||||
}
|
||||
|
||||
// GET api/<controller>/name/localhost:12345?lastsyncdate=yyyyMMddHHmmssfff
|
||||
// GET api/<controller>/name/xxx?sync=yyyyMMddHHmmssfff
|
||||
[HttpGet("name/{name}")]
|
||||
public Alias Get(string name, string lastsyncdate)
|
||||
public Alias Get(string name, string sync)
|
||||
{
|
||||
name = WebUtility.UrlDecode(name);
|
||||
List<Alias> aliases = _aliases.GetAliases().ToList();
|
||||
List<Alias> aliases = _aliases.GetAliases().ToList(); // cached
|
||||
Alias alias = null;
|
||||
alias = aliases.FirstOrDefault(item => item.Name == name);
|
||||
if (name != null && (alias == null && name.Contains("/")))
|
||||
if (_accessor.HttpContext != null)
|
||||
{
|
||||
// lookup alias without folder name
|
||||
alias = aliases.Find(item => item.Name == name.Substring(0, name.IndexOf("/", StringComparison.Ordinal)));
|
||||
name = (name == "~") ? "" : name;
|
||||
name = _accessor.HttpContext.Request.Host.Value + "/" + WebUtility.UrlDecode(name);
|
||||
var segments = name.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
// iterate segments in reverse order
|
||||
for (int i = segments.Length; i > 0; i--)
|
||||
{
|
||||
name = string.Join("/", segments, 0, i);
|
||||
alias = aliases.Find(item => item.Name == name);
|
||||
if (alias != null)
|
||||
{
|
||||
break; // found a matching alias
|
||||
}
|
||||
}
|
||||
}
|
||||
if (alias == null && aliases.Count > 0)
|
||||
if (alias == null && aliases.Any())
|
||||
{
|
||||
// use first alias if name does not exist
|
||||
alias = aliases.FirstOrDefault();
|
||||
@ -66,7 +79,7 @@ namespace Oqtane.Controllers
|
||||
if (alias != null)
|
||||
{
|
||||
alias.SyncDate = DateTime.UtcNow;
|
||||
alias.SyncEvents = _syncManager.GetSyncEvents(DateTime.ParseExact(lastsyncdate, "yyyyMMddHHmmssfff", CultureInfo.InvariantCulture));
|
||||
alias.SyncEvents = _syncManager.GetSyncEvents(alias.TenantId, DateTime.ParseExact(sync, "yyyyMMddHHmmssfff", CultureInfo.InvariantCulture));
|
||||
}
|
||||
return alias;
|
||||
}
|
||||
|
@ -20,15 +20,17 @@ namespace Oqtane.Controllers
|
||||
private readonly IModuleRepository _modules;
|
||||
private readonly IPageModuleRepository _pageModules;
|
||||
private readonly IUserPermissions _userPermissions;
|
||||
private readonly ITenantResolver _tenants;
|
||||
private readonly ISyncManager _syncManager;
|
||||
private readonly ILogManager _logger;
|
||||
|
||||
public PageController(IPageRepository pages, IModuleRepository modules, IPageModuleRepository pageModules, IUserPermissions userPermissions, ISyncManager syncManager, ILogManager logger)
|
||||
public PageController(IPageRepository pages, IModuleRepository modules, IPageModuleRepository pageModules, IUserPermissions userPermissions, ITenantResolver tenants, ISyncManager syncManager, ILogManager logger)
|
||||
{
|
||||
_pages = pages;
|
||||
_modules = modules;
|
||||
_pageModules = pageModules;
|
||||
_userPermissions = userPermissions;
|
||||
_tenants = tenants;
|
||||
_syncManager = syncManager;
|
||||
_logger = logger;
|
||||
}
|
||||
@ -120,7 +122,7 @@ namespace Oqtane.Controllers
|
||||
if (_userPermissions.IsAuthorized(User,PermissionNames.Edit, permissions))
|
||||
{
|
||||
page = _pages.AddPage(page);
|
||||
_syncManager.AddSyncEvent(EntityNames.Site, page.SiteId);
|
||||
_syncManager.AddSyncEvent(_tenants.GetTenant().TenantId, EntityNames.Site, page.SiteId);
|
||||
_logger.Log(LogLevel.Information, this, LogFunction.Create, "Page Added {Page}", page);
|
||||
}
|
||||
else
|
||||
@ -162,7 +164,7 @@ namespace Oqtane.Controllers
|
||||
page.IsPersonalizable = false;
|
||||
page.UserId = int.Parse(userid);
|
||||
page = _pages.AddPage(page);
|
||||
_syncManager.AddSyncEvent(EntityNames.Site, page.SiteId);
|
||||
_syncManager.AddSyncEvent(_tenants.GetTenant().TenantId, EntityNames.Site, page.SiteId);
|
||||
|
||||
// copy modules
|
||||
List<PageModule> pagemodules = _pageModules.GetPageModules(page.SiteId).ToList();
|
||||
@ -206,7 +208,7 @@ namespace Oqtane.Controllers
|
||||
if (ModelState.IsValid && _userPermissions.IsAuthorized(User, EntityNames.Page, page.PageId, PermissionNames.Edit))
|
||||
{
|
||||
page = _pages.UpdatePage(page);
|
||||
_syncManager.AddSyncEvent(EntityNames.Site, page.SiteId);
|
||||
_syncManager.AddSyncEvent(_tenants.GetTenant().TenantId, EntityNames.Site, page.SiteId);
|
||||
_logger.Log(LogLevel.Information, this, LogFunction.Update, "Page Updated {Page}", page);
|
||||
}
|
||||
else
|
||||
@ -236,7 +238,7 @@ namespace Oqtane.Controllers
|
||||
}
|
||||
order += 2;
|
||||
}
|
||||
_syncManager.AddSyncEvent(EntityNames.Site, siteid);
|
||||
_syncManager.AddSyncEvent(_tenants.GetTenant().TenantId, EntityNames.Site, siteid);
|
||||
_logger.Log(LogLevel.Information, this, LogFunction.Update, "Page Order Updated {SiteId} {PageId} {ParentId}", siteid, pageid, parentid);
|
||||
}
|
||||
else
|
||||
@ -255,7 +257,7 @@ namespace Oqtane.Controllers
|
||||
if (_userPermissions.IsAuthorized(User, EntityNames.Page, page.PageId, PermissionNames.Edit))
|
||||
{
|
||||
_pages.DeletePage(page.PageId);
|
||||
_syncManager.AddSyncEvent(EntityNames.Site, page.SiteId);
|
||||
_syncManager.AddSyncEvent(_tenants.GetTenant().TenantId, EntityNames.Site, page.SiteId);
|
||||
_logger.Log(LogLevel.Information, this, LogFunction.Delete, "Page Deleted {PageId}", page.PageId);
|
||||
}
|
||||
else
|
||||
|
@ -16,13 +16,15 @@ namespace Oqtane.Controllers
|
||||
{
|
||||
private readonly IPageModuleRepository _pageModules;
|
||||
private readonly IUserPermissions _userPermissions;
|
||||
private readonly ITenantResolver _tenants;
|
||||
private readonly ISyncManager _syncManager;
|
||||
private readonly ILogManager _logger;
|
||||
|
||||
public PageModuleController(IPageModuleRepository pageModules, IUserPermissions userPermissions, ISyncManager syncManager, ILogManager logger)
|
||||
public PageModuleController(IPageModuleRepository pageModules, IUserPermissions userPermissions, ITenantResolver tenants, ISyncManager syncManager, ILogManager logger)
|
||||
{
|
||||
_pageModules = pageModules;
|
||||
_userPermissions = userPermissions;
|
||||
_tenants = tenants;
|
||||
_syncManager = syncManager;
|
||||
_logger = logger;
|
||||
}
|
||||
@ -69,7 +71,7 @@ namespace Oqtane.Controllers
|
||||
if (ModelState.IsValid && _userPermissions.IsAuthorized(User, EntityNames.Page, pageModule.PageId, PermissionNames.Edit))
|
||||
{
|
||||
pageModule = _pageModules.AddPageModule(pageModule);
|
||||
_syncManager.AddSyncEvent(EntityNames.Page, pageModule.PageId);
|
||||
_syncManager.AddSyncEvent(_tenants.GetTenant().TenantId, EntityNames.Page, pageModule.PageId);
|
||||
_logger.Log(LogLevel.Information, this, LogFunction.Create, "Page Module Added {PageModule}", pageModule);
|
||||
}
|
||||
else
|
||||
@ -89,7 +91,7 @@ namespace Oqtane.Controllers
|
||||
if (ModelState.IsValid && _userPermissions.IsAuthorized(User, EntityNames.Module, pageModule.ModuleId, PermissionNames.Edit))
|
||||
{
|
||||
pageModule = _pageModules.UpdatePageModule(pageModule);
|
||||
_syncManager.AddSyncEvent(EntityNames.Page, pageModule.PageId);
|
||||
_syncManager.AddSyncEvent(_tenants.GetTenant().TenantId, EntityNames.Page, pageModule.PageId);
|
||||
_logger.Log(LogLevel.Information, this, LogFunction.Update, "Page Module Updated {PageModule}", pageModule);
|
||||
}
|
||||
else
|
||||
@ -119,7 +121,7 @@ namespace Oqtane.Controllers
|
||||
}
|
||||
order += 2;
|
||||
}
|
||||
_syncManager.AddSyncEvent(EntityNames.Page, pageid);
|
||||
_syncManager.AddSyncEvent(_tenants.GetTenant().TenantId, EntityNames.Page, pageid);
|
||||
_logger.Log(LogLevel.Information, this, LogFunction.Update, "Page Module Order Updated {PageId} {Pane}", pageid, pane);
|
||||
}
|
||||
else
|
||||
@ -138,7 +140,7 @@ namespace Oqtane.Controllers
|
||||
if (_userPermissions.IsAuthorized(User, EntityNames.Page, pagemodule.PageId, PermissionNames.Edit))
|
||||
{
|
||||
_pageModules.DeletePageModule(id);
|
||||
_syncManager.AddSyncEvent(EntityNames.Page, pagemodule.PageId);
|
||||
_syncManager.AddSyncEvent(_tenants.GetTenant().TenantId, EntityNames.Page, pagemodule.PageId);
|
||||
_logger.Log(LogLevel.Information, this, LogFunction.Delete, "Page Module Deleted {PageModuleId}", id);
|
||||
}
|
||||
else
|
||||
|
@ -76,7 +76,7 @@ namespace Oqtane.Controllers
|
||||
if (ModelState.IsValid)
|
||||
{
|
||||
site = _sites.UpdateSite(site);
|
||||
_syncManager.AddSyncEvent(EntityNames.Site, site.SiteId);
|
||||
_syncManager.AddSyncEvent(_tenants.GetTenant().TenantId, EntityNames.Site, site.SiteId);
|
||||
_logger.Log(site.SiteId, LogLevel.Information, this, LogFunction.Update, "Site Updated {Site}", site);
|
||||
}
|
||||
return site;
|
||||
|
@ -224,7 +224,7 @@ namespace Oqtane.Controllers
|
||||
}
|
||||
}
|
||||
user = _users.UpdateUser(user);
|
||||
_syncManager.AddSyncEvent(EntityNames.User, user.UserId);
|
||||
_syncManager.AddSyncEvent(_tenants.GetTenant().TenantId, EntityNames.User, user.UserId);
|
||||
user.Password = ""; // remove sensitive information
|
||||
_logger.Log(LogLevel.Information, this, LogFunction.Update, "User Updated {User}", user);
|
||||
}
|
||||
@ -406,16 +406,19 @@ namespace Oqtane.Controllers
|
||||
[HttpGet("authenticate")]
|
||||
public User Authenticate()
|
||||
{
|
||||
User user = new User();
|
||||
user.Username = User.Identity.Name;
|
||||
user.IsAuthenticated = User.Identity.IsAuthenticated;
|
||||
string roles = "";
|
||||
foreach (var claim in User.Claims.Where(item => item.Type == ClaimTypes.Role))
|
||||
User user = new User { IsAuthenticated = User.Identity.IsAuthenticated, Username = "", UserId = -1, Roles = "" };
|
||||
if (user.IsAuthenticated)
|
||||
{
|
||||
roles += claim.Value + ";";
|
||||
user.Username = User.Identity.Name;
|
||||
user.UserId = int.Parse(User.Claims.First(item => item.Type == ClaimTypes.PrimarySid).Value);
|
||||
string roles = "";
|
||||
foreach (var claim in User.Claims.Where(item => item.Type == ClaimTypes.Role))
|
||||
{
|
||||
roles += claim.Value + ";";
|
||||
}
|
||||
if (roles != "") roles = ";" + roles;
|
||||
user.Roles = roles;
|
||||
}
|
||||
if (roles != "") roles = ";" + roles;
|
||||
user.Roles = roles;
|
||||
return user;
|
||||
}
|
||||
|
||||
|
@ -13,13 +13,15 @@ namespace Oqtane.Controllers
|
||||
public class UserRoleController : Controller
|
||||
{
|
||||
private readonly IUserRoleRepository _userRoles;
|
||||
private readonly ITenantResolver _tenants;
|
||||
private readonly ISyncManager _syncManager;
|
||||
private readonly ILogManager _logger;
|
||||
|
||||
public UserRoleController(IUserRoleRepository userRoles, ISyncManager syncManager, ILogManager logger)
|
||||
public UserRoleController(IUserRoleRepository userRoles, ITenantResolver tenants, ISyncManager syncManager, ILogManager logger)
|
||||
{
|
||||
_userRoles = userRoles;
|
||||
_syncManager = syncManager;
|
||||
_tenants = tenants;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
@ -47,7 +49,7 @@ namespace Oqtane.Controllers
|
||||
if (ModelState.IsValid)
|
||||
{
|
||||
userRole = _userRoles.AddUserRole(userRole);
|
||||
_syncManager.AddSyncEvent(EntityNames.User, userRole.UserId);
|
||||
_syncManager.AddSyncEvent(_tenants.GetTenant().TenantId, EntityNames.User, userRole.UserId);
|
||||
_logger.Log(LogLevel.Information, this, LogFunction.Create, "User Role Added {UserRole}", userRole);
|
||||
}
|
||||
return userRole;
|
||||
@ -61,7 +63,7 @@ namespace Oqtane.Controllers
|
||||
if (ModelState.IsValid)
|
||||
{
|
||||
userRole = _userRoles.UpdateUserRole(userRole);
|
||||
_syncManager.AddSyncEvent(EntityNames.User, userRole.UserId);
|
||||
_syncManager.AddSyncEvent(_tenants.GetTenant().TenantId, EntityNames.User, userRole.UserId);
|
||||
_logger.Log(LogLevel.Information, this, LogFunction.Update, "User Role Updated {UserRole}", userRole);
|
||||
}
|
||||
return userRole;
|
||||
@ -74,7 +76,7 @@ namespace Oqtane.Controllers
|
||||
{
|
||||
UserRole userRole = _userRoles.GetUserRole(id);
|
||||
_userRoles.DeleteUserRole(id);
|
||||
_syncManager.AddSyncEvent(EntityNames.User, userRole.UserId);
|
||||
_syncManager.AddSyncEvent(_tenants.GetTenant().TenantId, EntityNames.User, userRole.UserId);
|
||||
_logger.Log(LogLevel.Information, this, LogFunction.Delete, "User Role Deleted {UserRole}", userRole);
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ namespace Oqtane.Infrastructure
|
||||
{
|
||||
public interface ISyncManager
|
||||
{
|
||||
List<SyncEvent> GetSyncEvents(DateTime lastSyncDate);
|
||||
void AddSyncEvent(string entityName, int entityId);
|
||||
List<SyncEvent> GetSyncEvents(int tenantId, DateTime lastSyncDate);
|
||||
void AddSyncEvent(int tenantId, string entityName, int entityId);
|
||||
}
|
||||
}
|
||||
|
@ -1,43 +1,28 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Oqtane.Models;
|
||||
using Oqtane.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Oqtane.Infrastructure;
|
||||
using Oqtane.Repository;
|
||||
|
||||
namespace Oqtane.Infrastructure
|
||||
{
|
||||
public class SyncManager : ISyncManager
|
||||
{
|
||||
private readonly IServiceScopeFactory _serviceScopeFactory;
|
||||
private List<SyncEvent> SyncEvents { get; set; }
|
||||
|
||||
public SyncManager(IServiceScopeFactory serviceScopeFactory)
|
||||
public SyncManager()
|
||||
{
|
||||
this._serviceScopeFactory = serviceScopeFactory;
|
||||
SyncEvents = new List<SyncEvent>();
|
||||
}
|
||||
|
||||
private int TenantId
|
||||
public List<SyncEvent> GetSyncEvents(int tenantId, DateTime lastSyncDate)
|
||||
{
|
||||
get
|
||||
{
|
||||
using (var scope = _serviceScopeFactory.CreateScope())
|
||||
{
|
||||
return scope.ServiceProvider.GetRequiredService<ITenantResolver>().GetTenant().TenantId;
|
||||
}
|
||||
}
|
||||
return SyncEvents.Where(item => item.TenantId == tenantId && item.ModifiedOn >= lastSyncDate).ToList();
|
||||
}
|
||||
|
||||
public List<SyncEvent> GetSyncEvents(DateTime lastSyncDate)
|
||||
public void AddSyncEvent(int tenantId, string entityName, int entityId)
|
||||
{
|
||||
return SyncEvents.Where(item => item.TenantId == TenantId && item.ModifiedOn >= lastSyncDate).ToList();
|
||||
}
|
||||
|
||||
public void AddSyncEvent(string entityName, int entityId)
|
||||
{
|
||||
SyncEvents.Add(new SyncEvent { TenantId = TenantId, EntityName = entityName, EntityId = entityId, ModifiedOn = DateTime.UtcNow });
|
||||
SyncEvents.Add(new SyncEvent { TenantId = tenantId, EntityName = entityName, EntityId = entityId, ModifiedOn = DateTime.UtcNow });
|
||||
// trim sync events
|
||||
SyncEvents.RemoveAll(item => item.ModifiedOn < DateTime.UtcNow.AddHours(-1));
|
||||
}
|
||||
|
@ -15,7 +15,6 @@ namespace Oqtane.Repository
|
||||
public TenantResolver(IHttpContextAccessor accessor, IAliasRepository aliasRepository, ITenantRepository tenantRepository, SiteState siteState)
|
||||
{
|
||||
int aliasId = -1;
|
||||
string aliasName = "";
|
||||
|
||||
if (siteState != null && siteState.Alias != null)
|
||||
{
|
||||
@ -23,29 +22,14 @@ namespace Oqtane.Repository
|
||||
_alias = siteState.Alias;
|
||||
}
|
||||
else
|
||||
{
|
||||
// get alias identifier based on request context
|
||||
{
|
||||
// get aliasid identifier based on request
|
||||
if (accessor.HttpContext != null)
|
||||
{
|
||||
// check if an alias is passed as a querystring parameter ( for cross tenant access )
|
||||
if (accessor.HttpContext.Request.Query.ContainsKey("aliasid"))
|
||||
string[] segments = accessor.HttpContext.Request.Path.Value.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (segments.Length > 1 && segments[1] == "api" && segments[0] != "~")
|
||||
{
|
||||
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;
|
||||
string[] segments = path.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (segments.Length > 1 && segments[1] == "api" && segments[0] != "~")
|
||||
{
|
||||
aliasName += "/" + segments[0];
|
||||
}
|
||||
|
||||
if (aliasName.EndsWith("/"))
|
||||
{
|
||||
aliasName = aliasName.Substring(0, aliasName.Length - 1);
|
||||
}
|
||||
aliasId = int.Parse(segments[0]);
|
||||
}
|
||||
}
|
||||
|
||||
@ -55,14 +39,11 @@ namespace Oqtane.Repository
|
||||
{
|
||||
_alias = aliases.FirstOrDefault(item => item.AliasId == aliasId);
|
||||
}
|
||||
else
|
||||
{
|
||||
_alias = aliases.FirstOrDefault(item => item.Name == aliasName || aliases.Count() == 1);
|
||||
}
|
||||
}
|
||||
|
||||
if (_alias != null)
|
||||
{
|
||||
// get the tenant
|
||||
IEnumerable<Tenant> tenants = tenantRepository.GetTenants(); // cached
|
||||
_tenant = tenants.FirstOrDefault(item => item.TenantId == _alias.TenantId);
|
||||
}
|
||||
|
Reference in New Issue
Block a user