Adds user management
This commit is contained in:
@@ -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