refactor(DirectorySearchService): Methoden asynchron gemacht

This commit is contained in:
Developer 02 2025-01-17 11:24:30 +01:00
parent 92a7b959ab
commit 90c85814b0
2 changed files with 122 additions and 15 deletions

View File

@ -13,14 +13,82 @@ namespace DigitalData.Core.Abstractions.Application
Dictionary<string, string> CustomSearchFilters { get; } Dictionary<string, string> CustomSearchFilters { get; }
bool ValidateCredentials(string dirEntryUsername, string dirEntryPassword); /// <summary>
/// Creates the connections to the server and returns a Boolean value that specifies
/// whether the specified username and password are valid.
/// </summary>
/// <param name="userName">The username that is validated on the server. See the Remarks section
/// for more information on the format of userName.</param>
/// <param name="password">The password that is validated on the server.</param>
/// <returns>True if the credentials are valid; otherwise, false.</returns>
bool ValidateCredentials(string userName, string password);
/// <summary>
/// Creates the connections to the server asynchronously and returns a Boolean value that specifies
/// whether the specified username and password are valid.
/// </summary>
/// <param name="userName">The username that is validated on the server. See the Remarks section
/// for more information on the format of userName.</param>
/// <param name="password">The password that is validated on the server.</param>
/// <returns>True if the credentials are valid; otherwise, false.</returns>
Task<bool> ValidateCredentialsAsync(string userName, string password);
/// <summary>
/// Finds all directory entries matching the specified filter.
/// </summary>
/// <param name="searchRoot">The search root.</param>
/// <param name="filter">The search filter.</param>
/// <param name="searchScope">The search scope.</param>
/// <param name="sizeLimit">The size limit.</param>
/// <param name="properties">The properties to load.</param>
/// <returns>A <see cref="DataResult{T}"/> containing the results.</returns>
DataResult<IEnumerable<ResultPropertyCollection>> FindAll(DirectoryEntry searchRoot, string filter, SearchScope searchScope = SearchScope.Subtree, int sizeLimit = 5000, params string[] properties); DataResult<IEnumerable<ResultPropertyCollection>> FindAll(DirectoryEntry searchRoot, string filter, SearchScope searchScope = SearchScope.Subtree, int sizeLimit = 5000, params string[] properties);
/// <summary>
/// Finds all directory entries matching the specified filter asynchronously.
/// </summary>
/// <param name="searchRoot">The search root.</param>
/// <param name="filter">The search filter.</param>
/// <param name="searchScope">The search scope.</param>
/// <param name="sizeLimit">The size limit.</param>
/// <param name="properties">The properties to load.</param>
/// <returns>A <see cref="DataResult{T}"/> containing the results.</returns>
Task<DataResult<IEnumerable<ResultPropertyCollection>>> FindAllAsync(DirectoryEntry searchRoot, string filter, SearchScope searchScope = SearchScope.Subtree, int sizeLimit = 5000, params string[] properties);
/// <summary>
/// Finds all directory entries matching the specified filter, using the user cache.
/// </summary>
/// <param name="username">The username.</param>
/// <param name="filter">The search filter.</param>
/// <param name="searchScope">The search scope.</param>
/// <param name="sizeLimit">The size limit.</param>
/// <param name="properties">The properties to load.</param>
/// <returns>A <see cref="DataResult{T}"/> containing the results.</returns>
DataResult<IEnumerable<ResultPropertyCollection>> FindAllByUserCache(string username, string filter, SearchScope searchScope = SearchScope.Subtree, int sizeLimit = 5000, params string[] properties); DataResult<IEnumerable<ResultPropertyCollection>> FindAllByUserCache(string username, string filter, SearchScope searchScope = SearchScope.Subtree, int sizeLimit = 5000, params string[] properties);
/// <summary>
/// Finds all directory entries matching the specified filter asynchronously, using the user cache.
/// </summary>
/// <param name="username">The username.</param>
/// <param name="filter">The search filter.</param>
/// <param name="searchScope">The search scope.</param>
/// <param name="sizeLimit">The size limit.</param>
/// <param name="properties">The properties to load.</param>
/// <returns>A <see cref="DataResult{T}"/> containing the results.</returns>
Task<DataResult<IEnumerable<ResultPropertyCollection>>> FindAllByUserCacheAsync(string username, string filter, SearchScope searchScope = SearchScope.Subtree, int sizeLimit = 5000, params string[] properties);
/// <summary>
/// Sets the search root in the cache.
/// </summary>
/// <param name="username">The directory entry username.</param>
/// <param name="password">The directory entry password.</param>
void SetSearchRootCache(string username, string password); void SetSearchRootCache(string username, string password);
/// <summary>
/// Gets the search root from the cache.
/// </summary>
/// <param name="username">The directory entry username.</param>
/// <returns>The cached <see cref="DirectoryEntry"/> if found; otherwise, null.</returns>
DirectoryEntry? GetSearchRootCache(string username); DirectoryEntry? GetSearchRootCache(string username);
} }
} }

