From 2f894881b23e527cdeb70499e57e7926bf2a7482 Mon Sep 17 00:00:00 2001 From: LD-Reborn Date: Sun, 28 Sep 2025 21:41:04 +0200 Subject: [PATCH] Added filter by username for /Users/Index, implemented /Users/Update --- src/Controllers/UsersController.cs | 69 +++++++++++++++++++++++++++--- src/Models/UsersRequestModels.cs | 13 ++++++ src/Services/LdapService.cs | 24 +++++++++++ 3 files changed, 100 insertions(+), 6 deletions(-) create mode 100644 src/Models/UsersRequestModels.cs diff --git a/src/Controllers/UsersController.cs b/src/Controllers/UsersController.cs index 7d3516c..125dc26 100644 --- a/src/Controllers/UsersController.cs +++ b/src/Controllers/UsersController.cs @@ -2,7 +2,7 @@ using Berufsschule_HAM.Services; using Microsoft.AspNetCore.Mvc; using Novell.Directory.Ldap; -using System.Threading.Tasks; +using Berufsschule_HAM.Models; [Route("[controller]")] public class UsersController : Controller @@ -17,10 +17,18 @@ public class UsersController : Controller } [HttpGet("Index")] - public async Task>> Index() + public async Task>> Index(string? uid = null) { - var users = await _ldap.ListUsersAsync(); - return users; + if (uid is null) + { + var users = await _ldap.ListUsersAsync(); + return users; + } + else + { + var user = await _ldap.GetUserByUidAsync(uid); + return [user]; + } } [HttpGet("Delete")] @@ -42,7 +50,7 @@ public class UsersController : Controller [HttpGet("Create")] public bool Create(string cn, string sn, string? title, string? uid, string userPassword, string? description, string jpegPhoto) - { + { try { jpegPhoto ??= System.IO.File.ReadAllText("wwwroot/user_default.jpeg"); // TODO: cleanup - make this a config setting @@ -69,5 +77,54 @@ public class UsersController : Controller return false; } } - + + [HttpPost("Update")] + public async Task Update([FromBody]UsersModifyRequestModel requestModel) + { + if (requestModel is null) + { + _logger.LogError("Unable to update a user because the UsersModifyRequestModel is null"); + return false; + } + string uid = requestModel.uid; + Dictionary? user = null; + if (requestModel.Cn is not null) + { + await _ldap.UpdateUser(uid, "cn", requestModel.Cn); + user ??= await _ldap.GetUserByUidAsync(uid); + string newUid = user["sn"].ToLower() + requestModel.Cn.ToLower(); + await _ldap.UpdateUser(uid, "uid", newUid); + uid = newUid; + } + if (requestModel.Sn is not null) + { + await _ldap.UpdateUser(uid, "sn", requestModel.Sn); + user ??= await _ldap.GetUserByUidAsync(uid); + string newUid = requestModel.Sn.ToLower() + user["cn"].ToLower(); + await _ldap.UpdateUser(uid, "uid", newUid); + uid = newUid; + } + if (requestModel.NewUid is not null) + { + await _ldap.UpdateUser(uid, "uid", requestModel.NewUid); + uid = requestModel.NewUid; + } + if (requestModel.Title is not null) + { + await _ldap.UpdateUser(uid, "title", requestModel.Title); + } + if (requestModel.Description is not null) + { + await _ldap.UpdateUser(uid, "description", requestModel.Description); + } + if (requestModel.JpegPhoto is not null) + { + await _ldap.UpdateUser(uid, "jpegPhoto", requestModel.JpegPhoto); + } + if (requestModel.UserPassword is not null) + { + await _ldap.UpdateUser(uid, "userPassword", requestModel.UserPassword); + } + return true; + } } diff --git a/src/Models/UsersRequestModels.cs b/src/Models/UsersRequestModels.cs new file mode 100644 index 0000000..b1f3535 --- /dev/null +++ b/src/Models/UsersRequestModels.cs @@ -0,0 +1,13 @@ +namespace Berufsschule_HAM.Models; + +public class UsersModifyRequestModel +{ + public required string uid { get; set; } + public string? NewUid { get; set; } = null; + public string? Cn { get; set; } = null; + public string? Sn { get; set; } = null; + public string? Title { get; set; } = null; + public string? Description { get; set; } = null; + public string? JpegPhoto { get; set; } = null; + public string? UserPassword { get; set; } = null; +} \ No newline at end of file diff --git a/src/Services/LdapService.cs b/src/Services/LdapService.cs index 06d9c17..f459a4f 100644 --- a/src/Services/LdapService.cs +++ b/src/Services/LdapService.cs @@ -55,6 +55,12 @@ public class LdapService : IDisposable return await ListObjectBy(UsersBaseDn, "", ["cn", "sn", "title", "uid", "jpegPhoto", "userPassword", "description"]); } + public async Task> GetUserByUidAsync(string uid) + { + return (await ListObjectBy(UsersBaseDn, $"uid={uid}", ["cn", "sn", "title", "uid", "jpegPhoto", "userPassword", "description"])).First(); + } + + public async Task>> ListDeviceAsync() { return await ListObjectBy(AssetsBaseDn, "(objectClass=device)", ["CN", "description", "l", "owner", "serialNumber"]); @@ -118,6 +124,24 @@ public class LdapService : IDisposable string dn = PrependRDN($"uid={uid}", UsersBaseDn); DeleteObjectByDn(dn); } + public async Task UpdateUser(string uid, string attributeName, string attributeValue) + { + await ConnectAndBind(); + string dn = PrependRDN($"uid={uid}", UsersBaseDn); + if (attributeName == "uid") + { + await _conn.RenameAsync(dn, $"uid={attributeValue}", true); + } + else + { + var modification = new LdapModification( + LdapModification.Replace, + new LdapAttribute(attributeName, attributeValue) + ); + await _conn.ModifyAsync(dn, modification); + } + } + public async void DeleteObjectByDn(string dn) {