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."); + + 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}"; + + CustomSearchFilters = dirSearchOptions.CustomSearchFilters; - var customSearchFiltersSection = configuration.GetSection("DirectorySearch:CustomSearchFilters"); - CustomSearchFilters = customSearchFiltersSection.Get>() ?? new(); - - var dayCounts = configuration.GetValue("DirectorySearch:UserCacheExpirationDays"); + var dayCounts = dirSearchOptions.UserCacheExpirationDays; if (dayCounts == default) _userCacheExpiration = default; else