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,67 @@
using FluentAssertions;
using Microsoft.EntityFrameworkCore;
using MySql.Data.MySqlClient;
using SlpModularCms.Core.Data;
using SlpModularCms.Core.Identity.Entities;
namespace SlpModularCms.Core.Tests.Identity;
/// <summary>
/// Evidence that an invitation keeps its full lifetime through the real MySQL/MariaDB storage:
/// <c>DateTimeOffset</c> is stored in a <c>datetime</c> column (no offset), so a provider that
/// stored local time but read it as UTC (or the reverse) would silently shorten or lengthen it.
/// </summary>
/// <remarks>
/// Needs a reachable MariaDB, taken from <c>ConnectionStrings__DefaultConnection</c> (set in CI; the
/// same variable the Api tests use). It uses its OWN database, created and dropped here, never the
/// one named in the variable. Without the variable the test does nothing, so a plain
/// <c>dotnet test</c> on a machine without MariaDB stays green.
/// </remarks>
public class DateTimeOffsetRoundTripTests
{
private static string? ScratchConnectionString()
{
var raw = Environment.GetEnvironmentVariable("ConnectionStrings__DefaultConnection");
if (string.IsNullOrWhiteSpace(raw)) return null;
var builder = new MySqlConnectionStringBuilder(raw) { Database = "roundtrip_datetime_probe" };
return builder.ConnectionString;
}
[Theory]
[InlineData(0)]
[InlineData(1)]
[InlineData(2)]
[InlineData(-5)]
public async Task ExpiryDate_SurvivesTheDatabase_WhateverOffsetItWasWrittenWith(int offsetHours)
{
var cs = ScratchConnectionString();
if (cs is null) return; // no MariaDB available here, see remarks
var options = new DbContextOptionsBuilder<ApplicationDbContext>().UseMySQL(cs).Options;
var instant = DateTimeOffset.UtcNow.AddHours(24);
var written = instant.ToOffset(TimeSpan.FromHours(offsetHours));
await using (var db = new ApplicationDbContext(options))
{
await db.Database.EnsureDeletedAsync();
await db.Database.EnsureCreatedAsync();
db.Invitations.Add(new Invitation { Email = "a@b.nl", Role = "User", Token = "t", ExpiryDate = written });
await db.SaveChangesAsync();
}
try
{
await using var db = new ApplicationDbContext(options);
var read = (await db.Invitations.AsNoTracking().SingleAsync()).ExpiryDate;
// The column keeps whole seconds only, so compare within a second.
(read - instant).Duration().Should().BeLessThan(TimeSpan.FromSeconds(1.5));
(read - DateTimeOffset.UtcNow).TotalHours.Should().BeApproximately(24, 0.01);
}
finally
{
await using var cleanup = new ApplicationDbContext(options);
await cleanup.Database.EnsureDeletedAsync();
}
}
}
@@ -363,4 +363,35 @@ public class InvitationServiceTests
(await _service.DeleteInvitationByIdAsync(inv.Id)).Should().BeTrue();
(await _context.Invitations.AnyAsync()).Should().BeFalse();
}
[Fact]
public async Task RegenerateInvitationAsync_GivesANewTokenAndLifetime_AndTheOldLinkStopsWorking()
{
var oldToken = await _service.CreateInvitationAsync("again@example.com", "User");
var inv = await _context.Invitations.SingleAsync();
inv.ExpiryDate = DateTimeOffset.UtcNow.AddHours(-3); // expired
await _context.SaveChangesAsync();
var newToken = await _service.RegenerateInvitationAsync(inv.Id);
newToken.Should().NotBeNullOrEmpty().And.NotBe(oldToken);
(await _service.ValidateInvitationAsync(oldToken)).Should().BeFalse();
(await _service.ValidateInvitationAsync(newToken!)).Should().BeTrue();
var reloaded = await _context.Invitations.AsNoTracking().SingleAsync();
(reloaded.ExpiryDate - DateTimeOffset.UtcNow).Should().BeCloseTo(TimeSpan.FromHours(24), TimeSpan.FromMinutes(1));
reloaded.Email.Should().Be("again@example.com");
}
[Fact]
public async Task RegenerateInvitationAsync_ReturnsNull_ForUnknownOrAcceptedInvitations()
{
(await _service.RegenerateInvitationAsync(Guid.NewGuid())).Should().BeNull();
var token = await _service.CreateInvitationAsync("done@example.com", "User");
var inv = await _context.Invitations.SingleAsync(i => i.Token == token);
inv.IsAccepted = true;
await _context.SaveChangesAsync();
(await _service.RegenerateInvitationAsync(inv.Id)).Should().BeNull();
}
}