DocSpace-client/common/ASC.Core.Common/Context/Impl/CoreConfiguration.cs

379 lines
12 KiB
C#
Raw Normal View History

2019-05-15 14:56:09 +00:00
/*
*
* (c) Copyright Ascensio System Limited 2010-2018
*
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
* In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that
* Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
*
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
*
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
*
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
*
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
* relevant author attributions when distributing the software. If the display of the logo in its graphic
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
* in every copy of the program you distribute.
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
*
*/
using System;
2020-02-17 08:58:14 +00:00
using System.Text;
2020-01-17 13:58:26 +00:00
2020-02-17 08:58:14 +00:00
using ASC.Common;
2019-10-31 11:28:30 +00:00
using ASC.Core.Caching;
2020-02-17 08:58:14 +00:00
using ASC.Core.Common.Settings;
2019-08-15 12:04:42 +00:00
using ASC.Core.Configuration;
2020-02-17 08:58:14 +00:00
using ASC.Core.Tenants;
2019-10-31 11:28:30 +00:00
using Microsoft.Extensions.Configuration;
2020-02-20 08:05:10 +00:00
using Microsoft.Extensions.Options;
2020-02-17 08:58:14 +00:00
2019-08-15 12:04:42 +00:00
using Newtonsoft.Json;
2019-10-31 11:28:30 +00:00
2019-05-15 14:56:09 +00:00
namespace ASC.Core
2020-10-19 19:04:07 +00:00
{
[Singletone]
2019-10-31 11:28:30 +00:00
public class CoreBaseSettings
{
2019-05-15 14:56:09 +00:00
private bool? standalone;
2019-10-31 11:28:30 +00:00
private bool? personal;
private bool? customMode;
2020-08-12 09:58:08 +00:00
private IConfiguration Configuration { get; }
2019-10-31 11:28:30 +00:00
public CoreBaseSettings(IConfiguration configuration)
{
Configuration = configuration;
}
2019-05-15 14:56:09 +00:00
public bool Standalone
{
2019-09-20 14:21:41 +00:00
get { return standalone ?? (bool)(standalone = Configuration["core:base-domain"] == "localhost"); }
2019-05-15 14:56:09 +00:00
}
public bool Personal
{
2020-02-17 08:58:14 +00:00
get
{
//TODO:if (CustomMode && HttpContext.Current != null && HttpContext.Current.Request.SailfishApp()) return true;
2021-06-02 16:13:47 +00:00
return personal ?? (bool)(personal = string.Compare(Configuration["core:personal"], "true", true) == 0);
2020-01-17 13:58:26 +00:00
}
2019-10-31 11:28:30 +00:00
}
public bool CustomMode
{
2021-06-02 16:13:47 +00:00
get { return customMode ?? (bool)(customMode = string.Compare(Configuration["core:custom-mode"], "true", true) == 0); }
2019-10-31 11:28:30 +00:00
}
}
2020-02-20 08:05:10 +00:00
class ConfigureCoreSettings : IConfigureNamedOptions<CoreSettings>
{
2020-08-12 09:58:08 +00:00
private IOptionsSnapshot<CachedTenantService> TenantService { get; }
private CoreBaseSettings CoreBaseSettings { get; }
private IConfiguration Configuration { get; }
2020-02-20 08:05:10 +00:00
public ConfigureCoreSettings(
IOptionsSnapshot<CachedTenantService> tenantService,
CoreBaseSettings coreBaseSettings,
IConfiguration configuration
)
{
TenantService = tenantService;
CoreBaseSettings = coreBaseSettings;
Configuration = configuration;
}
public void Configure(string name, CoreSettings options)
{
Configure(options);
options.TenantService = TenantService.Get(name);
}
public void Configure(CoreSettings options)
{
options.Configuration = Configuration;
options.CoreBaseSettings = CoreBaseSettings;
options.TenantService = TenantService.Value;
}
}
2020-10-19 15:53:15 +00:00
[Scope(typeof(ConfigureCoreSettings))]
2019-10-31 11:28:30 +00:00
public class CoreSettings
{
private string basedomain;
2019-05-15 14:56:09 +00:00
public string BaseDomain
{
get
{
if (basedomain == null)
{
2019-09-20 15:08:45 +00:00
basedomain = Configuration["core:base-domain"] ?? string.Empty;
2019-05-15 14:56:09 +00:00
}
string result;
2019-09-18 15:19:30 +00:00
if (CoreBaseSettings.Standalone || string.IsNullOrEmpty(basedomain))
2019-05-15 14:56:09 +00:00
{
result = GetSetting("BaseDomain") ?? basedomain;
}
else
{
result = basedomain;
}
return result;
}
set
{
2019-09-18 15:19:30 +00:00
if (CoreBaseSettings.Standalone || string.IsNullOrEmpty(basedomain))
2019-05-15 14:56:09 +00:00
{
SaveSetting("BaseDomain", value);
}
}
2019-10-31 11:28:30 +00:00
}
2020-02-20 08:05:10 +00:00
internal ITenantService TenantService { get; set; }
internal CoreBaseSettings CoreBaseSettings { get; set; }
internal IConfiguration Configuration { get; set; }
2019-10-31 11:28:30 +00:00
2020-02-20 08:05:10 +00:00
public CoreSettings()
{
2020-02-20 08:05:10 +00:00
}
public CoreSettings(
2020-02-20 14:57:48 +00:00
ITenantService tenantService,
2020-02-20 08:05:10 +00:00
CoreBaseSettings coreBaseSettings,
IConfiguration configuration)
2019-10-31 11:28:30 +00:00
{
TenantService = tenantService;
CoreBaseSettings = coreBaseSettings;
Configuration = configuration;
}
2019-10-10 10:59:22 +00:00
public string GetBaseDomain(string hostedRegion)
{
var baseHost = BaseDomain;
if (string.IsNullOrEmpty(hostedRegion) || string.IsNullOrEmpty(baseHost) || !baseHost.Contains("."))
{
return baseHost;
}
var subdomain = baseHost.Remove(baseHost.IndexOf('.') + 1);
return hostedRegion.StartsWith(subdomain) ? hostedRegion : (subdomain + hostedRegion.TrimStart('.'));
2019-10-31 11:28:30 +00:00
}
2019-05-15 14:56:09 +00:00
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);
}
2019-09-18 08:25:57 +00:00
TenantService.SetTenantSettings(tenant, key, bytes);
2019-05-15 14:56:09 +00:00
}
public string GetSetting(string key, int tenant = Tenant.DEFAULT_TENANT)
{
if (string.IsNullOrEmpty(key))
{
throw new ArgumentNullException("key");
}
2019-09-18 08:25:57 +00:00
var bytes = TenantService.GetTenantSettings(tenant, key);
2019-05-15 14:56:09 +00:00
var result = bytes != null ? Encoding.UTF8.GetString(Crypto.GetV(bytes, 2, false)) : null;
return result;
}
public string GetKey(int tenant)
{
2019-09-18 15:19:30 +00:00
if (CoreBaseSettings.Standalone)
2019-05-15 14:56:09 +00:00
{
var key = GetSetting("PortalId");
if (string.IsNullOrEmpty(key))
{
2019-09-18 08:25:57 +00:00
lock (TenantService)
2019-05-15 14:56:09 +00:00
{
// thread safe
key = GetSetting("PortalId");
if (string.IsNullOrEmpty(key))
{
SaveSetting("PortalId", key = Guid.NewGuid().ToString());
}
}
}
return key;
}
else
{
2019-09-18 08:25:57 +00:00
var t = TenantService.GetTenant(tenant);
2019-05-15 14:56:09 +00:00
if (t != null && !string.IsNullOrWhiteSpace(t.PaymentId))
return t.PaymentId;
2019-09-20 15:08:45 +00:00
return Configuration["core:payment:region"] + tenant;
2019-05-15 14:56:09 +00:00
}
}
public string GetAffiliateId(int tenant)
{
2019-09-18 08:25:57 +00:00
var t = TenantService.GetTenant(tenant);
2019-05-15 14:56:09 +00:00
if (t != null && !string.IsNullOrWhiteSpace(t.AffiliateId))
return t.AffiliateId;
return null;
2020-02-17 08:58:14 +00:00
}
public string GetCampaign(int tenant)
{
var t = TenantService.GetTenant(tenant);
if (t != null && !string.IsNullOrWhiteSpace(t.Campaign))
return t.Campaign;
return null;
2019-10-31 11:28:30 +00:00
}
}
2020-10-19 15:53:15 +00:00
[Scope]
2019-09-18 08:25:57 +00:00
public class CoreConfiguration
{
private long? personalMaxSpace;
2019-10-10 13:18:12 +00:00
public CoreConfiguration(CoreSettings coreSettings, TenantManager tenantManager, IConfiguration configuration)
2019-10-31 11:28:30 +00:00
{
CoreSettings = coreSettings;
TenantManager = tenantManager;
Configuration = configuration;
2019-09-18 08:25:57 +00:00
}
public long PersonalMaxSpace(SettingsManager settingsManager)
2019-09-18 08:25:57 +00:00
{
var quotaSettings = settingsManager.LoadForCurrentUser<PersonalQuotaSettings>();
2019-09-18 08:25:57 +00:00
if (quotaSettings.MaxSpace != long.MaxValue)
2019-10-31 11:28:30 +00:00
return quotaSettings.MaxSpace;
2019-09-18 08:25:57 +00:00
if (personalMaxSpace.HasValue)
return personalMaxSpace.Value;
if (!long.TryParse(Configuration["core:personal.maxspace"], out var value))
2019-09-18 08:25:57 +00:00
value = long.MaxValue;
personalMaxSpace = value;
return personalMaxSpace.Value;
}
public SmtpSettings SmtpSettings
{
get
{
var isDefaultSettings = false;
2019-09-18 13:33:01 +00:00
var tenant = TenantManager.GetCurrentTenant(false);
2019-09-18 08:25:57 +00:00
if (tenant != null)
{
var settingsValue = GetSetting("SmtpSettings", tenant.TenantId);
if (string.IsNullOrEmpty(settingsValue))
{
isDefaultSettings = true;
settingsValue = GetSetting("SmtpSettings");
}
var settings = SmtpSettings.Deserialize(settingsValue);
settings.IsDefaultSettings = isDefaultSettings;
return settings;
}
else
{
var settingsValue = GetSetting("SmtpSettings");
var settings = SmtpSettings.Deserialize(settingsValue);
settings.IsDefaultSettings = true;
return settings;
}
}
2019-09-18 13:33:01 +00:00
set { SaveSetting("SmtpSettings", value?.Serialize(), TenantManager.GetCurrentTenant().TenantId); }
2019-09-18 08:25:57 +00:00
}
2019-10-31 11:28:30 +00:00
2020-08-12 09:58:08 +00:00
private CoreSettings CoreSettings { get; }
private TenantManager TenantManager { get; }
private IConfiguration Configuration { get; }
2019-10-31 11:28:30 +00:00
2019-09-18 08:25:57 +00:00
#region Methods Get/Save Setting
2019-10-31 11:28:30 +00:00
2020-09-30 14:47:42 +00:00
public void SaveSetting(string key, string value, int tenant = Tenant.DEFAULT_TENANT)
{
CoreSettings.SaveSetting(key, value, tenant);
}
public string GetSetting(string key, int tenant = Tenant.DEFAULT_TENANT)
{
return CoreSettings.GetSetting(key, tenant);
}
2019-09-18 08:25:57 +00:00
#endregion
2020-09-30 14:47:42 +00:00
2019-05-15 14:56:09 +00:00
#region Methods Get/Set Section
2020-09-30 14:47:42 +00:00
2019-05-15 14:56:09 +00:00
public T GetSection<T>() where T : class
{
return GetSection<T>(typeof(T).Name);
}
public T GetSection<T>(int tenantId) where T : class
{
return GetSection<T>(tenantId, typeof(T).Name);
}
public T GetSection<T>(string sectionName) where T : class
{
2019-09-18 13:33:01 +00:00
return GetSection<T>(TenantManager.GetCurrentTenant().TenantId, sectionName);
2019-05-15 14:56:09 +00:00
}
public T GetSection<T>(int tenantId, string sectionName) where T : class
{
var serializedSection = GetSetting(sectionName, tenantId);
if (serializedSection == null && tenantId != Tenant.DEFAULT_TENANT)
{
serializedSection = GetSetting(sectionName, Tenant.DEFAULT_TENANT);
}
return serializedSection != null ? JsonConvert.DeserializeObject<T>(serializedSection) : null;
}
public void SaveSection<T>(string sectionName, T section) where T : class
{
2019-09-18 13:33:01 +00:00
SaveSection(TenantManager.GetCurrentTenant().TenantId, sectionName, section);
2019-05-15 14:56:09 +00:00
}
public void SaveSection<T>(T section) where T : class
{
SaveSection(typeof(T).Name, section);
}
public void SaveSection<T>(int tenantId, T section) where T : class
{
SaveSection(tenantId, typeof(T).Name, section);
}
public void SaveSection<T>(int tenantId, string sectionName, T section) where T : class
{
var serializedSection = section != null ? JsonConvert.SerializeObject(section) : null;
SaveSetting(sectionName, serializedSection, tenantId);
}
#endregion
2019-10-31 11:28:30 +00:00
}
2019-05-15 14:56:09 +00:00
}