Browse Source

Merge branch 'master' of https://github.com/volosoft/abp

pull/395/head
Halil ibrahim Kalkan 8 years ago
parent
commit
16af2ac271
  1. 73
      modules/audit-logging/src/Volo.Abp.AuditLogging.Domain.Shared/Volo/Abp/Paging/PagedResult.cs
  2. 13
      modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/IAuditLogRepository.cs
  3. 46
      modules/audit-logging/src/Volo.Abp.AuditLogging.EntityFrameworkCore/Volo/Abp/AuditLogging/EntityFrameworkCore/EfCoreAuditLogRepository.cs
  4. 38
      modules/audit-logging/src/Volo.Abp.AuditLogging.MongoDB/Volo/Abp/AuditLogging/MongoDB/MongoAuditLogRepository.cs
  5. 121
      modules/audit-logging/test/Volo.Abp.AuditLogging.TestBase/Volo/Abp/AuditLogging/AuditStore_Basic_Tests.cs

73
modules/audit-logging/src/Volo.Abp.AuditLogging.Domain.Shared/Volo/Abp/Paging/PagedResult.cs

@ -1,73 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Volo.Abp.Paging
{
public class PagedResult<T> : ListResultDto<T>, IPagedResult<T>
{
/// <inheritdoc />
public long TotalCount { get; set; }
/// <summary>
/// Creates a new <see cref="PagedResult{T}"/> object.
/// </summary>
public PagedResult()
{
}
/// <summary>
/// Creates a new <see cref="PagedResult{T}"/> object.
/// </summary>
/// <param name="totalCount">Total count of Items</param>
/// <param name="items">List of items in current page</param>
public PagedResult(long totalCount, IReadOnlyList<T> items)
: base(items)
{
TotalCount = totalCount;
}
}
public class ListResultDto<T> : IListResult<T>
{
/// <inheritdoc />
public IReadOnlyList<T> Items
{
get { return _items ?? (_items = new List<T>()); }
set { _items = value; }
}
private IReadOnlyList<T> _items;
/// <summary>
/// Creates a new <see cref="ListResultDto{T}"/> object.
/// </summary>
public ListResultDto()
{
}
/// <summary>
/// Creates a new <see cref="ListResultDto{T}"/> object.
/// </summary>
/// <param name="items">List of items</param>
public ListResultDto(IReadOnlyList<T> items)
{
Items = items;
}
}
public interface IListResult<T>
{
IReadOnlyList<T> Items { get; set; }
}
public interface IPagedResult<T> : IListResult<T>, IHasTotalCount
{
}
public interface IHasTotalCount
{
long TotalCount { get; set; }
}
}

13
modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/IAuditLogRepository.cs

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.Net; using System.Net;
using System.Threading.Tasks; using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories; using Volo.Abp.Domain.Repositories;
@ -7,15 +8,21 @@ namespace Volo.Abp.AuditLogging
{ {
public interface IAuditLogRepository : IBasicRepository<AuditLog, Guid> public interface IAuditLogRepository : IBasicRepository<AuditLog, Guid>
{ {
Task<Paging.PagedResult<AuditLog>> GetListAsync( Task<List<AuditLog>> GetListAsync(
string sorting = null, string sorting = null,
int maxResultCount = 50, int maxResultCount = 50,
int skipCount = 0, int skipCount = 0,
string filter = null,
string httpMethod = null, string httpMethod = null,
string url = null, string url = null,
string userName = null, string userName = null,
HttpStatusCode? httpStatusCode = null, HttpStatusCode? httpStatusCode = null,
bool includeDetails = false); bool includeDetails = true);
Task<long> GetCountAsync(
string httpMethod = null,
string url = null,
string userName = null,
HttpStatusCode? httpStatusCode = null,
bool includeDetails = true);
} }
} }

