From 2247b1b1db47de244d0cc74520214730ea944b9f Mon Sep 17 00:00:00 2001 From: "DESKTOP-GENO133\\IvanPlex" Date: Sat, 13 Jan 2024 11:48:20 -0700 Subject: [PATCH] added ability to notify user that they have a registration token. --- Controllers/AdminController.cs | 32 +------------- Helper/MailHelper.cs | 77 ++++++++++++++++++++++++++++++++++ Helper/StaticHelper.cs | 1 + Logic/LoginLogic.cs | 26 +++++++++--- Program.cs | 1 + Views/Admin/Index.cshtml | 11 +++-- 6 files changed, 110 insertions(+), 38 deletions(-) create mode 100644 Helper/MailHelper.cs diff --git a/Controllers/AdminController.cs b/Controllers/AdminController.cs index a704d34..fe62c74 100644 --- a/Controllers/AdminController.cs +++ b/Controllers/AdminController.cs @@ -24,11 +24,9 @@ namespace CarCareTracker.Controllers }; return View(viewModel); } - public IActionResult GenerateNewToken(string emailAddress) + public IActionResult GenerateNewToken(string emailAddress, bool autoNotify) { - var result = _loginLogic.GenerateUserToken(emailAddress); - //send an email test block. - SendEmail(emailAddress); + var result = _loginLogic.GenerateUserToken(emailAddress, autoNotify); return Json(result); } public IActionResult DeleteToken(int tokenId) @@ -36,31 +34,5 @@ namespace CarCareTracker.Controllers var result = _loginLogic.DeleteUserToken(tokenId); return Json(result); } - private bool SendEmail(string emailAddress) - { - var mailConfig = new MailConfig(); - string to = emailAddress; - string from = mailConfig.EmailFrom; - var server = mailConfig.EmailServer; - MailMessage message = new MailMessage(from, to); - message.Subject = "Using the new SMTP client."; - message.Body = @"Using this new feature, you can send an email message from an application very easily."; - SmtpClient client = new SmtpClient(server); - client.EnableSsl = mailConfig.UseSSL; - client.Port = mailConfig.Port; - client.Credentials = new NetworkCredential(mailConfig.Username, mailConfig.Password); - - try - { - client.Send(message); - return true; - } - catch (Exception ex) - { - Console.WriteLine("Exception caught in CreateTestMessage2(): {0}", - ex.ToString()); - return false; - } - } } } diff --git a/Helper/MailHelper.cs b/Helper/MailHelper.cs new file mode 100644 index 0000000..ed86618 --- /dev/null +++ b/Helper/MailHelper.cs @@ -0,0 +1,77 @@ +using CarCareTracker.Models; +using System.Net.Mail; +using System.Net; + +namespace CarCareTracker.Helper +{ + public interface IMailHelper + { + OperationResponse NotifyUserForRegistration(string emailAddress, string token); + OperationResponse NotifyUserForPasswordReset(string emailAddress, string token); + } + public class MailHelper : IMailHelper + { + private readonly MailConfig mailConfig; + public MailHelper( + IConfiguration config + ) { + //load mailConfig from Configuration + mailConfig = config.GetSection("MailConfig").Get(); + } + public OperationResponse NotifyUserForRegistration(string emailAddress, string token) + { + if (string.IsNullOrWhiteSpace(emailAddress) || string.IsNullOrWhiteSpace(token)) { + return new OperationResponse { Success = false, Message = "Email Address or Token is invalid" }; + } + string emailSubject = "Your Registration Token for LubeLogger"; + string emailBody = $"A token has been generated on your behalf, please complete your registration for LubeLogger using the token: {token}"; + var result = SendEmail(emailAddress, emailSubject, emailBody); + if (result) + { + return new OperationResponse { Success = true, Message = "Email Sent!" }; + } else + { + return new OperationResponse { Success = false, Message = StaticHelper.GenericErrorMessage }; + } + } + public OperationResponse NotifyUserForPasswordReset(string emailAddress, string token) + { + if (string.IsNullOrWhiteSpace(emailAddress) || string.IsNullOrWhiteSpace(token)) + { + return new OperationResponse { Success = false, Message = "Email Address or Token is invalid" }; + } + string emailSubject = "Your Password Reset Token for LubeLogger"; + string emailBody = $"A token has been generated on your behalf, please reset your password for LubeLogger using the token: {token}"; + var result = SendEmail(emailAddress, emailSubject, emailBody); + if (result) + { + return new OperationResponse { Success = true, Message = "Email Sent!" }; + } + else + { + return new OperationResponse { Success = false, Message = StaticHelper.GenericErrorMessage }; + } + } + private bool SendEmail(string emailTo, string emailSubject, string emailBody) { + string to = emailTo; + string from = mailConfig.EmailFrom; + var server = mailConfig.EmailServer; + MailMessage message = new MailMessage(from, to); + message.Subject = emailSubject; + message.Body = emailBody; + SmtpClient client = new SmtpClient(server); + client.EnableSsl = mailConfig.UseSSL; + client.Port = mailConfig.Port; + client.Credentials = new NetworkCredential(mailConfig.Username, mailConfig.Password); + try + { + client.Send(message); + return true; + } + catch (Exception ex) + { + return false; + } + } + } +} diff --git a/Helper/StaticHelper.cs b/Helper/StaticHelper.cs index 2780549..04633cc 100644 --- a/Helper/StaticHelper.cs +++ b/Helper/StaticHelper.cs @@ -7,6 +7,7 @@ { public static string DbName = "data/cartracker.db"; public static string UserConfigPath = "config/userConfig.json"; + public static string GenericErrorMessage = "An error occurred, please try again later"; public static string TruncateStrings(string input, int maxLength = 25) { diff --git a/Logic/LoginLogic.cs b/Logic/LoginLogic.cs index cefd889..30f8e21 100644 --- a/Logic/LoginLogic.cs +++ b/Logic/LoginLogic.cs @@ -10,7 +10,7 @@ namespace CarCareTracker.Logic { public interface ILoginLogic { - bool GenerateUserToken(string emailAddress); + OperationResponse GenerateUserToken(string emailAddress, bool autoNotify); bool DeleteUserToken(int tokenId); OperationResponse RegisterNewUser(LoginModel credentials); OperationResponse ResetUserPassword(LoginModel credentials); @@ -25,10 +25,12 @@ namespace CarCareTracker.Logic { private readonly IUserRecordDataAccess _userData; private readonly ITokenRecordDataAccess _tokenData; - public LoginLogic(IUserRecordDataAccess userData, ITokenRecordDataAccess tokenData) + private readonly IMailHelper _mailHelper; + public LoginLogic(IUserRecordDataAccess userData, ITokenRecordDataAccess tokenData, IMailHelper mailHelper) { _userData = userData; _tokenData = tokenData; + _mailHelper = mailHelper; } public OperationResponse RegisterNewUser(LoginModel credentials) { @@ -113,13 +115,13 @@ namespace CarCareTracker.Logic var result = _tokenData.GetTokens(); return result; } - public bool GenerateUserToken(string emailAddress) + public OperationResponse GenerateUserToken(string emailAddress, bool autoNotify) { //check if email address already has a token attached to it. var existingToken = _tokenData.GetTokenRecordByEmailAddress(emailAddress); if (existingToken.Id != default) { - return false; + return new OperationResponse { Success = false, Message = "There is an existing token tied to this email address" }; } var token = new Token() { @@ -127,7 +129,21 @@ namespace CarCareTracker.Logic EmailAddress = emailAddress }; var result = _tokenData.CreateNewToken(token); - return result; + if (result && autoNotify) + { + result = _mailHelper.NotifyUserForRegistration(emailAddress, token.Body).Success; + if (!result) + { + return new OperationResponse { Success = false, Message = "Token Generated, but Email failed to send, please check your SMTP settings." }; + } + } + if (result) + { + return new OperationResponse { Success = true, Message = "Token Generated!" }; + } else + { + return new OperationResponse { Success = false, Message = StaticHelper.GenericErrorMessage }; + } } public bool DeleteUserToken(int tokenId) { diff --git a/Program.cs b/Program.cs index c521fbe..f8597c5 100644 --- a/Program.cs +++ b/Program.cs @@ -26,6 +26,7 @@ builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); +builder.Services.AddSingleton(); //configur logic builder.Services.AddSingleton(); diff --git a/Views/Admin/Index.cshtml b/Views/Admin/Index.cshtml index a9be61b..d92583b 100644 --- a/Views/Admin/Index.cshtml +++ b/Views/Admin/Index.cshtml @@ -6,6 +6,10 @@
+
+ + +
@@ -88,11 +92,12 @@ }, }).then(function (result) { if (result.isConfirmed) { - $.get('/Admin/GenerateNewToken', {emailAddress: result.value.emailAddress}, function (data) { - if (data) { + var autoNotify = $("#enableAutoNotify").is(":checked"); + $.get('/Admin/GenerateNewToken', {emailAddress: result.value.emailAddress, autoNotify: autoNotify}, function (data) { + if (data.success) { reloadPage(); } else { - errorToast("An error occurred, make sure the email address doesn't already have a token generated for it.") + errorToast(data.message) } }); }