Kommentare zur Dokumentation hinzugefügt und Pakete konfiguriert.

This commit is contained in:
Developer 02
2024-06-20 16:20:50 +02:00
parent b7584a1632
commit 0ad92e7592
19 changed files with 588 additions and 56 deletions

View File

@@ -18,6 +18,14 @@ namespace DigitalData.Core.Application
private readonly DateTimeOffset _userCacheExpiration;
public Dictionary<string, string> CustomSearchFilters { get; }
/// <summary>
/// Initializes a new instance of the <see cref="DirectorySearchService"/> class.
/// </summary>
/// <param name="options">The options for directory search.</param>
/// <param name="memoryCache">The memory cache.</param>
/// <exception cref="InvalidOperationException">
/// Thrown if the server name or root directory is not configured.
/// </exception>
public DirectorySearchService(IOptions<DirectorySearchOptions> options, IMemoryCache memoryCache)
{
_memoryCache = memoryCache;
@@ -39,12 +47,27 @@ namespace DigitalData.Core.Application
_userCacheExpiration = DateTimeOffset.Now.Date.AddDays(dayCounts);
}
/// <summary>
/// Validates the credentials of a directory entry.
/// </summary>
/// <param name="dirEntryUsername">The directory entry username.</param>
/// <param name="dirEntryPassword">The directory entry password.</param>
/// <returns>True if the credentials are valid; otherwise, false.</returns>
public bool ValidateCredentials(string dirEntryUsername, string dirEntryPassword)
{
using var context = new PrincipalContext(ContextType.Domain, ServerName, Root);
return context.ValidateCredentials(dirEntryUsername, dirEntryPassword);
}
/// <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>
public DataResult<IEnumerable<ResultPropertyCollection>> FindAll(DirectoryEntry searchRoot, string filter, SearchScope searchScope = SearchScope.Subtree, int sizeLimit = 5000, params string[] properties)
{
List<ResultPropertyCollection> list = new();
@@ -74,10 +97,17 @@ namespace DigitalData.Core.Application
return Result.Success<IEnumerable<ResultPropertyCollection>>(list);
}
/// <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>
public DataResult<IEnumerable<ResultPropertyCollection>> FindAllByUserCache(string username, string filter, SearchScope searchScope = SearchScope.Subtree, int sizeLimit = 5000, params string[] properties)
{
List<ResultPropertyCollection> list = new();
_memoryCache.TryGetValue(username, out DirectoryEntry? searchRoot);
if (searchRoot is null)
@@ -86,6 +116,11 @@ namespace DigitalData.Core.Application
return FindAll(searchRoot, filter, searchScope, sizeLimit, properties);
}
/// <summary>
/// Sets the search root in the cache.
/// </summary>
/// <param name="dirEntryUsername">The directory entry username.</param>
/// <param name="dirEntryPassword">The directory entry password.</param>
public void SetSearchRootCache(string dirEntryUsername, string dirEntryPassword)
{
if (_userCacheExpiration == default)
@@ -94,6 +129,12 @@ namespace DigitalData.Core.Application
_memoryCache.Set(key: dirEntryUsername, new DirectoryEntry(path: SearchRootPath, username: dirEntryUsername, password: dirEntryPassword), absoluteExpiration: _userCacheExpiration);
}
/// <summary>
/// Gets the search root from the cache.
/// </summary>
/// <param name="dirEntryUsername">The directory entry username.</param>
/// <returns>The cached <see cref="DirectoryEntry"/> if found; otherwise, null.</returns>
public DirectoryEntry? GetSearchRootCache(string dirEntryUsername)
{
_memoryCache.TryGetValue(dirEntryUsername, out DirectoryEntry? root);