Added email helper method to send token for user account update.
This commit is contained in:
@@ -8,6 +8,7 @@ namespace CarCareTracker.Helper
|
|||||||
{
|
{
|
||||||
OperationResponse NotifyUserForRegistration(string emailAddress, string token);
|
OperationResponse NotifyUserForRegistration(string emailAddress, string token);
|
||||||
OperationResponse NotifyUserForPasswordReset(string emailAddress, string token);
|
OperationResponse NotifyUserForPasswordReset(string emailAddress, string token);
|
||||||
|
OperationResponse NotifyUserForAccountUpdate(string emailAddress, string token);
|
||||||
OperationResponse NotifyUserForReminders(Vehicle vehicle, List<string> emailAddresses, List<ReminderRecordViewModel> reminders);
|
OperationResponse NotifyUserForReminders(Vehicle vehicle, List<string> emailAddresses, List<ReminderRecordViewModel> reminders);
|
||||||
}
|
}
|
||||||
public class MailHelper : IMailHelper
|
public class MailHelper : IMailHelper
|
||||||
@@ -64,6 +65,28 @@ namespace CarCareTracker.Helper
|
|||||||
return new OperationResponse { Success = false, Message = StaticHelper.GenericErrorMessage };
|
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<string> emailAddresses, List<ReminderRecordViewModel> reminders)
|
public OperationResponse NotifyUserForReminders(Vehicle vehicle, List<string> emailAddresses, List<ReminderRecordViewModel> reminders)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(mailConfig.EmailServer))
|
if (string.IsNullOrWhiteSpace(mailConfig.EmailServer))
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ namespace CarCareTracker.Logic
|
|||||||
bool CheckIfUserIsValid(int userId);
|
bool CheckIfUserIsValid(int userId);
|
||||||
bool CreateRootUserCredentials(LoginModel credentials);
|
bool CreateRootUserCredentials(LoginModel credentials);
|
||||||
bool DeleteRootUserCredentials();
|
bool DeleteRootUserCredentials();
|
||||||
|
bool GenerateTokenForEmailAddress(string emailAddress, bool isPasswordReset);
|
||||||
List<UserData> GetAllUsers();
|
List<UserData> GetAllUsers();
|
||||||
List<Token> GetAllTokens();
|
List<Token> GetAllTokens();
|
||||||
|
|
||||||
@@ -196,21 +197,7 @@ namespace CarCareTracker.Logic
|
|||||||
if (existingUser.Id != default)
|
if (existingUser.Id != default)
|
||||||
{
|
{
|
||||||
//user exists, generate a token and send email.
|
//user exists, generate a token and send email.
|
||||||
//check to see if there is an existing token sent to the user.
|
GenerateTokenForEmailAddress(existingUser.EmailAddress, true);
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
//for security purposes we want to always return true for this method.
|
//for security purposes we want to always return true for this method.
|
||||||
//otherwise someone can spam the reset password method to sniff out users.
|
//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);
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user