From 1b9a2878c67f30795d874e74adf5440c4a0eeec6 Mon Sep 17 00:00:00 2001 From: maliming Date: Fri, 13 Jan 2023 10:55:39 +0800 Subject: [PATCH] Update `GetPruneListAsync` of `IOpenIddictAuthorizationRepository` --- .../EfCoreOpenIddictAuthorizationRepository.cs | 17 +++++++++-------- .../MongoOpenIddictAuthorizationRepository.cs | 18 +++++++++--------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/modules/openiddict/src/Volo.Abp.OpenIddict.EntityFrameworkCore/Volo/Abp/OpenIddict/Authorizations/EfCoreOpenIddictAuthorizationRepository.cs b/modules/openiddict/src/Volo.Abp.OpenIddict.EntityFrameworkCore/Volo/Abp/OpenIddict/Authorizations/EfCoreOpenIddictAuthorizationRepository.cs index e9b4c4b3fb..fcd1aabed5 100644 --- a/modules/openiddict/src/Volo.Abp.OpenIddict.EntityFrameworkCore/Volo/Abp/OpenIddict/Authorizations/EfCoreOpenIddictAuthorizationRepository.cs +++ b/modules/openiddict/src/Volo.Abp.OpenIddict.EntityFrameworkCore/Volo/Abp/OpenIddict/Authorizations/EfCoreOpenIddictAuthorizationRepository.cs @@ -8,6 +8,7 @@ using OpenIddict.Abstractions; using Volo.Abp.Domain.Repositories.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore; using Volo.Abp.OpenIddict.EntityFrameworkCore; +using Volo.Abp.OpenIddict.Tokens; namespace Volo.Abp.OpenIddict.Authorizations; @@ -81,13 +82,13 @@ public class EfCoreOpenIddictAuthorizationRepository : EfCoreRepository> GetPruneListAsync(DateTime date, int count, CancellationToken cancellationToken = default) { - var tokenQueryable = (await GetDbContextAsync()).Tokens.AsQueryable(); - return await (await GetDbSetAsync()) - .Where(x => x.CreationDate < date) - .Where(x => x.Status != OpenIddictConstants.Statuses.Valid || - (x.Type == OpenIddictConstants.AuthorizationTypes.AdHoc && tokenQueryable.Any(t => t.AuthorizationId == x.Id))) - .OrderBy(x => x.Id) - .Take(count) - .ToListAsync(GetCancellationToken(cancellationToken)); + return await (from authorization in (await GetQueryableAsync()) + join token in (await GetDbContextAsync()).Set() + on authorization.Id equals token.AuthorizationId into authorizationTokens + from authorizationToken in authorizationTokens.DefaultIfEmpty() + where authorization.CreationDate < date + where authorization.Status != OpenIddictConstants.Statuses.Valid || + (authorization.Type == OpenIddictConstants.AuthorizationTypes.AdHoc && authorizationToken == null) + select authorization).ToListAsync(cancellationToken: cancellationToken); } } diff --git a/modules/openiddict/src/Volo.Abp.OpenIddict.MongoDB/Volo/Abp/OpenIddict/Authorizations/MongoOpenIddictAuthorizationRepository.cs b/modules/openiddict/src/Volo.Abp.OpenIddict.MongoDB/Volo/Abp/OpenIddict/Authorizations/MongoOpenIddictAuthorizationRepository.cs index fe0042e54c..2f96602f67 100644 --- a/modules/openiddict/src/Volo.Abp.OpenIddict.MongoDB/Volo/Abp/OpenIddict/Authorizations/MongoOpenIddictAuthorizationRepository.cs +++ b/modules/openiddict/src/Volo.Abp.OpenIddict.MongoDB/Volo/Abp/OpenIddict/Authorizations/MongoOpenIddictAuthorizationRepository.cs @@ -67,16 +67,16 @@ public class MongoOpenIddictAuthorizationRepository : MongoDbRepository> GetPruneListAsync(DateTime date, int count, CancellationToken cancellationToken = default) { - var tokenQueryable = await GetMongoQueryableAsync(GetCancellationToken(cancellationToken)); - - var authorizations = await (await GetMongoQueryableAsync(GetCancellationToken(cancellationToken))) + var tokens = await (await GetMongoQueryableAsync(cancellationToken)) + .Where(x => x.AuthorizationId != null) + .Select(x => x.AuthorizationId.Value) + .ToListAsync(GetCancellationToken(cancellationToken)); + + + return await (await GetMongoQueryableAsync(cancellationToken)) .Where(x => x.CreationDate < date) .Where(x => x.Status != OpenIddictConstants.Statuses.Valid || - (x.Type == OpenIddictConstants.AuthorizationTypes.AdHoc)) - .OrderBy(x => x.Id) - .Take(count) - .ToListAsync(GetCancellationToken(cancellationToken)); - - return authorizations.Where(x => tokenQueryable.Any(t => t.AuthorizationId == x.Id)).ToList(); + (x.Type == OpenIddictConstants.AuthorizationTypes.AdHoc && !tokens.Contains(x.Id))) + .ToListAsync(cancellationToken: cancellationToken); } }