From 766e8d913dd46b42eeafb450fdf77293985c1fa3 Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Mon, 1 Jul 2024 15:46:09 +0200 Subject: [PATCH] =?UTF-8?q?Logger=20zu=20DirectoryController=20hinzugef?= =?UTF-8?q?=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/DirectoryController.cs | 184 ++++++++++++------ 1 file changed, 121 insertions(+), 63 deletions(-) diff --git a/DigitalData.UserManager.API/Controllers/DirectoryController.cs b/DigitalData.UserManager.API/Controllers/DirectoryController.cs index bcede38..94fb25f 100644 --- a/DigitalData.UserManager.API/Controllers/DirectoryController.cs +++ b/DigitalData.UserManager.API/Controllers/DirectoryController.cs @@ -19,8 +19,9 @@ namespace DigitalData.UserManager.API.Controllers private readonly IDirectorySearchService _dirSearchService; private readonly Dictionary _customSearchFilters; private readonly IStringLocalizer _localizer; + private readonly ILogger _logger; - public DirectoryController(IConfiguration configuration, IStringLocalizer localizer, IUserService userService, IDirectorySearchService directorySearchService) + public DirectoryController(IConfiguration configuration, IStringLocalizer localizer, IUserService userService, IDirectorySearchService directorySearchService, ILogger logger) { _localizer = localizer; _userService = userService; @@ -28,130 +29,187 @@ namespace DigitalData.UserManager.API.Controllers var customSearchFiltersSection = configuration.GetSection("DirectorySearch:CustomSearchFilters"); _customSearchFilters = customSearchFiltersSection.Get>() ?? new(); + _logger = logger; } [HttpGet("Root/{username}")] public IActionResult GetRootOf(string username) { - var root = _dirSearchService.GetSearchRootCache(username); - - return root is null ? NotFound() : Ok(new + try + { + 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 + }); + } + catch (Exception ex) { - guid = root.Guid, - nativeGuid = root.NativeGuid, - name = root.Name, - path = root.Path, - parentPath = root.Parent?.Path, - username = root.Username, - schemaClassName = root.SchemaClassName - }); + _logger.LogError(ex, "{Message}", ex.Message); + return StatusCode(StatusCodes.Status500InternalServerError); + } } [HttpGet("CustomSearchFilter")] public IActionResult GetAllCustomFilters(string? filtername) { - if (filtername is null) + try { - return Ok(_customSearchFilters); + if (filtername is null) + { + return Ok(_customSearchFilters); + } + else + { + _dirSearchService.CustomSearchFilters.TryGetValue(filtername, out string? filter); + return filter is null ? NotFound() : Ok(filter); + } } - else + catch (Exception ex) { - _dirSearchService.CustomSearchFilters.TryGetValue(filtername, out string? filter); - return filter is null ? NotFound() : Ok(filter); + _logger.LogError(ex, "{Message}", ex.Message); + return StatusCode(StatusCodes.Status500InternalServerError); } } [HttpPost("CreateSearchRoot")] public async Task CreateSearchRoot([FromBody] SearchRootCreateDto searchRootCreateDto) { - var dirEntryUsername = searchRootCreateDto.DirEntryUsername ?? CurrentUser; - if (dirEntryUsername is null) - return Unauthorized(); + try + { + var dirEntryUsername = searchRootCreateDto.DirEntryUsername ?? CurrentUser; + if (dirEntryUsername is null) + return Unauthorized(); - bool isValid = _dirSearchService.ValidateCredentials(dirEntryUsername, searchRootCreateDto.DirEntryPassword); + bool isValid = _dirSearchService.ValidateCredentials(dirEntryUsername, searchRootCreateDto.DirEntryPassword); - if (!isValid) - return Unauthorized(Result.Fail().Message(_localizer[Key.UserNotFound])); + 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])); + 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.DirEntryPassword); - return Ok(); + _dirSearchService.SetSearchRootCache(userResult.Data.Username, searchRootCreateDto.DirEntryPassword); + return Ok(); + } + catch (Exception ex) + { + _logger.LogError(ex, "{Message}", ex.Message); + return StatusCode(StatusCodes.Status500InternalServerError); + } } [HttpGet("SearchByFilter/{filter}")] public IActionResult SearchByFilter([FromRoute] string filter, string? dirEntryUsername, params string[] propName) { - dirEntryUsername ??= CurrentUser; + try + { + dirEntryUsername ??= CurrentUser; - if (dirEntryUsername is null) - return Unauthorized(); + if (dirEntryUsername is null) + return Unauthorized(); - var result = _dirSearchService.FindAllByUserCache(dirEntryUsername, filter, properties: propName); - return Ok(result); + var result = _dirSearchService.FindAllByUserCache(dirEntryUsername, filter, properties: propName); + return Ok(result); + } + catch (Exception ex) + { + _logger.LogError(ex, "{Message}", ex.Message); + return StatusCode(StatusCodes.Status500InternalServerError); + } } [HttpGet("SearchByFilterName/{filterName}")] public IActionResult SearchByFilterName([FromRoute] string filterName, string? dirEntryUsername, params string[] propName) { - dirEntryUsername ??= CurrentUser; + try + { + dirEntryUsername ??= CurrentUser; - if (dirEntryUsername is null) - return Unauthorized(); + if (dirEntryUsername is null) + return Unauthorized(); - _dirSearchService.CustomSearchFilters.TryGetValue(filterName, out string? filter); + _dirSearchService.CustomSearchFilters.TryGetValue(filterName, out string? filter); - if (filter is null) - return NotFound($"The filter named {filterName} does not exist."); + if (filter is null) + return NotFound($"The filter named {filterName} does not exist."); - var result = _dirSearchService.FindAllByUserCache(dirEntryUsername, filter, properties: propName); + var result = _dirSearchService.FindAllByUserCache(dirEntryUsername, filter, properties: propName); - return Ok(result); + return Ok(result); + } + catch (Exception ex) + { + _logger.LogError(ex, "{Message}", ex.Message); + return StatusCode(StatusCodes.Status500InternalServerError); + } } [HttpGet("Group")] public IActionResult GetGroups(string? dirEntryUsername, params string[] propName) { - dirEntryUsername ??= CurrentUser; + try + { + dirEntryUsername ??= CurrentUser; - if (dirEntryUsername is null) - return Unauthorized(); + if (dirEntryUsername is null) + return Unauthorized(); - _dirSearchService.CustomSearchFilters.TryGetValue("Group", out string? filter); + _dirSearchService.CustomSearchFilters.TryGetValue("Group", out string? filter); - 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."); + 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."); - var result = _dirSearchService.FindAllByUserCache(username: dirEntryUsername, filter, properties: propName); + var result = _dirSearchService.FindAllByUserCache(username: dirEntryUsername, filter, properties: propName); - return Ok(result); + return Ok(result); + } + catch (Exception ex) + { + _logger.LogError(ex, "{Message}", ex.Message); + return StatusCode(StatusCodes.Status500InternalServerError); + } } [HttpGet("User")] public IActionResult GetUsersByGroupName(string? dirEntryUsername, [FromQuery] string? groupName = null) { - string[] propName = { "memberof", "samaccountname", "givenname", "sn", "mail" }; - dirEntryUsername ??= CurrentUser; + try + { + string[] propName = { "memberof", "samaccountname", "givenname", "sn", "mail" }; + dirEntryUsername ??= CurrentUser; - if (dirEntryUsername is null) - return Unauthorized(); + if (dirEntryUsername is null) + return Unauthorized(); - _dirSearchService.CustomSearchFilters.TryGetValue("User", out string? filter); + _dirSearchService.CustomSearchFilters.TryGetValue("User", out string? filter); - 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."); + 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."); - var result = _dirSearchService.FindAllByUserCache(username: dirEntryUsername, filter, properties: propName); + var result = _dirSearchService.FindAllByUserCache(username: dirEntryUsername, filter, properties: propName); - if (groupName is not null && result.IsSuccess && result.Data is not null) - result.Data = result.Data - .Where(rp => rp.PropertyNames.Cast().Contains("memberof") && - rp["memberof"].Cast().Any(ldapDir => ldapDir.Contains(groupName))) - .ToList(); + if (groupName is not null && result.IsSuccess && result.Data is not null) + result.Data = result.Data + .Where(rp => rp.PropertyNames.Cast().Contains("memberof") && + rp["memberof"].Cast().Any(ldapDir => ldapDir.Contains(groupName))) + .ToList(); - return Ok(result); + return Ok(result); + } + catch (Exception ex) + { + _logger.LogError(ex, "{Message}", ex.Message); + return StatusCode(StatusCodes.Status500InternalServerError); + } } private string? CurrentUser