diff options
| author | ivarlovlie <git@ivarlovlie.no> | 2022-12-09 05:38:25 +0100 |
|---|---|---|
| committer | ivarlovlie <git@ivarlovlie.no> | 2022-12-09 05:38:25 +0100 |
| commit | 8da37c77cae0c7f712a775e3996afd9d84b0f9af (patch) | |
| tree | 98bc4898d9800f91387e075bac0fcf97be9969ab /code/api/src/Services/UserService.cs | |
| parent | 59055b8056bd92ea56e97e86d9ec255daf0d3129 (diff) | |
| download | greatoffice-8da37c77cae0c7f712a775e3996afd9d84b0f9af.tar.xz greatoffice-8da37c77cae0c7f712a775e3996afd9d84b0f9af.zip | |
feat: !WIP implement email validation
Diffstat (limited to 'code/api/src/Services/UserService.cs')
| -rw-r--r-- | code/api/src/Services/UserService.cs | 51 |
1 files changed, 36 insertions, 15 deletions
diff --git a/code/api/src/Services/UserService.cs b/code/api/src/Services/UserService.cs index 30231e8..8b925be 100644 --- a/code/api/src/Services/UserService.cs +++ b/code/api/src/Services/UserService.cs @@ -1,23 +1,26 @@ +using IOL.GreatOffice.Api.Data.Database.Queues; +using Microsoft.Extensions.Localization; + namespace IOL.GreatOffice.Api.Services; public class UserService { private readonly PasswordResetService _passwordResetService; + private readonly MailService _mailService; + private readonly ILogger<UserService> _logger; + private readonly IStringLocalizer<SharedResources> _localizer; + private readonly MainAppDatabase _database; + private string EmailValidationUrl; - /// <summary> - /// Provides methods to perform common operations on user data. - /// </summary> - /// <param name="passwordResetService"></param> - public UserService(PasswordResetService passwordResetService) { + public UserService(PasswordResetService passwordResetService, MailService mailService, IStringLocalizer<SharedResources> localizer, VaultService vaultService, MainAppDatabase database) { _passwordResetService = passwordResetService; + _mailService = mailService; + _localizer = localizer; + _database = database; + var configuration = vaultService.GetCurrentAppConfiguration(); + EmailValidationUrl = configuration.CANONICAL_BACKEND_URL + "/validate"; } - /// <summary> - /// Log in a user. - /// </summary> - /// <param name="httpContext"></param> - /// <param name="user"></param> - /// <param name="persist"></param> public async Task LogInUser(HttpContext httpContext, User user, bool persist = false) { var claims = new List<Claim> { new(AppClaims.USER_ID, user.Id.ToString()), @@ -38,13 +41,31 @@ public class UserService await httpContext.SignInAsync(principal, authenticationProperties); await _passwordResetService.DeleteRequestsForUserAsync(user.Id); + _logger.LogInformation("Logged in user {0}", user.Id); } - /// <summary> - /// Log out a user. - /// </summary> - /// <param name="httpContext"></param> public async Task LogOutUser(HttpContext httpContext) { await httpContext.SignOutAsync(); + _logger.LogInformation("Logged out user {0}", httpContext.User.FindFirst(AppClaims.USER_ID)); + } + + public async Task SendValidationEmail(User user) { + 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(), EmailValidationUrl + "?email=" + user.Username] + }; + var queueItem = new ValidationEmail() { + UserId = user.Id, + Id = Guid.NewGuid() + }; + await _mailService.SendMail(email); + queueItem.EmailSentAt = DateTime.UtcNow; + _database.ValidationEmails.Add(queueItem); + await _database.SaveChangesAsync(); } }
\ No newline at end of file |