46
modules/audit-logging/src/Volo.Abp.AuditLogging.EntityFrameworkCore/Volo/Abp/AuditLogging/EntityFrameworkCore/EfCoreAuditLogRepository.cs

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Linq.Dynamic.Core; using System.Linq.Dynamic.Core;
using System.Net; using System.Net;
@ -17,31 +18,52 @@ namespace Volo.Abp.AuditLogging.EntityFrameworkCore
} }
public async Task<Paging.PagedResult<AuditLog>> GetListAsync( public async Task<List<AuditLog>> GetListAsync(
string sorting = null, string sorting = null,
int maxResultCount = 50, int maxResultCount = 50,
int skipCount = 0, int skipCount = 0,
string filter = null,
string httpMethod = null, string httpMethod = null,
string url = null, string url = null,
string userName = null, string userName = null,
HttpStatusCode? httpStatusCode = null, HttpStatusCode? httpStatusCode = null,
bool includeDetails = false) bool includeDetails = true)
{ {
var query = DbSet.AsNoTracking() var query = GetListQuery(httpMethod, url, userName, httpStatusCode, includeDetails);
var auditLogs = await query.OrderBy(sorting ?? "executionTime desc")
.PageBy(skipCount, maxResultCount)
.ToListAsync();
return auditLogs;
}
public async Task<long> GetCountAsync(
string httpMethod = null,
string url = null,
string userName = null,
HttpStatusCode? httpStatusCode = null,
bool includeDetails = true)
{
var query = GetListQuery(httpMethod, url, userName, httpStatusCode, includeDetails);
var totalCount = await query.LongCountAsync();
return totalCount;
}
private IQueryable<AuditLog> GetListQuery(
string httpMethod = null,
string url = null,
string userName = null,
HttpStatusCode? httpStatusCode = null,
bool includeDetails = true)
{
return DbSet.AsNoTracking()
.IncludeDetails(includeDetails) .IncludeDetails(includeDetails)
.WhereIf(httpMethod != null, auditLog => auditLog.HttpMethod != null && auditLog.HttpMethod.ToLowerInvariant() == httpMethod.ToLowerInvariant()) .WhereIf(httpMethod != null, auditLog => auditLog.HttpMethod != null && auditLog.HttpMethod.ToLowerInvariant() == httpMethod.ToLowerInvariant())
.WhereIf(url != null, auditLog => auditLog.Url != null && auditLog.Url.ToLowerInvariant().Contains(url.ToLowerInvariant())) .WhereIf(url != null, auditLog => auditLog.Url != null && auditLog.Url.ToLowerInvariant().Contains(url.ToLowerInvariant()))
.WhereIf(userName != null, auditLog => auditLog.UserName != null && auditLog.UserName == userName) .WhereIf(userName != null, auditLog => auditLog.UserName != null && auditLog.UserName == userName)
.WhereIf(httpStatusCode != null && httpStatusCode > 0, auditLog => auditLog.HttpStatusCode == (int?)httpStatusCode); .WhereIf(httpStatusCode != null && httpStatusCode > 0, auditLog => auditLog.HttpStatusCode == (int?)httpStatusCode);
var totalCount = await query.LongCountAsync();
var auditLogs = await query.OrderBy(sorting)
.PageBy(skipCount, maxResultCount)
.ToListAsync();
return new Paging.PagedResult<AuditLog>(totalCount, auditLogs);
} }
public override IQueryable<AuditLog> WithDetails() public override IQueryable<AuditLog> WithDetails()

