U2 identity-mail-flows (backend): taal per gebruiker, uitnodigingsmail, opnieuw versturen, meldingen, stamp-controle
Kolommen Users.PreferredLanguage en Invitations.Language (additieve migratie). IdentityMailService verstuurt uitnodiging, nieuwe link en meldingen bij deactiveren, heractiveren en rolwijziging; een mailfout laat nooit een actie falen, het resultaat gaat mee in de respons naast de kopieerbare link. Nieuwe actie resend-invite. De security stamp zit in het access token en wordt vergeleken in de actief-controle: een wachtwoordwijziging beeindigt eerder uitgegeven access tokens, de refresh geeft stil een nieuw token. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SlpModularCms.Core.Email;
|
||||
using SlpModularCms.Core.Identity.Authorization;
|
||||
using SlpModularCms.Core.Identity.Entities;
|
||||
using SlpModularCms.Core.Identity.Models;
|
||||
@@ -17,11 +18,16 @@ public class UsersController : ControllerBase
|
||||
{
|
||||
private readonly IInvitationService _invitationService;
|
||||
private readonly UserManager<ApplicationUser> _userManager;
|
||||
private readonly IIdentityMailService _mail;
|
||||
|
||||
public UsersController(IInvitationService invitationService, UserManager<ApplicationUser> userManager)
|
||||
public UsersController(
|
||||
IInvitationService invitationService,
|
||||
UserManager<ApplicationUser> userManager,
|
||||
IIdentityMailService mail)
|
||||
{
|
||||
_invitationService = invitationService;
|
||||
_userManager = userManager;
|
||||
_mail = mail;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
@@ -53,7 +59,9 @@ public class UsersController : ControllerBase
|
||||
user.IsActive,
|
||||
user.CreatedAt,
|
||||
isPending,
|
||||
inviteLink));
|
||||
inviteLink,
|
||||
false,
|
||||
user.PreferredLanguage));
|
||||
}
|
||||
|
||||
// Include pending invitations that have no user account yet
|
||||
@@ -71,7 +79,8 @@ public class UsersController : ControllerBase
|
||||
inv.CreatedAt,
|
||||
true,
|
||||
inv.IsExpired ? null : $"/invite/complete?token={Uri.EscapeDataString(inv.Token)}",
|
||||
inv.IsExpired));
|
||||
inv.IsExpired,
|
||||
inv.Language));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,6 +108,18 @@ public class UsersController : ControllerBase
|
||||
return BadRequest(new { detail = "Email is already in use." });
|
||||
}
|
||||
|
||||
// null = leave the preference alone (older clients); "" = clear it (use the site default);
|
||||
// otherwise nl or en.
|
||||
if (request.Language is not null)
|
||||
{
|
||||
if (request.Language.Length == 0)
|
||||
user.PreferredLanguage = null;
|
||||
else if (MailLanguages.Normalize(request.Language) is { } language)
|
||||
user.PreferredLanguage = language;
|
||||
else
|
||||
return BadRequest(new { detail = "Unsupported language." });
|
||||
}
|
||||
|
||||
user.DisplayName = request.Name;
|
||||
user.Email = request.Email;
|
||||
user.UserName = request.Email;
|
||||
@@ -120,7 +141,9 @@ public class UsersController : ControllerBase
|
||||
user.IsActive,
|
||||
user.CreatedAt,
|
||||
false,
|
||||
null));
|
||||
null,
|
||||
false,
|
||||
user.PreferredLanguage));
|
||||
}
|
||||
|
||||
[HttpPost("invite")]
|
||||
@@ -130,9 +153,15 @@ public class UsersController : ControllerBase
|
||||
if (!UserManagementRules.CanAssign(requesterRole, request.Role))
|
||||
return Forbid();
|
||||
|
||||
var token = await _invitationService.CreateInvitationAsync(request.Email, request.Role);
|
||||
if (!string.IsNullOrEmpty(request.Language) && !MailLanguages.IsSupported(request.Language))
|
||||
return BadRequest(new { detail = "Unsupported language." });
|
||||
|
||||
var token = await _invitationService.CreateInvitationAsync(request.Email, request.Role, request.Language);
|
||||
var inviteLink = $"/invite/complete?token={Uri.EscapeDataString(token)}";
|
||||
return Ok(new { inviteLink });
|
||||
|
||||
// The invitation exists whatever happens to the mail; the result and the copyable link both go back.
|
||||
var mail = await _mail.SendInvitationAsync(request.Email, token, request.Language, isNewLink: false, HttpContext.RequestAborted);
|
||||
return Ok(new { inviteLink, mail = MailBody(mail) });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -142,16 +171,44 @@ public class UsersController : ControllerBase
|
||||
[HttpPost("{invitationId:guid}/regenerate-invite")]
|
||||
public async Task<IActionResult> RegenerateInvite(Guid invitationId)
|
||||
{
|
||||
var invitationRole = await GetInvitationRoleAsync(invitationId);
|
||||
if (invitationRole is null) return NotFound();
|
||||
var invitation = await GetInvitationAsync(invitationId);
|
||||
if (invitation is null) return NotFound();
|
||||
|
||||
if (!UserManagementRules.CanManage(await GetRequesterRoleAsync(), invitationRole))
|
||||
if (!UserManagementRules.CanManage(await GetRequesterRoleAsync(), invitation.Role))
|
||||
return Forbid();
|
||||
|
||||
var token = await _invitationService.RegenerateInvitationAsync(invitationId);
|
||||
if (token is null) return NotFound();
|
||||
|
||||
return Ok(new { inviteLink = $"/invite/complete?token={Uri.EscapeDataString(token)}" });
|
||||
var mail = await _mail.SendInvitationAsync(invitation.Email, token, invitation.Language, isNewLink: true, HttpContext.RequestAborted);
|
||||
return Ok(new { inviteLink = $"/invite/complete?token={Uri.EscapeDataString(token)}", mail = MailBody(mail) });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends the invitation mail again: the current link while it is valid, a freshly created one (new
|
||||
/// token and lifetime) when it has expired. Returns the link and the mail result, so a failed mail
|
||||
/// can be retried and the link can still be copied.
|
||||
/// </summary>
|
||||
[HttpPost("{invitationId:guid}/resend-invite")]
|
||||
public async Task<IActionResult> ResendInvite(Guid invitationId)
|
||||
{
|
||||
var invitation = await GetInvitationAsync(invitationId);
|
||||
if (invitation is null) return NotFound();
|
||||
|
||||
if (!UserManagementRules.CanManage(await GetRequesterRoleAsync(), invitation.Role))
|
||||
return Forbid();
|
||||
|
||||
var token = invitation.Token;
|
||||
var isNewLink = invitation.IsExpired;
|
||||
if (isNewLink)
|
||||
{
|
||||
var fresh = await _invitationService.RegenerateInvitationAsync(invitationId);
|
||||
if (fresh is null) return NotFound();
|
||||
token = fresh;
|
||||
}
|
||||
|
||||
var mail = await _mail.SendInvitationAsync(invitation.Email, token, invitation.Language, isNewLink, HttpContext.RequestAborted);
|
||||
return Ok(new { inviteLink = $"/invite/complete?token={Uri.EscapeDataString(token)}", mail = MailBody(mail) });
|
||||
}
|
||||
|
||||
[HttpPut("{userId:guid}/role")]
|
||||
@@ -194,6 +251,10 @@ public class UsersController : ControllerBase
|
||||
await _userManager.RemoveFromRolesAsync(target, targetRoles);
|
||||
|
||||
await _userManager.AddToRoleAsync(target, request.NewRole);
|
||||
|
||||
if (targetRole != request.NewRole)
|
||||
await _mail.SendRoleChangedAsync(target, request.NewRole, HttpContext.RequestAborted);
|
||||
|
||||
return Ok();
|
||||
}
|
||||
|
||||
@@ -218,11 +279,19 @@ public class UsersController : ControllerBase
|
||||
return BadRequest(new { detail = "At least one Owner must remain." });
|
||||
}
|
||||
|
||||
var wasActive = target.IsActive;
|
||||
target.IsActive = request.IsActive;
|
||||
var result = await _userManager.UpdateAsync(target);
|
||||
if (!result.Succeeded)
|
||||
return StatusCode(500, new { detail = "Failed to update user." });
|
||||
|
||||
// A notice only on a real change; a mail failure never fails the action.
|
||||
if (wasActive != request.IsActive)
|
||||
{
|
||||
if (request.IsActive) await _mail.SendAccountReactivatedAsync(target, HttpContext.RequestAborted);
|
||||
else await _mail.SendAccountDeactivatedAsync(target, HttpContext.RequestAborted);
|
||||
}
|
||||
|
||||
return Ok();
|
||||
}
|
||||
|
||||
@@ -282,9 +351,16 @@ public class UsersController : ControllerBase
|
||||
return roles.FirstOrDefault() ?? UserManagementRules.User;
|
||||
}
|
||||
|
||||
private async Task<string?> GetInvitationRoleAsync(Guid invitationId)
|
||||
private async Task<string?> GetInvitationRoleAsync(Guid invitationId) =>
|
||||
(await GetInvitationAsync(invitationId))?.Role;
|
||||
|
||||
private async Task<PendingInvitationInfo?> GetInvitationAsync(Guid invitationId)
|
||||
{
|
||||
var all = await _invitationService.GetAllPendingInvitationsAsync();
|
||||
return all?.FirstOrDefault(i => i.Id == invitationId)?.Role;
|
||||
return all?.FirstOrDefault(i => i.Id == invitationId);
|
||||
}
|
||||
|
||||
/// <summary>The mail result for the client; null when this site has no mail (nothing to show).</summary>
|
||||
private static object? MailBody(MailSendResult result) =>
|
||||
result.ErrorCode == MailSendResult.Disabled ? null : new { sent = result.Sent, error = result.ErrorCode };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user