mirror of
https://github.com/LD-Reborn/Berufsschule_HAM.git
synced 2025-12-20 06:51:55 +00:00
Fixed username update or user delete does not cause asset ownership update
This commit is contained in:
25
src/Helpers/SynchronizationHelper.cs
Normal file
25
src/Helpers/SynchronizationHelper.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user