refactor(DirectoryController): Ersetzen der Benutzersuche im Cache durch direkten DirectoryEntry aus Optionen
- Hinzufügen der Abhängigkeitsinjektion `IOptions<DirSearchRoot>`, um den vorkonfigurierten DirectoryEntry-Stamm zu verwenden. - Einführung des Feldes `_dirSearchRoot` für wiederverwendbare LDAP-Root-Bindung. - Ersetzen der Aufrufe von `FindAllByUserCache` durch `FindAll` unter Verwendung von `DirectoryEntry` aus `_dirSearchRoot`. - Entfernen der veralteten Endpunkte `GetRootOf` und `CreateSearchRoot`. - Sicherstellen des Musters `using var sRoot` für die ordnungsgemäße Entsorgung von DirectoryEntry-Instanzen.
This commit is contained in:
parent
f5471a8d01
commit
525a30b541
@ -9,6 +9,8 @@ using Microsoft.Extensions.Localization;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using DigitalData.Core.Abstraction.Application;
|
||||
using DigitalData.Core.Abstraction.Application.DTO;
|
||||
using Microsoft.Extensions.Options;
|
||||
using DigitalData.UserManager.API.Models;
|
||||
|
||||
namespace DigitalData.UserManager.API.Controllers;
|
||||
|
||||
@ -23,8 +25,9 @@ public class DirectoryController : ControllerBase
|
||||
private readonly Dictionary<string, string> _customSearchFilters;
|
||||
private readonly IStringLocalizer<Resource> _localizer;
|
||||
private readonly ILogger<DirectoryController> _logger;
|
||||
private readonly DirSearchRoot _dirSearchRoot;
|
||||
|
||||
public DirectoryController(IConfiguration configuration, IStringLocalizer<Resource> localizer, IUserService userService, IDirectorySearchService directorySearchService, ILogger<DirectoryController> logger)
|
||||
public DirectoryController(IConfiguration configuration, IStringLocalizer<Resource> localizer, IUserService userService, IDirectorySearchService directorySearchService, ILogger<DirectoryController> logger, IOptions<DirSearchRoot> dirSearchRootOptions)
|
||||
{
|
||||
_localizer = localizer;
|
||||
_userService = userService;
|
||||
@ -33,23 +36,7 @@ public class DirectoryController : ControllerBase
|
||||
var customSearchFiltersSection = configuration.GetSection("DirectorySearch:CustomSearchFilters");
|
||||
_customSearchFilters = customSearchFiltersSection.Get<Dictionary<string, string>>() ?? new();
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[HttpGet("Root/{username}")]
|
||||
public IActionResult GetRootOf(string username)
|
||||
{
|
||||
var root = _dirSearchService.GetSearchRootCache(username);
|
||||
|
||||
return root is null ? NotFound() : Ok(new
|
||||
{
|
||||
guid = root.Guid,
|
||||
nativeGuid = root.NativeGuid,
|
||||
name = root.Name,
|
||||
path = root.Path,
|
||||
parentPath = root.Parent?.Path,
|
||||
username = root.Username,
|
||||
schemaClassName = root.SchemaClassName
|
||||
});
|
||||
_dirSearchRoot = dirSearchRootOptions.Value;
|
||||
}
|
||||
|
||||
[HttpGet("CustomSearchFilter")]
|
||||
@ -66,26 +53,6 @@ public class DirectoryController : ControllerBase
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> CreateSearchRoot([FromBody] SearchRootCreateDto searchRootCreateDto)
|
||||
{
|
||||
var dirEntryUsername = searchRootCreateDto.Username ?? CurrentUser;
|
||||
if (dirEntryUsername is null)
|
||||
return Unauthorized();
|
||||
|
||||
bool isValid = _dirSearchService.ValidateCredentials(dirEntryUsername, searchRootCreateDto.Password);
|
||||
|
||||
if (!isValid)
|
||||
return Unauthorized(Result.Fail().Message(_localizer[Key.UserNotFound]));
|
||||
|
||||
var userResult = await _userService.ReadByUsernameAsync(dirEntryUsername);
|
||||
if (!userResult.IsSuccess || userResult.Data is null)
|
||||
return Unauthorized(Result.Fail().Message(_localizer[Key.UserNotFoundInLocalDB]));
|
||||
|
||||
_dirSearchService.SetSearchRootCache(userResult.Data.Username, searchRootCreateDto.Password);
|
||||
return Ok();
|
||||
}
|
||||
|
||||
[HttpGet("SearchByFilter/{filter}")]
|
||||
public IActionResult SearchByFilter([FromRoute] string filter, string? dirEntryUsername, params string[] propName)
|
||||
{
|
||||
@ -94,7 +61,9 @@ public class DirectoryController : ControllerBase
|
||||
if (dirEntryUsername is null)
|
||||
return Unauthorized();
|
||||
|
||||
return _dirSearchService.FindAllByUserCache(dirEntryUsername, filter, properties: propName).Then(Ok, IActionResult (m, n) =>
|
||||
using var sRoot = _dirSearchRoot.ToDirectoryEntry;
|
||||
|
||||
return _dirSearchService.FindAll(sRoot, filter, properties: propName).Then(Ok, IActionResult (m, n) =>
|
||||
{
|
||||
_logger.LogNotice(n);
|
||||
return StatusCode(StatusCodes.Status424FailedDependency);
|
||||
@ -114,7 +83,9 @@ public class DirectoryController : ControllerBase
|
||||
if (filter is null)
|
||||
return NotFound($"The filter named {filterName} does not exist.");
|
||||
|
||||
return _dirSearchService.FindAllByUserCache(dirEntryUsername, filter, properties: propName).Then(Ok, IActionResult (m, n) =>
|
||||
using var sRoot = _dirSearchRoot.ToDirectoryEntry;
|
||||
|
||||
return _dirSearchService.FindAll(sRoot, filter, properties: propName).Then(Ok, IActionResult (m, n) =>
|
||||
{
|
||||
_logger.LogNotice(n);
|
||||
return StatusCode(StatusCodes.Status424FailedDependency);
|
||||
@ -135,7 +106,9 @@ public class DirectoryController : ControllerBase
|
||||
if (filter is null)
|
||||
throw new InvalidOperationException("The LDAP Group Search filter configuration is missing in your appsettings. Please ensure it's added under DirectorySearch:CustomSearchFilters:Group to enable group searches.");
|
||||
|
||||
return _dirSearchService.FindAllByUserCache(username: dirEntryUsername, filter, properties: propName).Then(Ok, IActionResult (m, n) =>
|
||||
using var sRoot = _dirSearchRoot.ToDirectoryEntry;
|
||||
|
||||
return _dirSearchService.FindAll(_dirSearchRoot.ToDirectoryEntry, filter, properties: propName).Then(Ok, IActionResult (m, n) =>
|
||||
{
|
||||
_logger.LogNotice(n);
|
||||
return StatusCode(StatusCodes.Status424FailedDependency);
|
||||
@ -156,7 +129,9 @@ public class DirectoryController : ControllerBase
|
||||
if (filter is null)
|
||||
throw new InvalidOperationException("The LDAP User Search filter configuration is missing in your appsettings. Please ensure it's added under DirectorySearch:CustomSearchFilters:User to enable group searches.");
|
||||
|
||||
return _dirSearchService.FindAllByUserCache(username: dirEntryUsername, filter, properties: propName).Then(
|
||||
using var sRoot = _dirSearchRoot.ToDirectoryEntry;
|
||||
|
||||
return _dirSearchService.FindAll(sRoot, filter, properties: propName).Then(
|
||||
Success: data =>
|
||||
{
|
||||
if (groupName is not null)
|
||||
|
||||
@ -15,7 +15,6 @@
|
||||
"Root": "DC=dd-gan,DC=local,DC=digitaldata,DC=works",
|
||||
"Username": "FABRIK19-User01",
|
||||
"Password": "9bWOr0UGuHn_7VkC",
|
||||
"UserCacheExpirationDays": 1,
|
||||
"CustomSearchFilters": {
|
||||
"User": "(&(objectClass=user)(sAMAccountName=*))",
|
||||
"Group": "(&(objectClass=group) (samAccountName=*))"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user