DocSpace-client/common/services/ASC.AuditTrail/LoginEventsRepository.cs

142 lines
5.8 KiB
C#
Raw Normal View History

2020-10-06 07:06:05 +00:00
/*
*
* (c) Copyright Ascensio System Limited 2010-2020
2020-10-06 12:12:57 +00:00
*
* 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 <EFBFBD> 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 <EFBFBD> 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
2020-10-06 07:06:05 +00:00
*
*/
2020-10-06 12:12:57 +00:00
2020-10-06 07:06:05 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
2020-10-14 13:27:12 +00:00
using ASC.AuditTrail.Mappers;
2020-10-06 07:06:05 +00:00
using ASC.Common;
2020-10-14 13:27:12 +00:00
using ASC.Core.Common.EF;
using ASC.Core.Common.EF.Context;
using ASC.Core.Common.EF.Model;
using ASC.Core.Users;
using Newtonsoft.Json;
2020-10-06 07:06:05 +00:00
namespace ASC.AuditTrail.Data
2020-10-19 15:53:15 +00:00
{
[Scope]
2020-10-06 07:06:05 +00:00
public class LoginEventsRepository
{
2021-07-15 10:07:47 +00:00
private UserFormatter UserFormatter { get; }
2020-10-06 13:03:51 +00:00
private AuditActionMapper AuditActionMapper { get; }
2021-08-03 14:11:53 +00:00
private MessagesContext MessagesContext { get => LazyMessagesContext.Value; }
private Lazy<MessagesContext> LazyMessagesContext { get; }
2020-10-06 07:06:05 +00:00
public LoginEventsRepository(UserFormatter userFormatter, AuditActionMapper auditActionMapper, DbContextManager<MessagesContext> dbMessagesContext)
2020-10-06 07:06:05 +00:00
{
UserFormatter = userFormatter;
2020-10-06 13:03:51 +00:00
AuditActionMapper = auditActionMapper;
2021-08-03 14:11:53 +00:00
LazyMessagesContext = new Lazy<MessagesContext>(() => dbMessagesContext.Value);
2020-10-06 07:06:05 +00:00
}
2022-01-24 09:57:01 +00:00
private sealed class Query
2020-10-06 07:06:05 +00:00
{
public LoginEvents LoginEvents { get; set; }
public User User { get; set; }
}
public IEnumerable<LoginEvent> GetLast(int tenant, int chunk)
2020-10-14 13:27:12 +00:00
{
var query =
(from b in MessagesContext.LoginEvents
from p in MessagesContext.Users.AsQueryable().Where(p => b.UserId == p.Id).DefaultIfEmpty()
2020-10-14 13:27:12 +00:00
where b.TenantId == tenant
orderby b.Date descending
select new Query { LoginEvents = b, User = p })
.Take(chunk);
return query.AsEnumerable().Select(ToLoginEvent).ToList();
2020-10-06 07:06:05 +00:00
}
2020-10-14 13:27:12 +00:00
public IEnumerable<LoginEvent> Get(int tenant, DateTime fromDate, DateTime to)
2020-10-06 07:06:05 +00:00
{
2020-10-14 13:27:12 +00:00
var query =
from q in MessagesContext.LoginEvents
from p in MessagesContext.Users.AsQueryable().Where(p => q.UserId == p.Id).DefaultIfEmpty()
2020-10-14 13:27:12 +00:00
where q.TenantId == tenant
where q.Date >= fromDate
where q.Date <= to
orderby q.Date descending
select new Query { LoginEvents = q, User = p };
return query.AsEnumerable().Select(ToLoginEvent).ToList();
2020-10-06 07:06:05 +00:00
}
public int GetCount(int tenant, DateTime? from = null, DateTime? to = null)
{
2021-08-03 14:11:53 +00:00
var query = MessagesContext.LoginEvents
2021-11-02 12:12:28 +00:00
.AsQueryable()
2020-10-06 07:06:05 +00:00
.Where(l => l.TenantId == tenant);
if (from.HasValue && to.HasValue)
2020-10-14 13:27:12 +00:00
{
2022-01-19 13:36:03 +00:00
query = query.Where(l => l.Date >= from && l.Date <= to);
2020-10-06 07:06:05 +00:00
}
return query.Count();
}
2020-10-14 13:27:12 +00:00
private LoginEvent ToLoginEvent(Query query)
2020-10-06 07:06:05 +00:00
{
2020-10-14 13:27:12 +00:00
var evt = new LoginEvent
{
Id = query.LoginEvents.Id,
IP = query.LoginEvents.Ip,
Login = query.LoginEvents.Login,
Browser = query.LoginEvents.Browser,
Platform = query.LoginEvents.Platform,
Date = query.LoginEvents.Date,
TenantId = query.LoginEvents.TenantId,
UserId = query.LoginEvents.UserId,
Page = query.LoginEvents.Page,
Action = query.LoginEvents.Action
};
if (query.LoginEvents.Description != null)
2020-10-06 07:06:05 +00:00
{
2020-10-14 13:27:12 +00:00
evt.Description = JsonConvert.DeserializeObject<IList<string>>(
query.LoginEvents.Description,
new JsonSerializerSettings
{
DateTimeZoneHandling = DateTimeZoneHandling.Utc
});
}
2020-10-15 08:04:06 +00:00
evt.UserName = (!string.IsNullOrEmpty(query.User?.FirstName) && !string.IsNullOrEmpty(query.User?.LastName))
2020-10-14 13:27:12 +00:00
? UserFormatter.GetUserName(query.User.FirstName, query.User.LastName)
: !string.IsNullOrWhiteSpace(evt.Login)
? evt.Login
: evt.UserId == Core.Configuration.Constants.Guest.ID
? AuditReportResource.GuestAccount
: AuditReportResource.UnknownAccount;
evt.ActionText = AuditActionMapper.GetActionText(evt);
return evt;
2020-10-06 07:06:05 +00:00
}
}
}