38
modules/audit-logging/src/Volo.Abp.AuditLogging.MongoDB/Volo/Abp/AuditLogging/MongoDB/MongoAuditLogRepository.cs

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Linq.Dynamic.Core; using System.Linq.Dynamic.Core;
using System.Net; using System.Net;
@ -18,22 +19,37 @@ namespace Volo.Abp.AuditLogging.MongoDB
} }
public async Task<Paging.PagedResult<AuditLog>> GetListAsync(string sorting = null, int maxResultCount = 50, int skipCount = 0, string filter = null, public async Task<List<AuditLog>> GetListAsync(string sorting = null, int maxResultCount = 50, int skipCount = 0,
string httpMethod = null, string url = null, string userName = null, HttpStatusCode? httpStatusCode = null, bool includeDetails = false) string httpMethod = null, string url = null, string userName = null, HttpStatusCode? httpStatusCode = null, bool includeDetails = true)
{ {
var query = GetMongoQueryable() var query = GetListQuery(httpMethod, url, userName, httpStatusCode, includeDetails);
.WhereIf(httpMethod != null, auditLog => auditLog.HttpMethod != null && auditLog.HttpMethod.ToLowerInvariant() == httpMethod.ToLowerInvariant())
.WhereIf(url != null, auditLog => auditLog.Url != null && auditLog.Url.ToLowerInvariant().Contains(url.ToLowerInvariant()))
.WhereIf(userName != null, auditLog => auditLog.UserName != null && auditLog.UserName == userName)
.WhereIf(httpStatusCode != null && httpStatusCode > 0, auditLog => auditLog.HttpStatusCode == (int?)httpStatusCode);
var totalCount = await query.As<IMongoQueryable<AuditLog>>().LongCountAsync(); return await query.OrderBy(sorting ?? "executionTime desc").As<IMongoQueryable<AuditLog>>()
var questions = await query.OrderBy(sorting).As<IMongoQueryable<AuditLog>>()
.PageBy<AuditLog, IMongoQueryable<AuditLog>>(skipCount, maxResultCount) .PageBy<AuditLog, IMongoQueryable<AuditLog>>(skipCount, maxResultCount)
.ToListAsync(); .ToListAsync();
}
public async Task<long> GetCountAsync(string httpMethod = null, string url = null, string userName = null, HttpStatusCode? httpStatusCode = null, bool includeDetails = true)
{
var query = GetListQuery(httpMethod, url, userName, httpStatusCode, includeDetails);
return new Paging.PagedResult<AuditLog>(totalCount, questions); var count = await query.As<IMongoQueryable<AuditLog>>().LongCountAsync();
return count;
}
private IQueryable<AuditLog> GetListQuery(
string httpMethod = null,
string url = null,
string userName = null,
HttpStatusCode? httpStatusCode = null,
bool includeDetails = true)
{
return GetMongoQueryable()
.WhereIf(httpMethod != null, auditLog => auditLog.HttpMethod != null && auditLog.HttpMethod.ToLowerInvariant() == httpMethod.ToLowerInvariant())
.WhereIf(url != null, auditLog => auditLog.Url != null && auditLog.Url.ToLowerInvariant().Contains(url.ToLowerInvariant()))
.WhereIf(userName != null, auditLog => auditLog.UserName != null && auditLog.UserName == userName)
.WhereIf(httpStatusCode != null && httpStatusCode > 0, auditLog => auditLog.HttpStatusCode == (int?)httpStatusCode);
} }
} }
} }

