aboutsummaryrefslogtreecommitdiffstats
path: root/code/api/src/Services/EmailValidationService.cs
blob: 5d909f3c5fba0158d59339a28bc631860e6e11a6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
namespace IOL.GreatOffice.Api.Services;

public class EmailValidationService
{
    private readonly IStringLocalizer<SharedResources> _localizer;
    private readonly MainAppDatabase _database;
    private readonly MailService _mailService;
    private readonly ILogger<EmailValidationService> _logger;
    private readonly string EmailValidationUrl;

    public EmailValidationService(IStringLocalizer<SharedResources> localizer, MainAppDatabase database, MailService mailService, ILogger<EmailValidationService> logger)
    {
        _localizer = localizer;
        _database = database;
        _mailService = mailService;
        _logger = logger;
        EmailValidationUrl = Program.AppConfiguration.CANONICAL_BACKEND_URL + "/_/validate";
    }

    public bool FulfillEmailValidationRequest(Guid id, Guid userId)
    {
        var item = _database.ValidationEmails.FirstOrDefault(c => c.Id == id);
        if (item == default)
        {
            _logger.LogDebug("Did not find email validation request with id: {requestId}", id);
            return false;
        }

        if (item.UserId != userId)
        {
            _logger.LogInformation("An unknown user tried to validate the email validation request {requestId}", id);
            return false;
        }

        var user = _database.Users.FirstOrDefault(c => c.Id == item.UserId);
        if (user == default)
        {
            _database.ValidationEmails.Remove(item);
            _database.SaveChanges();
            _logger.LogInformation("Deleting request {requestId} because user does not exist anymore", id);
            return false;
        }

        user.EmailLastValidated = AppDateTime.UtcNow;
        _database.ValidationEmails.Remove(item);
        _database.Users.Update(user);
        _database.SaveChanges();
        _logger.LogInformation("Successfully validated the email for user {userId}", user.Id);
        return true;
    }

    public async Task SendValidationEmailAsync(User user)
    {
        var queueItem = new ValidationEmail()
        {
            UserId = user.Id,
            Id = Guid.NewGuid()
        };
        var email = new MailService.PostmarkEmail()
        {
            To = user.Username,
            Subject = _localizer["Greatoffice Email Validation"],
            TextBody = _localizer["""
Hello {0},

Validate your email address by opening this link in a browser {1}
""", user.DisplayName(true), EmailValidationUrl + "?id=" + queueItem.Id]
        };
        queueItem.EmailSentAt = AppDateTime.UtcNow;
        _database.ValidationEmails.Add(queueItem);
        await _database.SaveChangesAsync();
        await Task.Run(async () => await _mailService.SendMailAsync(email));
    }
}