Initial commit

This commit is contained in:
2025-09-26 11:40:52 +02:00
commit 95e9f3232c
201 changed files with 146461 additions and 0 deletions

13
Services/LdapOptions.cs Normal file
View File

@@ -0,0 +1,13 @@
namespace Berufsschule_HAM.Services;
public class LdapOptions
{
public required string Host { get; set; }
public int Port { get; set; } = 389;
public bool UseSsl { get; set; } = false;
public required string BindDn { get; set; }
public required string BindPassword { get; set; }
public required string BaseDn { get; set; }
public string AssetsOu { get; set; } = "ou=assets";
public string LocationsOu { get; set; } = "ou=locations";
public string UsersOu { get; set; } = "ou=users";
}

119
Services/LdapService.cs Normal file
View File

@@ -0,0 +1,119 @@
using Novell.Directory.Ldap;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Berufsschule_HAM.Models;
using System.Text.Json;
namespace Berufsschule_HAM.Services;
public class LdapService : IDisposable
{
private readonly LdapOptions _opts;
private readonly LdapConnection _conn;
public LdapService(IOptions<LdapOptions> options)
{
_opts = options.Value;
_conn = new LdapConnection { SecureSocketLayer = _opts.UseSsl };
ConnectAndBind();
}
private void ConnectAndBind()
{
if (!_conn.Connected)
{
Console.WriteLine(_opts.Host);
Console.WriteLine(_opts.Port);
_conn.Connect(_opts.Host, _opts.Port);
}
_conn.Bind(_opts.BindDn, _opts.BindPassword);
}
private string AssetsBaseDn => string.IsNullOrEmpty(_opts.AssetsOu) ? _opts.BaseDn : $"{_opts.AssetsOu},{_opts.BaseDn}";
private string LocationsBaseDn => string.IsNullOrEmpty(_opts.LocationsOu) ? _opts.BaseDn : $"{_opts.LocationsOu},{_opts.BaseDn}";
private string UsersBaseDn => string.IsNullOrEmpty(_opts.UsersOu) ? _opts.BaseDn : $"{_opts.UsersOu},{_opts.BaseDn}";
public async Task<IEnumerable<Dictionary<string, string>>> ListLocationsAsync()
{
return await ListObjectBy(LocationsBaseDn, "(ou=locations)", ["l", "street", "description"]);
}
public async Task<IEnumerable<Dictionary<string, string>>> ListUsersAsync()
{
return await ListObjectBy(UsersBaseDn, "", ["cn", "sn", "userPassword"]);
//return await ListObjectBy("objectClass=person");
}
public async Task<IEnumerable<Dictionary<string, string>>> ListDeviceAsync()
{
return await ListObjectBy(AssetsBaseDn, "(objectClass=device)", ["CN", "description", "l", "owner", "serialNumber"]);
}
public void CreateUser(LdapAttributeSet attributeSet)
{
CreateObject(UsersBaseDn, attributeSet);
}
public void CreateAsset(LdapAttributeSet attributeSet)
{
CreateObject(AssetsBaseDn, attributeSet);
}
public void CreateLocation(LdapAttributeSet attributeSet)
{
CreateObject(LocationsBaseDn, attributeSet);
}
public async Task<IEnumerable<Dictionary<string, string>>> ListObjectBy(string baseDn, string filter, string[] attributes)
{
return await Task.Run(() =>
{
ConnectAndBind();
var search = _conn.Search(
baseDn,
LdapConnection.SCOPE_SUB,
$"{filter}",
attributes,
false);
var list = new List<Dictionary<string, string>>();
while (search.hasMore())
{
try
{
var e = search.next();
var attributeSet = e.getAttributeSet().ToArray();
if (attributeSet.Length == 0) { continue; }
Dictionary<string, string> attributeMap = [];
foreach (LdapAttribute attribute in attributeSet.Cast<LdapAttribute>())
{
attributeMap[attribute.Name] = attribute.StringValue;
}
list.Add(attributeMap);
}
catch (LdapException) { }
}
return list;
});
}
public void DeleteObjectByDn(string dn)
{
_conn.Delete(dn);
}
public void CreateObject(string dn, LdapAttributeSet attributeSet)
{
LdapEntry ldapEntry = new(dn, attributeSet);
_conn.Add(ldapEntry);
}
public void Dispose()
{
if (_conn != null && _conn.Connected)
{
_conn.Disconnect();
}
}
}