From 0697f5ff58c0ee20f841123e9469ba1b5a8a1f66 Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Thu, 13 Jun 2024 15:44:57 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20DirectorySearchOptions-Klasse=20hinzuge?= =?UTF-8?q?f=C3=BCgt=20und=20DirectorySearchService=20refaktoriert,=20um?= =?UTF-8?q?=20IOptions=20f=C3=BCr=20eine=20verbesserte=20Konfigurationsver?= =?UTF-8?q?waltung=20zu=20verwenden.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DigitalData.Core.Application/DIExtensions.cs | 28 +++++++++++++++---- .../DigitalData.Core.Application.csproj | 6 ---- .../DirectorySearchOptions.cs | 13 +++++++++ .../DirectorySearchService.cs | 19 ++++++------- 4 files changed, 44 insertions(+), 22 deletions(-) create mode 100644 DigitalData.Core.Application/DirectorySearchOptions.cs diff --git a/DigitalData.Core.Application/DIExtensions.cs b/DigitalData.Core.Application/DIExtensions.cs index 2000121..31983f6 100644 --- a/DigitalData.Core.Application/DIExtensions.cs +++ b/DigitalData.Core.Application/DIExtensions.cs @@ -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) + /// + /// Adds the directory search service to the . + /// + /// The to add services to. + /// + /// Optional. An instance of to configure the directory search service. + /// If not provided, the options need to be configured separately. + /// + /// The updated . + /// + /// This method adds the necessary services for directory search functionality, including memory caching. + /// If is not provided, ensure to configure the options separately + /// using the pattern. + /// + public static IServiceCollection AddDirectorySearchService(this IServiceCollection service, DirectorySearchOptions? directorySearchOptions = null) { - service.AddMemoryCache(); - service.AddScoped(); - return service; + if(directorySearchOptions is not null) + service.AddSingleton(Options.Create(directorySearchOptions)); + + return service + .AddMemoryCache() + .AddScoped(); } public static IServiceCollection AddJWTService(this IServiceCollection services, Func tokenDescriptorFactory) diff --git a/DigitalData.Core.Application/DigitalData.Core.Application.csproj b/DigitalData.Core.Application/DigitalData.Core.Application.csproj index 30d2e29..c47f59a 100644 --- a/DigitalData.Core.Application/DigitalData.Core.Application.csproj +++ b/DigitalData.Core.Application/DigitalData.Core.Application.csproj @@ -6,12 +6,6 @@ enable - - - - - - diff --git a/DigitalData.Core.Application/DirectorySearchOptions.cs b/DigitalData.Core.Application/DirectorySearchOptions.cs new file mode 100644 index 0000000..9a1225c --- /dev/null +++ b/DigitalData.Core.Application/DirectorySearchOptions.cs @@ -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 CustomSearchFilters { get; init; } = new(); + } +} \ No newline at end of file diff --git a/DigitalData.Core.Application/DirectorySearchService.cs b/DigitalData.Core.Application/DirectorySearchService.cs index 9fe3321..a3494da 100644 --- a/DigitalData.Core.Application/DirectorySearchService.cs +++ b/DigitalData.Core.Application/DirectorySearchService.cs @@ -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 CustomSearchFilters { get; } - public DirectorySearchService(IConfiguration configuration, ILogger logger, IMemoryCache memoryCache) + public DirectorySearchService(IOptions 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."); - SearchRootPath = $"LDAP://{ServerName}/{Root}"; + Root = dirSearchOptions.Root ?? throw new InvalidOperationException("The root for directory search is not configured. Please specify the 'DirectorySearch:Root' in the configuration."); - var customSearchFiltersSection = configuration.GetSection("DirectorySearch:CustomSearchFilters"); - CustomSearchFilters = customSearchFiltersSection.Get>() ?? new(); + SearchRootPath = $"LDAP://{ServerName}/{Root}"; + + CustomSearchFilters = dirSearchOptions.CustomSearchFilters; - var dayCounts = configuration.GetValue("DirectorySearch:UserCacheExpirationDays"); + var dayCounts = dirSearchOptions.UserCacheExpirationDays; if (dayCounts == default) _userCacheExpiration = default; else