121
modules/audit-logging/test/Volo.Abp.AuditLogging.TestBase/Volo/Abp/AuditLogging/AuditStore_Basic_Tests.cs

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net;
using System.Threading.Tasks; using System.Threading.Tasks;
using Shouldly; using Shouldly;
using Volo.Abp.Auditing; using Volo.Abp.Auditing;
@ -9,7 +10,7 @@ using Xunit;
namespace Volo.Abp.AuditLogging namespace Volo.Abp.AuditLogging
{ {
public abstract class AuditStore_Basic_Tests<TStartupModule> : AuditLoggingTestBase<TStartupModule> public abstract class AuditStore_Basic_Tests<TStartupModule> : AuditLoggingTestBase<TStartupModule>
where TStartupModule : IAbpModule where TStartupModule : IAbpModule
{ {
private readonly IAuditingStore _auditingStore; private readonly IAuditingStore _auditingStore;
@ -22,7 +23,7 @@ namespace Volo.Abp.AuditLogging
} }
[Fact] [Fact]
public async Task Should_Save_A_Audit_Log() public async Task Should_Save_An_Audit_Log()
{ {
//Arrange //Arrange
var userId = new Guid("4456fb0d-74cc-4807-9eee-23e551e6cb06"); var userId = new Guid("4456fb0d-74cc-4807-9eee-23e551e6cb06");
@ -39,7 +40,7 @@ namespace Volo.Abp.AuditLogging
ClientIpAddress = ipAddress, ClientIpAddress = ipAddress,
ClientName = "MyDesktop", ClientName = "MyDesktop",
BrowserInfo = "Chrome", BrowserInfo = "Chrome",
Comments = new List<string> { firstComment, "Second Comment"}, Comments = new List<string> { firstComment, "Second Comment" },
EntityChanges = EntityChanges =
{ {
new EntityChangeInfo new EntityChangeInfo
@ -76,5 +77,119 @@ namespace Volo.Abp.AuditLogging
insertedLog.EntityChanges.Count.ShouldBeGreaterThan(0); insertedLog.EntityChanges.Count.ShouldBeGreaterThan(0);
insertedLog.EntityChanges.First().PropertyChanges.Count.ShouldBeGreaterThan(0); insertedLog.EntityChanges.First().PropertyChanges.Count.ShouldBeGreaterThan(0);
} }
[Fact]
public async Task Should_Get_List_Of_Audit_Logs()
{
var userId = new Guid("4456fb0d-74cc-4807-9eee-23e551e6cb06");
var ipAddress = "153.1.7.61";
var firstComment = "first Comment";
var log1 = new AuditLogInfo
{
UserId = userId,
ImpersonatorUserId = Guid.NewGuid(),
ImpersonatorTenantId = Guid.NewGuid(),
ExecutionTime = DateTime.Today,
ExecutionDuration = 42,
ClientIpAddress = ipAddress,
ClientName = "MyDesktop",
BrowserInfo = "Chrome",
Comments = new List<string> { firstComment, "Second Comment" },
UserName = "Douglas",
EntityChanges = {
new EntityChangeInfo
{
EntityId = Guid.NewGuid().ToString(),
EntityTypeFullName = "Volo.Abp.AuditLogging.TestEntity",
ChangeType = EntityChangeType.Created,
ChangeTime = DateTime.Now,
PropertyChanges = new List<EntityPropertyChangeInfo>
{
new EntityPropertyChangeInfo
{
PropertyTypeFullName = typeof(string).FullName,
PropertyName = "Name",
NewValue = "New value",
OriginalValue = null
}
}
},
new EntityChangeInfo
{
EntityId = Guid.NewGuid().ToString(),
EntityTypeFullName = "Volo.Abp.AuditLogging.TestEntity",
ChangeType = EntityChangeType.Created,
ChangeTime = DateTime.Now,
PropertyChanges = new List<EntityPropertyChangeInfo>
{
new EntityPropertyChangeInfo
{
PropertyTypeFullName = typeof(string).FullName,
PropertyName = "Name",
NewValue = "New value",
OriginalValue = null
}
}
}
}
};
var log2 = new AuditLogInfo
{
UserId = userId,
ImpersonatorUserId = Guid.NewGuid(),
ImpersonatorTenantId = Guid.NewGuid(),
ExecutionTime = DateTime.Today,
ExecutionDuration = 42,
ClientIpAddress = ipAddress,
ClientName = "MyDesktop",
BrowserInfo = "Chrome",
Comments = new List<string> { firstComment, "Second Comment" },
HttpStatusCode = (int?)HttpStatusCode.BadGateway,
EntityChanges = {
new EntityChangeInfo
{
EntityId = Guid.NewGuid().ToString(),
EntityTypeFullName = "Volo.Abp.AuditLogging.TestEntity",
ChangeType = EntityChangeType.Created,
ChangeTime = DateTime.Now,
PropertyChanges = new List<EntityPropertyChangeInfo>
{
new EntityPropertyChangeInfo
{
PropertyTypeFullName = typeof(string).FullName,
PropertyName = "Name",
NewValue = "New value",
OriginalValue = null
}
}
}
}
};
await _auditingStore.SaveAsync(log1);
await _auditingStore.SaveAsync(log2);
var allLogsCount = await _auditLogRepository.GetCountAsync();
var onlyLog1QueryResult = await _auditLogRepository.GetListAsync(includeDetails: true, userName: "Douglas");
var onlyLog2QueryResult = await _auditLogRepository.GetListAsync(includeDetails: true, httpStatusCode: HttpStatusCode.BadGateway);
allLogsCount.ShouldBe(2);
onlyLog1QueryResult.Count.ShouldBe(1);
onlyLog1QueryResult.FirstOrDefault().UserName.ShouldBe("Douglas");
onlyLog1QueryResult.FirstOrDefault().EntityChanges.Count.ShouldBe(2);
onlyLog2QueryResult.Count.ShouldBe(1);
onlyLog2QueryResult.FirstOrDefault().HttpStatusCode.ShouldBe((int?)HttpStatusCode.BadGateway);
onlyLog2QueryResult.FirstOrDefault().EntityChanges.Count.ShouldBe(1);
}
} }
} }

Loading…
Cancel
Save