mirror of
https://github.com/LD-Reborn/Berufsschule_HAM.git
synced 2025-12-20 06:51:55 +00:00
Added filter by username for /Users/Index, implemented /Users/Update
This commit is contained in:
@@ -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<IEnumerable<Dictionary<string, string>>> Index()
|
||||
public async Task<IEnumerable<Dictionary<string, string>>> 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<bool> 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<string, string>? 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;
|
||||
}
|
||||
}
|
||||
|
||||
13
src/Models/UsersRequestModels.cs
Normal file
13
src/Models/UsersRequestModels.cs
Normal file
@@ -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;
|
||||
}
|
||||
@@ -55,6 +55,12 @@ public class LdapService : IDisposable
|
||||
return await ListObjectBy(UsersBaseDn, "", ["cn", "sn", "title", "uid", "jpegPhoto", "userPassword", "description"]);
|
||||
}
|
||||
|
||||
public async Task<Dictionary<string, string>> GetUserByUidAsync(string uid)
|
||||
{
|
||||
return (await ListObjectBy(UsersBaseDn, $"uid={uid}", ["cn", "sn", "title", "uid", "jpegPhoto", "userPassword", "description"])).First();
|
||||
}
|
||||
|
||||
|
||||
public async Task<IEnumerable<Dictionary<string, string>>> 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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user