-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
OSOE-572: Add "Generate Reset Password Token" workflow activity
- Loading branch information
Showing
12 changed files
with
232 additions
and
0 deletions.
There are no files selected for viewing
102 changes: 102 additions & 0 deletions
102
Lombiq.HelpfulExtensions/Extensions/Workflows/Activities/GenerateResetPasswordTokenTask.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
using Lombiq.HelpfulExtensions.Extensions.Workflows.Models; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.AspNetCore.Routing; | ||
using Microsoft.Extensions.Localization; | ||
using OrchardCore.Users; | ||
using OrchardCore.Users.Models; | ||
using OrchardCore.Workflows.Abstractions.Models; | ||
using OrchardCore.Workflows.Activities; | ||
using OrchardCore.Workflows.Models; | ||
using OrchardCore.Workflows.Services; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Lombiq.HelpfulExtensions.Extensions.Workflows.Activities; | ||
|
||
public class GenerateResetPasswordTokenTask : TaskActivity | ||
{ | ||
private readonly IStringLocalizer<GenerateResetPasswordTokenTask> T; | ||
private readonly LinkGenerator _linkGenerator; | ||
private readonly IHttpContextAccessor _hca; | ||
private readonly UserManager<IUser> _userManager; | ||
private readonly IWorkflowScriptEvaluator _workflowScriptEvaluator; | ||
|
||
public override string Name => nameof(GenerateResetPasswordTokenTask); | ||
public override LocalizedString DisplayText => T["Generate reset password token"]; | ||
public override LocalizedString Category => T["User"]; | ||
|
||
public WorkflowExpression<User> User | ||
{ | ||
get => GetProperty(() => new WorkflowExpression<User>()); | ||
set => SetProperty(value); | ||
} | ||
|
||
public string ResetPasswordTokenPropertyKey | ||
{ | ||
get => GetProperty<string>(); | ||
set => SetProperty(value); | ||
} | ||
|
||
public string ResetPasswordUrlPropertyKey | ||
{ | ||
get => GetProperty<string>(); | ||
set => SetProperty(value); | ||
} | ||
|
||
public GenerateResetPasswordTokenTask( | ||
IStringLocalizer<GenerateResetPasswordTokenTask> localizer, | ||
LinkGenerator linkGenerator, | ||
IHttpContextAccessor hca, | ||
UserManager<IUser> userManager, | ||
IWorkflowScriptEvaluator workflowScriptEvaluator) | ||
{ | ||
T = localizer; | ||
_linkGenerator = linkGenerator; | ||
_hca = hca; | ||
_userManager = userManager; | ||
_workflowScriptEvaluator = workflowScriptEvaluator; | ||
} | ||
|
||
public override IEnumerable<Outcome> GetPossibleOutcomes( | ||
WorkflowExecutionContext workflowContext, | ||
ActivityContext activityContext) => | ||
Outcomes(T["Done"], T["Error"]); | ||
|
||
public override async Task<ActivityExecutionResult> ExecuteAsync( | ||
WorkflowExecutionContext workflowContext, | ||
ActivityContext activityContext) | ||
{ | ||
var user = await _workflowScriptEvaluator.EvaluateAsync(User, workflowContext); | ||
|
||
if (user == null) return Outcomes("Error"); | ||
|
||
var generatedToken = await _userManager.GeneratePasswordResetTokenAsync(user); | ||
user.ResetToken = Convert.ToBase64String(Encoding.UTF8.GetBytes(generatedToken)); | ||
var resetPasswordUrl = _linkGenerator.GetUriByAction( | ||
_hca.HttpContext, | ||
"ResetPassword", | ||
"ResetPassword", | ||
new { area = "OrchardCore.Users", code = user.ResetToken }); | ||
|
||
workflowContext.LastResult = new GenerateResetPasswordTokenResult | ||
{ | ||
ResetPasswordToken = user.ResetToken, | ||
ResetPasswordUrl = resetPasswordUrl, | ||
}; | ||
|
||
if (!string.IsNullOrEmpty(ResetPasswordTokenPropertyKey)) | ||
{ | ||
workflowContext.Properties[ResetPasswordTokenPropertyKey] = user.ResetToken; | ||
} | ||
|
||
if (!string.IsNullOrEmpty(ResetPasswordUrlPropertyKey)) | ||
{ | ||
workflowContext.Properties[ResetPasswordUrlPropertyKey] = resetPasswordUrl; | ||
} | ||
|
||
return Outcomes("Done"); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...fulExtensions/Extensions/Workflows/Drivers/GenerateResetPasswordTokenTaskDisplayDriver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using Lombiq.HelpfulExtensions.Extensions.Workflows.Activities; | ||
using Lombiq.HelpfulExtensions.Extensions.Workflows.ViewModels; | ||
using Microsoft.Extensions.Localization; | ||
using OrchardCore.DisplayManagement.ModelBinding; | ||
using OrchardCore.DisplayManagement.Views; | ||
using OrchardCore.Users.Models; | ||
using OrchardCore.Workflows.Display; | ||
using OrchardCore.Workflows.Models; | ||
using System.Threading.Tasks; | ||
|
||
namespace Lombiq.HelpfulExtensions.Extensions.Workflows.Drivers; | ||
|
||
public class GenerateResetPasswordTokenTaskDisplayDriver : ActivityDisplayDriver< | ||
GenerateResetPasswordTokenTask, | ||
GenerateResetPasswordTokenTaskViewModel> | ||
{ | ||
private readonly IStringLocalizer T; | ||
|
||
public GenerateResetPasswordTokenTaskDisplayDriver(IStringLocalizer<GenerateResetPasswordTokenTaskDisplayDriver> localizer) => | ||
T = localizer; | ||
|
||
protected override void EditActivity(GenerateResetPasswordTokenTask activity, GenerateResetPasswordTokenTaskViewModel model) | ||
{ | ||
model.UserExpression = activity.User.Expression; | ||
model.ResetPasswordTokenPropertyKey = activity.ResetPasswordTokenPropertyKey; | ||
model.ResetPasswordUrlPropertyKey = activity.ResetPasswordUrlPropertyKey; | ||
} | ||
|
||
public override async Task<IDisplayResult> UpdateAsync(GenerateResetPasswordTokenTask model, IUpdateModel updater) | ||
{ | ||
var viewModel = new GenerateResetPasswordTokenTaskViewModel(); | ||
if (await updater.TryUpdateModelAsync(viewModel, Prefix)) | ||
{ | ||
model.User = new WorkflowExpression<User>(viewModel.UserExpression); | ||
model.ResetPasswordTokenPropertyKey = viewModel.ResetPasswordTokenPropertyKey; | ||
model.ResetPasswordUrlPropertyKey = viewModel.ResetPasswordUrlPropertyKey; | ||
} | ||
|
||
return Edit(model); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
Lombiq.HelpfulExtensions/Extensions/Workflows/Models/GenerateResetPasswordTokenResult.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Lombiq.HelpfulExtensions.Extensions.Workflows.Models; | ||
|
||
public class GenerateResetPasswordTokenResult | ||
{ | ||
public string ResetPasswordToken { get; set; } | ||
public string ResetPasswordUrl { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using Fluid; | ||
using Lombiq.HelpfulExtensions.Extensions.Workflows.Activities; | ||
using Lombiq.HelpfulExtensions.Extensions.Workflows.Drivers; | ||
using Lombiq.HelpfulExtensions.Extensions.Workflows.Models; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using OrchardCore.Modules; | ||
using OrchardCore.Workflows.Helpers; | ||
|
||
namespace Lombiq.HelpfulExtensions.Extensions.Workflows; | ||
|
||
[Feature(FeatureIds.ResetPasswordActivity)] | ||
public class Startup : StartupBase | ||
{ | ||
public override void ConfigureServices(IServiceCollection services) | ||
{ | ||
services.AddActivity<GenerateResetPasswordTokenTask, GenerateResetPasswordTokenTaskDisplayDriver>(); | ||
services.Configure<TemplateOptions>(option => | ||
option.MemberAccessStrategy.Register<GenerateResetPasswordTokenResult>()); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...pfulExtensions/Extensions/Workflows/ViewModels/GenerateResetPasswordTokenTaskViewModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace Lombiq.HelpfulExtensions.Extensions.Workflows.ViewModels; | ||
|
||
public class GenerateResetPasswordTokenTaskViewModel | ||
{ | ||
[Required] | ||
public string UserExpression { get; set; } | ||
public string ResetPasswordTokenPropertyKey { get; set; } | ||
public string ResetPasswordUrlPropertyKey { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
Lombiq.HelpfulExtensions/Views/Items/GenerateResetPasswordTokenTask.Fields.Design.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
@using OrchardCore.Workflows.Helpers | ||
@model OrchardCore.Workflows.ViewModels.ActivityViewModel<Lombiq.HelpfulExtensions.Extensions.Workflows.Activities.GenerateResetPasswordTokenTask> | ||
|
||
<header> | ||
<h4> | ||
<i class="fa fa-user" aria-hidden="true"></i>@Model.Activity.GetTitleOrDefault(() => T["Generate Reset Password Token"]) | ||
</h4> | ||
</header> | ||
|
||
<span>@T["User expression: <em>{0}</em>", Model.Activity.User?.Expression]</span> |
20 changes: 20 additions & 0 deletions
20
Lombiq.HelpfulExtensions/Views/Items/GenerateResetPasswordTokenTask.Fields.Edit.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
@model Lombiq.HelpfulExtensions.Extensions.Workflows.ViewModels.GenerateResetPasswordTokenTaskViewModel | ||
|
||
<div class="mb-3" asp-validation-class-for="UserExpression"> | ||
<label asp-for="UserExpression">@T["User Expression"]</label> | ||
<input type="text" asp-for="UserExpression" class="form-control code" /> | ||
<span asp-validation-for="UserExpression"></span> | ||
<span class="hint">@T["Enter a JavaScript expression that evaluates to the User object."]</span> | ||
</div> | ||
<div class="mb-3" asp-validation-class-for="ResetPasswordTokenPropertyKey"> | ||
<label asp-for="ResetPasswordTokenPropertyKey">@T["Reset Password Token Property Key"]</label> | ||
<input type="text" asp-for="ResetPasswordTokenPropertyKey" class="form-control" /> | ||
<span asp-validation-for="ResetPasswordTokenPropertyKey"></span> | ||
<span class="hint">@T["Optional key of the property in the Workflow context where this Task will set the reset password token to."]</span> | ||
</div> | ||
<div class="mb-3" asp-validation-class-for="ResetPasswordUrlPropertyKey"> | ||
<label asp-for="ResetPasswordUrlPropertyKey">@T["Reset Password URL Property Key"]</label> | ||
<input type="text" asp-for="ResetPasswordUrlPropertyKey" class="form-control" /> | ||
<span asp-validation-for="ResetPasswordUrlPropertyKey"></span> | ||
<span class="hint">@T["Optional key of the property in the Workflow context where this Task will set the reset password URL to."]</span> | ||
</div> |
2 changes: 2 additions & 0 deletions
2
Lombiq.HelpfulExtensions/Views/Items/GenerateResetPasswordTokenTask.Fields.Thumbnail.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<h4 class="card-title"><i class="fa fa-user" aria-hidden="true"></i>@T["Generate Reset Password Token"]</h4> | ||
<p>@T["Generates a reset password token for the user in the workflow context or the currently logged in user and puts it into the Workflow context."]</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters