From e26c32e2a459a6fd38719e3129b23043d2b06a35 Mon Sep 17 00:00:00 2001 From: maliming Date: Thu, 28 Dec 2023 17:55:36 +0800 Subject: [PATCH] Add `GetListAsync` and `GetCountAsync ` to `IIdentitySessionRepository `. --- .../IIdentitySecurityLogRepository.cs | 2 +- .../Identity/IIdentitySessionRepository.cs | 15 ++++++++- .../AbpIdentityEntityFrameworkCoreModule.cs | 2 +- ...=> EfCoreIdentitySecurityLogRepository.cs} | 6 ++-- .../EfCoreIdentitySessionRepository.cs | 30 +++++++++++++++-- .../MongoIdentitySecurityLogRepository.cs | 2 +- .../MongoDB/MongoIdentitySessionRepository.cs | 32 +++++++++++++++++-- 7 files changed, 78 insertions(+), 11 deletions(-) rename modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/{EFCoreIdentitySecurityLogRepository.cs => EfCoreIdentitySecurityLogRepository.cs} (96%) 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 2ceaa8ed4c..8a1e50795f 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 @@ -10,7 +10,7 @@ public interface IIdentitySecurityLogRepository : IBasicRepository> GetListAsync( string sorting = null, - int maxResultCount = 50, + int maxResultCount = int.MaxValue, int skipCount = 0, DateTime? startTime = null, DateTime? endTime = null, diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentitySessionRepository.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentitySessionRepository.cs index b425b24c33..7c1af6937c 100644 --- a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentitySessionRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentitySessionRepository.cs @@ -12,7 +12,20 @@ public interface IIdentitySessionRepository : IBasicRepository GetAsync(string sessionId, CancellationToken cancellationToken = default); - Task> GetListAsync(Guid userId, CancellationToken cancellationToken = default); + Task> GetListAsync( + string sorting = null, + int maxResultCount = int.MaxValue, + int skipCount = 0, + Guid? userId = null, + string device = null, + string clientId = null, + CancellationToken cancellationToken = default); + + Task GetCountAsync( + Guid? userId = null, + string device = null, + string clientId = null, + CancellationToken cancellationToken = default); Task DeleteAllAsync(Guid userId, Guid? exceptSessionId = null, CancellationToken cancellationToken = default); diff --git a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/AbpIdentityEntityFrameworkCoreModule.cs b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/AbpIdentityEntityFrameworkCoreModule.cs index 16433977d7..c9dcc70e68 100644 --- a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/AbpIdentityEntityFrameworkCoreModule.cs +++ b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/AbpIdentityEntityFrameworkCoreModule.cs @@ -17,7 +17,7 @@ public class AbpIdentityEntityFrameworkCoreModule : AbpModule options.AddRepository(); options.AddRepository(); options.AddRepository(); - options.AddRepository(); + options.AddRepository(); options.AddRepository(); options.AddRepository(); options.AddRepository(); 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 similarity index 96% rename from modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EFCoreIdentitySecurityLogRepository.cs rename to modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentitySecurityLogRepository.cs index 0e5c2a9b4c..2043293395 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 @@ -10,9 +10,9 @@ using Volo.Abp.EntityFrameworkCore; namespace Volo.Abp.Identity.EntityFrameworkCore; -public class EFCoreIdentitySecurityLogRepository : EfCoreRepository, IIdentitySecurityLogRepository +public class EfCoreIdentitySecurityLogRepository : EfCoreRepository, IIdentitySecurityLogRepository { - public EFCoreIdentitySecurityLogRepository(IDbContextProvider dbContextProvider) + public EfCoreIdentitySecurityLogRepository(IDbContextProvider dbContextProvider) : base(dbContextProvider) { @@ -20,7 +20,7 @@ public class EFCoreIdentitySecurityLogRepository : EfCoreRepository> GetListAsync( string sorting = null, - int maxResultCount = 50, + int maxResultCount = int.MaxValue, int skipCount = 0, DateTime? startTime = null, DateTime? endTime = null, diff --git a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentitySessionRepository.cs b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentitySessionRepository.cs index 5a583531aa..1b39ecef29 100644 --- a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentitySessionRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentitySessionRepository.cs @@ -35,9 +35,35 @@ public class EfCoreIdentitySessionRepository : EfCoreRepository> GetListAsync(Guid userId, CancellationToken cancellationToken = default) + public virtual async Task> GetListAsync( + string sorting = null, + int maxResultCount = int.MaxValue, + int skipCount = 0, + Guid? userId = null, + string device = null, + string clientId = null, + CancellationToken cancellationToken = default) { - return await (await GetDbSetAsync()).Where(x => x.UserId == userId).ToListAsync(GetCancellationToken(cancellationToken)); + return await (await GetDbSetAsync()) + .WhereIf(userId.HasValue, x => x.UserId == userId) + .WhereIf(!device.IsNullOrWhiteSpace(), x => x.Device == device) + .WhereIf(!clientId.IsNullOrWhiteSpace(), x => x.ClientId == clientId) + .OrderBy(sorting.IsNullOrWhiteSpace() ? $"{nameof(IdentitySession.LastAccessed)} desc" : sorting) + .PageBy(skipCount, maxResultCount) + .ToListAsync(GetCancellationToken(cancellationToken)); + } + + public virtual async Task GetCountAsync( + Guid? userId = null, + string device = null, + string clientId = null, + CancellationToken cancellationToken = default) + { + return await (await GetDbSetAsync()) + .WhereIf(userId.HasValue, x => x.UserId == userId) + .WhereIf(!device.IsNullOrWhiteSpace(), x => x.Device == device) + .WhereIf(!clientId.IsNullOrWhiteSpace(), x => x.ClientId == clientId) + .LongCountAsync(GetCancellationToken(cancellationToken)); } public virtual async Task DeleteAllAsync(Guid userId, Guid? exceptSessionId = null, CancellationToken cancellationToken = default) 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 de5587d8d9..6a875444f9 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 @@ -21,7 +21,7 @@ public class MongoIdentitySecurityLogRepository : public virtual async Task> GetListAsync( string sorting = null, - int maxResultCount = 50, + int maxResultCount = int.MaxValue, int skipCount = 0, DateTime? startTime = null, DateTime? endTime = null, diff --git a/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentitySessionRepository.cs b/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentitySessionRepository.cs index 222d1d432b..3ce5cf6bf6 100644 --- a/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentitySessionRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentitySessionRepository.cs @@ -1,5 +1,7 @@ using System; using System.Collections.Generic; +using System.Linq; +using System.Linq.Dynamic.Core; using System.Threading; using System.Threading.Tasks; using MongoDB.Driver; @@ -36,11 +38,37 @@ public class MongoIdentitySessionRepository : MongoDbRepository> GetListAsync(Guid userId, CancellationToken cancellationToken = default) + public virtual async Task> GetListAsync( + string sorting = null, + int maxResultCount = int.MaxValue, + int skipCount = 0, + Guid? userId = null, + string device = null, + string clientId = null, + CancellationToken cancellationToken = default) { return await (await GetMongoQueryableAsync(GetCancellationToken(cancellationToken))) + .WhereIf(userId.HasValue, x => x.UserId == userId) + .WhereIf(!device.IsNullOrWhiteSpace(), x => x.Device == device) + .WhereIf(!clientId.IsNullOrWhiteSpace(), x => x.ClientId == clientId) + .OrderBy(sorting.IsNullOrWhiteSpace() ? $"{nameof(IdentitySession.LastAccessed)} desc" : sorting) + .PageBy(skipCount, maxResultCount) .As>() - .Where(x => x.UserId == userId).ToListAsync(GetCancellationToken(cancellationToken)); + .ToListAsync(GetCancellationToken(cancellationToken)); + } + + public virtual async Task GetCountAsync( + Guid? userId = null, + string device = null, + string clientId = null, + CancellationToken cancellationToken = default) + { + return await (await GetMongoQueryableAsync(GetCancellationToken(cancellationToken))) + .WhereIf(userId.HasValue, x => x.UserId == userId) + .WhereIf(!device.IsNullOrWhiteSpace(), x => x.Device == device) + .WhereIf(!clientId.IsNullOrWhiteSpace(), x => x.ClientId == clientId) + .As>() + .LongCountAsync(GetCancellationToken(cancellationToken)); } public virtual async Task DeleteAllAsync(Guid userId, Guid? exceptSessionId = null, CancellationToken cancellationToken = default)