|
|
|
|
@@ -6,9 +6,13 @@ using System.DirectoryServices;
|
|
|
|
|
|
|
|
|
|
namespace DigitalData.ActiveDirectory;
|
|
|
|
|
|
|
|
|
|
public record DirectorySearchQuery(string? Filter = null, SearchScope Scope = SearchScope.Subtree, int SizeLimit = 5000, params string[] properties)
|
|
|
|
|
public record DirectorySearchQuery(string? Filter = null, SearchScope Scope = SearchScope.Subtree, int SizeLimit = 5000, params string[] Property)
|
|
|
|
|
: IRequest<IEnumerable<ResultPropertyCollection>>
|
|
|
|
|
{
|
|
|
|
|
public record Root(string Path, string? Username = null, string? Password = null);
|
|
|
|
|
|
|
|
|
|
public Root? SearchRoot { get; set; }
|
|
|
|
|
|
|
|
|
|
public Action<DirectorySearcher>? AfterInit { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -25,13 +29,6 @@ public class DirectorySearchQueryHandler : IRequestHandler<DirectorySearchQuery,
|
|
|
|
|
_mapper = mapper;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DirectoryEntry DirectoryEntry => new ()
|
|
|
|
|
{
|
|
|
|
|
Path = _options.Value.Path,
|
|
|
|
|
Username = _options.Value.Username,
|
|
|
|
|
Password = _options.Value.Password
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//TODO: add resolver to handle SearchRoot and AfterInit mapping
|
|
|
|
|
public Task<IEnumerable<ResultPropertyCollection>> Handle(DirectorySearchQuery request, CancellationToken cancellationToken = default)
|
|
|
|
|
{
|
|
|
|
|
@@ -39,14 +36,19 @@ public class DirectorySearchQueryHandler : IRequestHandler<DirectorySearchQuery,
|
|
|
|
|
{
|
|
|
|
|
using var searcher = _mapper.Map<DirectorySearcher>(request);
|
|
|
|
|
|
|
|
|
|
searcher.SearchRoot = DirectoryEntry;
|
|
|
|
|
searcher.SearchRoot ??= new()
|
|
|
|
|
{
|
|
|
|
|
Path = _options.Value.Path,
|
|
|
|
|
Username = _options.Value.Username,
|
|
|
|
|
Password = _options.Value.Password
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
request.AfterInit?.Invoke(searcher);
|
|
|
|
|
|
|
|
|
|
if (request.properties.Length > 0)
|
|
|
|
|
if (request.Property.Length > 0)
|
|
|
|
|
searcher.PropertiesToLoad.Clear();
|
|
|
|
|
|
|
|
|
|
searcher.PropertiesToLoad.AddRange(request.properties.Where(p => p is not null).ToArray());
|
|
|
|
|
searcher.PropertiesToLoad.AddRange(request.Property.Where(p => p is not null).ToArray());
|
|
|
|
|
var res = searcher.FindAll().Cast<SearchResult>().Select(r => r.Properties);
|
|
|
|
|
return res.Any() ? res : throw new NotFoundException();
|
|
|
|
|
});
|
|
|
|
|
|