Initial commit

This commit is contained in:
oqtane
2019-05-04 20:32:08 -04:00
committed by Shaun Walker
parent 2f232eea7e
commit d71de1c21f
177 changed files with 8536 additions and 0 deletions

View File

@ -0,0 +1,33 @@
using Microsoft.EntityFrameworkCore;
using Oqtane.Models;
using System;
namespace Oqtane.Repository
{
public class ContextBase : DbContext
{
private Tenant tenant;
public ContextBase(ITenantRepository TenantRepository)
{
tenant = TenantRepository.GetTenant();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(tenant.DBConnectionString
.Replace("|DataDirectory|", AppDomain.CurrentDomain.GetData("DataDirectory").ToString())
);
base.OnConfiguring(optionsBuilder);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
if (tenant.DBSchema != "")
{
modelBuilder.HasDefaultSchema(tenant.DBSchema);
}
base.OnModelCreating(modelBuilder);
}
}
}

View File

@ -0,0 +1,12 @@
using Microsoft.EntityFrameworkCore;
using Oqtane.Models;
namespace Oqtane.Repository
{
public class HostContext : DbContext
{
public HostContext(DbContextOptions<HostContext> options) : base(options) { }
public virtual DbSet<Tenant> Tenant { get; set; }
}
}

View File

@ -0,0 +1,10 @@
using System.Collections.Generic;
using Oqtane.Models;
namespace Oqtane.Repository
{
public interface IModuleDefinitionRepository
{
IEnumerable<ModuleDefinition> GetModuleDefinitions();
}
}

View File

@ -0,0 +1,15 @@
using System.Collections.Generic;
using Oqtane.Models;
namespace Oqtane.Repository
{
public interface IModuleRepository
{
IEnumerable<Module> GetModules();
IEnumerable<Module> GetModules(int SiteId, string ModuleDefinitionName);
void AddModule(Module Module);
void UpdateModule(Module Module);
Module GetModule(int ModuleId);
void DeleteModule(int ModuleId);
}
}

View File

@ -0,0 +1,15 @@
using System.Collections.Generic;
using Oqtane.Models;
namespace Oqtane.Repository
{
public interface IPageModuleRepository
{
IEnumerable<PageModule> GetPageModules();
IEnumerable<PageModule> GetPageModules(int PageId);
void AddPageModule(PageModule PageModule);
void UpdatePageModule(PageModule PageModule);
PageModule GetPageModule(int PageModuleId);
void DeletePageModule(int PageModuleId);
}
}

View File

@ -0,0 +1,15 @@
using System.Collections.Generic;
using Oqtane.Models;
namespace Oqtane.Repository
{
public interface IPageRepository
{
IEnumerable<Page> GetPages();
IEnumerable<Page> GetPages(int SiteId);
void AddPage(Page Page);
void UpdatePage(Page Page);
Page GetPage(int PageId);
void DeletePage(int PageId);
}
}

View File

@ -0,0 +1,14 @@
using System.Collections.Generic;
using Oqtane.Models;
namespace Oqtane.Repository
{
public interface ISiteRepository
{
IEnumerable<Site> GetSites();
void AddSite(Site site);
void UpdateSite(Site site);
Site GetSite(int siteId);
void DeleteSite(int siteId);
}
}

View File

@ -0,0 +1,10 @@
using System.Collections.Generic;
using Oqtane.Models;
namespace Oqtane.Repository
{
public interface ISkinRepository
{
IEnumerable<Skin> GetSkins();
}
}

View File

@ -0,0 +1,9 @@
using Oqtane.Models;
namespace Oqtane.Repository
{
public interface ITenantRepository
{
Tenant GetTenant();
}
}

View File

@ -0,0 +1,14 @@
using System.Collections.Generic;
using Oqtane.Models;
namespace Oqtane.Repository
{
public interface IUserRepository
{
IEnumerable<User> GetUsers();
void AddUser(User User);
void UpdateUser(User User);
User GetUser(int UserId);
void DeleteUser(int UserId);
}
}

View File

