Adds user management
This commit is contained in:
@@ -4,3 +4,15 @@ public record CreateOwnerRequest(string Name, string Email, string Password);
|
||||
public record LoginRequest(string Email, string Password);
|
||||
public record InviteUserRequest(string Email, string Role);
|
||||
public record CompleteSetupRequest(string Token, string Password);
|
||||
public record ChangeRoleRequest(string NewRole);
|
||||
public record SetUserActiveRequest(bool IsActive);
|
||||
public record PendingInvitationInfo(Guid Id, string Email, string Role, string Token, DateTimeOffset CreatedAt);
|
||||
public record UserDto(
|
||||
Guid Id,
|
||||
string Email,
|
||||
string Name,
|
||||
string Role,
|
||||
bool IsActive,
|
||||
DateTimeOffset CreatedAt,
|
||||
bool InvitationPending,
|
||||
string? InviteLink);
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
using SlpModularCms.Core.Identity.Models;
|
||||
|
||||
namespace SlpModularCms.Core.Identity.Services;
|
||||
|
||||
/// <summary>
|
||||
@@ -19,4 +21,29 @@ public interface IInvitationService
|
||||
/// Voltooit de uitnodiging door een gebruiker aan te maken met het opgegeven wachtwoord.
|
||||
/// </summary>
|
||||
Task CompleteInvitationAsync(string token, string password);
|
||||
|
||||
/// <summary>
|
||||
/// Geeft de pending uitnodiging voor een e-mailadres terug, of (false, null) als er geen is.
|
||||
/// </summary>
|
||||
Task<(bool IsPending, string? Token)> GetPendingInvitationByEmailAsync(string email);
|
||||
|
||||
/// <summary>
|
||||
/// Verwijdert alle pending (niet-geaccepteerde) uitnodigingen voor een e-mailadres.
|
||||
/// </summary>
|
||||
Task DeletePendingInvitationsByEmailAsync(string email);
|
||||
|
||||
/// <summary>
|
||||
/// Geeft alle actieve (niet-geaccepteerde, niet-verlopen) uitnodigingen terug.
|
||||
/// </summary>
|
||||
Task<IReadOnlyList<PendingInvitationInfo>> GetAllPendingInvitationsAsync();
|
||||
|
||||
/// <summary>
|
||||
/// Verwijdert een pending uitnodiging op basis van het ID. Retourneert false als niet gevonden.
|
||||
/// </summary>
|
||||
Task<bool> DeleteInvitationByIdAsync(Guid invitationId);
|
||||
|
||||
/// <summary>
|
||||
/// Wijzigt de rol van een pending uitnodiging. Retourneert false als niet gevonden.
|
||||
/// </summary>
|
||||
Task<bool> UpdateInvitationRoleAsync(Guid invitationId, string newRole);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ using Microsoft.EntityFrameworkCore;
|
||||
using SlpModularCms.Core.Data;
|
||||
using SlpModularCms.Core.Exceptions;
|
||||
using SlpModularCms.Core.Identity.Entities;
|
||||
using SlpModularCms.Core.Identity.Models;
|
||||
|
||||
namespace SlpModularCms.Core.Identity.Services;
|
||||
|
||||
@@ -77,6 +78,59 @@ public class InvitationService : IInvitationService
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task<(bool IsPending, string? Token)> GetPendingInvitationByEmailAsync(string email)
|
||||
{
|
||||
var invitation = await _context.Invitations
|
||||
.FirstOrDefaultAsync(i => i.Email == email && !i.IsAccepted && i.ExpiryDate > DateTimeOffset.UtcNow);
|
||||
return invitation is null ? (false, null) : (true, invitation.Token);
|
||||
}
|
||||
|
||||
public async Task DeletePendingInvitationsByEmailAsync(string email)
|
||||
{
|
||||
var invitations = await _context.Invitations
|
||||
.Where(i => i.Email == email && !i.IsAccepted)
|
||||
.ToListAsync();
|
||||
if (invitations.Count > 0)
|
||||
{
|
||||
_context.Invitations.RemoveRange(invitations);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<PendingInvitationInfo>> GetAllPendingInvitationsAsync()
|
||||
{
|
||||
var invitations = await _context.Invitations
|
||||
.Where(i => !i.IsAccepted && i.ExpiryDate > DateTimeOffset.UtcNow)
|
||||
.OrderBy(i => i.CreatedAt)
|
||||
.ToListAsync();
|
||||
|
||||
return invitations
|
||||
.Select(i => new PendingInvitationInfo(i.Id, i.Email, i.Role, i.Token, i.CreatedAt))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteInvitationByIdAsync(Guid invitationId)
|
||||
{
|
||||
var invitation = await _context.Invitations
|
||||
.FirstOrDefaultAsync(i => i.Id == invitationId && !i.IsAccepted);
|
||||
if (invitation is null) return false;
|
||||
|
||||
_context.Invitations.Remove(invitation);
|
||||
await _context.SaveChangesAsync();
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateInvitationRoleAsync(Guid invitationId, string newRole)
|
||||
{
|
||||
var invitation = await _context.Invitations
|
||||
.FirstOrDefaultAsync(i => i.Id == invitationId && !i.IsAccepted);
|
||||
if (invitation is null) return false;
|
||||
|
||||
invitation.Role = newRole;
|
||||
await _context.SaveChangesAsync();
|
||||
return true;
|
||||
}
|
||||
|
||||
private static string GenerateSecureToken()
|
||||
{
|
||||
var bytes = new byte[32];
|
||||
|
||||
Reference in New Issue
Block a user