feat: DirectorySearchOptions-Klasse hinzugefügt und DirectorySearchService refaktoriert, um IOptions für eine verbesserte Konfigurationsverwaltung zu verwenden.
This commit is contained in:
parent
ee045d5bfd
commit
0697f5ff58
@ -1,10 +1,9 @@
|
||||
using AutoMapper;
|
||||
using DigitalData.Core.Contracts.Application;
|
||||
using DigitalData.Core.Contracts.Infrastructure;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System.Configuration;
|
||||
|
||||
namespace DigitalData.Core.Application
|
||||
{
|
||||
@ -58,11 +57,28 @@ namespace DigitalData.Core.Application
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddDirectorySearchService(this IServiceCollection service)
|
||||
/// <summary>
|
||||
/// Adds the directory search service to the <see cref="IServiceCollection"/>.
|
||||
/// </summary>
|
||||
/// <param name="service">The <see cref="IServiceCollection"/> to add services to.</param>
|
||||
/// <param name="directorySearchOptions">
|
||||
/// Optional. An instance of <see cref="DirectorySearchOptions"/> to configure the directory search service.
|
||||
/// If not provided, the options need to be configured separately.
|
||||
/// </param>
|
||||
/// <returns>The updated <see cref="IServiceCollection"/>.</returns>
|
||||
/// <remarks>
|
||||
/// This method adds the necessary services for directory search functionality, including memory caching.
|
||||
/// If <paramref name="directorySearchOptions"/> is not provided, ensure to configure the options separately
|
||||
/// using the <see cref="IOptions{TOptions}"/> pattern.
|
||||
/// </remarks>
|
||||
public static IServiceCollection AddDirectorySearchService(this IServiceCollection service, DirectorySearchOptions? directorySearchOptions = null)
|
||||
{
|
||||
service.AddMemoryCache();
|
||||
service.AddScoped<IDirectorySearchService, DirectorySearchService>();
|
||||
return service;
|
||||
if(directorySearchOptions is not null)
|
||||
service.AddSingleton(Options.Create(directorySearchOptions));
|
||||
|
||||
return service
|
||||
.AddMemoryCache()
|
||||
.AddScoped<IDirectorySearchService, DirectorySearchService>();
|
||||
}
|
||||
|
||||
public static IServiceCollection AddJWTService<TClaimValue>(this IServiceCollection services, Func<TClaimValue, SecurityTokenDescriptor> tokenDescriptorFactory)
|
||||
|
||||
@ -6,12 +6,6 @@
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Remove="NewFolder\**" />
|
||||
<EmbeddedResource Remove="NewFolder\**" />
|
||||
<None Remove="NewFolder\**" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper" Version="13.0.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="7.0.0" />
|
||||
|
||||
13
DigitalData.Core.Application/DirectorySearchOptions.cs
Normal file
13
DigitalData.Core.Application/DirectorySearchOptions.cs
Normal file
@ -0,0 +1,13 @@
|
||||
namespace DigitalData.Core.Application
|
||||
{
|
||||
public class DirectorySearchOptions
|
||||
{
|
||||
public string? ServerName { get; init; }
|
||||
|
||||
public string? Root { get; init; }
|
||||
|
||||
public int UserCacheExpirationDays { get; init; }
|
||||
|
||||
public Dictionary<string, string> CustomSearchFilters { get; init; } = new();
|
||||
}
|
||||
}
|
||||
@ -1,12 +1,10 @@
|
||||
using DigitalData.Core.Contracts.Application;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.DirectoryServices;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using System.DirectoryServices.AccountManagement;
|
||||
using Microsoft.Extensions.Localization;
|
||||
using DigitalData.Core.DTO;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace DigitalData.Core.Application
|
||||
{
|
||||
@ -20,20 +18,21 @@ namespace DigitalData.Core.Application
|
||||
private readonly DateTimeOffset _userCacheExpiration;
|
||||
public Dictionary<string, string> CustomSearchFilters { get; }
|
||||
|
||||
public DirectorySearchService(IConfiguration configuration, ILogger<DirectorySearchService> logger, IMemoryCache memoryCache)
|
||||
public DirectorySearchService(IOptions<DirectorySearchOptions> options, IMemoryCache memoryCache)
|
||||
{
|
||||
_memoryCache = memoryCache;
|
||||
|
||||
ServerName = configuration["DirectorySearch:ServerName"] ?? throw new InvalidOperationException("The server name for directory search is not configured. Please specify the 'DirectorySearch:ServerName' in the configuration.");
|
||||
var dirSearchOptions = options.Value;
|
||||
|
||||
Root = configuration["DirectorySearch:Root"] ?? throw new InvalidOperationException("The root for directory search is not configured. Please specify the 'DirectorySearch:Root' in the configuration.");
|
||||
ServerName = dirSearchOptions.ServerName ?? throw new InvalidOperationException("The server name for directory search is not configured. Please specify the 'DirectorySearch:ServerName' in the configuration.");
|
||||
|
||||
Root = dirSearchOptions.Root ?? throw new InvalidOperationException("The root for directory search is not configured. Please specify the 'DirectorySearch:Root' in the configuration.");
|
||||
|
||||
SearchRootPath = $"LDAP://{ServerName}/{Root}";
|
||||
|
||||
var customSearchFiltersSection = configuration.GetSection("DirectorySearch:CustomSearchFilters");
|
||||
CustomSearchFilters = customSearchFiltersSection.Get<Dictionary<string, string>>() ?? new();
|
||||
CustomSearchFilters = dirSearchOptions.CustomSearchFilters;
|
||||
|
||||
var dayCounts = configuration.GetValue<int>("DirectorySearch:UserCacheExpirationDays");
|
||||
var dayCounts = dirSearchOptions.UserCacheExpirationDays;
|
||||
if (dayCounts == default)
|
||||
_userCacheExpiration = default;
|
||||
else
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user