Gebruikersbeheer herkent de aanroeper aan het gebruikers-id van het token

Een echt access token heeft geen naam-claim, dus User.Identity.Name was null en elke aanroeper gold als
Beheerder: een Eigenaar kreeg 403 bij het toekennen van de Eigenaar-rol, en de controles voor 'jezelf'
(niet zelf verwijderen of deactiveren) sloegen nooit aan. Nu via het id van het token. Regressietest met
echte tokens en de echte pipeline.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-08 21:29:24 +01:00
co-authored by Claude Sonnet 5.5
parent f242da8429
commit ef7df53988
5 changed files with 293 additions and 6 deletions
@@ -1,3 +1,4 @@
using System.Security.Claims;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SlpModularCms.Core.SiteSettings;
@@ -24,7 +25,7 @@ public class SiteSettingsController : ControllerBase
[Authorize(Policy = "OwnerOnly")]
public async Task<IActionResult> SetMail([FromBody] UpdateMailSettingsRequest request, CancellationToken ct)
{
var result = await _settings.SetMailAsync(request.FromAddress, request.FromName, User.Identity?.Name, ct);
var result = await _settings.SetMailAsync(request.FromAddress, request.FromName, User.FindFirstValue(ClaimTypes.Email) ?? User.Identity?.Name, ct);
if (result.Ok) return Ok(await _settings.GetMailAsync(ct));
return result.Error == SetMailResult.MailDisabled
@@ -334,17 +334,36 @@ public class UsersController : ControllerBase
return NoContent();
}
/// <summary>
/// The caller's user id from the token. A real access token carries the id (<c>sub</c>, mapped to the name
/// identifier) and the e-mail address, but NO name claim, so <c>User.Identity.Name</c> is null: anything that
/// has to know who is calling must go by this id.
/// </summary>
private Guid? CurrentUserId =>
Guid.TryParse(User.FindFirstValue(ClaimTypes.NameIdentifier) ?? User.FindFirstValue("sub"), out var id) ? id : null;
private bool IsSelf(ApplicationUser target) =>
string.Equals(target.Email, User.Identity?.Name, StringComparison.OrdinalIgnoreCase);
CurrentUserId is { } id
? id == target.Id
: string.Equals(target.Email, User.Identity?.Name, StringComparison.OrdinalIgnoreCase);
private async Task<ApplicationUser?> GetRequesterAsync()
{
if (CurrentUserId is { } id)
return await _userManager.FindByIdAsync(id.ToString());
var name = User.Identity?.Name;
return name is not null ? await _userManager.FindByNameAsync(name) : null;
}
/// <summary>
/// The role of the caller from the database (not the token, which can be stale). A caller that cannot
/// be resolved is treated as an Administrator, the most restricted role that reaches this controller.
/// The role of the caller from the database (not the token, which can be stale), found by the user id of
/// the token. A caller that cannot be resolved is treated as an Administrator, the most restricted role
/// that reaches this controller.
/// </summary>
private async Task<string> GetRequesterRoleAsync()
{
var name = User.Identity?.Name;
var requester = name is not null ? await _userManager.FindByNameAsync(name) : null;
var requester = await GetRequesterAsync();
if (requester is null) return UserManagementRules.Administrator;
var roles = await _userManager.GetRolesAsync(requester);