@ -0,0 +1,123 @@
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using Oqtane.Models;
using System.Reflection;
using System;
using Oqtane.Modules;
namespace Oqtane.Repository
{
public class ModuleDefinitionRepository : IModuleDefinitionRepository
{
private readonly List<ModuleDefinition> moduledefinitions;
public ModuleDefinitionRepository()
{
moduledefinitions = LoadModuleDefinitions();
}
private List<ModuleDefinition> LoadModuleDefinitions()
{
List<ModuleDefinition> moduledefinitions = new List<ModuleDefinition>();
// iterate through Oqtane module assemblies
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
{
if (assembly.FullName.StartsWith("Oqtane.Client") || assembly.FullName.StartsWith("Oqtane.Module."))
{
moduledefinitions = LoadModuleDefinitionsFromAssembly(moduledefinitions, assembly);
}
}
return moduledefinitions;
}
private List<ModuleDefinition> LoadModuleDefinitionsFromAssembly(List<ModuleDefinition> moduledefinitions, Assembly assembly)
{
ModuleDefinition moduledefinition;
Type[] modulecontroltypes = assembly.GetTypes().Where(item => item.GetInterfaces().Contains(typeof(IModuleControl))).ToArray();
foreach (Type modulecontroltype in modulecontroltypes)
{
if (modulecontroltype.Name != "ModuleBase" && !modulecontroltype.Namespace.EndsWith(".Controls"))
{
string[] typename = modulecontroltype.AssemblyQualifiedName.Split(',').Select(item => item.Trim()).ToList().ToArray();
string[] segments = typename[0].Split('.');
Array.Resize(ref segments, segments.Length - 1);
string ModuleType = string.Join(".", segments);
string QualifiedModuleType = ModuleType + ", " + typename[1];
int index = moduledefinitions.FindIndex(item => item.ModuleDefinitionName == QualifiedModuleType);
if (index == -1)
{
/// determine if this module implements IModule
Type moduletype = assembly.GetTypes()
.Where(item => item.Namespace.StartsWith(ModuleType))
.Where(item => item.GetInterfaces().Contains(typeof(IModule)))
.FirstOrDefault();
if (moduletype != null)
{
var moduleobject = Activator.CreateInstance(moduletype);
moduledefinition = new ModuleDefinition
{
ModuleDefinitionName = QualifiedModuleType,
Name = (string)moduletype.GetProperty("Name").GetValue(moduleobject),
Description = (string)moduletype.GetProperty("Description").GetValue(moduleobject),
Version = (string)moduletype.GetProperty("Version").GetValue(moduleobject),
Owner = (string)moduletype.GetProperty("Owner").GetValue(moduleobject),
Url = (string)moduletype.GetProperty("Url").GetValue(moduleobject),
Contact = (string)moduletype.GetProperty("Contact").GetValue(moduleobject),
License = (string)moduletype.GetProperty("License").GetValue(moduleobject),
Dependencies = (string)moduletype.GetProperty("Dependencies").GetValue(moduleobject),
ControlTypeTemplate = ModuleType + ".{Control}" + ", " + typename[1],
ControlTypeRoutes = "",
AssemblyName = assembly.FullName.Split(",")[0]
};
}
else
{
moduledefinition = new ModuleDefinition
{
ModuleDefinitionName = QualifiedModuleType,
Name = ModuleType.Substring(ModuleType.LastIndexOf(".") + 1),
Description = ModuleType.Substring(ModuleType.LastIndexOf(".") + 1),
Version = new Version(1, 0, 0).ToString(),
Owner = "",
Url = "",
Contact = "",
License = "",
Dependencies = "",
ControlTypeTemplate = ModuleType + ".{Control}" + ", " + typename[1],
ControlTypeRoutes = "",
AssemblyName = assembly.FullName.Split(",")[0]
};
}
moduledefinitions.Add(moduledefinition);
index = moduledefinitions.FindIndex(item => item.ModuleDefinitionName == QualifiedModuleType);
}
moduledefinition = moduledefinitions[index];
// actions
var modulecontrolobject = Activator.CreateInstance(modulecontroltype);
string actions = (string)modulecontroltype.GetProperty("Actions").GetValue(modulecontrolobject);
if (actions != "")
{
foreach(string action in actions.Split(','))
{
moduledefinition.ControlTypeRoutes += (action + "=" + modulecontroltype.FullName + ", " + typename[1] + ";");
}
}
moduledefinitions[index] = moduledefinition;
}
}
return moduledefinitions;
}
public IEnumerable<ModuleDefinition> GetModuleDefinitions()
{
return moduledefinitions;
}
}
}

