-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SendConfirmEmailRule.cs
143 lines (130 loc) · 4.5 KB
/
SendConfirmEmailRule.cs
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using AutoMapper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Security.Claims;
using System.Threading.Tasks;
namespace ServiceBricks.Security
{
/// <summary>
/// This business rule happens when a user registers.
/// </summary>
public partial class SendConfirmEmailRule : BusinessRule
{
private readonly ILogger _logger;
private readonly IServiceBus _serviceBus;
public SendConfirmEmailRule(
ILoggerFactory loggerFactory,
IServiceBus serviceBus)
{
_logger = loggerFactory.CreateLogger<SendConfirmEmailRule>();
_serviceBus = serviceBus;
Priority = PRIORITY_NORMAL;
}
/// <summary>
/// Register the business rule.
/// </summary>
public static void RegisterRule(IBusinessRuleRegistry registry)
{
registry.RegisterItem(
typeof(SendConfirmEmailProcess),
typeof(SendConfirmEmailRule));
}
/// <summary>
/// Execute the business rule.
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public override IResponse ExecuteRule(IBusinessRuleContext context)
{
var response = new Response();
try
{
var obj = context.Object as SendConfirmEmailProcess;
if (obj == null)
return response;
// Create Email Event
var emailHtml = EMAIL_TEMPLATE_HTML.Replace("{0}", obj.CallbackUrl);
var emailText = EMAIL_TEMPLATE_TEXT.Replace("{0}", obj.CallbackUrl);
ApplicationEmailDto email = new ApplicationEmailDto()
{
ToAddress = obj.ApplicationUser.Email,
Subject = "Verify Your Email",
Body = emailText,
BodyHtml = emailHtml,
IsHtml = true
};
var createEmailBroadcast = new CreateApplicationEmailBroadcast(email);
_serviceBus.Send(createEmailBroadcast);
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message);
response.AddMessage(ResponseMessage.CreateError(LocalizationResource.ERROR_BUSINESS_RULE));
}
return response;
}
/// <summary>
/// Execute the business rule.
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public override async Task<IResponse> ExecuteRuleAsync(IBusinessRuleContext context)
{
var response = new Response();
try
{
var obj = context.Object as SendConfirmEmailProcess;
if (obj == null)
return response;
// Create Email Event
var emailHtml = EMAIL_TEMPLATE_HTML.Replace("{0}", obj.CallbackUrl);
var emailText = EMAIL_TEMPLATE_TEXT.Replace("{0}", obj.CallbackUrl);
ApplicationEmailDto email = new ApplicationEmailDto()
{
ToAddress = obj.ApplicationUser.Email,
Subject = "Verify Your Email",
Body = emailText,
BodyHtml = emailHtml,
IsHtml = true
};
var createEmailBroadcast = new CreateApplicationEmailBroadcast(email);
await _serviceBus.SendAsync(createEmailBroadcast);
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message);
response.AddMessage(ResponseMessage.CreateError(LocalizationResource.ERROR_BUSINESS_RULE));
}
return response;
}
private string EMAIL_TEMPLATE_TEXT = @"Verify Your Email.
Copy and paste the following link into a web browser to verify your email.
{0}";
private string EMAIL_TEMPLATE_HTML = @"
<!DOCTYPE html>
<html lang=""en"">
<head>
<meta name=""viewport"" content=""width=device-width"">
<title>Verify Your Email</title>
<body>
<h1>Verify Your Email</h1>
<p>
<a href=""{0}"">Click Here to verify your email.</a>
</p>
<p>
Or
</p>
<p>
Copy and paste the following link into a web browser to verify your email.
</p>
<p>
{0}
</p>
</body>
</html>
";
}
}