DocSpace-client/common/ASC.Core.Common/MultiRegionHostedSolution.cs

283 lines
9.9 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;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
2019-11-25 09:49:12 +00:00
using System.Security;
2019-12-16 14:55:59 +00:00
2019-11-06 15:03:09 +00:00
using ASC.Common.Logging;
2019-08-15 12:04:42 +00:00
using ASC.Common.Utils;
2019-05-15 14:56:09 +00:00
using ASC.Core.Billing;
2019-11-06 15:03:09 +00:00
using ASC.Core.Security.Authentication;
2019-11-25 09:49:12 +00:00
using ASC.Core.Tenants;
2019-12-16 14:55:59 +00:00
2019-12-17 08:27:38 +00:00
using Microsoft.EntityFrameworkCore;
2019-11-06 15:03:09 +00:00
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
2019-12-17 08:27:38 +00:00
using DbContext = ASC.Core.Common.EF.Context.DbContext;
2019-05-15 14:56:09 +00:00
namespace ASC.Core
{
public class MultiRegionHostedSolution
{
private readonly Dictionary<string, HostedSolution> regions = new Dictionary<string, HostedSolution>();
2019-11-06 15:03:09 +00:00
private readonly string dbid;
2020-08-12 09:58:08 +00:00
private IConfiguration Configuraion { get; }
private CookieStorage CookieStorage { get; }
private EFLoggerFactory LoggerFactory { get; }
private IOptionsSnapshot<HostedSolution> HostedSolutionOptions { get; }
2019-12-03 15:20:21 +00:00
2019-11-06 15:03:09 +00:00
public MultiRegionHostedSolution(string dbid,
IConfiguration configuraion,
2019-12-17 08:27:38 +00:00
CookieStorage cookieStorage,
2020-02-20 08:05:10 +00:00
EFLoggerFactory loggerFactory,
IOptionsSnapshot<HostedSolution> hostedSolutionOptions)
2019-05-15 14:56:09 +00:00
{
2019-11-06 15:03:09 +00:00
this.dbid = dbid;
Configuraion = configuraion;
2019-12-17 08:27:38 +00:00
CookieStorage = cookieStorage;
LoggerFactory = loggerFactory;
2020-02-20 08:05:10 +00:00
HostedSolutionOptions = hostedSolutionOptions;
2019-10-09 10:09:54 +00:00
Initialize();
2019-05-15 14:56:09 +00:00
}
public List<Tenant> GetTenants(DateTime from)
{
return GetRegionServices()
.SelectMany(r => r.GetTenants(from))
.ToList();
}
public List<Tenant> FindTenants(string login)
{
return FindTenants(login, null);
}
public List<Tenant> FindTenants(string login, string password)
{
var result = new List<Tenant>();
Exception error = null;
foreach (var service in GetRegionServices())
{
try
{
result.AddRange(service.FindTenants(login, password));
}
catch (SecurityException exception)
{
error = exception;
}
}
if (!result.Any() && error != null)
{
throw error;
}
return result;
}
public void RegisterTenant(string region, TenantRegistrationInfo ri, out Tenant tenant)
{
ri.HostedRegion = region;
GetRegionService(region).RegisterTenant(ri, out tenant);
}
public Tenant GetTenant(string domain)
{
foreach (var service in GetRegionServices())
{
var tenant = service.GetTenant(domain);
if (tenant != null)
{
return tenant;
}
}
return null;
}
public Tenant GetTenant(string region, int tenantId)
{
return GetRegionService(region).GetTenant(tenantId);
}
public Tenant SaveTenant(string region, Tenant tenant)
{
return GetRegionService(region).SaveTenant(tenant);
}
public string CreateAuthenticationCookie(string region, int tenantId, string login, string password)
{
2019-10-18 08:48:27 +00:00
return GetRegionService(region).CreateAuthenticationCookie(CookieStorage, tenantId, login, password);
2019-05-15 14:56:09 +00:00
}
public string CreateAuthenticationCookie(string region, int tenantId, Guid userId)
{
2019-10-18 08:48:27 +00:00
return GetRegionService(region).CreateAuthenticationCookie(CookieStorage, tenantId, userId);
2019-05-15 14:56:09 +00:00
}
public Tariff GetTariff(string region, int tenantId, bool withRequestToPaymentSystem = true)
{
return GetRegionService(region).GetTariff(tenantId, withRequestToPaymentSystem);
}
public void SetTariff(string region, int tenant, bool paid)
{
GetRegionService(region).SetTariff(tenant, paid);
}
public void SetTariff(string region, int tenant, Tariff tariff)
{
GetRegionService(region).SetTariff(tenant, tariff);
}
public void SaveButton(string region, int tariffId, string partnerId, string buttonUrl)
{
GetRegionService(region).SaveButton(tariffId, partnerId, buttonUrl);
}
public TenantQuota GetTenantQuota(string region, int tenant)
{
return GetRegionService(region).GetTenantQuota(tenant);
}
public void CheckTenantAddress(string address)
{
foreach (var service in GetRegionServices())
{
service.CheckTenantAddress(address);
}
}
public IEnumerable<string> GetRegions()
{
return GetRegionServices().Select(s => s.Region).ToList();
}
private IEnumerable<HostedSolution> GetRegionServices()
{
2019-08-15 13:16:39 +00:00
return regions.Where(x => !string.IsNullOrEmpty(x.Key))
2019-05-15 14:56:09 +00:00
.Select(x => x.Value);
}
private HostedSolution GetRegionService(string region)
{
return regions[region];
}
private void Initialize()
2019-11-06 15:03:09 +00:00
{
var connectionStrings = Configuraion.GetConnectionStrings();
var dbConnectionStrings = Configuraion.GetConnectionStrings(dbid);
2019-10-09 16:08:37 +00:00
2019-10-09 10:09:54 +00:00
if (Convert.ToBoolean(Configuraion["core.multi-hosted.config-only"] ?? "false"))
2019-05-15 14:56:09 +00:00
{
2019-10-09 16:08:37 +00:00
foreach (var cs in Configuraion.GetConnectionStrings())
2019-05-15 14:56:09 +00:00
{
2019-10-09 10:09:54 +00:00
if (cs.Name.StartsWith(dbid + "."))
2019-05-15 14:56:09 +00:00
{
2019-10-09 10:09:54 +00:00
var name = cs.Name.Substring(dbid.Length + 1);
2020-02-20 08:05:10 +00:00
regions[name] = HostedSolutionOptions.Get(cs.Name);
2019-10-09 10:09:54 +00:00
}
}
2019-05-15 14:56:09 +00:00
2020-02-20 08:05:10 +00:00
regions[dbid] = HostedSolutionOptions.Get(dbid);
2019-10-09 10:09:54 +00:00
if (!regions.ContainsKey(string.Empty))
{
2020-02-20 08:05:10 +00:00
regions[string.Empty] = HostedSolutionOptions.Get(dbid);
2019-10-09 10:09:54 +00:00
}
}
else
{
2019-05-15 14:56:09 +00:00
2019-10-09 10:09:54 +00:00
var find = false;
2019-10-09 16:08:37 +00:00
foreach (var cs in connectionStrings)
2019-12-17 08:27:38 +00:00
{
var dbContextOptionsBuilder = new DbContextOptionsBuilder<DbContext>();
var options = dbContextOptionsBuilder
.UseMySql(cs.ConnectionString)
.UseLoggerFactory(LoggerFactory)
.Options;
using var dbContext = new DbContext(options);
2019-10-09 10:09:54 +00:00
if (cs.Name.StartsWith(dbid + "."))
{
var name = cs.Name.Substring(dbid.Length + 1);
2020-02-20 08:05:10 +00:00
regions[name] = HostedSolutionOptions.Get(name);
2019-10-09 10:09:54 +00:00
find = true;
}
}
if (find)
{
2020-02-20 08:05:10 +00:00
regions[dbid] = HostedSolutionOptions.Get(dbid);
2019-10-09 10:09:54 +00:00
if (!regions.ContainsKey(string.Empty))
{
2020-02-20 08:05:10 +00:00
regions[string.Empty] = HostedSolutionOptions.Get(dbid);
2019-10-09 10:09:54 +00:00
}
}
else
{
2019-10-09 16:08:37 +00:00
foreach (var connectionString in connectionStrings)
2019-10-09 10:09:54 +00:00
{
try
2019-12-17 08:27:38 +00:00
{
var dbContextOptionsBuilder = new DbContextOptionsBuilder<DbContext>();
var options = dbContextOptionsBuilder
.UseMySql(connectionString.ConnectionString)
.UseLoggerFactory(LoggerFactory)
.Options;
using var dbContext = new DbContext(options);
var q = dbContext.Regions.ToList();
foreach (var r in q)
{
var cs = new System.Configuration.ConnectionStringSettings(r.Region, r.ConnectionString, r.Provider);
if (!regions.ContainsKey(string.Empty))
{
2020-02-20 08:05:10 +00:00
regions[string.Empty] = HostedSolutionOptions.Get(cs.Name);
2019-12-17 08:27:38 +00:00
}
2020-02-20 08:05:10 +00:00
regions[cs.Name] = HostedSolutionOptions.Get(cs.Name);
2019-12-17 08:27:38 +00:00
}
2019-05-15 14:56:09 +00:00
}
2019-10-09 10:09:54 +00:00
catch (DbException) { }
2019-05-15 14:56:09 +00:00
}
}
}
}
}
}