View File

@ -0,0 +1,97 @@
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using Oqtane.Models;
namespace Oqtane.Repository
{
public class ModuleRepository : IModuleRepository
{
private TenantContext db;
public ModuleRepository(TenantContext context)
{
db = context;
}
public IEnumerable<Module> GetModules()
{
try
{
return db.Module.ToList();
}
catch
{
throw;
}
}
public IEnumerable<Module> GetModules(int SiteId, string ModuleDefinitionName)
{
try
{
return db.Module
.Where(item => item.SiteId == SiteId)
.Where(item => item.ModuleDefinitionName == ModuleDefinitionName)
.ToList();
}
catch
{
throw;
}
}
public void AddModule(Module Module)
{
try
{
db.Module.Add(Module);
db.SaveChanges();
}
catch
{
throw;
}
}
public void UpdateModule(Module Module)
{
try
{
db.Entry(Module).State = EntityState.Modified;
db.SaveChanges();
}
catch
{
throw;
}
}
public Module GetModule(int ModuleId)
{
try
{
Module Module = db.Module.Find(ModuleId);
return Module;
}
catch
{
throw;
}
}
public void DeleteModule(int ModuleId)
{
try
{
Module Module = db.Module.Find(ModuleId);
db.Module.Remove(Module);
db.SaveChanges();
}
catch
{
throw;
}
}
}
}

View File

@ -0,0 +1,96 @@
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using Oqtane.Models;
namespace Oqtane.Repository
{
public class PageModuleRepository : IPageModuleRepository
{
private TenantContext db;
public PageModuleRepository(TenantContext context)
{
db = context;
}
public IEnumerable<PageModule> GetPageModules()
{
try
{
return db.PageModule.ToList();
}
catch
{
throw;
}
}
public IEnumerable<PageModule> GetPageModules(int PageId)
{
try
{
List<PageModule> pagemodules = db.PageModule.Where(item => item.PageId == PageId)
.Include(item => item.Module)
.ToList();
return pagemodules;
}
catch
{
throw;
}
}
public void AddPageModule(PageModule PageModule)
{
try
{
db.PageModule.Add(PageModule);
db.SaveChanges();
}
catch
{
throw;
}
}
public void UpdatePageModule(PageModule PageModule)
{
try
{
db.Entry(PageModule).State = EntityState.Modified;
db.SaveChanges();
}
catch
{
throw;
}
}
public PageModule GetPageModule(int PageModuleId)
{
try
{
PageModule PageModule = db.PageModule.Find(PageModuleId);
return PageModule;
}
catch
{
throw;
}
}
public void DeletePageModule(int PageModuleId)
{
try
{
PageModule PageModule = db.PageModule.Find(PageModuleId);
db.PageModule.Remove(PageModule);
db.SaveChanges();
}
catch
{
throw;
}
}
}
}

View File

@ -0,0 +1,94 @@
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using Oqtane.Models;
namespace Oqtane.Repository
{
public class PageRepository : IPageRepository
{
private TenantContext db;
public PageRepository(TenantContext context)
{
db = context;
}
public IEnumerable<Page> GetPages()
{
try
{
return db.Page.ToList();
}
catch
{
throw;
}
}
public IEnumerable<Page> GetPages(int SiteId)
{
try
{
return db.Page.Where(item => item.SiteId == SiteId).ToList();
}
catch
{
throw;
}
}
public void AddPage(Page Page)
{
try
{
db.Page.Add(Page);
db.SaveChanges();
}
catch
{
throw;
}
}
public void UpdatePage(Page Page)
{
try
{
db.Entry(Page).State = EntityState.Modified;
db.SaveChanges();
}
catch
{
throw;
}
}
public Page GetPage(int PageId)
{
try
{
Page Page = db.Page.Find(PageId);
return Page;
}
catch
{
throw;
}
}
public void DeletePage(int PageId)
{
try
{
Page Page = db.Page.Find(PageId);
db.Page.Remove(Page);
db.SaveChanges();
}
catch
{
throw;
}
}
}
}

