mirror of
https://github.com/LD-Reborn/Berufsschule_HAM.git
synced 2025-12-20 06:51:55 +00:00
63 lines
1.8 KiB
C#
63 lines
1.8 KiB
C#
// Controllers/UsersController.cs
|
|
using Berufsschule_HAM.Services;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Novell.Directory.Ldap;
|
|
using System.Threading.Tasks;
|
|
|
|
[Route("[controller]")]
|
|
public class UsersController : Controller
|
|
{
|
|
private readonly LdapService _ldap;
|
|
public UsersController(LdapService ldap) => _ldap = ldap;
|
|
|
|
[HttpGet("Index")]
|
|
public async Task<IEnumerable<Dictionary<string, string>>> Index()
|
|
{
|
|
var users = await _ldap.ListUsersAsync();
|
|
return users;
|
|
}
|
|
|
|
[HttpGet("Delete")]
|
|
public async Task<bool> Delete(string dn)
|
|
{
|
|
return await Task.Run(() =>
|
|
{
|
|
try
|
|
{
|
|
_ldap.DeleteObjectByDn(dn);
|
|
return true;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return false;
|
|
}
|
|
});
|
|
}
|
|
|
|
[HttpGet("Create")]
|
|
public async Task<bool> Create(string cn, string sn, string userPassword)
|
|
{
|
|
// return await new Task<bool>(() =>
|
|
// {
|
|
try
|
|
{
|
|
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;
|
|
}
|
|
// });
|
|
}
|
|
|
|
}
|