Compare commits
2 Commits
cb28ce39a1
...
0697f5ff58
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0697f5ff58 | ||
|
|
ee045d5bfd |
@@ -27,10 +27,9 @@ namespace DigitalData.Core.Application
|
|||||||
/// Initializes a new instance of the BasicCRUDService with the specified repository, translation service, and AutoMapper configuration.
|
/// Initializes a new instance of the BasicCRUDService with the specified repository, translation service, and AutoMapper configuration.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="repository">The CRUD repository for accessing and manipulating entity data.</param>
|
/// <param name="repository">The CRUD repository for accessing and manipulating entity data.</param>
|
||||||
/// <param name="translationService">The service used for key-based text translations, facilitating localization.</param>
|
|
||||||
/// <param name="mapper">The AutoMapper instance for mapping between DTOs and entities.</param>
|
/// <param name="mapper">The AutoMapper instance for mapping between DTOs and entities.</param>
|
||||||
public BasicCRUDService(TCRUDRepository repository, IStringLocalizer defaultLocalizer, IMapper mapper) :
|
public BasicCRUDService(TCRUDRepository repository, IMapper mapper) :
|
||||||
base(repository, defaultLocalizer, mapper)
|
base(repository, mapper)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ using DigitalData.Core.Contracts.Infrastructure;
|
|||||||
using AutoMapper;
|
using AutoMapper;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.ComponentModel.DataAnnotations;
|
using System.ComponentModel.DataAnnotations;
|
||||||
using Microsoft.Extensions.Localization;
|
|
||||||
using DigitalData.Core.DTO;
|
using DigitalData.Core.DTO;
|
||||||
|
|
||||||
namespace DigitalData.Core.Application
|
namespace DigitalData.Core.Application
|
||||||
@@ -22,23 +21,19 @@ namespace DigitalData.Core.Application
|
|||||||
protected readonly TCRUDRepository _repository;
|
protected readonly TCRUDRepository _repository;
|
||||||
protected readonly IMapper _mapper;
|
protected readonly IMapper _mapper;
|
||||||
protected readonly PropertyInfo? _keyPropertyInfo;
|
protected readonly PropertyInfo? _keyPropertyInfo;
|
||||||
protected readonly IStringLocalizer _localizer;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the CRUDService class with the specified repository, translation service, and mapper.
|
/// Initializes a new instance of the CRUDService class with the specified repository, translation service, and mapper.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="repository">The CRUD repository for accessing the database.</param>
|
/// <param name="repository">The CRUD repository for accessing the database.</param>
|
||||||
/// <param name="localizer">The localizer for translating messages based on culture.</param>
|
|
||||||
/// <param name="mapper">The AutoMapper instance for mapping between DTOs and entity objects.</param>
|
/// <param name="mapper">The AutoMapper instance for mapping between DTOs and entity objects.</param>
|
||||||
public CRUDService(TCRUDRepository repository, IStringLocalizer localizer, IMapper mapper)
|
public CRUDService(TCRUDRepository repository, IMapper mapper)
|
||||||
{
|
{
|
||||||
_repository = repository;
|
_repository = repository;
|
||||||
_mapper = mapper;
|
_mapper = mapper;
|
||||||
|
|
||||||
_keyPropertyInfo = typeof(TEntity).GetProperties()
|
_keyPropertyInfo = typeof(TEntity).GetProperties()
|
||||||
.FirstOrDefault(prop => Attribute.IsDefined(prop, typeof(KeyAttribute)));
|
.FirstOrDefault(prop => Attribute.IsDefined(prop, typeof(KeyAttribute)));
|
||||||
|
|
||||||
_localizer = localizer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -62,7 +57,7 @@ namespace DigitalData.Core.Application
|
|||||||
{
|
{
|
||||||
var entity = await _repository.ReadByIdAsync(id);
|
var entity = await _repository.ReadByIdAsync(id);
|
||||||
return entity is null
|
return entity is null
|
||||||
? Result.Fail<TReadDto>().Message(_localizer[Key.EntityDoesNotExist])
|
? Result.Fail<TReadDto>()
|
||||||
: Result.Success(_mapper.MapOrThrow<TReadDto>(entity));
|
: Result.Success(_mapper.MapOrThrow<TReadDto>(entity));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,7 +81,7 @@ namespace DigitalData.Core.Application
|
|||||||
{
|
{
|
||||||
var entity = _mapper.MapOrThrow<TEntity>(updateDto);
|
var entity = _mapper.MapOrThrow<TEntity>(updateDto);
|
||||||
bool isUpdated = await _repository.UpdateAsync(entity);
|
bool isUpdated = await _repository.UpdateAsync(entity);
|
||||||
return isUpdated ? Result.Success() : Result.Fail().Message(_localizer[Key.UpdateFailed]);
|
return isUpdated ? Result.Success() : Result.Fail();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -99,10 +94,10 @@ namespace DigitalData.Core.Application
|
|||||||
TEntity? entity = await _repository.ReadByIdAsync(id);
|
TEntity? entity = await _repository.ReadByIdAsync(id);
|
||||||
|
|
||||||
if (entity is null)
|
if (entity is null)
|
||||||
return Result.Fail().Message(_localizer[Key.DeletionFailed], _localizer[Key.EntityDoesNotExist]);
|
return Result.Fail();
|
||||||
|
|
||||||
bool isDeleted = await _repository.DeleteAsync(entity);
|
bool isDeleted = await _repository.DeleteAsync(entity);
|
||||||
return isDeleted ? Result.Success() : Result.Fail().Message(_localizer[Key.DeletionFailed]);
|
return isDeleted ? Result.Success() : Result.Fail();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|||||||
@@ -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" />
|
||||||
|
|||||||
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 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
|
||||||
{
|
{
|
||||||
@@ -19,28 +17,26 @@ namespace DigitalData.Core.Application
|
|||||||
public string SearchRootPath { get; }
|
public string SearchRootPath { get; }
|
||||||
private readonly DateTimeOffset _userCacheExpiration;
|
private readonly DateTimeOffset _userCacheExpiration;
|
||||||
public Dictionary<string, string> CustomSearchFilters { get; }
|
public Dictionary<string, string> CustomSearchFilters { get; }
|
||||||
protected readonly IStringLocalizer _localizer;
|
|
||||||
|
|
||||||
public DirectorySearchService(IConfiguration configuration, ILogger<DirectorySearchService> logger, IMemoryCache memoryCache, IStringLocalizer localizer)
|
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}";
|
||||||
|
|
||||||
var customSearchFiltersSection = configuration.GetSection("DirectorySearch:CustomSearchFilters");
|
CustomSearchFilters = dirSearchOptions.CustomSearchFilters;
|
||||||
CustomSearchFilters = customSearchFiltersSection.Get<Dictionary<string, string>>() ?? new();
|
|
||||||
|
|
||||||
var dayCounts = configuration.GetValue<int>("DirectorySearch:UserCacheExpirationDays");
|
var dayCounts = dirSearchOptions.UserCacheExpirationDays;
|
||||||
if (dayCounts == default)
|
if (dayCounts == default)
|
||||||
_userCacheExpiration = default;
|
_userCacheExpiration = default;
|
||||||
else
|
else
|
||||||
_userCacheExpiration = DateTimeOffset.Now.Date.AddDays(dayCounts);
|
_userCacheExpiration = DateTimeOffset.Now.Date.AddDays(dayCounts);
|
||||||
|
|
||||||
_localizer = localizer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool ValidateCredentials(string dirEntryUsername, string dirEntryPassword)
|
public bool ValidateCredentials(string dirEntryUsername, string dirEntryPassword)
|
||||||
@@ -85,7 +81,7 @@ namespace DigitalData.Core.Application
|
|||||||
_memoryCache.TryGetValue(username, out DirectoryEntry? searchRoot);
|
_memoryCache.TryGetValue(username, out DirectoryEntry? searchRoot);
|
||||||
|
|
||||||
if (searchRoot is null)
|
if (searchRoot is null)
|
||||||
return Result.Fail<IEnumerable<ResultPropertyCollection>>().Message(_localizer[Key.DirSearcherDisconnected]);
|
return Result.Fail<IEnumerable<ResultPropertyCollection>>();
|
||||||
|
|
||||||
return FindAll(searchRoot, filter, searchScope, sizeLimit, properties);
|
return FindAll(searchRoot, filter, searchScope, sizeLimit, properties);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user