View File

@ -0,0 +1,82 @@
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using Oqtane.Models;
namespace Oqtane.Repository
{
public class SiteRepository : ISiteRepository
{
private TenantContext db;
public SiteRepository(TenantContext context)
{
db = context;
}
public IEnumerable<Site> GetSites()
{
try
{
return db.Site.ToList();
}
catch
{
throw;
}
}
public void AddSite(Site site)
{
try
{
db.Site.Add(site);
db.SaveChanges();
}
catch
{
throw;
}
}
public void UpdateSite(Site site)
{
try
{
db.Entry(site).State = EntityState.Modified;
db.SaveChanges();
}
catch
{
throw;
}
}
public Site GetSite(int siteId)
{
try
{
Site site = db.Site.Find(siteId);
return site;
}
catch
{
throw;
}
}
public void DeleteSite(int siteId)
{
try
{
Site site = db.Site.Find(siteId);
db.Site.Remove(site);
db.SaveChanges();
}
catch
{
throw;
}
}
}
}

View File

@ -0,0 +1,123 @@
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using Oqtane.Models;
using System.Reflection;
using System;
using Oqtane.Skins;
namespace Oqtane.Repository
{
public class SkinRepository : ISkinRepository
{
private readonly List<Skin> skins;
public SkinRepository()
{
skins = LoadSkins();
}
private List<Skin> LoadSkins()
{
List<Skin> skins = new List<Skin>();
// iterate through Oqtane skin assemblies
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
{
if (assembly.FullName.StartsWith("Oqtane.Client") || assembly.FullName.StartsWith("Oqtane.Skin."))
{
skins = LoadSkinsFromAssembly(skins, assembly);
}
}
return skins;
}
private List<Skin> LoadSkinsFromAssembly(List<Skin> skins, Assembly assembly)
{
Skin skin;
Type[] skincontroltypes = assembly.GetTypes().Where(item => item.GetInterfaces().Contains(typeof(ISkinControl))).ToArray();
foreach (Type skincontroltype in skincontroltypes)
{
if (skincontroltype.Name != "SkinBase")
{
string[] typename = skincontroltype.AssemblyQualifiedName.Split(',').Select(item => item.Trim()).ToList().ToArray();
string[] segments = typename[0].Split('.');
Array.Resize(ref segments, segments.Length - 1);
string Namespace = string.Join(".", segments);
int index = skins.FindIndex(item => item.SkinName == Namespace);
if (index == -1)
{
/// determine if this skin implements ISkin
Type skintype = assembly.GetTypes()
.Where(item => item.Namespace.StartsWith(Namespace))
.Where(item => item.GetInterfaces().Contains(typeof(ISkin))).FirstOrDefault();
if (skintype != null)
{
var skinobject = Activator.CreateInstance(skintype);
skin = new Skin
{
SkinName = Namespace,
Name = (string)skintype.GetProperty("Name").GetValue(skinobject),
Version = (string)skintype.GetProperty("Version").GetValue(skinobject),
Owner = (string)skintype.GetProperty("Owner").GetValue(skinobject),
Url = (string)skintype.GetProperty("Url").GetValue(skinobject),
Contact = (string)skintype.GetProperty("Contact").GetValue(skinobject),
License = (string)skintype.GetProperty("License").GetValue(skinobject),
Dependencies = (string)skintype.GetProperty("Dependencies").GetValue(skinobject),
SkinControls = "",
PaneLayouts = "",
ContainerControls = "",
AssemblyName = assembly.FullName.Split(",")[0]
};
}
else
{
skin = new Skin
{
SkinName = Namespace,
Name = skincontroltype.Name,
Version = new Version(1, 0, 0).ToString(),
Owner = "",
Url = "",
Contact = "",
License = "",
Dependencies = "",
SkinControls = "",
PaneLayouts = "",
ContainerControls = "",
AssemblyName = assembly.FullName.Split(",")[0]
};
}
skins.Add(skin);
index = skins.FindIndex(item => item.SkinName == Namespace);
}
skin = skins[index];
// layouts and skins
if (skincontroltype.FullName.EndsWith("Layout"))
{
skin.PaneLayouts += (skincontroltype.FullName + ", " + typename[1] + ";");
}
else
{
skin.SkinControls += (skincontroltype.FullName + ", " + typename[1] + ";");
}
// containers
Type[] containertypes = assembly.GetTypes().Where(item => item.Namespace.StartsWith(Namespace)).Where(item => item.GetInterfaces().Contains(typeof(IContainerControl))).ToArray();
foreach (Type containertype in containertypes)
{
skin.ContainerControls += (containertype.FullName + ", " + typename[1] + ";");
}
skins[index] = skin;
}
}
return skins;
}
public IEnumerable<Skin> GetSkins()
{
return skins;
}
}
}

