feat: DirectorySearchOptions-Klasse hinzugefügt und DirectorySearchService refaktoriert, um IOptions für eine verbesserte Konfigurationsverwaltung zu verwenden.

This commit is contained in:
Developer 02 2024-06-13 15:44:57 +02:00
parent ee045d5bfd
commit 0697f5ff58
4 changed files with 44 additions and 22 deletions

View File

@ -1,10 +1,9 @@
using AutoMapper; using AutoMapper;
using DigitalData.Core.Contracts.Application; using DigitalData.Core.Contracts.Application;
using DigitalData.Core.Contracts.Infrastructure; using DigitalData.Core.Contracts.Infrastructure;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens; using Microsoft.IdentityModel.Tokens;
using System.Configuration;
namespace DigitalData.Core.Application namespace DigitalData.Core.Application
{ {
@ -58,11 +57,28 @@ namespace DigitalData.Core.Application
return services; 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(); if(directorySearchOptions is not null)
service.AddScoped<IDirectorySearchService, DirectorySearchService>(); service.AddSingleton(Options.Create(directorySearchOptions));
return service;
return service
.AddMemoryCache()
.AddScoped<IDirectorySearchService, DirectorySearchService>();
} }
public static IServiceCollection AddJWTService<TClaimValue>(this IServiceCollection services, Func<TClaimValue, SecurityTokenDescriptor> tokenDescriptorFactory) public static IServiceCollection AddJWTService<TClaimValue>(this IServiceCollection services, Func<TClaimValue, SecurityTokenDescriptor> tokenDescriptorFactory)

View File

@ -6,12 +6,6 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<Compile Remove="NewFolder\**" />
<EmbeddedResource Remove="NewFolder\**" />
<None Remove="NewFolder\**" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="AutoMapper" Version="13.0.1" /> <PackageReference Include="AutoMapper" Version="13.0.1" />
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="7.0.0" /> <PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="7.0.0" />

View 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();
}
}

View File

@ -1,12 +1,10 @@
using DigitalData.Core.Contracts.Application; using DigitalData.Core.Contracts.Application;
using Microsoft.Extensions.Logging;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.DirectoryServices; using System.DirectoryServices;
using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Configuration;
using System.DirectoryServices.AccountManagement; using System.DirectoryServices.AccountManagement;
using Microsoft.Extensions.Localization;
using DigitalData.Core.DTO; using DigitalData.Core.DTO;
using Microsoft.Extensions.Options;
namespace DigitalData.Core.Application namespace DigitalData.Core.Application
{ {
@ -20,20 +18,21 @@ namespace DigitalData.Core.Application
private readonly DateTimeOffset _userCacheExpiration; private readonly DateTimeOffset _userCacheExpiration;
public Dictionary<string, string> CustomSearchFilters { get; } public Dictionary<string, string> CustomSearchFilters { get; }
public DirectorySearchService(IConfiguration configuration, ILogger<DirectorySearchService> logger, IMemoryCache memoryCache) public DirectorySearchService(IOptions<DirectorySearchOptions> options, IMemoryCache memoryCache)
{ {
_memoryCache = 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}"; SearchRootPath = $"LDAP://{ServerName}/{Root}";
CustomSearchFilters = dirSearchOptions.CustomSearchFilters;
var customSearchFiltersSection = configuration.GetSection("DirectorySearch:CustomSearchFilters"); var dayCounts = dirSearchOptions.UserCacheExpirationDays;
CustomSearchFilters = customSearchFiltersSection.Get<Dictionary<string, string>>() ?? new();
var dayCounts = configuration.GetValue<int>("DirectorySearch:UserCacheExpirationDays");
if (dayCounts == default) if (dayCounts == default)
_userCacheExpiration = default; _userCacheExpiration = default;
else else