From 787a09546ff7cd9f32225d6f20ebe98449fe1bd4 Mon Sep 17 00:00:00 2001 From: maliming <6908465+maliming@users.noreply.github.com> Date: Fri, 10 Jul 2020 13:50:53 +0800 Subject: [PATCH] Add filter for IIdentitySecurityLogRepository. --- .../Pages/Account/Login.cshtml.cs | 3 +- .../IIdentitySecurityLogRepository.cs | 8 +++ .../Abp/Identity/IdentitySecurityLogEvent.cs | 15 +++++ .../Identity/IdentitySecurityLogHandler.cs | 5 ++ .../EFCoreIdentitySecurityLogRepository.cs | 13 +++- .../MongoIdentitySecurityLogRepository.cs | 62 ++++++++++++------- 6 files changed, 81 insertions(+), 25 deletions(-) diff --git a/modules/account/src/Volo.Abp.Account.Web/Pages/Account/Login.cshtml.cs b/modules/account/src/Volo.Abp.Account.Web/Pages/Account/Login.cshtml.cs index 8dc2ef3273..ca2125396c 100644 --- a/modules/account/src/Volo.Abp.Account.Web/Pages/Account/Login.cshtml.cs +++ b/modules/account/src/Volo.Abp.Account.Web/Pages/Account/Login.cshtml.cs @@ -226,8 +226,7 @@ namespace Volo.Abp.Account.Web.Pages.Account { Identity = IdentitySecurityLogIdentityConsts.IdentityExternal, Action = result.ToIdentitySecurityLogAction(), - UserName = user.Name, - TenantId = user.TenantId + UserName = user.Name }); return RedirectSafely(returnUrl, returnUrlHash); diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentitySecurityLogRepository.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentitySecurityLogRepository.cs index 8732355c14..f68f289b4f 100644 --- a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentitySecurityLogRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentitySecurityLogRepository.cs @@ -17,6 +17,7 @@ namespace Volo.Abp.Identity string applicationName = null, string identity = null, string action = null, + Guid? userId = null, string userName = null, string clientId = null, string correlationId = null, @@ -29,9 +30,16 @@ namespace Volo.Abp.Identity string applicationName = null, string identity = null, string action = null, + Guid? userId = null, string userName = null, string clientId = null, string correlationId = null, CancellationToken cancellationToken = default); + + Task GetByUserIdAsync( + Guid id, + Guid userId, + bool includeDetails = false, + CancellationToken cancellationToken = default); } } diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentitySecurityLogEvent.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentitySecurityLogEvent.cs index 93296619f6..faac2d5033 100644 --- a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentitySecurityLogEvent.cs +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentitySecurityLogEvent.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using Volo.Abp.MultiTenancy; namespace Volo.Abp.Identity @@ -14,5 +15,19 @@ namespace Volo.Abp.Identity public string UserName { get; set; } public string ClientId { get; set; } + + public Dictionary ExtraProperties { get; } + + public IdentitySecurityLogEvent() + { + ExtraProperties = new Dictionary(); + } + + public virtual IdentitySecurityLogEvent WithProperty(string key, object value) + { + ExtraProperties[key] = value; + return this; + } + } } diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentitySecurityLogHandler.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentitySecurityLogHandler.cs index 4e0c46b2f7..5000d4d288 100644 --- a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentitySecurityLogHandler.cs +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentitySecurityLogHandler.cs @@ -48,6 +48,11 @@ namespace Volo.Abp.Identity { securityLog.ClientId = eventData.ClientId; } + + foreach (var property in eventData.ExtraProperties) + { + securityLog.ExtraProperties[property.Key] = property.Value; + } }; if (CurrentUser.IsAuthenticated) diff --git a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EFCoreIdentitySecurityLogRepository.cs b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EFCoreIdentitySecurityLogRepository.cs index 1801a49136..137341a25e 100644 --- a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EFCoreIdentitySecurityLogRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EFCoreIdentitySecurityLogRepository.cs @@ -27,6 +27,7 @@ namespace Volo.Abp.Identity.EntityFrameworkCore string applicationName = null, string identity = null, string action = null, + Guid? userId = null, string userName = null, string clientId = null, string correlationId = null, @@ -39,6 +40,7 @@ namespace Volo.Abp.Identity.EntityFrameworkCore applicationName, identity, action, + userId, userName, clientId, correlationId @@ -55,6 +57,7 @@ namespace Volo.Abp.Identity.EntityFrameworkCore string applicationName = null, string identity = null, string action = null, + Guid? userId = null, string userName = null, string clientId = null, string correlationId = null, @@ -66,6 +69,7 @@ namespace Volo.Abp.Identity.EntityFrameworkCore applicationName, identity, action, + userId, userName, clientId, correlationId @@ -74,12 +78,18 @@ namespace Volo.Abp.Identity.EntityFrameworkCore return await query.LongCountAsync(GetCancellationToken(cancellationToken)); } - protected virtual IQueryable GetListQuery( + public async Task GetByUserIdAsync(Guid id, Guid userId, bool includeDetails = false, CancellationToken cancellationToken = default) + { + return await DbSet.FirstOrDefaultAsync(x => x.Id == id && x.UserId == userId, GetCancellationToken(cancellationToken)); + } + + protected virtual IQueryable GetListQuery( DateTime? startTime = null, DateTime? endTime = null, string applicationName = null, string identity = null, string action = null, + Guid? userId = null, string userName = null, string clientId = null, string correlationId = null) @@ -90,6 +100,7 @@ namespace Volo.Abp.Identity.EntityFrameworkCore .WhereIf(!applicationName.IsNullOrWhiteSpace(), securityLog => securityLog.ApplicationName == applicationName) .WhereIf(!identity.IsNullOrWhiteSpace(), securityLog => securityLog.Identity == identity) .WhereIf(!action.IsNullOrWhiteSpace(), securityLog => securityLog.Action == action) + .WhereIf(userId.HasValue, securityLog => securityLog.UserId == userId) .WhereIf(!userName.IsNullOrWhiteSpace(), securityLog => securityLog.UserName == userName) .WhereIf(!clientId.IsNullOrWhiteSpace(), securityLog => securityLog.ClientId == clientId) .WhereIf(!correlationId.IsNullOrWhiteSpace(), securityLog => securityLog.CorrelationId == correlationId); diff --git a/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentitySecurityLogRepository.cs b/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentitySecurityLogRepository.cs index 5fccb0d2ab..4336abaaae 100644 --- a/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentitySecurityLogRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentitySecurityLogRepository.cs @@ -11,7 +11,8 @@ using Volo.Abp.MongoDB; namespace Volo.Abp.Identity.MongoDB { - public class MongoIdentitySecurityLogRepository : MongoDbRepository, IIdentitySecurityLogRepository + public class MongoIdentitySecurityLogRepository : + MongoDbRepository, IIdentitySecurityLogRepository { public MongoIdentitySecurityLogRepository(IMongoDbContextProvider dbContextProvider) : base(dbContextProvider) @@ -27,6 +28,7 @@ namespace Volo.Abp.Identity.MongoDB string applicationName = null, string identity = null, string action = null, + Guid? userId = null, string userName = null, string clientId = null, string correlationId = null, @@ -39,6 +41,7 @@ namespace Volo.Abp.Identity.MongoDB applicationName, identity, action, + userId, userName, clientId, correlationId @@ -56,6 +59,7 @@ namespace Volo.Abp.Identity.MongoDB string applicationName = null, string identity = null, string action = null, + Guid? userId = null, string userName = null, string clientId = null, string correlationId = null, @@ -67,33 +71,47 @@ namespace Volo.Abp.Identity.MongoDB applicationName, identity, action, + userId, userName, clientId, correlationId ); - return await query.As>().LongCountAsync(GetCancellationToken(cancellationToken)); + return await query.As>() + .LongCountAsync(GetCancellationToken(cancellationToken)); } - protected virtual IQueryable GetListQuery( - DateTime? startTime = null, - DateTime? endTime = null, - string applicationName = null, - string identity = null, - string action = null, - string userName = null, - string clientId = null, - string correlationId = null) - { - return GetMongoQueryable() - .WhereIf(startTime.HasValue, securityLog => securityLog.CreationTime >= startTime) - .WhereIf(endTime.HasValue, securityLog => securityLog.CreationTime >= endTime) - .WhereIf(!applicationName.IsNullOrWhiteSpace(), securityLog => securityLog.ApplicationName == applicationName) - .WhereIf(!identity.IsNullOrWhiteSpace(), securityLog => securityLog.Identity == identity) - .WhereIf(!action.IsNullOrWhiteSpace(), securityLog => securityLog.Action == action) - .WhereIf(!userName.IsNullOrWhiteSpace(), securityLog => securityLog.UserName == userName) - .WhereIf(!clientId.IsNullOrWhiteSpace(), securityLog => securityLog.ClientId == clientId) - .WhereIf(!correlationId.IsNullOrWhiteSpace(), securityLog => securityLog.CorrelationId == correlationId); - } + + public async Task GetByUserIdAsync(Guid id, Guid userId, bool includeDetails = false, + CancellationToken cancellationToken = default) + { + return await GetMongoQueryable().FirstOrDefaultAsync(x => x.Id == id && x.UserId == userId, + GetCancellationToken(cancellationToken)); + } + + protected virtual IQueryable GetListQuery( + DateTime? startTime = null, + DateTime? endTime = null, + string applicationName = null, + string identity = null, + string action = null, + Guid? userId = null, + string userName = null, + string clientId = null, + string correlationId = null) + { + return GetMongoQueryable() + .WhereIf(startTime.HasValue, securityLog => securityLog.CreationTime >= startTime) + .WhereIf(endTime.HasValue, securityLog => securityLog.CreationTime >= endTime) + .WhereIf(!applicationName.IsNullOrWhiteSpace(), + securityLog => securityLog.ApplicationName == applicationName) + .WhereIf(!identity.IsNullOrWhiteSpace(), securityLog => securityLog.Identity == identity) + .WhereIf(!action.IsNullOrWhiteSpace(), securityLog => securityLog.Action == action) + .WhereIf(userId.HasValue, securityLog => securityLog.UserId == userId) + .WhereIf(!userName.IsNullOrWhiteSpace(), securityLog => securityLog.UserName == userName) + .WhereIf(!clientId.IsNullOrWhiteSpace(), securityLog => securityLog.ClientId == clientId) + .WhereIf(!correlationId.IsNullOrWhiteSpace(), + securityLog => securityLog.CorrelationId == correlationId); + } } }