Fixed username update or user delete does not cause asset ownership update

This commit is contained in:
2025-11-16 21:07:15 +01:00
parent 3dd283b32a
commit a6e58ee880
2 changed files with 31 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
using Berufsschule_HAM.Models;
using Berufsschule_HAM.Services;
namespace Berufsschule_HAM.Helpers;
public static class SynchronizationHelper
{
public static async Task SyncAssetOwnership(LdapService ldap, string oldName, string newName)
{
bool remove = string.IsNullOrEmpty(newName);
string uidString = $"uid={newName}";
IEnumerable<AssetModel> assets = await ldap.ListDeviceAsync();
var tasks = assets.Where(asset => asset.Owner == $"uid={oldName}").ToList().Select(async asset =>
{
if (remove)
{
await ldap.DeleteAttribute(ldap.AssetsBaseDn, "cn", asset.Cn, "owner");
}
else
{
await ldap.UpdateAsset(asset.Cn, "owner", uidString);
}
});
await Task.WhenAll(tasks);
}
}