diff --git a/src/Controllers/LocationsController.cs b/src/Controllers/LocationsController.cs index da82bd4..17174fe 100644 --- a/src/Controllers/LocationsController.cs +++ b/src/Controllers/LocationsController.cs @@ -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 }; diff --git a/src/Controllers/UsersController.cs b/src/Controllers/UsersController.cs index 8b3f85f..2391301 100644 --- a/src/Controllers/UsersController.cs +++ b/src/Controllers/UsersController.cs @@ -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) diff --git a/src/Helpers/SynchronizationHelper.cs b/src/Helpers/SynchronizationHelper.cs index 39e2c18..1b89710 100644 --- a/src/Helpers/SynchronizationHelper.cs +++ b/src/Helpers/SynchronizationHelper.cs @@ -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 assets = await ldap.ListDeviceAsync(); - var tasks = assets.Where(asset => asset.Owner == $"uid={oldName}").ToList().Select(async asset => + IEnumerable 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 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 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)); + }; } } \ No newline at end of file