AuditTrail: project reconstruction

This commit is contained in:
Maksim Chegulov 2022-02-01 12:11:19 +03:00
parent 724065389f
commit 200b8a0212
11 changed files with 264 additions and 219 deletions

View File

@ -0,0 +1,17 @@
using System;
namespace ASC.AuditTrail.Attributes
{
[AttributeUsage(AttributeTargets.Property)]
public class EventAttribute : Attribute
{
public string Resource { get; private set; }
public int Order { get; private set; }
public EventAttribute(string resource, int order = 0)
{
Resource = resource;
Order = order;
}
}
}

View File

@ -26,8 +26,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq;
using ASC.AuditTrail.Models;
using ASC.Common;
using ASC.Common.Logging;
using ASC.MessagingSystem;

View File

@ -25,8 +25,9 @@
using ASC.MessagingSystem;
using ASC.AuditTrail.Attributes;
namespace ASC.AuditTrail
namespace ASC.AuditTrail.Models
{
public class AuditEvent : BaseEvent
{

View File

@ -0,0 +1,10 @@
using ASC.Core.Common.EF;
namespace ASC.AuditTrail.Models
{
public class AuditEventQuery
{
public Core.Common.EF.Model.AuditEvent AuditEvent { get; set; }
public User User { get; set; }
}
}

View File

@ -29,11 +29,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection;
using ASC.AuditTrail.Attributes;
using CsvHelper.Configuration;
namespace ASC.AuditTrail
namespace ASC.AuditTrail.Models
{
public class BaseEvent
{
@ -82,18 +84,4 @@ namespace ASC.AuditTrail
}
}
}
[AttributeUsage(AttributeTargets.Property)]
internal class EventAttribute : Attribute
{
public string Resource { get; private set; }
public int Order { get; private set; }
public EventAttribute(string resource, int order = 0)
{
Resource = resource;
Order = order;
}
}
}

View File

