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

322 lines
11 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;
2019-08-15 12:04:42 +00:00
using System.Text;
using ASC.Common.Utils;
2019-08-15 12:04:42 +00:00
using ASC.Core.Configuration;
using ASC.Core.Tenants;
2019-09-20 14:21:41 +00:00
using Microsoft.Extensions.Configuration;
2019-08-15 12:04:42 +00:00
using Newtonsoft.Json;
2019-05-15 14:56:09 +00:00
namespace ASC.Core
2019-09-18 08:25:57 +00:00
{
2019-09-18 15:19:30 +00:00
public class CoreBaseSettings
2019-09-18 08:25:57 +00:00
{
2019-05-15 14:56:09 +00:00
private bool? standalone;
2019-09-18 08:25:57 +00:00
private bool? personal;
private bool? customMode;
2019-09-20 14:21:41 +00:00
public IConfiguration Configuration { get; }
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
{
2019-09-20 14:21:41 +00:00
get { return personal ?? (bool)(personal = Configuration["core.personal"] == "true"); }
2019-09-18 08:25:57 +00:00
}
2019-06-06 14:41:16 +00:00
public bool CustomMode
{
2019-09-20 14:21:41 +00:00
get { return customMode ?? (bool)(customMode = Configuration["core.custom-mode"] == "true"); }
2019-09-18 08:25:57 +00:00
}
2019-09-18 15:19:30 +00:00
}
public class CoreSettings
{
private string basedomain;
2019-08-15 12:04:42 +00:00
2019-05-15 14:56:09 +00:00
public string BaseDomain
{
get
{
if (basedomain == null)
{
basedomain = ConfigurationManager.AppSettings["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-09-18 08:25:57 +00:00
}
public ITenantService TenantService { get; }
2019-09-18 15:19:30 +00:00
public CoreBaseSettings CoreBaseSettings { get; }
2019-09-18 08:25:57 +00:00
2019-09-18 15:19:30 +00:00
public CoreSettings(ITenantService tenantService, CoreBaseSettings coreBaseSettings)
2019-09-18 08:25:57 +00:00
{
TenantService = tenantService;
2019-09-18 15:19:30 +00:00
CoreBaseSettings = coreBaseSettings;
2019-09-18 08:25:57 +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;
return ConfigurationManager.AppSettings["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;
2019-09-18 08:25:57 +00:00
}
}
public class CoreConfiguration
{
private long? personalMaxSpace;
2019-09-18 15:19:30 +00:00
public CoreConfiguration(CoreBaseSettings coreBaseSettings, CoreSettings coreSettings, TenantManager tenantManager)
2019-09-18 08:25:57 +00:00
{
2019-09-18 15:19:30 +00:00
CoreBaseSettings = coreBaseSettings;
2019-09-18 08:25:57 +00:00
CoreSettings = coreSettings;
2019-09-18 13:33:01 +00:00
TenantManager = tenantManager;
2019-09-18 08:25:57 +00:00
}
public bool Standalone
{
2019-09-18 15:19:30 +00:00
get => CoreBaseSettings.Standalone;
2019-09-18 08:25:57 +00:00
}
public bool Personal
{
2019-09-18 15:19:30 +00:00
get => CoreBaseSettings.Personal;
2019-05-15 14:56:09 +00:00
}
2019-09-18 08:25:57 +00:00
public bool CustomMode
{
2019-09-18 15:19:30 +00:00
get => CoreBaseSettings.CustomMode;
2019-09-18 08:25:57 +00:00
}
public long PersonalMaxSpace(PersonalQuotaSettings personalQuotaSettings)
{
var quotaSettings = personalQuotaSettings.LoadForCurrentUser();
if (quotaSettings.MaxSpace != long.MaxValue)
return quotaSettings.MaxSpace;
if (personalMaxSpace.HasValue)
return personalMaxSpace.Value;
if (!long.TryParse(ConfigurationManager.AppSettings["core.personal.maxspace"], out var value))
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
}
public string BaseDomain
{
get => CoreSettings.BaseDomain;
set => CoreSettings.BaseDomain = value;
}
2019-09-18 15:19:30 +00:00
public CoreBaseSettings CoreBaseSettings { get; }
2019-09-18 08:25:57 +00:00
public CoreSettings CoreSettings { get; }
2019-09-18 13:33:01 +00:00
public TenantManager TenantManager { get; }
2019-09-18 08:25:57 +00:00
#region Methods Get/Save Setting
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) => CoreSettings.GetSetting(key, tenant);
#endregion
public string GetKey(int tenant) => CoreSettings.GetKey(tenant);
public string GetAffiliateId(int tenant) => CoreSettings.GetAffiliateId(tenant);
2019-05-15 14:56:09 +00:00
#region Methods Get/Set Section
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
}
}