mirror of
https://github.com/LD-Reborn/Berufsschule_HAM.git
synced 2025-12-20 06:51:55 +00:00
Merge pull request #314 from LD-Reborn/232-bug-username-update-does-not-cause-update-of-assets
Fixed username update or user delete does not cause asset ownership u…
This commit is contained in:
@@ -51,7 +51,9 @@ public class UsersController : Controller
|
||||
{
|
||||
try
|
||||
{
|
||||
var syncAssetOwnership = SynchronizationHelper.SyncAssetOwnership(_ldap, uid, "");
|
||||
await _ldap.DeleteUserAsync(uid);
|
||||
await syncAssetOwnership;
|
||||
return new UsersDeleteRequestModel(true);
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -122,8 +124,10 @@ 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 _ldap.UpdateUser(uid, "uid", requestModel.NewUid);
|
||||
uid = requestModel.NewUid;
|
||||
await syncAssetOwnership;
|
||||
}
|
||||
if (requestModel.Title is not null)
|
||||
{
|
||||
@@ -166,8 +170,10 @@ public class UsersController : Controller
|
||||
}
|
||||
if (newUid != uid)
|
||||
{
|
||||
var syncAssetOwnership = SynchronizationHelper.SyncAssetOwnership(_ldap, uid, newUid);
|
||||
await _ldap.UpdateUser(uid, "uid", newUid);
|
||||
uid = newUid;
|
||||
await syncAssetOwnership;
|
||||
}
|
||||
return new() { Success = true, NewUid = uid };
|
||||
} catch (Exception ex)
|
||||
|
||||
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