Enhance user management: prevent deletion of the last admin user and restrict admin type changes for the last admin account
All checks were successful
Build And Push Dev Docker Image / docker (push) Successful in 1m47s

Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
a.beging@eas-solutions.de
2026-04-30 09:53:05 +02:00
parent c4d7fd6ed5
commit cefa47a176
3 changed files with 28 additions and 11 deletions

View File

@@ -105,6 +105,13 @@ namespace FoodsharingSiegen.Server.Data.Service
var user = await Context.Users!.Include(x => x.Interactions).FirstOrDefaultAsync(x => x.Id == userId);
if (user == null) return new(new Exception("User not found"));
if (user.Type == UserType.Admin)
{
var adminCount = await Context.Users!.CountAsync(x => x.Type == UserType.Admin && x.Id != userId);
if (adminCount == 0)
return new(new Exception("Der letzte Administrator kann nicht gelöscht werden."));
}
// Interaktionen vom aktuellen Nutzer übernehmen
if(CurrentUser?.Id != null)
foreach (var userInteraction in user.Interactions)
@@ -184,6 +191,13 @@ namespace FoodsharingSiegen.Server.Data.Service
var entityUser = await Context.Users!.FirstOrDefaultAsync(x => x.Id == user.Id);
if (entityUser == null) return new(new Exception("User not found"));
if (entityUser.Type == UserType.Admin && user.Type != UserType.Admin)
{
var adminCount = await Context.Users!.CountAsync(x => x.Type == UserType.Admin && x.Id != user.Id);
if (adminCount == 0)
return new(new Exception("Der Typ des letzten Administrators kann nicht geändert werden."));
}
if (entityUser.Mail != user.Mail ||
entityUser.Type != user.Type ||
entityUser.Groups != user.Groups)