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;
|
2019-07-22 12:31:09 +00:00
|
|
|
|
2020-02-17 08:58:14 +00:00
|
|
|
using ASC.Common;
|
2019-07-22 12:31:09 +00:00
|
|
|
using ASC.Common.Caching;
|
2020-02-17 08:58:14 +00:00
|
|
|
using ASC.Core.Data;
|
2020-07-17 10:52:28 +00:00
|
|
|
using ASC.Core.Tenants;
|
|
|
|
|
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.Caching
|
2020-10-19 15:53:15 +00:00
|
|
|
{
|
|
|
|
[Singletone]
|
2020-02-17 08:58:14 +00:00
|
|
|
class TenantServiceCache
|
|
|
|
{
|
|
|
|
private const string KEY = "tenants";
|
|
|
|
private TimeSpan CacheExpiration { get; set; }
|
|
|
|
internal ICache Cache { get; }
|
|
|
|
internal ICacheNotify<TenantCacheItem> CacheNotifyItem { get; }
|
|
|
|
internal ICacheNotify<TenantSetting> CacheNotifySettings { get; }
|
|
|
|
|
2021-01-12 17:51:14 +00:00
|
|
|
public TenantServiceCache(
|
|
|
|
CoreBaseSettings coreBaseSettings,
|
|
|
|
ICacheNotify<TenantCacheItem> cacheNotifyItem,
|
|
|
|
ICacheNotify<TenantSetting> cacheNotifySettings,
|
|
|
|
ICache cache)
|
2020-02-17 08:58:14 +00:00
|
|
|
{
|
|
|
|
CacheNotifyItem = cacheNotifyItem;
|
|
|
|
CacheNotifySettings = cacheNotifySettings;
|
2021-01-12 17:51:14 +00:00
|
|
|
Cache = cache;
|
2020-02-17 08:58:14 +00:00
|
|
|
CacheExpiration = TimeSpan.FromMinutes(2);
|
|
|
|
|
2019-10-21 12:33:02 +00:00
|
|
|
cacheNotifyItem.Subscribe((t) =>
|
|
|
|
{
|
|
|
|
var tenants = GetTenantStore();
|
|
|
|
tenants.Remove(t.TenantId);
|
|
|
|
tenants.Clear(coreBaseSettings);
|
2020-02-17 08:58:14 +00:00
|
|
|
}, CacheNotifyAction.InsertOrUpdate);
|
2019-10-21 12:33:02 +00:00
|
|
|
|
|
|
|
cacheNotifySettings.Subscribe((s) =>
|
|
|
|
{
|
|
|
|
Cache.Remove(s.Key);
|
2020-02-17 08:58:14 +00:00
|
|
|
}, CacheNotifyAction.Remove);
|
|
|
|
}
|
|
|
|
|
2019-10-21 12:33:02 +00:00
|
|
|
internal TenantStore GetTenantStore()
|
|
|
|
{
|
|
|
|
var store = Cache.Get<TenantStore>(KEY);
|
|
|
|
if (store == null)
|
|
|
|
{
|
|
|
|
Cache.Insert(KEY, store = new TenantStore(), DateTime.UtcNow.Add(CacheExpiration));
|
|
|
|
}
|
|
|
|
return store;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
internal class TenantStore
|
|
|
|
{
|
|
|
|
private readonly Dictionary<int, Tenant> byId = new Dictionary<int, Tenant>();
|
|
|
|
private readonly Dictionary<string, Tenant> byDomain = new Dictionary<string, Tenant>();
|
|
|
|
private readonly object locker = new object();
|
|
|
|
|
|
|
|
|
|
|
|
public Tenant Get(int id)
|
|
|
|
{
|
|
|
|
Tenant t;
|
|
|
|
lock (locker)
|
|
|
|
{
|
|
|
|
byId.TryGetValue(id, out t);
|
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Tenant Get(string domain)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(domain)) return null;
|
|
|
|
|
|
|
|
Tenant t;
|
|
|
|
lock (locker)
|
|
|
|
{
|
|
|
|
byDomain.TryGetValue(domain, out t);
|
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Insert(Tenant t, string ip = null)
|
|
|
|
{
|
|
|
|
if (t == null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Remove(t.TenantId);
|
|
|
|
lock (locker)
|
|
|
|
{
|
|
|
|
byId[t.TenantId] = t;
|
|
|
|
byDomain[t.TenantAlias] = t;
|
|
|
|
if (!string.IsNullOrEmpty(t.MappedDomain)) byDomain[t.MappedDomain] = t;
|
|
|
|
if (!string.IsNullOrEmpty(ip)) byDomain[ip] = t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Remove(int id)
|
|
|
|
{
|
|
|
|
var t = Get(id);
|
|
|
|
if (t != null)
|
|
|
|
{
|
|
|
|
lock (locker)
|
|
|
|
{
|
|
|
|
byId.Remove(id);
|
|
|
|
byDomain.Remove(t.TenantAlias);
|
|
|
|
if (!string.IsNullOrEmpty(t.MappedDomain))
|
|
|
|
{
|
|
|
|
byDomain.Remove(t.MappedDomain);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
internal void Clear(CoreBaseSettings coreBaseSettings)
|
|
|
|
{
|
|
|
|
if (!coreBaseSettings.Standalone) return;
|
|
|
|
lock (locker)
|
|
|
|
{
|
|
|
|
byId.Clear();
|
|
|
|
byDomain.Clear();
|
|
|
|
}
|
|
|
|
}
|
2020-02-17 08:58:14 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-19 15:53:15 +00:00
|
|
|
|
|
|
|
[Scope]
|
2020-02-20 08:05:10 +00:00
|
|
|
class ConfigureCachedTenantService : IConfigureNamedOptions<CachedTenantService>
|
|
|
|
{
|
2020-08-12 09:58:08 +00:00
|
|
|
private IOptionsSnapshot<DbTenantService> Service { get; }
|
|
|
|
private TenantServiceCache TenantServiceCache { get; }
|
2020-02-20 08:05:10 +00:00
|
|
|
|
|
|
|
public ConfigureCachedTenantService(
|
|
|
|
IOptionsSnapshot<DbTenantService> service,
|
|
|
|
TenantServiceCache tenantServiceCache)
|
|
|
|
{
|
|
|
|
Service = service;
|
|
|
|
TenantServiceCache = tenantServiceCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Configure(string name, CachedTenantService options)
|
|
|
|
{
|
|
|
|
Configure(options);
|
|
|
|
options.Service = Service.Get(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Configure(CachedTenantService options)
|
|
|
|
{
|
|
|
|
options.Service = Service.Value;
|
|
|
|
options.TenantServiceCache = TenantServiceCache;
|
|
|
|
options.CacheNotifyItem = TenantServiceCache.CacheNotifyItem;
|
|
|
|
options.CacheNotifySettings = TenantServiceCache.CacheNotifySettings;
|
|
|
|
}
|
2019-10-21 12:33:02 +00:00
|
|
|
}
|
2020-10-21 13:39:06 +00:00
|
|
|
|
|
|
|
[Scope]
|
2019-10-22 16:08:37 +00:00
|
|
|
class CachedTenantService : ITenantService
|
2019-05-15 14:56:09 +00:00
|
|
|
{
|
2020-02-20 08:05:10 +00:00
|
|
|
internal ITenantService Service { get; set; }
|
|
|
|
|
2020-02-17 08:58:14 +00:00
|
|
|
private readonly ICache cache;
|
2020-02-20 08:05:10 +00:00
|
|
|
internal ICacheNotify<TenantSetting> CacheNotifySettings { get; set; }
|
|
|
|
internal ICacheNotify<TenantCacheItem> CacheNotifyItem { get; set; }
|
2019-05-15 14:56:09 +00:00
|
|
|
|
2020-02-17 08:58:14 +00:00
|
|
|
private TimeSpan SettingsExpiration { get; set; }
|
2020-02-20 08:05:10 +00:00
|
|
|
internal TenantServiceCache TenantServiceCache { get; set; }
|
2020-02-17 08:58:14 +00:00
|
|
|
|
2020-02-20 08:05:10 +00:00
|
|
|
public CachedTenantService()
|
2020-02-17 08:58:14 +00:00
|
|
|
{
|
|
|
|
SettingsExpiration = TimeSpan.FromMinutes(2);
|
2020-02-20 14:57:48 +00:00
|
|
|
}
|
2020-02-17 08:58:14 +00:00
|
|
|
|
2021-01-12 17:51:14 +00:00
|
|
|
public CachedTenantService(DbTenantService service, TenantServiceCache tenantServiceCache, ICache cache) : this()
|
2020-02-20 14:57:48 +00:00
|
|
|
{
|
2021-01-12 17:51:14 +00:00
|
|
|
this.cache = cache;
|
2020-02-20 14:57:48 +00:00
|
|
|
Service = service ?? throw new ArgumentNullException("service");
|
2019-10-21 12:33:02 +00:00
|
|
|
TenantServiceCache = tenantServiceCache;
|
2020-02-20 14:57:48 +00:00
|
|
|
CacheNotifyItem = tenantServiceCache.CacheNotifyItem;
|
|
|
|
CacheNotifySettings = tenantServiceCache.CacheNotifySettings;
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void ValidateDomain(string domain)
|
|
|
|
{
|
2020-02-20 08:05:10 +00:00
|
|
|
Service.ValidateDomain(domain);
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public IEnumerable<Tenant> GetTenants(string login, string passwordHash)
|
|
|
|
{
|
2020-02-20 08:05:10 +00:00
|
|
|
return Service.GetTenants(login, passwordHash);
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public IEnumerable<Tenant> GetTenants(DateTime from, bool active = true)
|
|
|
|
{
|
2020-02-20 08:05:10 +00:00
|
|
|
return Service.GetTenants(from, active);
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Tenant GetTenant(int id)
|
|
|
|
{
|
2019-10-21 12:33:02 +00:00
|
|
|
var tenants = TenantServiceCache.GetTenantStore();
|
2019-05-15 14:56:09 +00:00
|
|
|
var t = tenants.Get(id);
|
|
|
|
if (t == null)
|
|
|
|
{
|
2020-02-20 08:05:10 +00:00
|
|
|
t = Service.GetTenant(id);
|
2019-05-15 14:56:09 +00:00
|
|
|
if (t != null)
|
|
|
|
{
|
|
|
|
tenants.Insert(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Tenant GetTenant(string domain)
|
|
|
|
{
|
2019-10-21 12:33:02 +00:00
|
|
|
var tenants = TenantServiceCache.GetTenantStore();
|
2019-05-15 14:56:09 +00:00
|
|
|
var t = tenants.Get(domain);
|
|
|
|
if (t == null)
|
|
|
|
{
|
2020-02-20 08:05:10 +00:00
|
|
|
t = Service.GetTenant(domain);
|
2019-05-15 14:56:09 +00:00
|
|
|
if (t != null)
|
|
|
|
{
|
|
|
|
tenants.Insert(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Tenant GetTenantForStandaloneWithoutAlias(string ip)
|
|
|
|
{
|
2019-10-21 12:33:02 +00:00
|
|
|
var tenants = TenantServiceCache.GetTenantStore();
|
2019-05-15 14:56:09 +00:00
|
|
|
var t = tenants.Get(ip);
|
|
|
|
if (t == null)
|
|
|
|
{
|
2020-02-20 08:05:10 +00:00
|
|
|
t = Service.GetTenantForStandaloneWithoutAlias(ip);
|
2019-05-15 14:56:09 +00:00
|
|
|
if (t != null)
|
|
|
|
{
|
|
|
|
tenants.Insert(t, ip);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2019-10-10 10:59:22 +00:00
|
|
|
public Tenant SaveTenant(CoreSettings coreSettings, Tenant tenant)
|
2019-05-15 14:56:09 +00:00
|
|
|
{
|
2020-02-20 08:05:10 +00:00
|
|
|
tenant = Service.SaveTenant(coreSettings, tenant);
|
|
|
|
CacheNotifyItem.Publish(new TenantCacheItem() { TenantId = tenant.TenantId }, CacheNotifyAction.InsertOrUpdate);
|
2019-05-15 14:56:09 +00:00
|
|
|
return tenant;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void RemoveTenant(int id, bool auto = false)
|
|
|
|
{
|
2020-02-20 08:05:10 +00:00
|
|
|
Service.RemoveTenant(id, auto);
|
|
|
|
CacheNotifyItem.Publish(new TenantCacheItem() { TenantId = id }, CacheNotifyAction.InsertOrUpdate);
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public IEnumerable<TenantVersion> GetTenantVersions()
|
|
|
|
{
|
2020-02-20 08:05:10 +00:00
|
|
|
return Service.GetTenantVersions();
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public byte[] GetTenantSettings(int tenant, string key)
|
|
|
|
{
|
|
|
|
var cacheKey = string.Format("settings/{0}/{1}", tenant, key);
|
|
|
|
var data = cache.Get<byte[]>(cacheKey);
|
|
|
|
if (data == null)
|
|
|
|
{
|
2020-02-20 08:05:10 +00:00
|
|
|
data = Service.GetTenantSettings(tenant, key);
|
2019-05-15 14:56:09 +00:00
|
|
|
cache.Insert(cacheKey, data ?? new byte[0], DateTime.UtcNow + SettingsExpiration);
|
|
|
|
}
|
|
|
|
return data == null ? null : data.Length == 0 ? null : data;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetTenantSettings(int tenant, string key, byte[] data)
|
|
|
|
{
|
2020-02-20 08:05:10 +00:00
|
|
|
Service.SetTenantSettings(tenant, key, data);
|
2019-05-15 14:56:09 +00:00
|
|
|
var cacheKey = string.Format("settings/{0}/{1}", tenant, key);
|
2020-02-20 08:05:10 +00:00
|
|
|
CacheNotifySettings.Publish(new TenantSetting { Key = cacheKey }, CacheNotifyAction.Remove);
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
2020-02-17 08:58:14 +00:00
|
|
|
}
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|