Updated users LDAP spec, changed to newer LDAP package, implemented /Users/Create endpoint

This commit is contained in:
2025-09-28 19:54:45 +02:00
parent 0406f1c9f5
commit cd802eceaf
4 changed files with 64 additions and 45 deletions

View File

@@ -8,7 +8,13 @@ using System.Threading.Tasks;
public class UsersController : Controller
{
private readonly LdapService _ldap;
public UsersController(LdapService ldap) => _ldap = ldap;
private readonly ILogger<UsersController> _logger;
public UsersController(LdapService ldap, ILogger<UsersController> logger)
{
_ldap = ldap;
_logger = logger;
}
[HttpGet("Index")]
public async Task<IEnumerable<Dictionary<string, string>>> Index()
@@ -35,28 +41,35 @@ public class UsersController : Controller
}
[HttpGet("Create")]
public async Task<bool> Create(string cn, string sn, string userPassword)
{
// return await new Task<bool>(() =>
// {
try
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
uid ??= sn.ToLower() + cn.ToLower();
title ??= "";
description ??= "{}";
LdapAttributeSet attributeSet = new LdapAttributeSet
{
LdapAttributeSet attributeSet = [];
attributeSet.Add(new LdapAttribute("objectClass", "organizationalPerson"));
attributeSet.Add(new LdapAttribute("objectClass", "person"));
//attributeSet.Add(new LdapAttribute("ou", "users"));
attributeSet.Add(new LdapAttribute("objectClass", "top"));
attributeSet.Add(new LdapAttribute("cn", cn));
attributeSet.Add(new LdapAttribute("sn", sn));
attributeSet.Add(new LdapAttribute("userPassword", userPassword));
_ldap.CreateUser(attributeSet);
return true;
}
catch (Exception ex)
{
return false;
}
// });
new LdapAttribute("objectClass", "inetOrgPerson"),
new LdapAttribute("objectClass", "person"),
new LdapAttribute("objectClass", "top"),
new LdapAttribute("cn", cn),
new LdapAttribute("sn", sn),
new LdapAttribute("title", title),
new LdapAttribute("uid", uid),
new LdapAttribute("jpegPhoto", jpegPhoto),
new LdapAttribute("description", description),
new LdapAttribute("userPassword", userPassword)
};
_ldap.CreateUser(uid, attributeSet);
return true;
}
catch (Exception ex)
{
_logger.LogError("Unable to create user: {ex.Message} - {ex.StackTrace}", [ex.Message, ex.StackTrace]);
return false;
}
}
}