| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using ProjectTemplate.Infrastructure.Data.Entities; |
| | | 3 | | |
| | | 4 | | namespace ProjectTemplate.Infrastructure.Data.ExternalLogins; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// EF Core implementation of <see cref="IExternalLoginAccountResolver" />. |
| | | 8 | | /// </summary> |
| | 22 | 9 | | public sealed class EfCoreExternalLoginAccountResolver(ApplicationDbContext dbContext) |
| | | 10 | | : IExternalLoginAccountResolver |
| | | 11 | | { |
| | 22 | 12 | | private readonly ApplicationDbContext _dbContext = dbContext; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Finds an external login account by the provider name and provider user ID. |
| | | 16 | | /// </summary> |
| | | 17 | | /// <param name="providerName">Provider name. This value is matched case-insensitively after trimming and Unicode no |
| | | 18 | | /// <param name="providerUserId">Provider user ID. This value is trimmed and Unicode-normalized, but remains case-se |
| | | 19 | | /// <param name="cancellationToken">Cancellation token to cancel the operation if needed.</param> |
| | | 20 | | /// <returns>Returns the matching <see cref="ExternalLoginAccount" /> if found; otherwise, returns null.</returns> |
| | | 21 | | public async Task<ExternalLoginAccount?> FindByProviderUserIdAsync( |
| | | 22 | | string providerName, |
| | | 23 | | string providerUserId, |
| | | 24 | | CancellationToken cancellationToken = default) |
| | | 25 | | { |
| | 18 | 26 | | if (string.IsNullOrWhiteSpace(providerName) || string.IsNullOrWhiteSpace(providerUserId)) |
| | | 27 | | { |
| | 8 | 28 | | return null; |
| | | 29 | | } |
| | | 30 | | |
| | 10 | 31 | | string normalizedProviderName = |
| | 10 | 32 | | PersistenceStringComparisonNormalizer.NormalizeRequiredLookupValue(providerName); |
| | | 33 | | |
| | 10 | 34 | | string normalizedProviderUserId = |
| | 10 | 35 | | PersistenceStringComparisonNormalizer.NormalizeRequiredDisplayValue(providerUserId); |
| | | 36 | | |
| | 10 | 37 | | return await _dbContext.ExternalLoginAccounts |
| | 10 | 38 | | .AsNoTracking() |
| | 10 | 39 | | .FirstOrDefaultAsync( |
| | 10 | 40 | | x => x.NormalizedProviderName == normalizedProviderName |
| | 10 | 41 | | && x.ProviderUserId == normalizedProviderUserId, |
| | 10 | 42 | | cancellationToken) |
| | 10 | 43 | | .ConfigureAwait(false); |
| | 18 | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Finds all external login accounts associated with a specific local user ID. |
| | | 48 | | /// </summary> |
| | | 49 | | /// <param name="localUserId"> |
| | | 50 | | /// Local user ID (the unique identifier for the user in the local system). If this value is Guid.Empty, an empty li |
| | | 51 | | /// </param> |
| | | 52 | | /// <param name="cancellationToken"> |
| | | 53 | | /// Cancellation token to cancel the operation if needed. |
| | | 54 | | /// </param> |
| | | 55 | | /// <returns> |
| | | 56 | | /// Returns a list of <see cref="ExternalLoginAccount" /> instances associated with the specified local user ID. If |
| | | 57 | | /// </returns> |
| | | 58 | | public async Task<IReadOnlyList<ExternalLoginAccount>> FindByLocalUserIdAsync( |
| | | 59 | | Guid localUserId, |
| | | 60 | | CancellationToken cancellationToken = default) |
| | | 61 | | { |
| | 4 | 62 | | return localUserId == Guid.Empty |
| | 4 | 63 | | ? [] |
| | 4 | 64 | | : (IReadOnlyList<ExternalLoginAccount>)await _dbContext.ExternalLoginAccounts |
| | 4 | 65 | | .AsNoTracking() |
| | 4 | 66 | | .Where(x => x.LocalUserId == localUserId) |
| | 4 | 67 | | .OrderBy(x => x.ProviderName) |
| | 4 | 68 | | .ThenBy(x => x.ProviderUserId) |
| | 4 | 69 | | .ToListAsync(cancellationToken) |
| | 4 | 70 | | .ConfigureAwait(false); |
| | 4 | 71 | | } |
| | | 72 | | } |