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

322 lines
9.9 KiB
C#
Raw Normal View History

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
2023-02-24 14:19:10 +00:00
namespace ASC.Core.Common.Settings;
2022-02-15 11:52:43 +00:00
[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]
2023-02-24 14:19:10 +00:00
public class SettingsManager
2022-02-15 11:52:43 +00:00
{
2022-07-28 12:29:03 +00:00
private readonly TimeSpan _expirationTimeout = TimeSpan.FromMinutes(5);
2023-02-24 14:19:10 +00:00
private readonly ILogger<SettingsManager> _logger;
2022-07-28 12:29:03 +00:00
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
2023-02-24 14:19:10 +00:00
public SettingsManager(
2022-02-15 11:52:43 +00:00
IServiceProvider serviceProvider,
DbSettingsManagerCache dbSettingsManagerCache,
2023-02-24 14:19:10 +00:00
ILogger<SettingsManager> 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
2023-02-24 14:19:10 +00:00
public void ClearCache<T>() where T : class, ISettings<T>
2022-02-15 11:52:43 +00:00
{
2023-02-24 14:19:10 +00:00
ClearCache<T>(TenantID);
2022-02-15 11:52:43 +00:00
}
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
{
2023-02-24 14:19:10 +00:00
var settings = Load<T>(tenantId, Guid.Empty);
2022-02-15 11:52:43 +00:00
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
}
2023-02-24 14:19:10 +00:00
public T GetDefault<T>() where T : class, ISettings<T>
2022-02-15 11:52:43 +00:00
{
2023-02-24 14:19:10 +00:00
var settingsInstance = ActivatorUtilities.CreateInstance<T>(_serviceProvider);
return settingsInstance.GetDefault();
}
2023-02-24 14:19:10 +00:00
public T Load<T>() where T : class, ISettings<T>
{
return Load<T>(TenantID, Guid.Empty);
}
2023-02-24 14:19:10 +00:00
public T Load<T>(Guid userId) where T : class, ISettings<T>
{
return Load<T>(TenantID, userId);
}
2023-02-24 14:19:10 +00:00
public T Load<T>(UserInfo user) where T : class, ISettings<T>
{
return Load<T>(TenantID, user.Id);
}
2023-02-24 14:19:10 +00:00
public T Load<T>(int tenantId) where T : class, ISettings<T>
{
return Load<T>(tenantId, Guid.Empty);
}
2023-02-24 14:19:10 +00:00
public T LoadForDefaultTenant<T>() where T : class, ISettings<T>
{
return Load<T>(Tenant.DefaultTenant);
}
2023-02-24 14:19:10 +00:00
public T LoadForCurrentUser<T>() where T : class, ISettings<T>
{
return Load<T>(CurrentUserID);
}
2021-08-20 07:52:52 +00:00
2023-02-24 14:19:10 +00:00
public bool Save<T>(T data) where T : class, ISettings<T>
{
return Save(data, TenantID, Guid.Empty);
}
2023-02-24 14:19:10 +00:00
public bool Save<T>(T data, Guid userId) where T : class, ISettings<T>
{
return Save(data, TenantID, userId);
}
2022-02-15 11:52:43 +00:00
2023-02-24 14:19:10 +00:00
public bool Save<T>(T data, UserInfo user) where T : class, ISettings<T>
{
return Save(data, TenantID, user.Id);
}
2022-02-15 11:52:43 +00:00
2023-02-24 14:19:10 +00:00
public bool Save<T>(T data, int tenantId) where T : class, ISettings<T>
{
return Save(data, tenantId, Guid.Empty);
}
2022-02-15 11:52:43 +00:00
2023-02-24 14:19:10 +00:00
public bool SaveForDefaultTenant<T>(T data) where T : class, ISettings<T>
{
return Save(data, Tenant.DefaultTenant);
}
2022-02-15 11:52:43 +00:00
2023-02-24 14:19:10 +00:00
public bool SaveForCurrentUser<T>(T data) where T : class, ISettings<T>
{
return Save(data, CurrentUserID);
}
2022-02-15 11:52:43 +00:00
2023-02-24 14:19:10 +00:00
public bool Manage<T>(Action<T> action) where T : class, ISettings<T>
{
var settings = Load<T>();
action(settings);
return Save(settings);
2022-02-15 11:52:43 +00:00
}
2023-03-01 17:32:44 +00:00
public bool ManageForCurrentUser<T>(Action<T> action) where T : class, ISettings<T>
{
var settings = LoadForCurrentUser<T>();
action(settings);
return SaveForCurrentUser(settings);
}
2023-02-24 14:19:10 +00:00
internal T Load<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;
}
2023-02-24 14:19:10 +00:00
private bool Save<T>(T settings, int tenantId, Guid userId) where T : class, ISettings<T>
2022-03-24 09:35:00 +00:00
{
2023-02-24 14:19:10 +00:00
ArgumentNullException.ThrowIfNull(settings);
2022-03-24 09:35:00 +00:00
2023-02-24 14:19:10 +00:00
using var webstudioDbContext = _dbContextFactory.CreateDbContext();
2022-02-15 11:52:43 +00:00
2023-02-24 14:19:10 +00:00
try
{
var key = settings.ID.ToString() + tenantId + userId;
var data = Serialize(settings);
var def = GetDefault<T>();
2022-02-15 11:52:43 +00:00
2023-02-24 14:19:10 +00:00
var defaultData = Serialize(def);
2022-02-15 11:52:43 +00:00
2023-02-24 14:19:10 +00:00
if (data.SequenceEqual(defaultData))
{
var strategy = webstudioDbContext.Database.CreateExecutionStrategy();
2022-09-23 14:14:55 +00:00
strategy.Execute(async () =>
2023-02-24 14:19:10 +00:00
{
2023-03-09 17:18:18 +00:00
using var webstudioDbContext = _dbContextFactory.CreateDbContext();
using var tr = await webstudioDbContext.Database.BeginTransactionAsync();
2023-02-24 14:19:10 +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();
2022-02-15 11:52:43 +00:00
2023-02-24 14:19:10 +00:00
if (s != null)
{
webstudioDbContext.WebstudioSettings.Remove(s);
}
2022-02-15 11:52:43 +00:00
await webstudioDbContext.SaveChangesAsync();
2022-02-15 11:52:43 +00:00
await tr.CommitAsync();
2023-03-10 13:09:32 +00:00
}).GetAwaiter()
.GetResult();
2023-02-24 14:19:10 +00:00
}
else
{
var s = new DbWebstudioSettings
{
Id = settings.ID,
UserId = userId,
TenantId = tenantId,
Data = data
};
2022-02-15 11:52:43 +00:00
2023-02-24 14:19:10 +00:00
webstudioDbContext.AddOrUpdate(webstudioDbContext.WebstudioSettings, s);
2022-02-15 11:52:43 +00:00
2023-02-24 14:19:10 +00:00
webstudioDbContext.SaveChanges();
}
2022-09-23 14:14:55 +00:00
2023-02-24 14:19:10 +00:00
_dbSettingsManagerCache.Remove(key);
2022-02-15 11:52:43 +00:00
2023-02-24 14:19:10 +00:00
_cache.Insert(key, settings, _expirationTimeout);
2022-02-15 11:52:43 +00:00
2023-02-24 14:19:10 +00:00
return true;
}
catch (Exception ex)
{
_logger.ErrorSaveSettingsFor(ex);
return false;
}
2022-02-15 11:52:43 +00:00
}
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);
}
}