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,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;
}
}
}
}