DocSpace-buildtools/common/ASC.Core.Common/Context/Impl/TenantManager.cs

322 lines
9.8 KiB
C#
Raw Normal View History

2022-03-15 18:00:53 +00:00
// (c) Copyright Ascensio System SIA 2010-2022
//
// This program is a free software product.
// You can redistribute it and/or modify it under the terms
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
// Foundation. In accordance with Section 7(a) of the GNU AGPL 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 details, see
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
//
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
//
// The interactive user interfaces in modified source and object code versions of the Program must
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
//
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
// trademark law for use of our trademarks.
//
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
2022-02-15 11:52:43 +00:00
namespace ASC.Core;
2022-07-28 12:29:03 +00:00
[Scope]
2022-02-15 11:52:43 +00:00
public class TenantManager
{
private Tenant _currentTenant;
public const string CurrentTenant = "CURRENT_TENANT";
2022-03-25 16:26:06 +00:00
internal ITenantService TenantService { get; set; }
internal IQuotaService QuotaService { get; set; }
internal ITariffService TariffService { get; set; }
2022-02-15 11:52:43 +00:00
private static readonly List<string> _thisCompAddresses = new List<string>();
2022-03-25 16:26:06 +00:00
internal IHttpContextAccessor HttpContextAccessor { get; set; }
internal CoreBaseSettings CoreBaseSettings { get; set; }
internal CoreSettings CoreSettings { get; set; }
2022-02-15 11:52:43 +00:00
static TenantManager()
{
_thisCompAddresses.Add("localhost");
_thisCompAddresses.Add(Dns.GetHostName().ToLowerInvariant());
_thisCompAddresses.AddRange(Dns.GetHostAddresses("localhost").Select(a => a.ToString()));
try
2020-02-20 08:05:10 +00:00
{
2022-02-15 11:52:43 +00:00
_thisCompAddresses.AddRange(Dns.GetHostAddresses(Dns.GetHostName()).Select(a => a.ToString()));
2020-02-20 08:05:10 +00:00
}
2022-02-15 11:52:43 +00:00
catch
2020-02-20 08:05:10 +00:00
{
2022-02-15 11:52:43 +00:00
// ignore
2020-02-20 08:05:10 +00:00
}
2022-02-15 11:52:43 +00:00
}
public TenantManager()
{
2020-02-20 08:05:10 +00:00
2022-02-15 11:52:43 +00:00
}
public TenantManager(
ITenantService tenantService,
IQuotaService quotaService,
ITariffService tariffService,
CoreBaseSettings coreBaseSettings,
CoreSettings coreSettings)
{
2022-03-25 16:26:06 +00:00
TenantService = tenantService;
QuotaService = quotaService;
TariffService = tariffService;
CoreBaseSettings = coreBaseSettings;
CoreSettings = coreSettings;
2022-02-15 11:52:43 +00:00
}
public TenantManager(
ITenantService tenantService,
IQuotaService quotaService,
ITariffService tariffService,
IHttpContextAccessor httpContextAccessor,
CoreBaseSettings coreBaseSettings,
CoreSettings coreSettings) : this(tenantService, quotaService, tariffService, coreBaseSettings, coreSettings)
{
2022-03-25 16:26:06 +00:00
HttpContextAccessor = httpContextAccessor;
2022-02-15 11:52:43 +00:00
}
public List<Tenant> GetTenants(bool active = true)
{
2022-03-25 16:26:06 +00:00
return TenantService.GetTenants(default, active).ToList();
2022-02-15 11:52:43 +00:00
}
public List<Tenant> GetTenants(List<int> ids)
{
2022-03-25 16:26:06 +00:00
return TenantService.GetTenants(ids).ToList();
2022-02-15 11:52:43 +00:00
}
public Tenant GetTenant(int tenantId)
{
2022-03-25 16:26:06 +00:00
return TenantService.GetTenant(tenantId);
2022-02-15 11:52:43 +00:00
}
public Tenant GetTenant(string domain)
{
if (string.IsNullOrEmpty(domain))
2020-02-20 08:05:10 +00:00
{
2022-02-15 11:52:43 +00:00
return null;
}
2020-02-20 08:05:10 +00:00
2022-02-15 11:52:43 +00:00
Tenant t = null;
if (_thisCompAddresses.Contains(domain, StringComparer.InvariantCultureIgnoreCase))
{
2022-03-25 16:26:06 +00:00
t = TenantService.GetTenant("localhost");
2020-02-20 08:05:10 +00:00
}
2022-02-15 11:52:43 +00:00
var isAlias = false;
if (t == null)
{
2022-03-25 16:26:06 +00:00
var baseUrl = CoreSettings.BaseDomain;
2022-02-15 11:52:43 +00:00
if (!string.IsNullOrEmpty(baseUrl) && domain.EndsWith("." + baseUrl, StringComparison.InvariantCultureIgnoreCase))
{
isAlias = true;
2022-03-25 16:26:06 +00:00
t = TenantService.GetTenant(domain.Substring(0, domain.Length - baseUrl.Length - 1));
2022-02-15 11:52:43 +00:00
}
}
if (t == null)
{
2022-03-25 16:26:06 +00:00
t = TenantService.GetTenant(domain);
2022-02-15 11:52:43 +00:00
}
2022-03-25 16:26:06 +00:00
if (t == null && CoreBaseSettings.Standalone && !isAlias)
2022-02-15 11:52:43 +00:00
{
2022-03-25 16:26:06 +00:00
t = TenantService.GetTenantForStandaloneWithoutAlias(domain);
2022-02-15 11:52:43 +00:00
}
return t;
2020-02-20 08:05:10 +00:00
}
2020-10-19 15:53:15 +00:00
2022-02-15 11:52:43 +00:00
public Tenant SetTenantVersion(Tenant tenant, int version)
2020-06-01 10:55:26 +00:00
{
2022-03-09 17:15:51 +00:00
ArgumentNullException.ThrowIfNull(tenant);
2020-02-20 08:05:10 +00:00
2022-02-15 11:52:43 +00:00
if (tenant.Version != version)
{
tenant.Version = version;
SaveTenant(tenant);
2020-02-20 08:05:10 +00:00
}
2022-02-15 11:52:43 +00:00
else
{
throw new ArgumentException("This is current version already");
2020-04-28 15:11:10 +00:00
}
2022-02-15 11:52:43 +00:00
return tenant;
}
public Tenant SaveTenant(Tenant tenant)
{
2022-03-25 16:26:06 +00:00
var newTenant = TenantService.SaveTenant(CoreSettings, tenant);
2022-02-15 11:52:43 +00:00
if (CallContext.GetData(CurrentTenant) is Tenant)
{
SetCurrentTenant(newTenant);
2021-11-16 17:40:15 +00:00
}
2022-02-15 11:52:43 +00:00
return newTenant;
}
public void RemoveTenant(int tenantId, bool auto = false)
{
2022-03-25 16:26:06 +00:00
TenantService.RemoveTenant(tenantId, auto);
2022-02-15 11:52:43 +00:00
}
public Tenant GetCurrentTenant(HttpContext context)
{
return GetCurrentTenant(true, context);
}
public Tenant GetCurrentTenant(bool throwIfNotFound, HttpContext context)
{
if (_currentTenant != null)
2021-11-16 17:40:15 +00:00
{
2022-02-15 11:52:43 +00:00
return _currentTenant;
2021-11-16 17:40:15 +00:00
}
2022-02-15 11:52:43 +00:00
Tenant tenant = null;
2022-02-15 11:52:43 +00:00
if (context != null)
{
tenant = context.Items[CurrentTenant] as Tenant;
if (tenant == null && context.Request != null)
{
2022-02-15 11:52:43 +00:00
tenant = GetTenant(context.Request.GetUrlRewriter().Host);
context.Items[CurrentTenant] = tenant;
}
2022-02-15 11:52:43 +00:00
}
2022-02-15 11:52:43 +00:00
if (tenant == null && throwIfNotFound)
2020-11-09 11:51:21 +00:00
{
2022-02-15 11:52:43 +00:00
throw new Exception("Could not resolve current tenant :-(.");
}
_currentTenant = tenant;
return tenant;
}
public Tenant GetCurrentTenant()
{
return GetCurrentTenant(true);
}
public Tenant GetCurrentTenant(bool throwIfNotFound)
{
2022-03-25 16:26:06 +00:00
return GetCurrentTenant(throwIfNotFound, HttpContextAccessor?.HttpContext);
2022-02-15 11:52:43 +00:00
}
public void SetCurrentTenant(Tenant tenant)
{
if (tenant != null)
{
_currentTenant = tenant;
2022-03-25 16:26:06 +00:00
if (HttpContextAccessor?.HttpContext != null)
2020-03-04 07:42:43 +00:00
{
2022-03-25 16:26:06 +00:00
HttpContextAccessor.HttpContext.Items[CurrentTenant] = tenant;
2020-03-04 07:42:43 +00:00
}
2020-11-09 11:51:21 +00:00
2022-02-15 11:52:43 +00:00
Thread.CurrentThread.CurrentCulture = tenant.GetCulture();
Thread.CurrentThread.CurrentUICulture = tenant.GetCulture();
}
}
2020-06-01 10:55:26 +00:00
2022-02-15 11:52:43 +00:00
public Tenant SetCurrentTenant(int tenantId)
{
var result = GetTenant(tenantId);
SetCurrentTenant(result);
2020-06-01 10:55:26 +00:00
2022-02-15 11:52:43 +00:00
return result;
}
2021-05-17 11:35:00 +00:00
2022-02-15 11:52:43 +00:00
public Tenant SetCurrentTenant(string domain)
{
var result = GetTenant(domain);
SetCurrentTenant(result);
return result;
}
public void CheckTenantAddress(string address)
{
2022-03-25 16:26:06 +00:00
TenantService.ValidateDomain(address);
2022-02-15 11:52:43 +00:00
}
public IEnumerable<TenantVersion> GetTenantVersions()
{
2022-03-25 16:26:06 +00:00
return TenantService.GetTenantVersions();
2022-02-15 11:52:43 +00:00
}
public IEnumerable<TenantQuota> GetTenantQuotas()
{
return GetTenantQuotas(false);
}
public IEnumerable<TenantQuota> GetTenantQuotas(bool all)
{
2022-03-25 16:26:06 +00:00
return QuotaService.GetTenantQuotas().Where(q => q.Tenant < 0 && (all || q.Visible)).OrderByDescending(q => q.Tenant).ToList();
2022-02-15 11:52:43 +00:00
}
public TenantQuota GetTenantQuota(int tenant)
{
2022-03-25 16:26:06 +00:00
var defaultQuota = QuotaService.GetTenantQuota(tenant) ?? QuotaService.GetTenantQuota(Tenant.DefaultTenant) ?? TenantQuota.Default;
if (defaultQuota.Tenant != tenant && TariffService != null)
2021-05-17 11:35:00 +00:00
{
2022-03-25 16:26:06 +00:00
var tariff = TariffService.GetTariff(tenant);
var currentQuota = QuotaService.GetTenantQuota(tariff.QuotaId);
2022-02-15 11:52:43 +00:00
if (currentQuota != null)
2021-05-17 11:35:00 +00:00
{
2022-02-15 11:52:43 +00:00
currentQuota = (TenantQuota)currentQuota.Clone();
2021-05-17 11:35:00 +00:00
2022-02-15 11:52:43 +00:00
if (currentQuota.ActiveUsers == -1)
{
currentQuota.ActiveUsers = tariff.Quantity;
currentQuota.MaxTotalSize *= currentQuota.ActiveUsers;
2022-06-01 14:07:08 +00:00
currentQuota.Price *= currentQuota.ActiveUsers;
2021-05-17 11:35:00 +00:00
}
2022-02-15 11:52:43 +00:00
return currentQuota;
}
2021-05-17 11:35:00 +00:00
}
2022-02-15 11:52:43 +00:00
return defaultQuota;
}
public IDictionary<string, Dictionary<string, decimal>> GetProductPriceInfo(bool all = true)
{
var productIds = GetTenantQuotas(all)
.Select(p => p.AvangateId)
.Where(id => !string.IsNullOrEmpty(id))
.Distinct()
.ToArray();
2022-03-25 16:26:06 +00:00
return TariffService.GetProductPriceInfo(productIds);
2022-02-15 11:52:43 +00:00
}
public TenantQuota SaveTenantQuota(TenantQuota quota)
{
2022-03-25 16:26:06 +00:00
quota = QuotaService.SaveTenantQuota(quota);
2022-02-15 11:52:43 +00:00
return quota;
}
public void SetTenantQuotaRow(TenantQuotaRow row, bool exchange)
{
2022-03-25 16:26:06 +00:00
QuotaService.SetTenantQuotaRow(row, exchange);
2022-02-15 11:52:43 +00:00
}
public List<TenantQuotaRow> FindTenantQuotaRows(int tenantId)
{
2022-03-25 16:26:06 +00:00
return QuotaService.FindTenantQuotaRows(tenantId).ToList();
2022-02-15 11:52:43 +00:00
}
2019-05-15 14:56:09 +00:00
}