@ -26,7 +26,7 @@
namespace ASC.AuditTrail
namespace ASC.AuditTrail.Models
{
public class LoginEvent : BaseEvent
{

View File

@ -0,0 +1,11 @@
using ASC.Core.Common.EF;
using ASC.Core.Common.EF.Model;
namespace ASC.AuditTrail.Models
{
public class LoginEventQuery
{
public LoginEvents LoginEvents { get; set; }
public User User { get; set; }
}
}

View File

@ -0,0 +1,7 @@
namespace ASC.AuditTrail.Models.Profiles
{
public class AuditEventProfile
{
}
}

View File

@ -0,0 +1,14 @@
using ASC.AuditTrail.Data;
using AutoMapper;
namespace ASC.AuditTrail.Models.Profiles
{
public class LoginEventProfile : Profile
{
public LoginEventProfile()
{
CreateMap<LoginEvent, LoginEventQuery>();
}
}
}

View File

@ -1,147 +1,142 @@
/*
*
* (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 <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.
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
using ASC.AuditTrail.Mappers;
using ASC.Common;
using ASC.Core.Common.EF;
using ASC.Core.Common.EF.Context;
using ASC.Core.Users;
using ASC.MessagingSystem;
using Newtonsoft.Json;
namespace ASC.AuditTrail
{
[Scope]
public class AuditEventsRepository
{
private AuditTrailContext AuditTrailContext => _lazyAuditTrailContext.Value;
private readonly MessageTarget _messageTarget;
private readonly UserFormatter _userFormatter;
private readonly Lazy<AuditTrailContext> _lazyAuditTrailContext;
private readonly AuditActionMapper _auditActionMapper;
public AuditEventsRepository(
MessageTarget messageTarget,
UserFormatter userFormatter,
DbContextManager<AuditTrailContext> dbContextManager,
AuditActionMapper auditActionMapper)
{
_messageTarget = messageTarget;
_userFormatter = userFormatter;
_lazyAuditTrailContext = new Lazy<AuditTrailContext>(() => dbContextManager.Value );
_auditActionMapper = auditActionMapper;
}
public IEnumerable<AuditEvent> GetLast(int tenant, int chunk) => Get(tenant, null, null, chunk);
public IEnumerable<AuditEvent> Get(int tenant, DateTime from, DateTime to) => Get(tenant, from, to, null);
private IEnumerable<AuditEvent> Get(int tenant, DateTime? fromDate, DateTime? to, int? limit)
{
var query =
from q in AuditTrailContext.AuditEvents
from p in AuditTrailContext.Users.Where(p => q.UserId == p.Id).DefaultIfEmpty()
where q.TenantId == tenant
orderby q.Date descending
select new Query { AuditEvent = q, User = p };
if (fromDate.HasValue && to.HasValue)
query = query.Where(q => q.AuditEvent.Date >= fromDate & q.AuditEvent.Date <= to);
if (limit.HasValue) query = query.Take((int)limit);
return query.AsEnumerable().Select(ToAuditEvent).ToList();
}
public int GetCount(int tenant, DateTime? from = null, DateTime? to = null)
{
IQueryable<Core.Common.EF.Model.AuditEvent> query = AuditTrailContext.AuditEvents
.Where(a => a.TenantId == tenant)
.OrderByDescending(a => a.Date);
if (from.HasValue && to.HasValue)
query = query.Where(a => a.Date >= from & a.Date <= to);
return query.Count();
}
private AuditEvent ToAuditEvent(Query query)
{
try
{
var evt = new AuditEvent
{
Id = query.AuditEvent.Id,
Ip = query.AuditEvent.Ip,
Initiator = query.AuditEvent.Initiator,
Browser = query.AuditEvent.Browser,
Platform = query.AuditEvent.Platform,
Date = query.AuditEvent.Date,
TenantId = query.AuditEvent.TenantId,
UserId = query.AuditEvent.UserId,
Page = query.AuditEvent.Page,
Action = query.AuditEvent.Action
};
if (query.AuditEvent.Description != null)
{
evt.Description = JsonConvert.DeserializeObject<IList<string>>(
Convert.ToString(query.AuditEvent.Description),
new JsonSerializerSettings { DateTimeZoneHandling = DateTimeZoneHandling.Utc });
}
evt.Target = _messageTarget.Parse(query.AuditEvent.Target);
evt.UserName = (query.User.FirstName != null && query.User.LastName != null) ? _userFormatter.GetUserName(query.User.FirstName, query.User.LastName) :
evt.UserId == Core.Configuration.Constants.CoreSystem.ID ? AuditReportResource.SystemAccount :
evt.UserId == Core.Configuration.Constants.Guest.ID ? AuditReportResource.GuestAccount :
evt.Initiator ?? AuditReportResource.UnknownAccount;
evt.ActionText = _auditActionMapper.GetActionText(evt);
evt.ActionTypeText = _auditActionMapper.GetActionTypeText(evt);
evt.Product = _auditActionMapper.GetProductText(evt);
evt.Module = _auditActionMapper.GetModuleText(evt);
return evt;
}
catch (Exception) { }
return null;
}
private class Query
{
public Core.Common.EF.Model.AuditEvent AuditEvent { get; set; }
public User User { get; set; }
}
}
/*
*
* (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 <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.
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
using ASC.AuditTrail.Mappers;
using ASC.AuditTrail.Models;
using ASC.Common;
using ASC.Core.Common.EF;
using ASC.Core.Common.EF.Context;
using ASC.Core.Users;
using ASC.MessagingSystem;
using Newtonsoft.Json;
namespace ASC.AuditTrail.Repositories
{
[Scope]
public class AuditEventsRepository
{
private AuditTrailContext AuditTrailContext => _lazyAuditTrailContext.Value;
private readonly MessageTarget _messageTarget;
private readonly UserFormatter _userFormatter;
private readonly Lazy<AuditTrailContext> _lazyAuditTrailContext;
private readonly AuditActionMapper _auditActionMapper;
public AuditEventsRepository(
MessageTarget messageTarget,
UserFormatter userFormatter,
DbContextManager<AuditTrailContext> dbContextManager,
AuditActionMapper auditActionMapper)
{
_messageTarget = messageTarget;
_userFormatter = userFormatter;
_lazyAuditTrailContext = new Lazy<AuditTrailContext>(() => dbContextManager.Value );
_auditActionMapper = auditActionMapper;
}
public IEnumerable<AuditEvent> GetLast(int tenant, int chunk) => Get(tenant, null, null, chunk);
public IEnumerable<AuditEvent> Get(int tenant, DateTime from, DateTime to) => Get(tenant, from, to, null);
private IEnumerable<AuditEvent> Get(int tenant, DateTime? fromDate, DateTime? to, int? limit)
{
var query =
from q in AuditTrailContext.AuditEvents
from p in AuditTrailContext.Users.Where(p => q.UserId == p.Id).DefaultIfEmpty()
where q.TenantId == tenant
orderby q.Date descending
select new AuditEventQuery { AuditEvent = q, User = p };
if (fromDate.HasValue && to.HasValue)
query = query.Where(q => q.AuditEvent.Date >= fromDate & q.AuditEvent.Date <= to);
if (limit.HasValue) query = query.Take((int)limit);
return query.AsEnumerable().Select(ToAuditEvent).ToList();
}
public int GetCount(int tenant, DateTime? from = null, DateTime? to = null)
{
IQueryable<Core.Common.EF.Model.AuditEvent> query = AuditTrailContext.AuditEvents
.Where(a => a.TenantId == tenant)
.OrderByDescending(a => a.Date);
if (from.HasValue && to.HasValue)
query = query.Where(a => a.Date >= from & a.Date <= to);
return query.Count();
}
private AuditEvent ToAuditEvent(AuditEventQuery query)
{
try
{
var evt = new AuditEvent
{
Id = query.AuditEvent.Id,
Ip = query.AuditEvent.Ip,
Initiator = query.AuditEvent.Initiator,
Browser = query.AuditEvent.Browser,
Platform = query.AuditEvent.Platform,
Date = query.AuditEvent.Date,
TenantId = query.AuditEvent.TenantId,
UserId = query.AuditEvent.UserId,
Page = query.AuditEvent.Page,
Action = query.AuditEvent.Action
};
if (query.AuditEvent.Description != null)
{
evt.Description = JsonConvert.DeserializeObject<IList<string>>(
Convert.ToString(query.AuditEvent.Description),
new JsonSerializerSettings { DateTimeZoneHandling = DateTimeZoneHandling.Utc });
}
evt.Target = _messageTarget.Parse(query.AuditEvent.Target);
evt.UserName = (query.User.FirstName != null && query.User.LastName != null) ? _userFormatter.GetUserName(query.User.FirstName, query.User.LastName) :
evt.UserId == Core.Configuration.Constants.CoreSystem.ID ? AuditReportResource.SystemAccount :
evt.UserId == Core.Configuration.Constants.Guest.ID ? AuditReportResource.GuestAccount :
evt.Initiator ?? AuditReportResource.UnknownAccount;
evt.ActionText = _auditActionMapper.GetActionText(evt);
evt.ActionTypeText = _auditActionMapper.GetActionTypeText(evt);
evt.Product = _auditActionMapper.GetProductText(evt);
evt.Module = _auditActionMapper.GetModuleText(evt);
return evt;
}
catch (Exception) { }
return null;
}
}
}

View File

@ -28,59 +28,60 @@
using System;
using System.Collections.Generic;
using System.Linq;
using ASC.AuditTrail.Mappers;
using ASC.AuditTrail.Models;
using ASC.Common;
using ASC.Core.Common.EF;
using ASC.Core.Common.EF.Context;
using ASC.Core.Common.EF.Model;
using ASC.Core.Users;
using Newtonsoft.Json;
namespace ASC.AuditTrail.Data
{
namespace ASC.AuditTrail.Data.Repositories
{
[Scope]
public class LoginEventsRepository
{
private MessagesContext MessagesContext => _lazyMessagesContext.Value;
{
private MessagesContext MessagesContext => _lazyMessagesContext.Value;
private readonly UserFormatter _userFormatter;
private readonly AuditActionMapper _auditActionMapper;
private readonly UserFormatter _userFormatter;
private readonly AuditActionMapper _auditActionMapper;
private readonly Lazy<MessagesContext> _lazyMessagesContext;
public LoginEventsRepository(
UserFormatter userFormatter,
AuditActionMapper auditActionMapper,
public LoginEventsRepository(
UserFormatter userFormatter,
AuditActionMapper auditActionMapper,
DbContextManager<MessagesContext> dbMessagesContext)
{
_userFormatter = userFormatter;
_auditActionMapper = auditActionMapper;
_auditActionMapper = auditActionMapper;
_lazyMessagesContext = new Lazy<MessagesContext>(() => dbMessagesContext.Value);
}
public IEnumerable<LoginEvent> GetLast(int tenant, int chunk)
{
var query =
(from b in MessagesContext.LoginEvents
from p in MessagesContext.Users.Where(p => b.UserId == p.Id).DefaultIfEmpty()
where b.TenantId == tenant
orderby b.Date descending
select new LoginEventQuery { LoginEvents = b, User = p })
.Take(chunk);
{
var query =
(from b in MessagesContext.LoginEvents
from p in MessagesContext.Users.Where(p => b.UserId == p.Id).DefaultIfEmpty()
where b.TenantId == tenant
orderby b.Date descending
select new LoginEventQuery { LoginEvents = b, User = p })
.Take(chunk);
return query.AsEnumerable().Select(ToLoginEvent).ToList();
}
public IEnumerable<LoginEvent> Get(int tenant, DateTime fromDate, DateTime to)
{
var query =
from q in MessagesContext.LoginEvents
from p in MessagesContext.Users.Where(p => q.UserId == p.Id).DefaultIfEmpty()
where q.TenantId == tenant
where q.Date >= fromDate
where q.Date <= to
orderby q.Date descending
var query =
from q in MessagesContext.LoginEvents
from p in MessagesContext.Users.Where(p => q.UserId == p.Id).DefaultIfEmpty()
where q.TenantId == tenant
where q.Date >= fromDate
where q.Date <= to
orderby q.Date descending
select new LoginEventQuery { LoginEvents = q, User = p };
return query.AsEnumerable().Select(ToLoginEvent).ToList();
@ -98,27 +99,27 @@ namespace ASC.AuditTrail.Data
private LoginEvent ToLoginEvent(LoginEventQuery query)
{
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
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)
if (query.LoginEvents.Description != null)
evt.Description = JsonConvert.DeserializeObject<IList<string>>(
query.LoginEvents.Description,
new JsonSerializerSettings
{
DateTimeZoneHandling = DateTimeZoneHandling.Utc
});
new JsonSerializerSettings
{
DateTimeZoneHandling = DateTimeZoneHandling.Utc
});
evt.UserName = (!string.IsNullOrEmpty(query.User?.FirstName) && !string.IsNullOrEmpty(query.User?.LastName))
? _userFormatter.GetUserName(query.User.FirstName, query.User.LastName)
@ -128,15 +129,15 @@ namespace ASC.AuditTrail.Data
? AuditReportResource.GuestAccount
: AuditReportResource.UnknownAccount;
evt.ActionText = _auditActionMapper.GetActionText(evt);
evt.ActionText = _auditActionMapper.GetActionText(evt);
return evt;
}
}
public class LoginEventQuery
{
public LoginEvents LoginEvents { get; set; }
public User User { get; set; }
}
public class LoginEventQuery
{
public LoginEvents LoginEvents { get; set; }
public User User { get; set; }
}
}