-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
RegisterUserTask.cs
181 lines (163 loc) · 7.56 KB
/
RegisterUserTask.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
using System.Collections.Generic;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc.Localization;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using OrchardCore.DisplayManagement.ModelBinding;
using OrchardCore.Email;
using OrchardCore.Users.Models;
using OrchardCore.Users.Services;
using OrchardCore.Workflows.Abstractions.Models;
using OrchardCore.Workflows.Activities;
using OrchardCore.Workflows.Models;
using OrchardCore.Workflows.Services;
namespace OrchardCore.Users.Workflows.Activities
{
public class RegisterUserTask : TaskActivity
{
private readonly IUserService _userService;
private readonly UserManager<IUser> _userManager;
private readonly IWorkflowExpressionEvaluator _expressionEvaluator;
private readonly LinkGenerator _linkGenerator;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IUpdateModelAccessor _updateModelAccessor;
private readonly IStringLocalizer S;
private readonly HtmlEncoder _htmlEncoder;
public RegisterUserTask(
IUserService userService,
UserManager<IUser> userManager,
IWorkflowExpressionEvaluator expressionEvaluator,
LinkGenerator linkGenerator,
IHttpContextAccessor httpContextAccessor,
IUpdateModelAccessor updateModelAccessor,
IStringLocalizer<RegisterUserTask> localizer,
HtmlEncoder htmlEncoder)
{
_userService = userService;
_userManager = userManager;
_expressionEvaluator = expressionEvaluator;
_linkGenerator = linkGenerator;
_httpContextAccessor = httpContextAccessor;
_updateModelAccessor = updateModelAccessor;
S = localizer;
_htmlEncoder = htmlEncoder;
}
// The technical name of the activity. Activities on a workflow definition reference this name.
public override string Name => nameof(RegisterUserTask);
public override LocalizedString DisplayText => S["Register User Task"];
// The category to which this activity belongs. The activity picker groups activities by this category.
public override LocalizedString Category => S["User"];
// The message to display.
public bool SendConfirmationEmail
{
get => GetProperty(() => true);
set => SetProperty(value);
}
public WorkflowExpression<string> ConfirmationEmailSubject
{
get => GetProperty(() => new WorkflowExpression<string>());
set => SetProperty(value);
}
// The message to display.
public WorkflowExpression<string> ConfirmationEmailTemplate
{
get => GetProperty(() => new WorkflowExpression<string>());
set => SetProperty(value);
}
public bool RequireModeration
{
get => GetProperty(() => false);
set => SetProperty(value);
}
// Returns the possible outcomes of this activity.
public override IEnumerable<Outcome> GetPossibleOutcomes(WorkflowExecutionContext workflowContext, ActivityContext activityContext)
{
return Outcomes(S["Done"], S["Valid"], S["Invalid"]);
}
// This is the heart of the activity and actually performs the work to be done.
public override async Task<ActivityExecutionResult> ExecuteAsync(WorkflowExecutionContext workflowContext, ActivityContext activityContext)
{
bool isValid = false;
IFormCollection form = null;
string email = null;
if (_httpContextAccessor.HttpContext != null)
{
form = _httpContextAccessor.HttpContext.Request.Form;
email = form["Email"];
isValid = !string.IsNullOrWhiteSpace(email);
}
var outcome = isValid ? "Valid" : "Invalid";
if (isValid)
{
var userName = form["UserName"];
if (string.IsNullOrWhiteSpace(userName))
{
userName = email.Replace('@', '+');
}
var errors = new Dictionary<string, string>();
var user = (User)await _userService.CreateUserAsync(new User() { UserName = userName, Email = email, IsEnabled = !RequireModeration }, null, (key, message) => errors.Add(key, message));
if (errors.Count > 0)
{
var updater = _updateModelAccessor.ModelUpdater;
if (updater != null)
{
foreach (var item in errors)
{
updater.ModelState.TryAddModelError(item.Key, S[item.Value]);
}
}
outcome = "Invalid";
}
else if (SendConfirmationEmail)
{
var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
var uri = _linkGenerator.GetUriByAction(_httpContextAccessor.HttpContext, "ConfirmEmail",
"Registration", new { area = "OrchardCore.Users", userId = user.UserId, code });
workflowContext.Properties["EmailConfirmationUrl"] = uri;
var subject = await _expressionEvaluator.EvaluateAsync(ConfirmationEmailSubject, workflowContext, null);
var localizedSubject = new LocalizedString(nameof(RegisterUserTask), subject);
var body = await _expressionEvaluator.EvaluateAsync(ConfirmationEmailTemplate, workflowContext, _htmlEncoder);
var localizedBody = new LocalizedHtmlString(nameof(RegisterUserTask), body);
var message = new MailMessage()
{
To = email,
Subject = localizedSubject.ResourceNotFound ? subject : localizedSubject.Value,
Body = localizedBody.IsResourceNotFound ? body : localizedBody.Value,
IsBodyHtml = true
};
var smtpService = _httpContextAccessor.HttpContext.RequestServices.GetService<ISmtpService>();
if (smtpService == null)
{
var updater = _updateModelAccessor.ModelUpdater;
if (updater != null)
{
updater.ModelState.TryAddModelError("", S["No email service is available"]);
}
outcome = "Invalid";
}
else
{
var result = await smtpService.SendAsync(message);
if (!result.Succeeded)
{
var updater = _updateModelAccessor.ModelUpdater;
if (updater != null)
{
foreach (var item in result.Errors)
{
updater.ModelState.TryAddModelError(item.Name, item.Value);
}
}
outcome = "Invalid";
}
}
}
}
return Outcomes("Done", outcome);
}
}
}