mirror of
https://github.com/LD-Reborn/Berufsschule_HAM.git
synced 2025-12-20 06:51:55 +00:00
Fixed location update does not cause update of assets and users, fixed ldap async issue
This commit is contained in:
@@ -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));
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user