View File

@ -8,6 +8,7 @@ using Microsoft.Extensions.Options;
namespace DigitalData.Core.Application namespace DigitalData.Core.Application
{ {
//TODO: rename as DirectorySearcher
[SuppressMessage("Interoperability", "CA1416:Validate platform compatibility", Justification = "<Pending>")] [SuppressMessage("Interoperability", "CA1416:Validate platform compatibility", Justification = "<Pending>")]
public class DirectorySearchService : IDirectorySearchService public class DirectorySearchService : IDirectorySearchService
{ {
@ -45,17 +46,31 @@ namespace DigitalData.Core.Application
} }
/// <summary> /// <summary>
/// Validates the credentials of a directory entry. /// Creates the connections to the server and returns a Boolean value that specifies
/// whether the specified username and password are valid.
/// </summary> /// </summary>
/// <param name="dirEntryUsername">The directory entry username.</param> /// <param name="userName">The username that is validated on the server. See the Remarks section
/// <param name="dirEntryPassword">The directory entry password.</param> /// for more information on the format of userName.</param>
/// <param name="password">The password that is validated on the server.</param>
/// <returns>True if the credentials are valid; otherwise, false.</returns> /// <returns>True if the credentials are valid; otherwise, false.</returns>
public bool ValidateCredentials(string dirEntryUsername, string dirEntryPassword) public bool ValidateCredentials(string userName, string password)
{ {
using var context = new PrincipalContext(ContextType.Domain, ServerName, Root); using var context = new PrincipalContext(ContextType.Domain, ServerName, Root);
return context.ValidateCredentials(dirEntryUsername, dirEntryPassword); return context.ValidateCredentials(userName, password);
} }
/// <summary>
/// Creates the connections to the server asynchronously and returns a Boolean value that specifies
/// whether the specified username and password are valid.
/// </summary>
/// <param name="userName">The username that is validated on the server. See the Remarks section
/// for more information on the format of userName.</param>
/// <param name="password">The password that is validated on the server.</param>
/// <returns>True if the credentials are valid; otherwise, false.</returns>
public Task<bool> ValidateCredentialsAsync(string userName, string password) => Task.Run(()
=> ValidateCredentials(userName, password));
//TODO: remove unnecessary DataResult
/// <summary> /// <summary>
/// Finds all directory entries matching the specified filter. /// Finds all directory entries matching the specified filter.
/// </summary> /// </summary>
@ -69,7 +84,7 @@ namespace DigitalData.Core.Application
{ {
List<ResultPropertyCollection> list = new(); List<ResultPropertyCollection> list = new();
var searcher = new DirectorySearcher() using var searcher = new DirectorySearcher()
{ {
Filter = filter, Filter = filter,
SearchScope = searchScope, SearchScope = searchScope,
@ -94,6 +109,18 @@ namespace DigitalData.Core.Application
return Result.Success<IEnumerable<ResultPropertyCollection>>(list); return Result.Success<IEnumerable<ResultPropertyCollection>>(list);
} }
/// <summary>
/// Finds all directory entries matching the specified filter asynchronously.
/// </summary>
/// <param name="searchRoot">The search root.</param>
/// <param name="filter">The search filter.</param>
/// <param name="searchScope">The search scope.</param>
/// <param name="sizeLimit">The size limit.</param>
/// <param name="properties">The properties to load.</param>
/// <returns>A <see cref="DataResult{T}"/> containing the results.</returns>
public Task<DataResult<IEnumerable<ResultPropertyCollection>>> FindAllAsync(DirectoryEntry searchRoot, string filter, SearchScope searchScope = SearchScope.Subtree, int sizeLimit = 5000, params string[] properties) => Task.Run(()
=> FindAll(searchRoot, filter, searchScope, sizeLimit, properties));
/// <summary> /// <summary>
/// Finds all directory entries matching the specified filter, using the user cache. /// Finds all directory entries matching the specified filter, using the user cache.
/// </summary> /// </summary>
@ -113,27 +140,39 @@ namespace DigitalData.Core.Application
return FindAll(searchRoot, filter, searchScope, sizeLimit, properties); return FindAll(searchRoot, filter, searchScope, sizeLimit, properties);
} }
/// <summary>
/// Finds all directory entries matching the specified filter asynchronously, using the user cache.
/// </summary>
/// <param name="username">The username.</param>
/// <param name="filter">The search filter.</param>
/// <param name="searchScope">The search scope.</param>
/// <param name="sizeLimit">The size limit.</param>
/// <param name="properties">The properties to load.</param>
/// <returns>A <see cref="DataResult{T}"/> containing the results.</returns>
public Task<DataResult<IEnumerable<ResultPropertyCollection>>> FindAllByUserCacheAsync(string username, string filter, SearchScope searchScope = SearchScope.Subtree, int sizeLimit = 5000, params string[] properties) => Task.Run(()
=> FindAllByUserCache(username, filter, searchScope, sizeLimit, properties));
/// <summary> /// <summary>
/// Sets the search root in the cache. /// Sets the search root in the cache.
/// </summary> /// </summary>
/// <param name="dirEntryUsername">The directory entry username.</param> /// <param name="username">The directory entry username.</param>
/// <param name="dirEntryPassword">The directory entry password.</param> /// <param name="password">The directory entry password.</param>
public void SetSearchRootCache(string dirEntryUsername, string dirEntryPassword) public void SetSearchRootCache(string username, string password)
{ {
if (_userCacheExpiration is DateTimeOffset cacheExpiration) if (_userCacheExpiration is DateTimeOffset cacheExpiration)
_memoryCache.Set(key: dirEntryUsername, new DirectoryEntry(path: SearchRootPath, username: dirEntryUsername, password: dirEntryPassword), absoluteExpiration: cacheExpiration); _memoryCache.Set(key: username, new DirectoryEntry(path: SearchRootPath, username: username, password: password), absoluteExpiration: cacheExpiration);
else else
_memoryCache.Set(key: dirEntryUsername, new DirectoryEntry(path: SearchRootPath, username: dirEntryUsername, password: dirEntryPassword)); _memoryCache.Set(key: username, new DirectoryEntry(path: SearchRootPath, username: username, password: password));
} }
/// <summary> /// <summary>
/// Gets the search root from the cache. /// Gets the search root from the cache.
/// </summary> /// </summary>
/// <param name="dirEntryUsername">The directory entry username.</param> /// <param name="username">The directory entry username.</param>
/// <returns>The cached <see cref="DirectoryEntry"/> if found; otherwise, null.</returns> /// <returns>The cached <see cref="DirectoryEntry"/> if found; otherwise, null.</returns>
public DirectoryEntry? GetSearchRootCache(string dirEntryUsername) public DirectoryEntry? GetSearchRootCache(string username)
{ {
_memoryCache.TryGetValue(dirEntryUsername, out DirectoryEntry? root); _memoryCache.TryGetValue(username, out DirectoryEntry? root);
return root; return root;
} }
} }