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-02-21 15:35:57 +00:00
|
|
|
|
|
|
|
using IsolationLevel = System.Data.IsolationLevel;
|
|
|
|
|
|
|
|
namespace ASC.MessagingSystem.Data;
|
|
|
|
|
|
|
|
[Singletone(Additional = typeof(MessagesRepositoryExtension))]
|
|
|
|
public class MessagesRepository : IDisposable
|
|
|
|
{
|
|
|
|
private DateTime _lastSave = DateTime.UtcNow;
|
2022-02-21 15:48:48 +00:00
|
|
|
private bool _timerStarted;
|
2022-02-21 15:35:57 +00:00
|
|
|
private readonly TimeSpan _cacheTime;
|
|
|
|
private readonly IDictionary<string, EventMessage> _cache;
|
2022-02-21 15:48:48 +00:00
|
|
|
private readonly IServiceScopeFactory _serviceScopeFactory;
|
2022-02-21 15:35:57 +00:00
|
|
|
private readonly IMapper _mapper;
|
2022-05-26 09:01:54 +00:00
|
|
|
private readonly ILogger<MessagesRepository> _logger;
|
2022-02-21 15:35:57 +00:00
|
|
|
private readonly Timer _timer;
|
2022-06-09 12:11:45 +00:00
|
|
|
private readonly int _cacheLimit;
|
2022-06-20 09:15:08 +00:00
|
|
|
private readonly HashSet<MessageAction> _forceSaveAuditActions = new HashSet<MessageAction>
|
|
|
|
{ MessageAction.RoomInviteLinkUsed, MessageAction.UserSentPasswordChangeInstructions };
|
2022-04-14 19:42:15 +00:00
|
|
|
|
2022-06-09 12:11:45 +00:00
|
|
|
public MessagesRepository(IServiceScopeFactory serviceScopeFactory, ILogger<MessagesRepository> logger, IMapper mapper, IConfiguration configuration)
|
2022-02-21 15:35:57 +00:00
|
|
|
{
|
|
|
|
_cacheTime = TimeSpan.FromMinutes(1);
|
|
|
|
_cache = new Dictionary<string, EventMessage>();
|
|
|
|
_timerStarted = false;
|
|
|
|
|
2022-03-17 10:13:52 +00:00
|
|
|
_logger = logger;
|
2022-02-21 15:35:57 +00:00
|
|
|
_serviceScopeFactory = serviceScopeFactory;
|
|
|
|
|
|
|
|
_timer = new Timer(FlushCache);
|
|
|
|
|
|
|
|
_mapper = mapper;
|
2022-06-09 12:11:45 +00:00
|
|
|
|
|
|
|
var minutes = configuration["messaging:CacheTimeFromMinutes"];
|
|
|
|
var limit = configuration["messaging:CacheLimit"];
|
|
|
|
|
|
|
|
_cacheTime = int.TryParse(minutes, out var cacheTime) ? TimeSpan.FromMinutes(cacheTime) : TimeSpan.FromMinutes(1);
|
|
|
|
_cacheLimit = int.TryParse(limit, out var cacheLimit) ? cacheLimit : 100;
|
2022-02-21 15:35:57 +00:00
|
|
|
}
|
|
|
|
|
2022-06-09 12:11:45 +00:00
|
|
|
~MessagesRepository()
|
2022-02-21 15:35:57 +00:00
|
|
|
{
|
2022-06-09 12:11:45 +00:00
|
|
|
FlushCache(true);
|
|
|
|
}
|
2022-06-20 09:15:08 +00:00
|
|
|
|
2022-06-09 12:11:45 +00:00
|
|
|
private bool ForseSave(EventMessage message)
|
|
|
|
{
|
2022-02-21 15:35:57 +00:00
|
|
|
// messages with action code < 2000 are related to login-history
|
2022-06-20 09:15:08 +00:00
|
|
|
if ((int)message.Action < 2000)
|
2022-02-21 15:35:57 +00:00
|
|
|
{
|
2022-06-09 12:11:45 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-06-20 09:15:08 +00:00
|
|
|
return _forceSaveAuditActions.Contains(message.Action);
|
2022-06-09 12:11:45 +00:00
|
|
|
}
|
2022-02-21 15:35:57 +00:00
|
|
|
|
2022-06-09 12:11:45 +00:00
|
|
|
public int Add(EventMessage message)
|
|
|
|
{
|
|
|
|
if (ForseSave(message))
|
|
|
|
{
|
|
|
|
var id = 0;
|
|
|
|
if (!string.IsNullOrEmpty(message.UAHeader))
|
2022-05-16 17:19:41 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2022-06-09 12:11:45 +00:00
|
|
|
MessageSettings.AddInfoMessage(message);
|
2022-05-16 17:19:41 +00:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2022-06-09 12:11:45 +00:00
|
|
|
_logger.ErrorWithException("Add " + message.Id, e);
|
2022-05-16 17:19:41 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-09 12:11:45 +00:00
|
|
|
|
2022-02-21 15:35:57 +00:00
|
|
|
using var scope = _serviceScopeFactory.CreateScope();
|
2022-06-20 09:15:08 +00:00
|
|
|
using var ef = scope.ServiceProvider.GetService<DbContextManager<MessagesContext>>().Get("messages");
|
|
|
|
|
2022-06-09 12:11:45 +00:00
|
|
|
if ((int)message.Action < 2000)
|
|
|
|
{
|
|
|
|
id = AddLoginEvent(message, ef);
|
|
|
|
}
|
2022-05-16 17:19:41 +00:00
|
|
|
else
|
|
|
|
{
|
2022-06-09 12:11:45 +00:00
|
|
|
id = AddAuditEvent(message, ef);
|
|
|
|
}
|
|
|
|
return id;
|
|
|
|
}
|
2022-02-21 15:35:57 +00:00
|
|
|
|
|
|
|
var now = DateTime.UtcNow;
|
|
|
|
var key = string.Format("{0}|{1}|{2}|{3}", message.TenantId, message.UserId, message.Id, now.Ticks);
|
|
|
|
|
|
|
|
lock (_cache)
|
|
|
|
{
|
|
|
|
_cache[key] = message;
|
|
|
|
|
|
|
|
if (!_timerStarted)
|
|
|
|
{
|
|
|
|
_timer.Change(0, 100);
|
|
|
|
_timerStarted = true;
|
|
|
|
}
|
|
|
|
}
|
2022-06-09 12:11:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
private void FlushCache(object state)
|
|
|
|
{
|
|
|
|
FlushCache(false);
|
2022-02-21 15:35:57 +00:00
|
|
|
}
|
|
|
|
|
2022-06-09 12:11:45 +00:00
|
|
|
private void FlushCache(bool isDisposed = false)
|
2022-02-21 15:35:57 +00:00
|
|
|
{
|
|
|
|
List<EventMessage> events = null;
|
|
|
|
|
2022-06-09 12:11:45 +00:00
|
|
|
if (DateTime.UtcNow > _lastSave.Add(_cacheTime) || _cache.Count > _cacheLimit || isDisposed)
|
2022-02-21 15:35:57 +00:00
|
|
|
{
|
|
|
|
lock (_cache)
|
|
|
|
{
|
|
|
|
_timer.Change(-1, -1);
|
|
|
|
_timerStarted = false;
|
|
|
|
|
|
|
|
events = new List<EventMessage>(_cache.Values);
|
|
|
|
_cache.Clear();
|
|
|
|
_lastSave = DateTime.UtcNow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (events == null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-20 09:15:08 +00:00
|
|
|
using var scope = _serviceScopeFactory.CreateScope();
|
|
|
|
using var ef = scope.ServiceProvider.GetService<DbContextManager<MessagesContext>>().Get("messages");
|
2022-05-25 13:55:38 +00:00
|
|
|
var strategy = ef.Database.CreateExecutionStrategy();
|
|
|
|
|
|
|
|
strategy.Execute(() =>
|
|
|
|
{
|
2022-02-21 15:35:57 +00:00
|
|
|
using var tx = ef.Database.BeginTransaction(IsolationLevel.ReadUncommitted);
|
|
|
|
var dict = new Dictionary<string, ClientInfo>();
|
2022-06-20 09:15:08 +00:00
|
|
|
|
2022-02-21 15:35:57 +00:00
|
|
|
foreach (var message in events)
|
|
|
|
{
|
|
|
|
if (!string.IsNullOrEmpty(message.UAHeader))
|
|
|
|
{
|
|
|
|
try
|
2022-06-09 12:11:45 +00:00
|
|
|
{
|
|
|
|
MessageSettings.AddInfoMessage(message, dict);
|
2022-02-21 15:35:57 +00:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2022-05-13 10:47:01 +00:00
|
|
|
_logger.ErrorFlushCache(message.Id, e);
|
2022-02-21 15:35:57 +00:00
|
|
|
}
|
2022-06-09 12:11:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!ForseSave(message))
|
|
|
|
{
|
2022-05-25 13:55:38 +00:00
|
|
|
// messages with action code < 2000 are related to login-history
|
2022-06-09 12:11:45 +00:00
|
|
|
if ((int)message.Action < 2000)
|
|
|
|
{
|
|
|
|
AddLoginEvent(message, ef);
|
|
|
|
}
|
|
|
|
else
|
2022-05-25 13:55:38 +00:00
|
|
|
{
|
|
|
|
AddAuditEvent(message, ef);
|
|
|
|
}
|
|
|
|
}
|
2022-02-21 15:35:57 +00:00
|
|
|
}
|
2022-06-20 09:15:08 +00:00
|
|
|
|
2022-02-21 15:35:57 +00:00
|
|
|
tx.Commit();
|
2022-06-01 11:44:30 +00:00
|
|
|
});
|
2022-02-21 15:35:57 +00:00
|
|
|
}
|
|
|
|
|
2022-06-09 12:11:45 +00:00
|
|
|
private int AddLoginEvent(EventMessage message, MessagesContext dbContext)
|
2022-02-21 15:35:57 +00:00
|
|
|
{
|
|
|
|
var loginEvent = _mapper.Map<EventMessage, LoginEvent>(message);
|
|
|
|
|
|
|
|
dbContext.LoginEvents.Add(loginEvent);
|
|
|
|
dbContext.SaveChanges();
|
2022-05-16 17:19:41 +00:00
|
|
|
|
2022-06-09 12:11:45 +00:00
|
|
|
return loginEvent.Id;
|
2022-02-21 15:35:57 +00:00
|
|
|
}
|
|
|
|
|
2022-06-09 12:11:45 +00:00
|
|
|
private int AddAuditEvent(EventMessage message, MessagesContext dbContext)
|
2022-02-21 15:35:57 +00:00
|
|
|
{
|
|
|
|
var auditEvent = _mapper.Map<EventMessage, AuditEvent>(message);
|
|
|
|
|
|
|
|
dbContext.AuditEvents.Add(auditEvent);
|
|
|
|
dbContext.SaveChanges();
|
|
|
|
|
2022-06-09 12:11:45 +00:00
|
|
|
return auditEvent.Id;
|
2022-02-21 15:35:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
if (_timer != null)
|
|
|
|
{
|
|
|
|
_timer.Dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class MessagesRepositoryExtension
|
|
|
|
{
|
|
|
|
public static void Register(DIHelper services)
|
|
|
|
{
|
|
|
|
services.TryAdd<DbContextManager<MessagesContext>>();
|
2022-03-18 10:31:02 +00:00
|
|
|
services.TryAdd<EventTypeConverter>();
|
2022-02-21 15:35:57 +00:00
|
|
|
}
|
|
|
|
}
|