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;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Net;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Web;
|
2019-08-08 13:35:22 +00:00
|
|
|
|
2020-02-17 08:58:14 +00:00
|
|
|
using ASC.Common;
|
2019-08-08 13:35:22 +00:00
|
|
|
using ASC.Common.Notify.Engine;
|
|
|
|
using ASC.Core.Billing;
|
2020-02-17 08:58:14 +00:00
|
|
|
using ASC.Core.Caching;
|
|
|
|
using ASC.Core.Tenants;
|
2019-10-22 16:08:37 +00:00
|
|
|
|
2020-02-17 08:58:14 +00:00
|
|
|
using Microsoft.AspNetCore.Http;
|
2020-02-20 08:05:10 +00:00
|
|
|
using Microsoft.Extensions.Options;
|
2020-02-17 08:58:14 +00:00
|
|
|
|
2019-05-15 14:56:09 +00:00
|
|
|
namespace ASC.Core
|
2020-10-19 19:04:07 +00:00
|
|
|
{
|
|
|
|
[Scope]
|
2020-02-20 08:05:10 +00:00
|
|
|
class ConfigureTenantManager : IConfigureNamedOptions<TenantManager>
|
|
|
|
{
|
2020-08-12 09:58:08 +00:00
|
|
|
private IOptionsSnapshot<CachedTenantService> TenantService { get; }
|
|
|
|
private IOptionsSnapshot<CachedQuotaService> QuotaService { get; }
|
|
|
|
private IOptionsSnapshot<TariffService> TariffService { get; }
|
|
|
|
private IHttpContextAccessor HttpContextAccessor { get; }
|
|
|
|
private CoreBaseSettings CoreBaseSettings { get; }
|
|
|
|
private CoreSettings CoreSettings { get; }
|
2020-02-20 08:05:10 +00:00
|
|
|
|
|
|
|
public ConfigureTenantManager(
|
|
|
|
IOptionsSnapshot<CachedTenantService> tenantService,
|
|
|
|
IOptionsSnapshot<CachedQuotaService> quotaService,
|
|
|
|
IOptionsSnapshot<TariffService> tariffService,
|
|
|
|
IHttpContextAccessor httpContextAccessor,
|
|
|
|
CoreBaseSettings coreBaseSettings,
|
|
|
|
CoreSettings coreSettings
|
|
|
|
)
|
|
|
|
{
|
|
|
|
TenantService = tenantService;
|
|
|
|
QuotaService = quotaService;
|
|
|
|
TariffService = tariffService;
|
|
|
|
HttpContextAccessor = httpContextAccessor;
|
|
|
|
CoreBaseSettings = coreBaseSettings;
|
|
|
|
CoreSettings = coreSettings;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Configure(string name, TenantManager options)
|
|
|
|
{
|
|
|
|
Configure(options);
|
|
|
|
|
|
|
|
options.TenantService = TenantService.Get(name);
|
|
|
|
options.QuotaService = QuotaService.Get(name);
|
|
|
|
options.TariffService = TariffService.Get(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Configure(TenantManager options)
|
|
|
|
{
|
2020-05-17 13:08:20 +00:00
|
|
|
options.HttpContextAccessor = HttpContextAccessor;
|
2020-02-20 08:05:10 +00:00
|
|
|
options.CoreBaseSettings = CoreBaseSettings;
|
|
|
|
options.CoreSettings = CoreSettings;
|
|
|
|
|
|
|
|
options.TenantService = TenantService.Value;
|
|
|
|
options.QuotaService = QuotaService.Value;
|
|
|
|
options.TariffService = TariffService.Value;
|
|
|
|
}
|
|
|
|
}
|
2020-10-19 15:53:15 +00:00
|
|
|
|
|
|
|
[Scope(typeof(ConfigureTenantManager))]
|
2019-05-15 14:56:09 +00:00
|
|
|
public class TenantManager
|
2020-06-01 10:55:26 +00:00
|
|
|
{
|
2020-11-09 11:51:21 +00:00
|
|
|
private Tenant CurrentTenant { get; set; }
|
2020-06-01 10:55:26 +00:00
|
|
|
|
2019-05-15 14:56:09 +00:00
|
|
|
public const string CURRENT_TENANT = "CURRENT_TENANT";
|
2020-02-20 08:05:10 +00:00
|
|
|
internal ITenantService TenantService { get; set; }
|
|
|
|
internal IQuotaService QuotaService { get; set; }
|
2020-02-20 14:57:48 +00:00
|
|
|
internal ITariffService TariffService { get; set; }
|
|
|
|
|
2020-09-28 15:42:03 +00:00
|
|
|
private static readonly List<string> thisCompAddresses = new List<string>();
|
2020-02-17 08:58:14 +00:00
|
|
|
|
2020-05-17 13:08:20 +00:00
|
|
|
internal IHttpContextAccessor HttpContextAccessor { get; set; }
|
2020-02-20 08:05:10 +00:00
|
|
|
internal CoreBaseSettings CoreBaseSettings { get; set; }
|
|
|
|
internal CoreSettings CoreSettings { get; set; }
|
2020-02-17 08:58:14 +00:00
|
|
|
|
|
|
|
static TenantManager()
|
|
|
|
{
|
2019-05-15 14:56:09 +00:00
|
|
|
thisCompAddresses.Add("localhost");
|
|
|
|
thisCompAddresses.Add(Dns.GetHostName().ToLowerInvariant());
|
|
|
|
thisCompAddresses.AddRange(Dns.GetHostAddresses("localhost").Select(a => a.ToString()));
|
|
|
|
try
|
|
|
|
{
|
|
|
|
thisCompAddresses.AddRange(Dns.GetHostAddresses(Dns.GetHostName()).Select(a => a.ToString()));
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
// ignore
|
2020-02-17 08:58:14 +00:00
|
|
|
}
|
2019-09-09 12:56:33 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 08:05:10 +00:00
|
|
|
public TenantManager()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2019-09-09 12:56:33 +00:00
|
|
|
|
2020-02-17 08:58:14 +00:00
|
|
|
public TenantManager(
|
|
|
|
ITenantService tenantService,
|
|
|
|
IQuotaService quotaService,
|
|
|
|
ITariffService tariffService,
|
|
|
|
CoreBaseSettings coreBaseSettings,
|
2019-09-18 08:25:57 +00:00
|
|
|
CoreSettings coreSettings)
|
2019-09-09 12:56:33 +00:00
|
|
|
{
|
2020-02-20 14:57:48 +00:00
|
|
|
TenantService = tenantService;
|
|
|
|
QuotaService = quotaService;
|
|
|
|
TariffService = tariffService;
|
2020-02-17 08:58:14 +00:00
|
|
|
CoreBaseSettings = coreBaseSettings;
|
|
|
|
CoreSettings = coreSettings;
|
2020-04-28 15:11:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public TenantManager(
|
|
|
|
ITenantService tenantService,
|
|
|
|
IQuotaService quotaService,
|
|
|
|
ITariffService tariffService,
|
|
|
|
IHttpContextAccessor httpContextAccessor,
|
|
|
|
CoreBaseSettings coreBaseSettings,
|
|
|
|
CoreSettings coreSettings) : this(tenantService, quotaService, tariffService, coreBaseSettings, coreSettings)
|
|
|
|
{
|
2020-05-17 13:08:20 +00:00
|
|
|
HttpContextAccessor = httpContextAccessor;
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public List<Tenant> GetTenants(bool active = true)
|
|
|
|
{
|
2020-02-20 08:05:10 +00:00
|
|
|
return TenantService.GetTenants(default, active).ToList();
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Tenant GetTenant(int tenantId)
|
|
|
|
{
|
2020-02-20 08:05:10 +00:00
|
|
|
return TenantService.GetTenant(tenantId);
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Tenant GetTenant(string domain)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(domain)) return null;
|
|
|
|
|
|
|
|
Tenant t = null;
|
|
|
|
if (thisCompAddresses.Contains(domain, StringComparer.InvariantCultureIgnoreCase))
|
|
|
|
{
|
2020-02-20 08:05:10 +00:00
|
|
|
t = TenantService.GetTenant("localhost");
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
var isAlias = false;
|
|
|
|
if (t == null)
|
|
|
|
{
|
2019-09-18 08:25:57 +00:00
|
|
|
var baseUrl = CoreSettings.BaseDomain;
|
2019-08-15 13:16:39 +00:00
|
|
|
if (!string.IsNullOrEmpty(baseUrl) && domain.EndsWith("." + baseUrl, StringComparison.InvariantCultureIgnoreCase))
|
2019-05-15 14:56:09 +00:00
|
|
|
{
|
|
|
|
isAlias = true;
|
2020-02-20 08:05:10 +00:00
|
|
|
t = TenantService.GetTenant(domain.Substring(0, domain.Length - baseUrl.Length - 1));
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (t == null)
|
|
|
|
{
|
2020-02-20 08:05:10 +00:00
|
|
|
t = TenantService.GetTenant(domain);
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
2019-09-18 15:19:30 +00:00
|
|
|
if (t == null && CoreBaseSettings.Standalone && !isAlias)
|
2019-05-15 14:56:09 +00:00
|
|
|
{
|
2020-02-20 08:05:10 +00:00
|
|
|
t = TenantService.GetTenantForStandaloneWithoutAlias(domain);
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Tenant SetTenantVersion(Tenant tenant, int version)
|
|
|
|
{
|
|
|
|
if (tenant == null) throw new ArgumentNullException("tenant");
|
|
|
|
if (tenant.Version != version)
|
|
|
|
{
|
|
|
|
tenant.Version = version;
|
2020-10-12 19:39:23 +00:00
|
|
|
SaveTenant(tenant);
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new ArgumentException("This is current version already");
|
|
|
|
}
|
|
|
|
return tenant;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Tenant SaveTenant(Tenant tenant)
|
|
|
|
{
|
2020-02-20 08:05:10 +00:00
|
|
|
var newTenant = TenantService.SaveTenant(CoreSettings, tenant);
|
2019-08-16 09:08:46 +00:00
|
|
|
if (CallContext.GetData(CURRENT_TENANT) is Tenant) SetCurrentTenant(newTenant);
|
2019-05-15 14:56:09 +00:00
|
|
|
|
|
|
|
return newTenant;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void RemoveTenant(int tenantId, bool auto = false)
|
|
|
|
{
|
2020-02-20 08:05:10 +00:00
|
|
|
TenantService.RemoveTenant(tenantId, auto);
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
|
2019-08-08 13:35:22 +00:00
|
|
|
public Tenant GetCurrentTenant(HttpContext context)
|
2019-05-15 14:56:09 +00:00
|
|
|
{
|
2019-08-08 13:35:22 +00:00
|
|
|
return GetCurrentTenant(true, context);
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
|
2019-08-08 13:35:22 +00:00
|
|
|
public Tenant GetCurrentTenant(bool throwIfNotFound, HttpContext context)
|
2020-11-09 11:51:21 +00:00
|
|
|
{
|
2020-03-04 07:42:43 +00:00
|
|
|
if (CurrentTenant != null)
|
|
|
|
{
|
|
|
|
return CurrentTenant;
|
|
|
|
}
|
2020-11-09 11:51:21 +00:00
|
|
|
|
|
|
|
Tenant tenant = null;
|
2020-03-04 07:42:43 +00:00
|
|
|
|
2019-08-08 09:26:58 +00:00
|
|
|
if (context != null)
|
2019-05-15 14:56:09 +00:00
|
|
|
{
|
2019-08-08 09:26:58 +00:00
|
|
|
tenant = context.Items[CURRENT_TENANT] as Tenant;
|
|
|
|
if (tenant == null && context.Request != null)
|
2019-05-15 14:56:09 +00:00
|
|
|
{
|
2019-08-08 09:26:58 +00:00
|
|
|
tenant = GetTenant(context.Request.GetUrlRewriter().Host);
|
|
|
|
context.Items[CURRENT_TENANT] = tenant;
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-09 11:51:21 +00:00
|
|
|
|
2019-05-15 14:56:09 +00:00
|
|
|
if (tenant == null && throwIfNotFound)
|
|
|
|
{
|
|
|
|
throw new Exception("Could not resolve current tenant :-(.");
|
2020-05-17 13:08:20 +00:00
|
|
|
}
|
2020-06-01 10:55:26 +00:00
|
|
|
|
|
|
|
CurrentTenant = tenant;
|
|
|
|
|
2019-05-15 14:56:09 +00:00
|
|
|
return tenant;
|
2020-02-17 08:58:14 +00:00
|
|
|
}
|
|
|
|
|
2019-08-08 13:35:22 +00:00
|
|
|
public Tenant GetCurrentTenant()
|
|
|
|
{
|
|
|
|
return GetCurrentTenant(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Tenant GetCurrentTenant(bool throwIfNotFound)
|
|
|
|
{
|
2020-06-09 07:54:05 +00:00
|
|
|
return GetCurrentTenant(throwIfNotFound, HttpContextAccessor?.HttpContext);
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetCurrentTenant(Tenant tenant)
|
|
|
|
{
|
|
|
|
if (tenant != null)
|
2020-03-04 07:42:43 +00:00
|
|
|
{
|
|
|
|
CurrentTenant = tenant;
|
2020-05-17 13:08:20 +00:00
|
|
|
if (HttpContextAccessor?.HttpContext != null)
|
2019-05-15 14:56:09 +00:00
|
|
|
{
|
2020-05-17 13:08:20 +00:00
|
|
|
HttpContextAccessor.HttpContext.Items[CURRENT_TENANT] = tenant;
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
Thread.CurrentThread.CurrentCulture = tenant.GetCulture();
|
|
|
|
Thread.CurrentThread.CurrentUICulture = tenant.GetCulture();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-08 09:26:58 +00:00
|
|
|
public Tenant SetCurrentTenant(int tenantId)
|
2020-02-17 08:58:14 +00:00
|
|
|
{
|
2019-08-08 09:26:58 +00:00
|
|
|
var result = GetTenant(tenantId);
|
2020-02-17 08:58:14 +00:00
|
|
|
SetCurrentTenant(result);
|
2019-08-08 09:26:58 +00:00
|
|
|
return result;
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
|
2019-08-08 09:26:58 +00:00
|
|
|
public Tenant SetCurrentTenant(string domain)
|
2020-02-17 08:58:14 +00:00
|
|
|
{
|
2019-08-08 09:26:58 +00:00
|
|
|
var result = GetTenant(domain);
|
2020-02-17 08:58:14 +00:00
|
|
|
SetCurrentTenant(result);
|
2019-08-08 09:26:58 +00:00
|
|
|
return result;
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void CheckTenantAddress(string address)
|
|
|
|
{
|
2020-02-20 08:05:10 +00:00
|
|
|
TenantService.ValidateDomain(address);
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public IEnumerable<TenantVersion> GetTenantVersions()
|
|
|
|
{
|
2020-02-20 08:05:10 +00:00
|
|
|
return TenantService.GetTenantVersions();
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<TenantQuota> GetTenantQuotas()
|
|
|
|
{
|
|
|
|
return GetTenantQuotas(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public IEnumerable<TenantQuota> GetTenantQuotas(bool all)
|
|
|
|
{
|
2020-02-20 08:05:10 +00:00
|
|
|
return QuotaService.GetTenantQuotas().Where(q => q.Id < 0 && (all || q.Visible)).OrderByDescending(q => q.Id).ToList();
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
2021-05-17 11:35:00 +00:00
|
|
|
|
|
|
|
public TenantQuota GetTenantQuota(int tenant)
|
|
|
|
{
|
|
|
|
var defaultQuota = QuotaService.GetTenantQuota(tenant) ?? QuotaService.GetTenantQuota(Tenant.DEFAULT_TENANT) ?? TenantQuota.Default;
|
|
|
|
if (defaultQuota.Id != tenant && TariffService != null)
|
|
|
|
{
|
|
|
|
var tariff = TariffService.GetTariff(tenant);
|
|
|
|
var currentQuota = QuotaService.GetTenantQuota(tariff.QuotaId);
|
|
|
|
if (currentQuota != null)
|
|
|
|
{
|
|
|
|
currentQuota = (TenantQuota)currentQuota.Clone();
|
|
|
|
|
|
|
|
if (currentQuota.ActiveUsers == -1)
|
|
|
|
{
|
|
|
|
currentQuota.ActiveUsers = tariff.Quantity;
|
|
|
|
currentQuota.MaxTotalSize *= currentQuota.ActiveUsers;
|
|
|
|
}
|
|
|
|
|
|
|
|
return currentQuota;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return defaultQuota;
|
|
|
|
}
|
2019-05-15 14:56:09 +00:00
|
|
|
|
2021-05-17 11:35:00 +00:00
|
|
|
public IDictionary<string, Dictionary<string, decimal>> GetProductPriceInfo(bool all = true)
|
2019-05-15 14:56:09 +00:00
|
|
|
{
|
|
|
|
var productIds = GetTenantQuotas(all)
|
|
|
|
.Select(p => p.AvangateId)
|
|
|
|
.Where(id => !string.IsNullOrEmpty(id))
|
|
|
|
.Distinct()
|
|
|
|
.ToArray();
|
2020-02-20 08:05:10 +00:00
|
|
|
return TariffService.GetProductPriceInfo(productIds);
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public TenantQuota SaveTenantQuota(TenantQuota quota)
|
|
|
|
{
|
2020-02-20 08:05:10 +00:00
|
|
|
quota = QuotaService.SaveTenantQuota(quota);
|
2019-05-15 14:56:09 +00:00
|
|
|
return quota;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetTenantQuotaRow(TenantQuotaRow row, bool exchange)
|
|
|
|
{
|
2020-02-20 08:05:10 +00:00
|
|
|
QuotaService.SetTenantQuotaRow(row, exchange);
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
|
2020-12-28 13:22:08 +00:00
|
|
|
public List<TenantQuotaRow> FindTenantQuotaRows(int tenantId)
|
2019-05-15 14:56:09 +00:00
|
|
|
{
|
2020-12-28 13:22:08 +00:00
|
|
|
return QuotaService.FindTenantQuotaRows(tenantId).ToList();
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
2020-02-17 08:58:14 +00:00
|
|
|
}
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|