/* * * (c) Copyright Ascensio System Limited 2010-2020 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ using System; using System.Collections.Generic; using ASC.AuditTrail.Mappers; using System.Linq; using ASC.Core.Tenants; using ASC.Core.Users; using ASC.MessagingSystem; using Newtonsoft.Json; using ASC.Core.Common.EF.Context; using ASC.Core.Common.EF; using ASC.Common; namespace ASC.AuditTrail { public class AuditEventsRepository { private MessageTarget MessageTarget { get; set; } private UserFormatter UserFormatter { get; set; } private AuditTrailContext AuditTrailContext { get; } public AuditEventsRepository(MessageTarget messageTarget, UserFormatter userFormatter, DbContextManager dbContextManager) { MessageTarget = messageTarget; UserFormatter = userFormatter; AuditTrailContext = dbContextManager.Value; } public IEnumerable GetLast(int tenant, int chunk) { return Get(tenant, null, null, chunk); } public IEnumerable Get(int tenant, DateTime from, DateTime to) { return Get(tenant, from, to, null); } private class Query { public Core.Common.EF.Model.AuditEvent AuditEvent { get; set; } public User User { get; set; } } private IEnumerable Get(int tenant, DateTime? from, DateTime? to, int? limit) { var query = AuditTrailContext.AuditEvents.Join(AuditTrailContext.User, a => a.UserId, u => u.Id, (a, u) => new Query { AuditEvent = a, User = u }) .Where(q => q.AuditEvent.TenantId == tenant) .OrderBy(q => q.AuditEvent.Date); if (from.HasValue && to.HasValue) { query = (IOrderedQueryable)query.Where(q => q.AuditEvent.Date >= from & q.AuditEvent.Date <= to); } if (limit.HasValue) { query = (IOrderedQueryable)query.Take((int)limit); } return ToAuditEvent(query.ToList()); /* var q = new SqlQuery("audit_events a") .Select(auditColumns.Select(x => "a." + x).ToArray()) .LeftOuterJoin("core_user u", Exp.EqColumns("a.user_id", "u.id")) .Select("u.firstname", "u.lastname") .Where("a.tenant_id", tenant) .OrderBy("a.date", false); if (from.HasValue && to.HasValue) { q.Where(Exp.Between("a.date", from.Value, to.Value)); } if (limit.HasValue) { q.SetMaxResults(limit.Value); } using (var db = new DbManager(dbid)) { return db.ExecuteList(q) .Select(ToAuditEvent) .Where(x => x != null) .ToList(); }*/ } public int GetCount(int tenant, DateTime? from = null, DateTime? to = null) { var query = AuditTrailContext.AuditEvents .Where(a => a.TenantId == tenant) .OrderBy(a => a.Date); if (from.HasValue && to.HasValue) { query = (IOrderedQueryable)query.Where(a => a.Date >= from & a.Date <= to); } return query.Count(); /* var q = new SqlQuery("audit_events a") .SelectCount() .Where("a.tenant_id", tenant) .OrderBy("a.date", false); if (from.HasValue && to.HasValue) { q.Where(Exp.Between("a.date", from.Value, to.Value)); } using (var db = new DbManager(dbid)) { return db.ExecuteScalar(q); }*/ } private IEnumerable ToAuditEvent(List list) { var auditEvents = new List(); list.ForEach(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>( 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); auditEvents.Add(evt); } catch (Exception) { } }); return auditEvents; } } public static class AuditEventsRepositoryExtension { public static DIHelper AddAuditEventsRepositoryService(this DIHelper services) { _ = services.TryAddScoped(); return services .AddUserFormatter() .AddAuditTrailContextService() .AddMessageTargetService(); } } }