diff --git a/Helper/MailHelper.cs b/Helper/MailHelper.cs index 9f30c5f..a2e2cb5 100644 --- a/Helper/MailHelper.cs +++ b/Helper/MailHelper.cs @@ -8,6 +8,7 @@ namespace CarCareTracker.Helper { OperationResponse NotifyUserForRegistration(string emailAddress, string token); OperationResponse NotifyUserForPasswordReset(string emailAddress, string token); + OperationResponse NotifyUserForAccountUpdate(string emailAddress, string token); OperationResponse NotifyUserForReminders(Vehicle vehicle, List emailAddresses, List reminders); } public class MailHelper : IMailHelper @@ -64,6 +65,28 @@ namespace CarCareTracker.Helper return new OperationResponse { Success = false, Message = StaticHelper.GenericErrorMessage }; } } + public OperationResponse NotifyUserForAccountUpdate(string emailAddress, string token) + { + if (string.IsNullOrWhiteSpace(mailConfig.EmailServer)) + { + return new OperationResponse { Success = false, Message = "SMTP Server Not Setup" }; + } + if (string.IsNullOrWhiteSpace(emailAddress) || string.IsNullOrWhiteSpace(token)) + { + return new OperationResponse { Success = false, Message = "Email Address or Token is invalid" }; + } + string emailSubject = "Your User Account Update Token for LubeLogger"; + string emailBody = $"A token has been generated on your behalf, please update your account 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 NotifyUserForReminders(Vehicle vehicle, List emailAddresses, List reminders) { if (string.IsNullOrWhiteSpace(mailConfig.EmailServer)) diff --git a/Logic/LoginLogic.cs b/Logic/LoginLogic.cs index 490089d..f151f73 100644 --- a/Logic/LoginLogic.cs +++ b/Logic/LoginLogic.cs @@ -25,6 +25,7 @@ namespace CarCareTracker.Logic bool CheckIfUserIsValid(int userId); bool CreateRootUserCredentials(LoginModel credentials); bool DeleteRootUserCredentials(); + bool GenerateTokenForEmailAddress(string emailAddress, bool isPasswordReset); List GetAllUsers(); List GetAllTokens(); @@ -196,21 +197,7 @@ namespace CarCareTracker.Logic if (existingUser.Id != default) { //user exists, generate a token and send email. - //check to see if there is an existing token sent to the user. - var existingToken = _tokenData.GetTokenRecordByEmailAddress(existingUser.EmailAddress); - if (existingToken.Id == default) - { - var token = new Token() - { - Body = NewToken(), - EmailAddress = existingUser.EmailAddress - }; - var result = _tokenData.CreateNewToken(token); - if (result) - { - result = _mailHelper.NotifyUserForPasswordReset(existingUser.EmailAddress, token.Body).Success; - } - } + GenerateTokenForEmailAddress(existingUser.EmailAddress, true); } //for security purposes we want to always return true for this method. //otherwise someone can spam the reset password method to sniff out users. @@ -460,5 +447,30 @@ namespace CarCareTracker.Logic { return Guid.NewGuid().ToString().Substring(0, 8); } + public bool GenerateTokenForEmailAddress(string emailAddress, bool isPasswordReset) + { + bool result = false; + //check if there is already a token tied to this email address. + var existingToken = _tokenData.GetTokenRecordByEmailAddress(emailAddress); + if (existingToken.Id == default) + { + //no token, generate one and send. + var token = new Token() + { + Body = NewToken(), + EmailAddress = emailAddress + }; + result = _tokenData.CreateNewToken(token); + if (result) + { + result = isPasswordReset ? _mailHelper.NotifyUserForPasswordReset(emailAddress, token.Body).Success : _mailHelper.NotifyUserForAccountUpdate(emailAddress, token.Body).Success; + } + } else + { + //token exists, send it again. + result = isPasswordReset ? _mailHelper.NotifyUserForPasswordReset(emailAddress, existingToken.Body).Success : _mailHelper.NotifyUserForAccountUpdate(emailAddress, existingToken.Body).Success; + } + return result; + } } }