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-01-26 10:43:08 +00:00
|
|
|
using JsonSerializer = System.Text.Json.JsonSerializer;
|
2019-10-22 11:21:44 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
namespace ASC.Core.Data;
|
|
|
|
|
|
|
|
[Singletone]
|
|
|
|
public class DbSettingsManagerCache
|
2020-10-19 15:53:15 +00:00
|
|
|
{
|
2022-02-15 11:52:43 +00:00
|
|
|
public ICache Cache { get; }
|
|
|
|
private readonly ICacheNotify<SettingsCacheItem> _notify;
|
|
|
|
|
|
|
|
public DbSettingsManagerCache(ICacheNotify<SettingsCacheItem> notify, ICache cache)
|
2020-02-20 08:05:10 +00:00
|
|
|
{
|
2022-02-15 11:52:43 +00:00
|
|
|
Cache = cache;
|
|
|
|
_notify = notify;
|
|
|
|
_notify.Subscribe((i) => Cache.Remove(i.Key), CacheNotifyAction.Remove);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Remove(string key)
|
|
|
|
{
|
|
|
|
_notify.Publish(new SettingsCacheItem { Key = key }, CacheNotifyAction.Remove);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Scope]
|
2022-07-28 12:29:03 +00:00
|
|
|
public class DbSettingsManager
|
2022-02-15 11:52:43 +00:00
|
|
|
{
|
2022-07-28 12:29:03 +00:00
|
|
|
private readonly TimeSpan _expirationTimeout = TimeSpan.FromMinutes(5);
|
|
|
|
|
|
|
|
private readonly ILogger<DbSettingsManager> _logger;
|
|
|
|
private readonly ICache _cache;
|
2022-02-15 11:52:43 +00:00
|
|
|
private readonly IServiceProvider _serviceProvider;
|
|
|
|
private readonly DbSettingsManagerCache _dbSettingsManagerCache;
|
|
|
|
private readonly AuthContext _authContext;
|
2022-07-28 12:29:03 +00:00
|
|
|
private readonly TenantManager _tenantManager;
|
|
|
|
private readonly IDbContextFactory<WebstudioDbContext> _dbContextFactory;
|
2022-02-15 11:52:43 +00:00
|
|
|
|
2022-07-28 12:29:03 +00:00
|
|
|
public DbSettingsManager(
|
2022-02-15 11:52:43 +00:00
|
|
|
IServiceProvider serviceProvider,
|
|
|
|
DbSettingsManagerCache dbSettingsManagerCache,
|
2022-07-28 12:29:03 +00:00
|
|
|
ILogger<DbSettingsManager> logger,
|
2022-02-15 11:52:43 +00:00
|
|
|
AuthContext authContext,
|
2022-07-28 12:29:03 +00:00
|
|
|
TenantManager tenantManager,
|
|
|
|
IDbContextFactory<WebstudioDbContext> dbContextFactory)
|
2022-02-15 11:52:43 +00:00
|
|
|
{
|
|
|
|
_serviceProvider = serviceProvider;
|
|
|
|
_dbSettingsManagerCache = dbSettingsManagerCache;
|
|
|
|
_authContext = authContext;
|
|
|
|
_tenantManager = tenantManager;
|
2022-07-28 12:29:03 +00:00
|
|
|
_dbContextFactory = dbContextFactory;
|
|
|
|
_cache = dbSettingsManagerCache.Cache;
|
|
|
|
_logger = logger;
|
2022-02-15 11:52:43 +00:00
|
|
|
}
|
2020-02-20 08:05:10 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
private int _tenantID;
|
|
|
|
private int TenantID
|
|
|
|
{
|
|
|
|
get
|
2020-02-20 08:05:10 +00:00
|
|
|
{
|
2022-03-17 15:01:39 +00:00
|
|
|
if (_tenantID == 0)
|
|
|
|
{
|
2022-07-28 12:29:03 +00:00
|
|
|
_tenantID = _tenantManager.GetCurrentTenant().Id;
|
2022-03-17 15:01:39 +00:00
|
|
|
}
|
2020-02-20 08:05:10 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
return _tenantID;
|
2020-02-20 08:05:10 +00:00
|
|
|
}
|
2022-02-15 11:52:43 +00:00
|
|
|
}
|
|
|
|
//
|
|
|
|
private Guid? _currentUserID;
|
|
|
|
private Guid CurrentUserID
|
|
|
|
{
|
|
|
|
get
|
2020-02-20 08:05:10 +00:00
|
|
|
{
|
2022-07-28 12:29:03 +00:00
|
|
|
_currentUserID ??= _authContext.CurrentAccount.ID;
|
2020-02-20 08:05:10 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
return _currentUserID.Value;
|
2020-02-20 08:05:10 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-19 15:53:15 +00:00
|
|
|
|
2022-03-24 09:35:00 +00:00
|
|
|
public bool SaveSettings<T>(T settings, int tenantId) where T : class, ISettings<T>
|
2022-02-15 11:52:43 +00:00
|
|
|
{
|
|
|
|
return SaveSettingsFor(settings, tenantId, Guid.Empty);
|
|
|
|
}
|
2022-02-14 21:02:57 +00:00
|
|
|
|
2022-03-24 09:35:00 +00:00
|
|
|
public T LoadSettings<T>(int tenantId) where T : class, ISettings<T>
|
2022-02-15 11:52:43 +00:00
|
|
|
{
|
|
|
|
return LoadSettingsFor<T>(tenantId, Guid.Empty);
|
|
|
|
}
|
|
|
|
|
2022-03-24 09:35:00 +00:00
|
|
|
public void ClearCache<T>(int tenantId) where T : class, ISettings<T>
|
2022-02-15 11:52:43 +00:00
|
|
|
{
|
|
|
|
var settings = LoadSettings<T>(tenantId);
|
|
|
|
var key = settings.ID.ToString() + tenantId + Guid.Empty;
|
|
|
|
|
2022-07-28 12:29:03 +00:00
|
|
|
_dbSettingsManagerCache.Remove(key);
|
2022-02-15 11:52:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-24 09:35:00 +00:00
|
|
|
public bool SaveSettingsFor<T>(T settings, int tenantId, Guid userId) where T : class, ISettings<T>
|
2022-02-15 11:52:43 +00:00
|
|
|
{
|
2022-03-09 17:15:51 +00:00
|
|
|
ArgumentNullException.ThrowIfNull(settings);
|
2022-02-15 11:52:43 +00:00
|
|
|
|
2022-07-28 12:29:03 +00:00
|
|
|
using var webstudioDbContext = _dbContextFactory.CreateDbContext();
|
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
var key = settings.ID.ToString() + tenantId + userId;
|
|
|
|
var data = Serialize(settings);
|
2022-03-24 09:35:00 +00:00
|
|
|
var def = GetDefault<T>();
|
2022-02-15 11:52:43 +00:00
|
|
|
|
|
|
|
var defaultData = Serialize(def);
|
|
|
|
|
|
|
|
if (data.SequenceEqual(defaultData))
|
2022-02-14 21:02:57 +00:00
|
|
|
{
|
2022-07-28 12:29:03 +00:00
|
|
|
var strategy = webstudioDbContext.Database.CreateExecutionStrategy();
|
2022-05-25 13:55:38 +00:00
|
|
|
|
|
|
|
strategy.Execute(() =>
|
2021-08-20 07:52:52 +00:00
|
|
|
{
|
2022-07-28 12:29:03 +00:00
|
|
|
using var tr = webstudioDbContext.Database.BeginTransaction();
|
2022-05-25 13:55:38 +00:00
|
|
|
// remove default settings
|
2022-07-28 12:29:03 +00:00
|
|
|
var s = webstudioDbContext.WebstudioSettings
|
2022-05-25 13:55:38 +00:00
|
|
|
.Where(r => r.Id == settings.ID)
|
|
|
|
.Where(r => r.TenantId == tenantId)
|
|
|
|
.Where(r => r.UserId == userId)
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
|
|
|
if (s != null)
|
|
|
|
{
|
2022-07-28 12:29:03 +00:00
|
|
|
webstudioDbContext.WebstudioSettings.Remove(s);
|
2022-05-25 13:55:38 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 12:29:03 +00:00
|
|
|
webstudioDbContext.SaveChanges();
|
2022-05-25 13:55:38 +00:00
|
|
|
|
|
|
|
tr.Commit();
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2021-08-20 07:52:52 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var s = new DbWebstudioSettings
|
2022-02-14 21:02:57 +00:00
|
|
|
{
|
2022-02-15 11:52:43 +00:00
|
|
|
Id = settings.ID,
|
|
|
|
UserId = userId,
|
|
|
|
TenantId = tenantId,
|
|
|
|
Data = data
|
|
|
|
};
|
2022-02-14 21:02:57 +00:00
|
|
|
|
2022-07-28 12:29:03 +00:00
|
|
|
webstudioDbContext.AddOrUpdate(r => r.WebstudioSettings, s);
|
2022-02-15 11:52:43 +00:00
|
|
|
|
2022-07-28 12:29:03 +00:00
|
|
|
webstudioDbContext.SaveChanges();
|
2022-02-14 21:02:57 +00:00
|
|
|
}
|
2022-02-15 11:52:43 +00:00
|
|
|
|
2022-07-28 12:29:03 +00:00
|
|
|
_dbSettingsManagerCache.Remove(key);
|
2022-02-15 11:52:43 +00:00
|
|
|
|
2022-07-28 12:29:03 +00:00
|
|
|
_cache.Insert(key, settings, _expirationTimeout);
|
2022-02-15 11:52:43 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2022-07-28 12:29:03 +00:00
|
|
|
_logger.ErrorSaveSettingsFor(ex);
|
2022-02-15 11:52:43 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 09:35:00 +00:00
|
|
|
internal T LoadSettingsFor<T>(int tenantId, Guid userId) where T : class, ISettings<T>
|
2022-02-15 11:52:43 +00:00
|
|
|
{
|
2022-03-24 09:35:00 +00:00
|
|
|
var def = GetDefault<T>();
|
|
|
|
var key = def.ID.ToString() + tenantId + userId;
|
2022-02-15 11:52:43 +00:00
|
|
|
|
|
|
|
try
|
2020-07-08 10:10:09 +00:00
|
|
|
{
|
2022-07-28 12:29:03 +00:00
|
|
|
var settings = _cache.Get<T>(key);
|
2022-02-15 11:52:43 +00:00
|
|
|
if (settings != null)
|
2022-02-04 17:59:58 +00:00
|
|
|
{
|
2022-02-15 11:52:43 +00:00
|
|
|
return settings;
|
|
|
|
}
|
2022-02-04 17:59:58 +00:00
|
|
|
|
2022-07-28 12:29:03 +00:00
|
|
|
using var webstudioDbContext = _dbContextFactory.CreateDbContext();
|
|
|
|
var result = webstudioDbContext.WebstudioSettings
|
2022-03-24 09:35:00 +00:00
|
|
|
.Where(r => r.Id == def.ID)
|
2022-02-15 11:52:43 +00:00
|
|
|
.Where(r => r.TenantId == tenantId)
|
|
|
|
.Where(r => r.UserId == userId)
|
|
|
|
.Select(r => r.Data)
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
|
|
|
if (result != null)
|
|
|
|
{
|
|
|
|
settings = Deserialize<T>(result);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
settings = def;
|
|
|
|
}
|
|
|
|
|
2022-07-28 12:29:03 +00:00
|
|
|
_cache.Insert(key, settings, _expirationTimeout);
|
2022-02-15 11:52:43 +00:00
|
|
|
|
|
|
|
return settings;
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2022-07-28 12:29:03 +00:00
|
|
|
_logger.ErrorLoadSettingsFor(ex);
|
2022-02-15 11:52:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
|
2022-03-24 09:35:00 +00:00
|
|
|
public T GetDefault<T>() where T : class, ISettings<T>
|
|
|
|
{
|
2022-07-28 12:29:03 +00:00
|
|
|
var settingsInstance = ActivatorUtilities.CreateInstance<T>(_serviceProvider);
|
2022-03-24 09:35:00 +00:00
|
|
|
return settingsInstance.GetDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
public T Load<T>() where T : class, ISettings<T>
|
2022-02-15 11:52:43 +00:00
|
|
|
{
|
|
|
|
return LoadSettings<T>(TenantID);
|
|
|
|
}
|
|
|
|
|
2022-03-24 09:35:00 +00:00
|
|
|
public T LoadForCurrentUser<T>() where T : class, ISettings<T>
|
2022-02-15 11:52:43 +00:00
|
|
|
{
|
|
|
|
return LoadForUser<T>(CurrentUserID);
|
|
|
|
}
|
|
|
|
|
2022-03-24 09:35:00 +00:00
|
|
|
public T LoadForUser<T>(Guid userId) where T : class, ISettings<T>
|
2022-02-15 11:52:43 +00:00
|
|
|
{
|
|
|
|
return LoadSettingsFor<T>(TenantID, userId);
|
|
|
|
}
|
|
|
|
|
2022-03-24 09:35:00 +00:00
|
|
|
public T LoadForDefaultTenant<T>() where T : class, ISettings<T>
|
2022-02-15 11:52:43 +00:00
|
|
|
{
|
|
|
|
return LoadForTenant<T>(Tenant.DefaultTenant);
|
|
|
|
}
|
|
|
|
|
2022-03-24 09:35:00 +00:00
|
|
|
public T LoadForTenant<T>(int tenantId) where T : class, ISettings<T>
|
2022-02-15 11:52:43 +00:00
|
|
|
{
|
|
|
|
return LoadSettings<T>(tenantId);
|
|
|
|
}
|
|
|
|
|
2022-03-24 09:35:00 +00:00
|
|
|
public virtual bool Save<T>(T data) where T : class, ISettings<T>
|
2022-02-15 11:52:43 +00:00
|
|
|
{
|
|
|
|
return SaveSettings(data, TenantID);
|
|
|
|
}
|
|
|
|
|
2022-03-24 09:35:00 +00:00
|
|
|
public bool SaveForCurrentUser<T>(T data) where T : class, ISettings<T>
|
2022-02-15 11:52:43 +00:00
|
|
|
{
|
|
|
|
return SaveForUser(data, CurrentUserID);
|
|
|
|
}
|
|
|
|
|
2022-03-24 09:35:00 +00:00
|
|
|
public bool SaveForUser<T>(T data, Guid userId) where T : class, ISettings<T>
|
2022-02-15 11:52:43 +00:00
|
|
|
{
|
|
|
|
return SaveSettingsFor(data, TenantID, userId);
|
|
|
|
}
|
|
|
|
|
2022-03-24 09:35:00 +00:00
|
|
|
public bool SaveForDefaultTenant<T>(T data) where T : class, ISettings<T>
|
2022-02-15 11:52:43 +00:00
|
|
|
{
|
|
|
|
return SaveForTenant(data, Tenant.DefaultTenant);
|
|
|
|
}
|
|
|
|
|
2022-03-24 09:35:00 +00:00
|
|
|
public bool SaveForTenant<T>(T data, int tenantId) where T : class, ISettings<T>
|
2022-02-15 11:52:43 +00:00
|
|
|
{
|
|
|
|
return SaveSettings(data, tenantId);
|
|
|
|
}
|
|
|
|
|
2022-03-24 09:35:00 +00:00
|
|
|
public void ClearCache<T>() where T : class, ISettings<T>
|
2022-02-15 11:52:43 +00:00
|
|
|
{
|
|
|
|
ClearCache<T>(TenantID);
|
|
|
|
}
|
|
|
|
|
|
|
|
private T Deserialize<T>(string data)
|
|
|
|
{
|
|
|
|
var options = new JsonSerializerOptions
|
2020-07-08 10:10:09 +00:00
|
|
|
{
|
2022-03-24 09:35:00 +00:00
|
|
|
PropertyNameCaseInsensitive = true,
|
2022-02-15 11:52:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return JsonSerializer.Deserialize<T>(data, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
private string Serialize<T>(T settings)
|
|
|
|
{
|
|
|
|
return JsonSerializer.Serialize(settings);
|
|
|
|
}
|
|
|
|
}
|