-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
OSOE-572: Add "Generate Reset Password Token" workflow activity #113
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
54f194f
Adding reset password token task
barthamark 7115d54
Adjusting Readme
barthamark 4702333
Using named parameter to overcome analyzer violation
barthamark 0306aae
Making keys configurable and updating feature ID
barthamark ee55025
Merge remote-tracking branch 'origin/dev' into issue/OSOE-572
barthamark b6f343a
Merge remote-tracking branch 'origin/dev' into issue/OSOE-572
barthamark a87e941
Retrieving User object using expression
barthamark a7b488d
Removin unnecessary checking
dministro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
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,104 @@ | ||
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 = !string.IsNullOrEmpty(User.Expression) | ||
? await _workflowScriptEvaluator.EvaluateAsync(User, workflowContext) | ||
: null; | ||
|
||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check is not necessary. See: https://github.com/OrchardCMS/OrchardCore/blob/68c5f85470ca1d9bd2663c011aa4f67138de2ab5/src/OrchardCore.Modules/OrchardCore.Workflows/Scripting/JavaScriptWorkflowScriptEvaluator.cs#L32