View File

@ -0,0 +1,39 @@
using Microsoft.EntityFrameworkCore;
using Oqtane.Models;
using System;
namespace Oqtane.Repository
{
public class TenantContext : DbContext
{
public virtual DbSet<Site> Site { get; set; }
public virtual DbSet<Page> Page { get; set; }
public virtual DbSet<PageModule> PageModule { get; set; }
public virtual DbSet<Module> Module { get; set; }
public virtual DbSet<User> User { get; set; }
private readonly Tenant tenant;
public TenantContext(ITenantRepository TenantRepository)
{
tenant = TenantRepository.GetTenant();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(tenant.DBConnectionString
.Replace("|DataDirectory|", AppDomain.CurrentDomain.GetData("DataDirectory").ToString())
);
base.OnConfiguring(optionsBuilder);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
if (tenant.DBSchema != "")
{
modelBuilder.HasDefaultSchema(tenant.DBSchema);
}
base.OnModelCreating(modelBuilder);
}
}
}

View File

@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Oqtane.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Caching.Memory;
namespace Oqtane.Repository
{
public class TenantRepository : ITenantRepository
{
private HostContext db;
private readonly IMemoryCache _cache;
private readonly string alias;
public TenantRepository(HostContext context, IMemoryCache cache, IHttpContextAccessor accessor)
{
db = context;
_cache = cache;
// get site alias based on request context
alias = accessor.HttpContext.Request.Host.Value;
string path = accessor.HttpContext.Request.Path.Value;
if (path.StartsWith("/~") && !path.StartsWith("/~/"))
{
alias += path.Substring(0, path.IndexOf("/", 1));
}
}
public Tenant GetTenant()
{
try
{
IEnumerable<Tenant> tenants = _cache.GetOrCreate("tenants", entry =>
{
entry.SlidingExpiration = TimeSpan.FromMinutes(30);
return db.Tenant.ToList();
});
Tenant tenant;
if (tenants.Count() == 1)
{
tenant = tenants.FirstOrDefault();
}
else
{
tenant = tenants.Where(item => item.Alias == alias).FirstOrDefault();
}
return tenant;
}
catch
{
throw;
}
}
}
}

View File

@ -0,0 +1,82 @@
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using Oqtane.Models;
namespace Oqtane.Repository
{
public class UserRepository : IUserRepository
{
private TenantContext db;
public UserRepository(TenantContext context)
{
db = context;
}
public IEnumerable<User> GetUsers()
{
try
{
return db.User.ToList();
}
catch
{
throw;
}
}
public void AddUser(User user)
{
try
{
db.User.Add(user);
db.SaveChanges();
}
catch
{
throw;
}
}
public void UpdateUser(User user)
{
try
{
db.Entry(user).State = EntityState.Modified;
db.SaveChanges();
}
catch
{
throw;
}
}
public User GetUser(int userId)
{
try
{
User user = db.User.Find(userId);
return user;
}
catch
{
throw;
}
}
public void DeleteUser(int userId)
{
try
{
User user = db.User.Find(userId);
db.User.Remove(user);
db.SaveChanges();
}
catch
{
throw;
}
}
}
}