Gebruikersbeheer: rolhierarchie in de API afdwingen en uitnodiging opnieuw aanmaken

Een Beheerder mag alleen gebruikers met rol User verwijderen, deactiveren, van rol wisselen
of hun uitnodiging intrekken, en kent nooit de Owner-rol toe. De regels staan op een plek
(UserManagementRules) en worden in de API afgedwongen. Nieuwe actie om een openstaande of
verlopen uitnodiging een nieuwe link te geven (oude link vervalt).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-08 15:59:10 +01:00
co-authored by Claude Sonnet 5.5
parent 0932ee36b3
commit 026a12a3f1
7 changed files with 548 additions and 30 deletions
@@ -0,0 +1,24 @@
namespace SlpModularCms.Core.Identity.Authorization;
/// <summary>
/// Who may act on whom in user management. One place, enforced by the API and mirrored by the UI.
/// </summary>
/// <remarks>
/// An Owner may manage everyone. An Administrator may manage only plain Users, never another
/// Administrator or an Owner, and may not hand out the Owner role. The "at least one Owner must
/// remain" rule is separate and stays in the controller.
/// </remarks>
public static class UserManagementRules
{
public const string Owner = "Owner";
public const string Administrator = "Administrator";
public const string User = "User";
/// <summary>May the requester delete, (de)activate, re-role or withdraw an invitation of a target holding this role?</summary>
public static bool CanManage(string requesterRole, string targetRole) =>
requesterRole == Owner || (requesterRole == Administrator && targetRole == User);
/// <summary>May the requester give somebody (or an invitation) this role?</summary>
public static bool CanAssign(string requesterRole, string role) =>
requesterRole == Owner || (requesterRole == Administrator && role != Owner);
}
@@ -46,4 +46,10 @@ public interface IInvitationService
/// Wijzigt de rol van een pending uitnodiging. Retourneert false als niet gevonden.
/// </summary>
Task<bool> UpdateInvitationRoleAsync(Guid invitationId, string newRole);
/// <summary>
/// Geeft een openstaande (ook verlopen) uitnodiging een nieuw token en een nieuwe geldigheid; het oude
/// token werkt daarna niet meer. Retourneert het nieuwe token, of null als niet gevonden.
/// </summary>
Task<string?> RegenerateInvitationAsync(Guid invitationId);
}
@@ -157,6 +157,18 @@ public class InvitationService : IInvitationService
return true;
}
public async Task<string?> RegenerateInvitationAsync(Guid invitationId)
{
var invitation = await _context.Invitations
.FirstOrDefaultAsync(i => i.Id == invitationId && !i.IsAccepted);
if (invitation is null) return null;
invitation.Token = GenerateSecureToken();
invitation.ExpiryDate = DateTimeOffset.UtcNow.AddHours(_settings.ExpiryHours);
await _context.SaveChangesAsync();
return invitation.Token;
}
private static string GenerateSecureToken()
{
var bytes = new byte[32];