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

@@ -69,6 +69,8 @@ public class LocationsController : Controller
}
try
{
LocationModel location = await _ldap.GetLocationByCnAsync(cn);
await SynchronizationHelper.SyncLocationName(_ldap, location.Location, "");
await _ldap.DeleteLocationAsync(cn);
return new(true);
}
@@ -92,7 +94,7 @@ public class LocationsController : Controller
string location = requestModel.Location;
LocationsDescription room = requestModel.Description;
string newLocation = StringHelpers.Slugify(room.Location + " " + room.RoomNumber + " " + room.Seat); // TODO: fix DRY violation
await SynchronizationHelper.SyncLocationName(_ldap, location, newLocation);
await _ldap.UpdateLocation(location, "description", JsonSerializer.Serialize(room));
await _ldap.UpdateLocation(location, "l", newLocation);
return new LocationsUpdateResponseModel { Success = true };

View File

@@ -51,9 +51,8 @@ public class UsersController : Controller
{
try
{
var syncAssetOwnership = SynchronizationHelper.SyncAssetOwnership(_ldap, uid, "");
await SynchronizationHelper.SyncAssetOwnership(_ldap, uid, "");
await _ldap.DeleteUserAsync(uid);
await syncAssetOwnership;
return new UsersDeleteRequestModel(true);
}
catch (Exception ex)
@@ -124,10 +123,9 @@ public class UsersController : Controller
UserModel? user = null;
if (requestModel.NewUid is not null && requestModel.NewUid.Length > 0)
{
var syncAssetOwnership = SynchronizationHelper.SyncAssetOwnership(_ldap, uid, requestModel.NewUid);
await SynchronizationHelper.SyncAssetOwnership(_ldap, uid, requestModel.NewUid);
await _ldap.UpdateUser(uid, "uid", requestModel.NewUid);
uid = requestModel.NewUid;
await syncAssetOwnership;
}
if (requestModel.Title is not null)
{
@@ -170,10 +168,9 @@ public class UsersController : Controller
}
if (newUid != uid)
{
var syncAssetOwnership = SynchronizationHelper.SyncAssetOwnership(_ldap, uid, newUid);
await SynchronizationHelper.SyncAssetOwnership(_ldap, uid, newUid);
await _ldap.UpdateUser(uid, "uid", newUid);
uid = newUid;
await syncAssetOwnership;
}
return new() { Success = true, NewUid = uid };
} catch (Exception ex)

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));
};
}
}