Fixed location update does not cause update of assets and users, fixed ldap async issue

This commit is contained in:
2025-11-16 21:47:33 +01:00
parent 465f01f934
commit 95111a6a3d
3 changed files with 38 additions and 11 deletions

View File

@@ -1,3 +1,4 @@
using System.Text.Json;
using Berufsschule_HAM.Models;
using Berufsschule_HAM.Services;
@@ -8,8 +9,8 @@ public static class SynchronizationHelper
{
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 =>
IEnumerable<AssetModel> assets = [.. (await ldap.ListDeviceAsync()).Where(asset => asset.Owner == $"uid={oldName}")];
foreach (AssetModel asset in assets)
{
if (remove)
{
@@ -19,7 +20,34 @@ public static class SynchronizationHelper
{
await ldap.UpdateAsset(asset.Cn, "owner", uidString);
}
});
await Task.WhenAll(tasks);
};
}
public static async Task SyncLocationName(LdapService ldap, string oldLocation, string newLocation)
{
bool remove = string.IsNullOrEmpty(newLocation);
// Update asset locations
IEnumerable<AssetModel> assets = [.. (await ldap.ListDeviceAsync()).Where(asset => asset.Location == oldLocation)];
foreach (AssetModel asset in assets)
{
if (remove)
{
await ldap.DeleteAttribute(ldap.AssetsBaseDn, "cn", asset.Cn, "l");
}
else
{
await ldap.UpdateAsset(asset.Cn, "l", newLocation);
}
};
// Update user workplace locations
string newUserLocation = remove ? "" : newLocation;
IEnumerable<UserModel> users = [.. (await ldap.ListUsersAsync()).Where(user => user.Description is not null && user.Description.Workplace == oldLocation)];
foreach (UserModel user in users)
{
if (user.Description is null) return;
user.Description.Workplace = newUserLocation;
await ldap.UpdateUser(user.Uid, "description", JsonSerializer.Serialize(user.Description));
};
}
}