refactor(DirectoryController): remove try-catch block
This commit is contained in:
parent
4725614bb3
commit
9fd66d41ca
@ -38,195 +38,139 @@ public class DirectoryController : ControllerBase
|
|||||||
[HttpGet("Root/{username}")]
|
[HttpGet("Root/{username}")]
|
||||||
public IActionResult GetRootOf(string username)
|
public IActionResult GetRootOf(string username)
|
||||||
{
|
{
|
||||||
try
|
var root = _dirSearchService.GetSearchRootCache(username);
|
||||||
{
|
|
||||||
var root = _dirSearchService.GetSearchRootCache(username);
|
|
||||||
|
|
||||||
return root is null ? NotFound() : Ok(new
|
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)
|
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, "{Message}", ex.Message);
|
guid = root.Guid,
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError);
|
nativeGuid = root.NativeGuid,
|
||||||
}
|
name = root.Name,
|
||||||
|
path = root.Path,
|
||||||
|
parentPath = root.Parent?.Path,
|
||||||
|
username = root.Username,
|
||||||
|
schemaClassName = root.SchemaClassName
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("CustomSearchFilter")]
|
[HttpGet("CustomSearchFilter")]
|
||||||
public IActionResult GetAllCustomFilters(string? filtername)
|
public IActionResult GetAllCustomFilters(string? filtername)
|
||||||
{
|
{
|
||||||
try
|
if (filtername is null)
|
||||||
{
|
{
|
||||||
if (filtername is null)
|
return Ok(_customSearchFilters);
|
||||||
{
|
|
||||||
return Ok(_customSearchFilters);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_dirSearchService.CustomSearchFilters.TryGetValue(filtername, out string? filter);
|
|
||||||
return filter is null ? NotFound() : Ok(filter);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
else
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, "{Message}", ex.Message);
|
_dirSearchService.CustomSearchFilters.TryGetValue(filtername, out string? filter);
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError);
|
return filter is null ? NotFound() : Ok(filter);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public async Task<IActionResult> CreateSearchRoot([FromBody] SearchRootCreateDto searchRootCreateDto)
|
public async Task<IActionResult> CreateSearchRoot([FromBody] SearchRootCreateDto searchRootCreateDto)
|
||||||
{
|
{
|
||||||
try
|
var dirEntryUsername = searchRootCreateDto.Username ?? CurrentUser;
|
||||||
{
|
if (dirEntryUsername is null)
|
||||||
var dirEntryUsername = searchRootCreateDto.Username ?? CurrentUser;
|
return Unauthorized();
|
||||||
if (dirEntryUsername is null)
|
|
||||||
return Unauthorized();
|
|
||||||
|
|
||||||
bool isValid = _dirSearchService.ValidateCredentials(dirEntryUsername, searchRootCreateDto.Password);
|
bool isValid = _dirSearchService.ValidateCredentials(dirEntryUsername, searchRootCreateDto.Password);
|
||||||
|
|
||||||
if (!isValid)
|
if (!isValid)
|
||||||
return Unauthorized(Result.Fail().Message(_localizer[Key.UserNotFound]));
|
return Unauthorized(Result.Fail().Message(_localizer[Key.UserNotFound]));
|
||||||
|
|
||||||
var userResult = await _userService.ReadByUsernameAsync(dirEntryUsername);
|
var userResult = await _userService.ReadByUsernameAsync(dirEntryUsername);
|
||||||
if (!userResult.IsSuccess || userResult.Data is null)
|
if (!userResult.IsSuccess || userResult.Data is null)
|
||||||
return Unauthorized(Result.Fail().Message(_localizer[Key.UserNotFoundInLocalDB]));
|
return Unauthorized(Result.Fail().Message(_localizer[Key.UserNotFoundInLocalDB]));
|
||||||
|
|
||||||
_dirSearchService.SetSearchRootCache(userResult.Data.Username, searchRootCreateDto.Password);
|
_dirSearchService.SetSearchRootCache(userResult.Data.Username, searchRootCreateDto.Password);
|
||||||
return Ok();
|
return Ok();
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.LogError(ex, "{Message}", ex.Message);
|
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("SearchByFilter/{filter}")]
|
[HttpGet("SearchByFilter/{filter}")]
|
||||||
public IActionResult SearchByFilter([FromRoute] string filter, string? dirEntryUsername, params string[] propName)
|
public IActionResult SearchByFilter([FromRoute] string filter, string? dirEntryUsername, params string[] propName)
|
||||||
{
|
{
|
||||||
try
|
dirEntryUsername ??= CurrentUser;
|
||||||
{
|
|
||||||
dirEntryUsername ??= CurrentUser;
|
|
||||||
|
|
||||||
if (dirEntryUsername is null)
|
if (dirEntryUsername is null)
|
||||||
return Unauthorized();
|
return Unauthorized();
|
||||||
|
|
||||||
return _dirSearchService.FindAllByUserCache(dirEntryUsername, filter, properties: propName).Then(Ok, IActionResult (m, n) =>
|
return _dirSearchService.FindAllByUserCache(dirEntryUsername, filter, properties: propName).Then(Ok, IActionResult (m, n) =>
|
||||||
{
|
|
||||||
_logger.LogNotice(n);
|
|
||||||
return StatusCode(StatusCodes.Status424FailedDependency);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, "{Message}", ex.Message);
|
_logger.LogNotice(n);
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError);
|
return StatusCode(StatusCodes.Status424FailedDependency);
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("SearchByFilterName/{filterName}")]
|
[HttpGet("SearchByFilterName/{filterName}")]
|
||||||
public IActionResult SearchByFilterName([FromRoute] string filterName, string? dirEntryUsername, params string[] propName)
|
public IActionResult SearchByFilterName([FromRoute] string filterName, string? dirEntryUsername, params string[] propName)
|
||||||
{
|
{
|
||||||
try
|
dirEntryUsername ??= CurrentUser;
|
||||||
|
|
||||||
|
if (dirEntryUsername is null)
|
||||||
|
return Unauthorized();
|
||||||
|
|
||||||
|
_dirSearchService.CustomSearchFilters.TryGetValue(filterName, out string? filter);
|
||||||
|
|
||||||
|
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) =>
|
||||||
{
|
{
|
||||||
dirEntryUsername ??= CurrentUser;
|
_logger.LogNotice(n);
|
||||||
|
return StatusCode(StatusCodes.Status424FailedDependency);
|
||||||
if (dirEntryUsername is null)
|
});
|
||||||
return Unauthorized();
|
|
||||||
|
|
||||||
_dirSearchService.CustomSearchFilters.TryGetValue(filterName, out string? filter);
|
|
||||||
|
|
||||||
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) =>
|
|
||||||
{
|
|
||||||
_logger.LogNotice(n);
|
|
||||||
return StatusCode(StatusCodes.Status424FailedDependency);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.LogError(ex, "{Message}", ex.Message);
|
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("Group")]
|
[HttpGet("Group")]
|
||||||
[Authorize]
|
[Authorize]
|
||||||
public IActionResult GetGroups(params string[] propName)
|
public IActionResult GetGroups(params string[] propName)
|
||||||
{
|
{
|
||||||
try
|
string dirEntryUsername = CurrentUser!;
|
||||||
|
|
||||||
|
if (dirEntryUsername is null)
|
||||||
|
return Unauthorized();
|
||||||
|
|
||||||
|
_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.");
|
||||||
|
|
||||||
|
return _dirSearchService.FindAllByUserCache(username: dirEntryUsername, filter, properties: propName).Then(Ok, IActionResult (m, n) =>
|
||||||
{
|
{
|
||||||
string dirEntryUsername = CurrentUser!;
|
_logger.LogNotice(n);
|
||||||
|
return StatusCode(StatusCodes.Status424FailedDependency);
|
||||||
if (dirEntryUsername is null)
|
});
|
||||||
return Unauthorized();
|
|
||||||
|
|
||||||
_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.");
|
|
||||||
|
|
||||||
return _dirSearchService.FindAllByUserCache(username: dirEntryUsername, filter, properties: propName).Then(Ok, IActionResult (m, n) =>
|
|
||||||
{
|
|
||||||
_logger.LogNotice(n);
|
|
||||||
return StatusCode(StatusCodes.Status424FailedDependency);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.LogError(ex, "{Message}", ex.Message);
|
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("User")]
|
[HttpGet("User")]
|
||||||
public IActionResult GetUsersByGroupName([FromQuery] string? groupName = null)
|
public IActionResult GetUsersByGroupName([FromQuery] string? groupName = null)
|
||||||
{
|
{
|
||||||
try
|
string[] propName = { "memberof", "samaccountname", "givenname", "sn", "mail" };
|
||||||
{
|
string dirEntryUsername = CurrentUser!;
|
||||||
string[] propName = { "memberof", "samaccountname", "givenname", "sn", "mail" };
|
|
||||||
string dirEntryUsername = CurrentUser!;
|
|
||||||
|
|
||||||
if (dirEntryUsername is null)
|
if (dirEntryUsername is null)
|
||||||
return Unauthorized();
|
return Unauthorized();
|
||||||
|
|
||||||
_dirSearchService.CustomSearchFilters.TryGetValue("User", out string? filter);
|
_dirSearchService.CustomSearchFilters.TryGetValue("User", out string? filter);
|
||||||
|
|
||||||
if (filter is null)
|
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.");
|
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(
|
return _dirSearchService.FindAllByUserCache(username: dirEntryUsername, filter, properties: propName).Then(
|
||||||
Success: data =>
|
Success: data =>
|
||||||
{
|
{
|
||||||
if (groupName is not null)
|
if (groupName is not null)
|
||||||
data = data
|
data = data
|
||||||
.Where(rp => rp.PropertyNames.Cast<string>().Contains("memberof") &&
|
.Where(rp => rp.PropertyNames.Cast<string>().Contains("memberof") &&
|
||||||
rp["memberof"].Cast<string>().Any(ldapDir => ldapDir.Contains(groupName)))
|
rp["memberof"].Cast<string>().Any(ldapDir => ldapDir.Contains(groupName)))
|
||||||
.ToList();
|
.ToList();
|
||||||
return Ok(data);
|
return Ok(data);
|
||||||
},
|
},
|
||||||
Fail: IActionResult (m, n) =>
|
Fail: IActionResult (m, n) =>
|
||||||
{
|
{
|
||||||
_logger.LogNotice(n);
|
_logger.LogNotice(n);
|
||||||
return StatusCode(StatusCodes.Status424FailedDependency);
|
return StatusCode(StatusCodes.Status424FailedDependency);
|
||||||
});
|
});
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.LogError(ex, "{Message}", ex.Message);
|
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private string? CurrentUser
|
private string? CurrentUser
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user