DI: TenantManager

This commit is contained in:
pavelbannov 2019-09-18 11:25:57 +03:00
parent f34dca4ebb
commit bcdbcf5ccc
8 changed files with 182 additions and 124 deletions

View File

@ -95,8 +95,9 @@ namespace ASC.Core.Billing
: base(connectionString, "tenant")
{
this.quotaService = quotaService;
this.tenantService = tenantService;
config = new CoreConfiguration(tenantService);
this.tenantService = tenantService;
var coreSettings = new CoreSettings(tenantService);
config = new CoreConfiguration(coreSettings);
CacheExpiration = DEFAULT_CACHE_EXPIRATION;
test = ConfigurationManager.AppSettings["core:payment:test"] == "true";
int.TryParse(ConfigurationManager.AppSettings["core:payment:delay"], out paymentDelay);

View File

@ -73,14 +73,14 @@ namespace ASC.Core
}
var tenantService = new CachedTenantService(new DbTenantService(cs));
var userService = new CachedUserService(new DbUserService(cs));
var azService = new CachedAzService(new DbAzService(cs));
var quotaService = QuotaCacheEnabled ? (IQuotaService)new CachedQuotaService(new DbQuotaService(cs)) : new DbQuotaService(cs);
var subService = new CachedSubscriptionService(new DbSubscriptionService(cs));
var tariffService = new TariffService(cs, quotaService, tenantService);
Configuration = new CoreConfiguration(tenantService);
TenantManager = new TenantManager(tenantService, quotaService, tariffService, null);
var coreSettings = new CoreSettings(tenantService);
Configuration = new CoreConfiguration(coreSettings);
TenantManager = new TenantManager(tenantService, quotaService, tariffService, null, coreSettings);
PaymentManager = new PaymentManager(Configuration, TenantManager, quotaService, tariffService);
AuthorizationManager = new AuthorizationManager(azService, TenantManager);
SubscriptionManager = new SubscriptionManager(subService);

View File

