DocSpace-buildtools/common/ASC.Core.Common/Data/DbSettingsManager.cs

336 lines
12 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 JsonSerializer = System.Text.Json.JsonSerializer;
2019-10-22 11:21:44 +00:00
2019-05-15 14:56:09 +00:00
namespace ASC.Core.Data
2020-10-19 15:53:15 +00:00
{
[Singletone]
2020-02-17 08:58:14 +00:00
public class DbSettingsManagerCache
{
2019-10-11 15:03:03 +00:00
public ICache Cache { get; }
private ICacheNotify<SettingsCacheItem> Notify { get; }
2020-02-17 08:58:14 +00:00
public DbSettingsManagerCache(ICacheNotify<SettingsCacheItem> notify, ICache cache)
2020-02-17 08:58:14 +00:00
{
2021-01-12 17:51:14 +00:00
Cache = cache;
2019-11-06 15:03:09 +00:00
Notify = notify;
Notify.Subscribe((i) => Cache.Remove(i.Key), ASC.Common.Caching.CacheNotifyAction.Remove);
2020-02-17 08:58:14 +00:00
}
public void Remove(string key)
{
Notify.Publish(new SettingsCacheItem { Key = key }, ASC.Common.Caching.CacheNotifyAction.Remove);
2020-02-17 08:58:14 +00:00
}
}
2020-10-22 17:57:18 +00:00
[Scope]
2020-02-20 08:05:10 +00:00
class ConfigureDbSettingsManager : IConfigureNamedOptions<DbSettingsManager>
{
private IServiceProvider ServiceProvider { get; }
private DbSettingsManagerCache DbSettingsManagerCache { get; }
private IOptionsMonitor<ILog> ILog { get; }
private AuthContext AuthContext { get; }
private IOptionsSnapshot<TenantManager> TenantManager { get; }
private DbContextManager<WebstudioDbContext> DbContextManager { get; }
public ConfigureDbSettingsManager(
IServiceProvider serviceProvider,
DbSettingsManagerCache dbSettingsManagerCache,
IOptionsMonitor<ILog> iLog,
AuthContext authContext,
IOptionsSnapshot<TenantManager> tenantManager,
DbContextManager<WebstudioDbContext> dbContextManager
)
{
ServiceProvider = serviceProvider;
DbSettingsManagerCache = dbSettingsManagerCache;
ILog = iLog;
AuthContext = authContext;
TenantManager = tenantManager;
DbContextManager = dbContextManager;
}
public void Configure(string name, DbSettingsManager options)
{
Configure(options);
options.TenantManager = TenantManager.Get(name);
2020-10-18 19:00:38 +00:00
options.LazyWebstudioDbContext = new Lazy<WebstudioDbContext>(() => DbContextManager.Get(name));
2020-02-20 08:05:10 +00:00
}
public void Configure(DbSettingsManager options)
{
options.ServiceProvider = ServiceProvider;
options.DbSettingsManagerCache = DbSettingsManagerCache;
options.AuthContext = AuthContext;
options.Log = ILog.CurrentValue;
options.TenantManager = TenantManager.Value;
2020-10-18 19:00:38 +00:00
options.LazyWebstudioDbContext = new Lazy<WebstudioDbContext>(() => DbContextManager.Value);
2020-02-20 08:05:10 +00:00
}
}
2020-10-19 15:53:15 +00:00
[Scope(typeof(ConfigureDbSettingsManager))]
2019-11-06 15:03:09 +00:00
public class DbSettingsManager
{
private readonly TimeSpan expirationTimeout = TimeSpan.FromMinutes(5);
2020-02-17 08:58:14 +00:00
2020-02-20 08:05:10 +00:00
internal ILog Log { get; set; }
internal ICache Cache { get; set; }
internal IServiceProvider ServiceProvider { get; set; }
internal DbSettingsManagerCache DbSettingsManagerCache { get; set; }
internal AuthContext AuthContext { get; set; }
internal TenantManager TenantManager { get; set; }
2020-10-18 19:00:38 +00:00
internal Lazy<WebstudioDbContext> LazyWebstudioDbContext { get; set; }
2021-08-16 12:20:41 +00:00
internal WebstudioDbContext WebstudioDbContext { get => LazyWebstudioDbContext.Value; }
2020-02-17 08:58:14 +00:00
2020-02-20 08:05:10 +00:00
public DbSettingsManager()
{
}
2020-02-17 08:58:14 +00:00
public DbSettingsManager(
IServiceProvider serviceProvider,
DbSettingsManagerCache dbSettingsManagerCache,
IOptionsMonitor<ILog> option,
AuthContext authContext,
TenantManager tenantManager,
2020-02-20 08:05:10 +00:00
DbContextManager<WebstudioDbContext> dbContextManager)
2020-02-17 08:58:14 +00:00
{
ServiceProvider = serviceProvider;
DbSettingsManagerCache = dbSettingsManagerCache;
AuthContext = authContext;
TenantManager = tenantManager;
Cache = dbSettingsManagerCache.Cache;
2020-02-20 08:05:10 +00:00
Log = option.CurrentValue;
2020-10-18 19:00:38 +00:00
LazyWebstudioDbContext = new Lazy<WebstudioDbContext>(() => dbContextManager.Value);
2020-02-17 08:58:14 +00:00
}
2020-05-07 11:02:29 +00:00
private int tenantID;
private int TenantID
{
2022-01-21 11:02:17 +00:00
get
{
if (tenantID == 0) tenantID = TenantManager.GetCurrentTenant().TenantId;
return tenantID;
}
2019-11-06 15:03:09 +00:00
}
//
2020-05-07 11:02:29 +00:00
private Guid? currentUserID;
private Guid CurrentUserID
{
2022-01-21 11:02:17 +00:00
get
{
currentUserID ??= AuthContext.CurrentAccount.ID;
return currentUserID.Value;
}
2020-02-17 08:58:14 +00:00
}
2019-11-06 15:03:09 +00:00
public bool SaveSettings<T>(T settings, int tenantId) where T : ISettings
{
return SaveSettingsFor(settings, tenantId, Guid.Empty);
}
public T LoadSettings<T>(int tenantId) where T : class, ISettings
{
return LoadSettingsFor<T>(tenantId, Guid.Empty);
}
public void ClearCache<T>(int tenantId) where T : class, ISettings
{
var settings = LoadSettings<T>(tenantId);
2020-02-17 08:58:14 +00:00
var key = settings.ID.ToString() + tenantId + Guid.Empty;
2019-11-06 15:03:09 +00:00
DbSettingsManagerCache.Remove(key);
}
public bool SaveSettingsFor<T>(T settings, int tenantId, Guid userId) where T : ISettings
{
2022-01-24 10:49:00 +00:00
if (settings == null) throw new ArgumentNullException(nameof(settings));
2019-11-06 15:03:09 +00:00
try
{
var key = settings.ID.ToString() + tenantId + userId;
var data = Serialize(settings);
2020-02-17 08:58:14 +00:00
var def = (T)settings.GetDefault(ServiceProvider);
2019-11-06 15:03:09 +00:00
var defaultData = Serialize(def);
2020-02-17 08:58:14 +00:00
2019-11-06 15:03:09 +00:00
if (data.SequenceEqual(defaultData))
2021-08-20 07:52:52 +00:00
{
using var tr = WebstudioDbContext.Database.BeginTransaction();
2020-02-17 08:58:14 +00:00
// remove default settings
var s = WebstudioDbContext.WebstudioSettings
.Where(r => r.Id == settings.ID)
.Where(r => r.TenantId == tenantId)
.Where(r => r.UserId == userId)
.FirstOrDefault();
if (s != null)
{
WebstudioDbContext.WebstudioSettings.Remove(s);
2021-08-20 07:52:52 +00:00
}
WebstudioDbContext.SaveChanges();
tr.Commit();
2019-11-06 15:03:09 +00:00
}
else
2020-02-17 08:58:14 +00:00
{
var s = new DbWebstudioSettings
{
Id = settings.ID,
UserId = userId,
TenantId = tenantId,
Data = data
};
2021-08-20 07:52:52 +00:00
WebstudioDbContext.AddOrUpdate(r => r.WebstudioSettings, s);
WebstudioDbContext.SaveChanges();
2020-02-17 08:58:14 +00:00
}
2019-12-04 09:04:32 +00:00
DbSettingsManagerCache.Remove(key);
2019-11-06 15:03:09 +00:00
Cache.Insert(key, settings, expirationTimeout);
return true;
}
catch (Exception ex)
{
2020-02-20 08:05:10 +00:00
Log.Error(ex);
2019-11-06 15:03:09 +00:00
return false;
}
}
internal T LoadSettingsFor<T>(int tenantId, Guid userId) where T : class, ISettings
{
2020-09-24 09:45:02 +00:00
var settingsInstance = ActivatorUtilities.CreateInstance<T>(ServiceProvider);
2019-11-06 15:03:09 +00:00
var key = settingsInstance.ID.ToString() + tenantId + userId;
2020-02-17 08:58:14 +00:00
var def = (T)settingsInstance.GetDefault(ServiceProvider);
2019-11-06 15:03:09 +00:00
try
{
var settings = Cache.Get<T>(key);
2020-02-17 08:58:14 +00:00
if (settings != null) return settings;
var result = WebstudioDbContext.WebstudioSettings
.Where(r => r.Id == settingsInstance.ID)
.Where(r => r.TenantId == tenantId)
.Where(r => r.UserId == userId)
.Select(r => r.Data)
.FirstOrDefault();
2019-12-04 09:04:32 +00:00
2019-12-05 08:51:28 +00:00
if (result != null)
2019-11-06 15:03:09 +00:00
{
2020-07-13 13:18:31 +00:00
settings = Deserialize<T>(result);
2019-11-06 15:03:09 +00:00
}
else
{
settings = def;
2020-02-17 08:58:14 +00:00
}
2019-11-06 15:03:09 +00:00
Cache.Insert(key, settings, expirationTimeout);
return settings;
}
catch (Exception ex)
{
2020-02-20 08:05:10 +00:00
Log.Error(ex);
2019-11-06 15:03:09 +00:00
}
return def;
}
2020-02-17 08:58:14 +00:00
public T Load<T>() where T : class, ISettings
{
return LoadSettings<T>(TenantID);
}
public T LoadForCurrentUser<T>() where T : class, ISettings
{
return LoadForUser<T>(CurrentUserID);
}
public T LoadForUser<T>(Guid userId) where T : class, ISettings
{
return LoadSettingsFor<T>(TenantID, userId);
}
public T LoadForDefaultTenant<T>() where T : class, ISettings
{
return LoadForTenant<T>(Tenant.DEFAULT_TENANT);
2019-11-06 15:03:09 +00:00
}
public T LoadForTenant<T>(int tenantId) where T : class, ISettings
{
return LoadSettings<T>(tenantId);
}
public virtual bool Save<T>(T data) where T : class, ISettings
{
return SaveSettings(data, TenantID);
}
public bool SaveForCurrentUser<T>(T data) where T : class, ISettings
{
2019-12-04 09:04:32 +00:00
return SaveForUser(data, CurrentUserID);
}
public bool SaveForUser<T>(T data, Guid userId) where T : class, ISettings
{
return SaveSettingsFor(data, TenantID, userId);
}
public bool SaveForDefaultTenant<T>(T data) where T : class, ISettings
{
2019-12-04 09:04:32 +00:00
return SaveForTenant(data, Tenant.DEFAULT_TENANT);
}
public bool SaveForTenant<T>(T data, int tenantId) where T : class, ISettings
{
2019-12-04 09:04:32 +00:00
return SaveSettings(data, tenantId);
}
public void ClearCache<T>() where T : class, ISettings
{
ClearCache<T>(TenantID);
2020-02-17 08:58:14 +00:00
}
2019-12-04 09:04:32 +00:00
private T Deserialize<T>(string data)
2020-07-08 10:10:09 +00:00
{
2022-02-04 17:59:58 +00:00
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
};
return JsonSerializer.Deserialize<T>(data, options);
2019-11-06 15:03:09 +00:00
}
2020-07-22 11:05:27 +00:00
private string Serialize<T>(T settings)
2020-07-08 10:10:09 +00:00
{
2020-07-08 15:30:23 +00:00
return JsonSerializer.Serialize(settings);
2019-11-06 15:03:09 +00:00
}
2020-02-17 08:58:14 +00:00
}
2019-05-15 14:56:09 +00:00
}