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

318 lines
11 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.Configuration;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
2019-12-10 15:31:37 +00:00
2019-05-15 14:56:09 +00:00
using ASC.Common.Caching;
using ASC.Common.Logging;
2019-12-04 09:04:32 +00:00
using ASC.Core.Common.EF;
using ASC.Core.Common.EF.Context;
using ASC.Core.Common.EF.Model;
using ASC.Core.Common.Settings;
using ASC.Core.Tenants;
2019-12-10 15:31:37 +00:00
using Microsoft.Extensions.DependencyInjection;
2019-10-31 11:28:30 +00:00
using Microsoft.Extensions.DependencyInjection.Extensions;
2019-10-22 11:21:44 +00:00
using Microsoft.Extensions.Options;
2019-05-15 14:56:09 +00:00
namespace ASC.Core.Data
2019-10-11 15:03:03 +00:00
{
public class DbSettingsManagerCache
{
public ICache Cache { get; }
public ICacheNotify<SettingsCacheItem> Notify { get; }
2019-11-06 15:03:09 +00:00
public DbSettingsManagerCache(ICacheNotify<SettingsCacheItem> notify)
2019-10-11 15:03:03 +00:00
{
Cache = AscCache.Memory;
2019-11-06 15:03:09 +00:00
Notify = notify;
2019-10-11 15:03:03 +00:00
Notify.Subscribe((i) => Cache.Remove(i.Key), CacheNotifyAction.Remove);
}
public void Remove(string key)
{
Notify.Publish(new SettingsCacheItem { Key = key }, CacheNotifyAction.Remove);
}
}
2019-11-06 15:03:09 +00:00
public class DbSettingsManager
{
private readonly ILog log;
private readonly TimeSpan expirationTimeout = TimeSpan.FromMinutes(5);
private readonly IDictionary<Type, DataContractJsonSerializer> jsonSerializers = new Dictionary<Type, DataContractJsonSerializer>();
private readonly string dbId;
2019-10-31 11:28:30 +00:00
private ICache Cache { get; }
private IServiceProvider ServiceProvider { get; }
private DbSettingsManagerCache DbSettingsManagerCache { get; }
public AuthContext AuthContext { get; }
public TenantManager TenantManager { get; }
2019-12-04 09:04:32 +00:00
private WebstudioDbContext WebstudioDbContext { get; }
2019-10-18 13:29:24 +00:00
public DbSettingsManager(
2019-11-06 15:03:09 +00:00
IServiceProvider serviceProvider,
2019-10-18 13:29:24 +00:00
DbSettingsManagerCache dbSettingsManagerCache,
IOptionsMonitor<ILog> option,
2019-12-04 09:04:32 +00:00
AuthContext authContext,
TenantManager tenantManager,
DbContextManager<WebstudioDbContext> dbContextManager) : this(null)
{
2019-11-06 15:03:09 +00:00
ServiceProvider = serviceProvider;
2019-10-11 15:03:03 +00:00
DbSettingsManagerCache = dbSettingsManagerCache;
AuthContext = authContext;
TenantManager = tenantManager;
2019-10-11 15:03:03 +00:00
Cache = dbSettingsManagerCache.Cache;
2019-11-06 15:03:09 +00:00
log = option.CurrentValue;
2019-12-04 09:04:32 +00:00
WebstudioDbContext = dbContextManager.Value;
2019-11-06 08:55:59 +00:00
}
2019-12-17 08:27:38 +00:00
//TODO: remove
2019-11-06 15:03:09 +00:00
public DbSettingsManager(ConnectionStringSettings connectionString)
{
dbId = connectionString != null ? connectionString.Name : "default";
}
private int TenantID
{
get { return TenantManager.GetCurrentTenant().TenantId; }
2019-11-06 15:03:09 +00:00
}
//
private Guid CurrentUserID
{
get { return AuthContext.CurrentAccount.ID; }
}
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);
2019-10-11 15:03:03 +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
{
if (settings == null) throw new ArgumentNullException("settings");
try
{
var key = settings.ID.ToString() + tenantId + userId;
var data = Serialize(settings);
2019-12-04 09:04:32 +00:00
2019-11-08 15:11:30 +00:00
var def = (T)settings.GetDefault(ServiceProvider);
2019-11-06 15:03:09 +00:00
var defaultData = Serialize(def);
2019-12-04 09:04:32 +00:00
var tr = WebstudioDbContext.Database.BeginTransaction();
2019-11-06 15:03:09 +00:00
if (data.SequenceEqual(defaultData))
2019-12-04 09:04:32 +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();
2020-01-20 15:09:52 +00:00
if (s != null)
{
WebstudioDbContext.WebstudioSettings.Remove(s);
}
2019-11-06 15:03:09 +00:00
}
else
2019-12-04 09:04:32 +00:00
{
var s = new DbWebstudioSettings
{
Id = settings.ID,
UserId = userId,
TenantId = tenantId,
2019-12-10 15:31:37 +00:00
Data = data
2019-12-04 09:04:32 +00:00
};
2019-12-17 13:01:59 +00:00
WebstudioDbContext.AddOrUpdate(r => r.WebstudioSettings, s);
2019-10-18 13:29:24 +00:00
}
2019-10-11 15:03:03 +00:00
2019-12-04 09:04:32 +00:00
WebstudioDbContext.SaveChanges();
tr.Commit();
DbSettingsManagerCache.Remove(key);
2019-11-06 15:03:09 +00:00
Cache.Insert(key, settings, expirationTimeout);
return true;
}
catch (Exception ex)
{
log.Error(ex);
return false;
}
}
internal T LoadSettingsFor<T>(int tenantId, Guid userId) where T : class, ISettings
{
var settingsInstance = Activator.CreateInstance<T>();
2019-11-06 15:03:09 +00:00
var key = settingsInstance.ID.ToString() + tenantId + userId;
2019-11-08 15:11:30 +00:00
var def = (T)settingsInstance.GetDefault(ServiceProvider);
2019-11-06 15:03:09 +00:00
try
{
var settings = Cache.Get<T>(key);
if (settings != null) return settings;
2019-12-04 09:04:32 +00:00
var result = WebstudioDbContext.WebstudioSettings
.Where(r => r.Id == settingsInstance.ID)
.Where(r => r.TenantId == tenantId)
.Where(r => r.UserId == userId)
2019-12-10 15:31:37 +00:00
.Select(r => r.Data)
2019-12-04 09:04:32 +00:00
.FirstOrDefault();
2019-12-05 08:51:28 +00:00
if (result != null)
2019-11-06 15:03:09 +00:00
{
2019-12-05 08:51:28 +00:00
settings = Deserialize<T>(result.ToString());
2019-11-06 15:03:09 +00:00
}
else
{
settings = def;
}
2019-11-06 15:03:09 +00:00
Cache.Insert(key, settings, expirationTimeout);
return settings;
}
catch (Exception ex)
{
log.Error(ex);
}
return def;
}
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);
}
2019-12-04 09:04:32 +00:00
private T Deserialize<T>(string data)
2019-11-06 15:03:09 +00:00
{
2019-12-04 09:04:32 +00:00
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(data));
var settings = GetJsonSerializer(typeof(T)).ReadObject(stream);
2019-11-06 15:03:09 +00:00
return (T)settings;
}
2019-12-04 09:04:32 +00:00
private string Serialize(ISettings settings)
2019-11-06 15:03:09 +00:00
{
2019-08-15 15:08:40 +00:00
using var stream = new MemoryStream();
GetJsonSerializer(settings.GetType()).WriteObject(stream, settings);
2019-12-04 09:04:32 +00:00
return Encoding.UTF8.GetString(stream.ToArray());
2019-11-06 15:03:09 +00:00
}
private DataContractJsonSerializer GetJsonSerializer(Type type)
{
lock (jsonSerializers)
{
if (!jsonSerializers.ContainsKey(type))
{
jsonSerializers[type] = new DataContractJsonSerializer(type);
}
return jsonSerializers[type];
}
}
2019-10-31 11:28:30 +00:00
}
public static class DbSettingsManagerExtension
2019-11-06 15:03:09 +00:00
{
public static IServiceCollection AddDbSettingsManagerService(this IServiceCollection services)
2019-10-31 11:28:30 +00:00
{
services.TryAddSingleton<DbSettingsManagerCache>();
services.TryAddScoped<DbSettingsManager>();
2019-11-06 15:03:09 +00:00
2019-12-23 14:57:21 +00:00
return services.AddWebstudioDbContextService();
2019-11-06 15:03:09 +00:00
}
}
2019-05-15 14:56:09 +00:00
}