@ -32,22 +32,14 @@ using ASC.Core.Tenants;
using Newtonsoft.Json;
namespace ASC.Core
{
public class CoreConfiguration
{
private readonly ITenantService tenantService;
{
public class CoreSettings
{
private bool? standalone;
private bool? personal;
private bool? customMode;
private long? personalMaxSpace;
private string basedomain;
public CoreConfiguration(ITenantService service)
{
tenantService = service;
}
private bool? personal;
private bool? customMode;
private string basedomain;
public bool Standalone
{
get { return standalone ?? (bool)(standalone = ConfigurationManager.AppSettings["core:base-domain"] == "localhost"); }
@ -56,11 +48,137 @@ namespace ASC.Core
public bool Personal
{
get { return personal ?? (bool)(personal = ConfigurationManager.AppSettings["core.personal"] == "true"); }
}
public bool CustomMode
{
get { return customMode ?? (bool)(customMode = ConfigurationManager.AppSettings["core.custom-mode"] == "true"); }
}
public string BaseDomain
{
get
{
if (basedomain == null)
{
basedomain = ConfigurationManager.AppSettings["core:base-domain"] ?? string.Empty;
}
string result;
if (Standalone || string.IsNullOrEmpty(basedomain))
{
result = GetSetting("BaseDomain") ?? basedomain;
}
else
{
result = basedomain;
}
return result;
}
set
{
if (Standalone || string.IsNullOrEmpty(basedomain))
{
SaveSetting("BaseDomain", value);
}
}
}
public ITenantService TenantService { get; }
public CoreSettings(ITenantService tenantService)
{
TenantService = tenantService;
}
public void SaveSetting(string key, string value, int tenant = Tenant.DEFAULT_TENANT)
{
if (string.IsNullOrEmpty(key))
{
throw new ArgumentNullException("key");
}
byte[] bytes = null;
if (value != null)
{
bytes = Crypto.GetV(Encoding.UTF8.GetBytes(value), 2, true);
}
TenantService.SetTenantSettings(tenant, key, bytes);
}
public string GetSetting(string key, int tenant = Tenant.DEFAULT_TENANT)
{
if (string.IsNullOrEmpty(key))
{
throw new ArgumentNullException("key");
}
var bytes = TenantService.GetTenantSettings(tenant, key);
var result = bytes != null ? Encoding.UTF8.GetString(Crypto.GetV(bytes, 2, false)) : null;
return result;
}
public string GetKey(int tenant)
{
if (Standalone)
{
var key = GetSetting("PortalId");
if (string.IsNullOrEmpty(key))
{
lock (TenantService)
{
// thread safe
key = GetSetting("PortalId");
if (string.IsNullOrEmpty(key))
{
SaveSetting("PortalId", key = Guid.NewGuid().ToString());
}
}
}
return key;
}
else
{
var t = TenantService.GetTenant(tenant);
if (t != null && !string.IsNullOrWhiteSpace(t.PaymentId))
return t.PaymentId;
return ConfigurationManager.AppSettings["core:payment:region"] + tenant;
}
}
public string GetAffiliateId(int tenant)
{
var t = TenantService.GetTenant(tenant);
if (t != null && !string.IsNullOrWhiteSpace(t.AffiliateId))
return t.AffiliateId;
return null;
}
}
public class CoreConfiguration
{
private long? personalMaxSpace;
public CoreConfiguration(CoreSettings coreSettings)
{
CoreSettings = coreSettings;
}
public bool Standalone
{
get => CoreSettings.Standalone;
}
public bool Personal
{
get => CoreSettings.Personal;
}
public bool CustomMode
{
get { return customMode ?? (bool)(customMode = ConfigurationManager.AppSettings["core.custom-mode"] == "true"); }
get => CoreSettings.CustomMode;
}
public long PersonalMaxSpace(PersonalQuotaSettings personalQuotaSettings)
@ -116,101 +234,23 @@ namespace ASC.Core
public string BaseDomain
{
get
{
if (basedomain == null)
{
basedomain = ConfigurationManager.AppSettings["core:base-domain"] ?? string.Empty;
}
string result;
if (Standalone || string.IsNullOrEmpty(basedomain))
{
result = GetSetting("BaseDomain") ?? basedomain;
}
else
{
result = basedomain;
}
return result;
}
set
{
if (Standalone || string.IsNullOrEmpty(basedomain))
{
SaveSetting("BaseDomain", value);
}
}
}
get => CoreSettings.BaseDomain;
set => CoreSettings.BaseDomain = value;
}
public CoreSettings CoreSettings { get; }
#region Methods Get/Save Setting
public void SaveSetting(string key, string value, int tenant = Tenant.DEFAULT_TENANT) => CoreSettings.SaveSetting(key, value, tenant);
public void SaveSetting(string key, string value, int tenant = Tenant.DEFAULT_TENANT)
{
if (string.IsNullOrEmpty(key))
{
throw new ArgumentNullException("key");
}
byte[] bytes = null;
if (value != null)
{
bytes = Crypto.GetV(Encoding.UTF8.GetBytes(value), 2, true);
}
tenantService.SetTenantSettings(tenant, key, bytes);
}
public string GetSetting(string key, int tenant = Tenant.DEFAULT_TENANT)
{
if (string.IsNullOrEmpty(key))
{
throw new ArgumentNullException("key");
}
var bytes = tenantService.GetTenantSettings(tenant, key);
var result = bytes != null ? Encoding.UTF8.GetString(Crypto.GetV(bytes, 2, false)) : null;
return result;
}
public string GetSetting(string key, int tenant = Tenant.DEFAULT_TENANT) => CoreSettings.GetSetting(key, tenant);
#endregion
public string GetKey(int tenant)
{
if (Standalone)
{
var key = GetSetting("PortalId");
if (string.IsNullOrEmpty(key))
{
lock (tenantService)
{
// thread safe
key = GetSetting("PortalId");
if (string.IsNullOrEmpty(key))
{
SaveSetting("PortalId", key = Guid.NewGuid().ToString());
}
}
}
return key;
}
else
{
var t = tenantService.GetTenant(tenant);
if (t != null && !string.IsNullOrWhiteSpace(t.PaymentId))
return t.PaymentId;
public string GetKey(int tenant) => CoreSettings.GetKey(tenant);
return ConfigurationManager.AppSettings["core:payment:region"] + tenant;
}
}
public string GetAffiliateId(int tenant)
{
var t = tenantService.GetTenant(tenant);
if (t != null && !string.IsNullOrWhiteSpace(t.AffiliateId))
return t.AffiliateId;
return null;
}
public string GetAffiliateId(int tenant) => CoreSettings.GetAffiliateId(tenant);
#region Methods Get/Set Section

View File

@ -47,7 +47,8 @@ namespace ASC.Core
private readonly ITariffService tariffService;
private static List<string> thisCompAddresses = new List<string>();
public HttpContext HttpContext { get; }
public HttpContext HttpContext { get; }
public CoreSettings CoreSettings { get; }
static TenantManager()
{
@ -64,11 +65,17 @@ namespace ASC.Core
}
}
public TenantManager(ITenantService tenantService, IQuotaService quotaService, ITariffService tariffService, IHttpContextAccessor httpContextAccessor)
public TenantManager(
ITenantService tenantService,
IQuotaService quotaService,
ITariffService tariffService,
IHttpContextAccessor httpContextAccessor,
CoreSettings coreSettings)
{
this.tenantService = tenantService;
this.quotaService = quotaService;
this.tariffService = tariffService;
CoreSettings = coreSettings;
HttpContext = httpContextAccessor?.HttpContext;
}
@ -95,7 +102,7 @@ namespace ASC.Core
var isAlias = false;
if (t == null)
{
var baseUrl = CoreContext.Configuration.BaseDomain;
var baseUrl = CoreSettings.BaseDomain;
if (!string.IsNullOrEmpty(baseUrl) && domain.EndsWith("." + baseUrl, StringComparison.InvariantCultureIgnoreCase))
{
isAlias = true;
@ -106,7 +113,7 @@ namespace ASC.Core
{
t = tenantService.GetTenant(domain);
}
if (t == null && CoreContext.Configuration.Standalone && !isAlias)
if (t == null && CoreSettings.Standalone && !isAlias)
{
t = tenantService.GetTenantForStandaloneWithoutAlias(domain);
}

View File

@ -69,8 +69,9 @@ namespace ASC.Core
tenantService = new DbTenantService(connectionString);
userService = new DbUserService(connectionString);
quotaService = new DbQuotaService(connectionString);
tariffService = new TariffService(connectionString, quotaService, tenantService);
clientTenantManager = new TenantManager(tenantService, quotaService, tariffService, null);
tariffService = new TariffService(connectionString, quotaService, tenantService);
var coreSettings = new CoreSettings(tenantService);
clientTenantManager = new TenantManager(tenantService, quotaService, tariffService, null, coreSettings);
settingsManager = new DbSettingsManager(connectionString);
Region = region ?? string.Empty;
DbId = connectionString.Name;

View File

@ -154,12 +154,18 @@ namespace ASC.Data.Storage
public StorageFactoryListener StorageFactoryListener { get; }
public StorageFactoryConfig StorageFactoryConfig { get; }
public StorageSettings StorageSettings { get; }
public TenantManager TenantManager { get; }
public StorageFactory(StorageFactoryListener storageFactoryListener, StorageFactoryConfig storageFactoryConfig, StorageSettings storageSettings)
public StorageFactory(
StorageFactoryListener storageFactoryListener,
StorageFactoryConfig storageFactoryConfig,
StorageSettings storageSettings,
TenantManager tenantManager)
{
StorageFactoryListener = storageFactoryListener;
StorageFactoryConfig = storageFactoryConfig;
StorageSettings = storageSettings;
TenantManager = tenantManager;
}
public IDataStore GetStorage(string tenant, string module)
@ -170,7 +176,7 @@ namespace ASC.Data.Storage
public IDataStore GetStorage(string configpath, string tenant, string module)
{
int.TryParse(tenant, out var tenantId);
return GetStorage(configpath, tenant, module, new TennantQuotaController(tenantId));
return GetStorage(configpath, tenant, module, new TennantQuotaController(tenantId, TenantManager));
}
public IDataStore GetStorage(string configpath, string tenant, string module, IQuotaController controller)
@ -218,7 +224,7 @@ namespace ASC.Data.Storage
}
int.TryParse(tenant, out var tenantId);
return GetDataStore(tenant, module, consumer, new TennantQuotaController(tenantId));
return GetDataStore(tenant, module, consumer, new TennantQuotaController(tenantId, TenantManager));
}
private IDataStore GetStoreAndCache(string tenant, string module, DataStoreConsumer consumer, IQuotaController controller)

View File

@ -37,11 +37,13 @@ namespace ASC.Data.Storage
private readonly int tenant;
private long currentSize;
public TenantManager TenantManager { get; }
public TennantQuotaController(int tenant)
public TennantQuotaController(int tenant, TenantManager tenantManager)
{
this.tenant = tenant;
currentSize = CoreContext.TenantManager.FindTenantQuotaRows(new TenantQuotaRowQuery(tenant))
TenantManager = tenantManager;
currentSize = TenantManager.FindTenantQuotaRows(new TenantQuotaRowQuery(tenant))
.Where(r => UsedInQuota(r.Tag))
.Sum(r => r.Counter);
}
@ -82,14 +84,14 @@ namespace ASC.Data.Storage
public long QuotaUsedGet(string module, string domain)
{
var path = string.IsNullOrEmpty(module) ? null : string.Format("/{0}/{1}", module, domain);
return CoreContext.TenantManager.FindTenantQuotaRows(new TenantQuotaRowQuery(tenant).WithPath(path))
return TenantManager.FindTenantQuotaRows(new TenantQuotaRowQuery(tenant).WithPath(path))
.Where(r => UsedInQuota(r.Tag))
.Sum(r => r.Counter);
}
public void QuotaUsedCheck(long size)
{
var quota = CoreContext.TenantManager.GetTenantQuota(tenant);
var quota = TenantManager.GetTenantQuota(tenant);
if (quota != null)
{
if (quota.MaxFileSize != 0 && quota.MaxFileSize < size)
@ -112,7 +114,7 @@ namespace ASC.Data.Storage
private void SetTenantQuotaRow(string module, string domain, long size, string dataTag, bool exchange)
{
CoreContext.TenantManager.SetTenantQuotaRow(
TenantManager.SetTenantQuotaRow(
new TenantQuotaRow { Tenant = tenant, Path = string.Format("/{0}/{1}", module, domain), Counter = size, Tag = dataTag },
exchange);
}

View File

@ -189,6 +189,7 @@ namespace ASC.People
.AddScoped<PeopleNamesSettings>()
.AddScoped<EmailValidationKeyProvider>()
.AddScoped<TenantUtil>()
.AddScoped<CoreSettings>()
.AddSingleton<WebPathSettings>()
.AddSingleton<BaseStorageSettingsListener>()
.AddScoped(typeof(IRecipientProvider), typeof(RecipientProviderImpl))