Initial commit with inital CMS
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
using System.Security.Cryptography;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SlpModularCms.Core.Data;
|
||||
using SlpModularCms.Core.Exceptions;
|
||||
using SlpModularCms.Core.Identity.Entities;
|
||||
|
||||
namespace SlpModularCms.Core.Identity.Services;
|
||||
|
||||
public class InvitationService : IInvitationService
|
||||
{
|
||||
private readonly ApplicationDbContext _context;
|
||||
private readonly UserManager<ApplicationUser> _userManager;
|
||||
|
||||
public InvitationService(ApplicationDbContext context, UserManager<ApplicationUser> userManager)
|
||||
{
|
||||
_context = context;
|
||||
_userManager = userManager;
|
||||
}
|
||||
|
||||
public async Task<string> CreateInvitationAsync(string email, string role)
|
||||
{
|
||||
var invitationForEmail = await _context.Invitations.FirstOrDefaultAsync(x => x.Email == email);
|
||||
var user = await _userManager.FindByEmailAsync(email);
|
||||
|
||||
if (invitationForEmail != null || user != null)
|
||||
{
|
||||
throw new InvitationOrUserAlreadyExistsException("Een uitnodiging of gebruiker voor dit e-mailadres bestaat al.");
|
||||
}
|
||||
|
||||
var token = GenerateSecureToken();
|
||||
var invitation = new Invitation
|
||||
{
|
||||
Email = email,
|
||||
Role = role,
|
||||
Token = token,
|
||||
ExpiryDate = DateTimeOffset.UtcNow.AddDays(1),
|
||||
IsAccepted = false
|
||||
};
|
||||
|
||||
_context.Invitations.Add(invitation);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return token;
|
||||
}
|
||||
|
||||
public async Task<bool> ValidateInvitationAsync(string token)
|
||||
{
|
||||
var invitation = await _context.Invitations.FirstOrDefaultAsync(i => i.Token == token);
|
||||
return invitation != null && !invitation.IsAccepted && !invitation.IsExpired;
|
||||
}
|
||||
|
||||
public async Task CompleteInvitationAsync(string token, string password)
|
||||
{
|
||||
var invitation = await _context.Invitations.FirstOrDefaultAsync(i => i.Token == token);
|
||||
if (invitation == null || invitation.IsAccepted || invitation.IsExpired)
|
||||
{
|
||||
throw new UnauthorizedException("Ongeldige of verlopen uitnodiging.");
|
||||
}
|
||||
|
||||
var user = new ApplicationUser
|
||||
{
|
||||
UserName = invitation.Email,
|
||||
Email = invitation.Email,
|
||||
IsActive = true
|
||||
};
|
||||
|
||||
var result = await _userManager.CreateAsync(user, password);
|
||||
if (!result.Succeeded)
|
||||
{
|
||||
throw new ValidationException("Kan gebruiker niet aanmaken: " + string.Join(", ", result.Errors.Select(e => e.Description)));
|
||||
}
|
||||
|
||||
await _userManager.AddToRoleAsync(user, invitation.Role);
|
||||
|
||||
invitation.IsAccepted = true;
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
private static string GenerateSecureToken()
|
||||
{
|
||||
var bytes = new byte[32];
|
||||
using var rng = RandomNumberGenerator.Create();
|
||||
rng.GetBytes(bytes);
|
||||
return Convert.ToBase64String(bytes);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user