Merge pull request #42 from LD-Reborn/38-feature-add-a-filter-to-usersindex

Added filter to /Users/Index to filter out fields
This commit is contained in:
LD50
2025-09-28 22:08:34 +02:00
committed by GitHub
3 changed files with 33 additions and 3 deletions

View File

@@ -17,16 +17,25 @@ public class UsersController : Controller
} }
[HttpGet("Index")] [HttpGet("Index")]
public async Task<IEnumerable<Dictionary<string, string>>> Index(string? uid = null) public async Task<IEnumerable<Dictionary<string, string>>> Index(UsersIndexRequestModel requestModel)
{ {
string? uid = requestModel.Uid;
List<string> attributes = ["cn", "sn", "title", "uid", "jpegPhoto", "userPassword", "description"];
if (!requestModel.Cn) attributes.Remove("cn");
if (!requestModel.Sn) attributes.Remove("sn");
if (!requestModel.Title) attributes.Remove("title");
if (!requestModel.JpegPhoto) attributes.Remove("jpegPhoto");
if (!requestModel.UserPassword) attributes.Remove("userPassword");
if (!requestModel.Description) attributes.Remove("description");
if (uid is null) if (uid is null)
{ {
var users = await _ldap.ListUsersAsync(); var users = await _ldap.ListUsersAsync([.. attributes]);
return users; return users;
} }
else else
{ {
var user = await _ldap.GetUserByUidAsync(uid); var user = await _ldap.GetUserByUidAsync(uid, [.. attributes]);
return [user]; return [user];
} }
} }

View File

@@ -1,5 +1,16 @@
namespace Berufsschule_HAM.Models; namespace Berufsschule_HAM.Models;
public class UsersIndexRequestModel
{
public string? Uid { get; set; } = null;
public bool Cn { get; set; } = true;
public bool Sn { get; set; } = true;
public bool Title { get; set; } = true;
public bool Description { get; set; } = true;
public bool JpegPhoto { get; set; } = true;
public bool UserPassword { get; set; } = true;
}
public class UsersModifyRequestModel public class UsersModifyRequestModel
{ {
public required string uid { get; set; } public required string uid { get; set; }

View File

@@ -55,11 +55,21 @@ public class LdapService : IDisposable
return await ListObjectBy(UsersBaseDn, "", ["cn", "sn", "title", "uid", "jpegPhoto", "userPassword", "description"]); return await ListObjectBy(UsersBaseDn, "", ["cn", "sn", "title", "uid", "jpegPhoto", "userPassword", "description"]);
} }
public async Task<IEnumerable<Dictionary<string, string>>> ListUsersAsync(string[] attributes)
{
return await ListObjectBy(UsersBaseDn, "", attributes);
}
public async Task<Dictionary<string, string>> GetUserByUidAsync(string uid) public async Task<Dictionary<string, string>> GetUserByUidAsync(string uid)
{ {
return (await ListObjectBy(UsersBaseDn, $"uid={uid}", ["cn", "sn", "title", "uid", "jpegPhoto", "userPassword", "description"])).First(); return (await ListObjectBy(UsersBaseDn, $"uid={uid}", ["cn", "sn", "title", "uid", "jpegPhoto", "userPassword", "description"])).First();
} }
public async Task<Dictionary<string, string>> GetUserByUidAsync(string uid, string[] attributes)
{
return (await ListObjectBy(UsersBaseDn, $"uid={uid}", attributes)).First();
}
public async Task<IEnumerable<Dictionary<string, string>>> ListDeviceAsync() public async Task<IEnumerable<Dictionary<string, string>>> ListDeviceAsync()
{ {