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

@ -96,7 +96,8 @@ namespace ASC.Core.Billing
{ {
this.quotaService = quotaService; this.quotaService = quotaService;
this.tenantService = tenantService; this.tenantService = tenantService;
config = new CoreConfiguration(tenantService); var coreSettings = new CoreSettings(tenantService);
config = new CoreConfiguration(coreSettings);
CacheExpiration = DEFAULT_CACHE_EXPIRATION; CacheExpiration = DEFAULT_CACHE_EXPIRATION;
test = ConfigurationManager.AppSettings["core:payment:test"] == "true"; test = ConfigurationManager.AppSettings["core:payment:test"] == "true";
int.TryParse(ConfigurationManager.AppSettings["core:payment:delay"], out paymentDelay); 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 tenantService = new CachedTenantService(new DbTenantService(cs));
var userService = new CachedUserService(new DbUserService(cs));
var azService = new CachedAzService(new DbAzService(cs)); var azService = new CachedAzService(new DbAzService(cs));
var quotaService = QuotaCacheEnabled ? (IQuotaService)new CachedQuotaService(new DbQuotaService(cs)) : new DbQuotaService(cs); var quotaService = QuotaCacheEnabled ? (IQuotaService)new CachedQuotaService(new DbQuotaService(cs)) : new DbQuotaService(cs);
var subService = new CachedSubscriptionService(new DbSubscriptionService(cs)); var subService = new CachedSubscriptionService(new DbSubscriptionService(cs));
var tariffService = new TariffService(cs, quotaService, tenantService); var tariffService = new TariffService(cs, quotaService, tenantService);
Configuration = new CoreConfiguration(tenantService); var coreSettings = new CoreSettings(tenantService);
TenantManager = new TenantManager(tenantService, quotaService, tariffService, null); Configuration = new CoreConfiguration(coreSettings);
TenantManager = new TenantManager(tenantService, quotaService, tariffService, null, coreSettings);
PaymentManager = new PaymentManager(Configuration, TenantManager, quotaService, tariffService); PaymentManager = new PaymentManager(Configuration, TenantManager, quotaService, tariffService);
AuthorizationManager = new AuthorizationManager(azService, TenantManager); AuthorizationManager = new AuthorizationManager(azService, TenantManager);
SubscriptionManager = new SubscriptionManager(subService); SubscriptionManager = new SubscriptionManager(subService);

View File

@ -33,21 +33,13 @@ using Newtonsoft.Json;
namespace ASC.Core namespace ASC.Core
{ {
public class CoreConfiguration public class CoreSettings
{ {
private readonly ITenantService tenantService;
private bool? standalone; private bool? standalone;
private bool? personal; private bool? personal;
private bool? customMode; private bool? customMode;
private long? personalMaxSpace;
private string basedomain; private string basedomain;
public CoreConfiguration(ITenantService service)
{
tenantService = service;
}
public bool Standalone public bool Standalone
{ {
get { return standalone ?? (bool)(standalone = ConfigurationManager.AppSettings["core:base-domain"] == "localhost"); } get { return standalone ?? (bool)(standalone = ConfigurationManager.AppSettings["core:base-domain"] == "localhost"); }
@ -63,6 +55,132 @@ namespace ASC.Core
get { return customMode ?? (bool)(customMode = ConfigurationManager.AppSettings["core.custom-mode"] == "true"); } 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 => CoreSettings.CustomMode;
}
public long PersonalMaxSpace(PersonalQuotaSettings personalQuotaSettings) public long PersonalMaxSpace(PersonalQuotaSettings personalQuotaSettings)
{ {
var quotaSettings = personalQuotaSettings.LoadForCurrentUser(); var quotaSettings = personalQuotaSettings.LoadForCurrentUser();
@ -116,101 +234,23 @@ namespace ASC.Core
public string BaseDomain public string BaseDomain
{ {
get get => CoreSettings.BaseDomain;
{ set => CoreSettings.BaseDomain = value;
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 CoreSettings CoreSettings { get; }
#region Methods Get/Save Setting #region Methods Get/Save Setting
public void SaveSetting(string key, string value, int tenant = Tenant.DEFAULT_TENANT) public void SaveSetting(string key, string value, int tenant = Tenant.DEFAULT_TENANT) => CoreSettings.SaveSetting(key, value, 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) public string GetSetting(string key, int tenant = Tenant.DEFAULT_TENANT) => CoreSettings.GetSetting(key, 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;
}
#endregion #endregion
public string GetKey(int tenant) public string GetKey(int tenant) => CoreSettings.GetKey(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) => CoreSettings.GetAffiliateId(tenant);
}
}
public string GetAffiliateId(int tenant)
{
var t = tenantService.GetTenant(tenant);
if (t != null && !string.IsNullOrWhiteSpace(t.AffiliateId))
return t.AffiliateId;
return null;
}
#region Methods Get/Set Section #region Methods Get/Set Section

View File

@ -48,6 +48,7 @@ namespace ASC.Core
private static List<string> thisCompAddresses = new List<string>(); private static List<string> thisCompAddresses = new List<string>();
public HttpContext HttpContext { get; } public HttpContext HttpContext { get; }
public CoreSettings CoreSettings { get; }
static TenantManager() 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.tenantService = tenantService;
this.quotaService = quotaService; this.quotaService = quotaService;
this.tariffService = tariffService; this.tariffService = tariffService;
CoreSettings = coreSettings;
HttpContext = httpContextAccessor?.HttpContext; HttpContext = httpContextAccessor?.HttpContext;
} }
@ -95,7 +102,7 @@ namespace ASC.Core
var isAlias = false; var isAlias = false;
if (t == null) if (t == null)
{ {
var baseUrl = CoreContext.Configuration.BaseDomain; var baseUrl = CoreSettings.BaseDomain;
if (!string.IsNullOrEmpty(baseUrl) && domain.EndsWith("." + baseUrl, StringComparison.InvariantCultureIgnoreCase)) if (!string.IsNullOrEmpty(baseUrl) && domain.EndsWith("." + baseUrl, StringComparison.InvariantCultureIgnoreCase))
{ {
isAlias = true; isAlias = true;
@ -106,7 +113,7 @@ namespace ASC.Core
{ {
t = tenantService.GetTenant(domain); t = tenantService.GetTenant(domain);
} }
if (t == null && CoreContext.Configuration.Standalone && !isAlias) if (t == null && CoreSettings.Standalone && !isAlias)
{ {
t = tenantService.GetTenantForStandaloneWithoutAlias(domain); t = tenantService.GetTenantForStandaloneWithoutAlias(domain);
} }

View File

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

View File

@ -154,12 +154,18 @@ namespace ASC.Data.Storage
public StorageFactoryListener StorageFactoryListener { get; } public StorageFactoryListener StorageFactoryListener { get; }
public StorageFactoryConfig StorageFactoryConfig { get; } public StorageFactoryConfig StorageFactoryConfig { get; }
public StorageSettings StorageSettings { 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; StorageFactoryListener = storageFactoryListener;
StorageFactoryConfig = storageFactoryConfig; StorageFactoryConfig = storageFactoryConfig;
StorageSettings = storageSettings; StorageSettings = storageSettings;
TenantManager = tenantManager;
} }
public IDataStore GetStorage(string tenant, string module) public IDataStore GetStorage(string tenant, string module)
@ -170,7 +176,7 @@ namespace ASC.Data.Storage
public IDataStore GetStorage(string configpath, string tenant, string module) public IDataStore GetStorage(string configpath, string tenant, string module)
{ {
int.TryParse(tenant, out var tenantId); 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) 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); 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) 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 readonly int tenant;
private long currentSize; private long currentSize;
public TenantManager TenantManager { get; }
public TennantQuotaController(int tenant) public TennantQuotaController(int tenant, TenantManager tenantManager)
{ {
this.tenant = tenant; this.tenant = tenant;
currentSize = CoreContext.TenantManager.FindTenantQuotaRows(new TenantQuotaRowQuery(tenant)) TenantManager = tenantManager;
currentSize = TenantManager.FindTenantQuotaRows(new TenantQuotaRowQuery(tenant))
.Where(r => UsedInQuota(r.Tag)) .Where(r => UsedInQuota(r.Tag))
.Sum(r => r.Counter); .Sum(r => r.Counter);
} }
@ -82,14 +84,14 @@ namespace ASC.Data.Storage
public long QuotaUsedGet(string module, string domain) public long QuotaUsedGet(string module, string domain)
{ {
var path = string.IsNullOrEmpty(module) ? null : string.Format("/{0}/{1}", module, 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)) .Where(r => UsedInQuota(r.Tag))
.Sum(r => r.Counter); .Sum(r => r.Counter);
} }
public void QuotaUsedCheck(long size) public void QuotaUsedCheck(long size)
{ {
var quota = CoreContext.TenantManager.GetTenantQuota(tenant); var quota = TenantManager.GetTenantQuota(tenant);
if (quota != null) if (quota != null)
{ {
if (quota.MaxFileSize != 0 && quota.MaxFileSize < size) 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) 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 }, new TenantQuotaRow { Tenant = tenant, Path = string.Format("/{0}/{1}", module, domain), Counter = size, Tag = dataTag },
exchange); exchange);
} }

View File

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