Skip to content
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 8 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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.Users.Services;
using OrchardCore.Workflows.Abstractions.Models;
using OrchardCore.Workflows.Activities;
using OrchardCore.Workflows.Models;
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 IUserService _userService;

public override string Name => nameof(GenerateResetPasswordTokenTask);
public override LocalizedString DisplayText => T["Generate reset password token"];
public override LocalizedString Category => T["User"];

public GenerateResetPasswordTokenTask(
IStringLocalizer<GenerateResetPasswordTokenTask> localizer,
LinkGenerator linkGenerator,
IHttpContextAccessor hca,
UserManager<IUser> userManager,
IUserService userService)
{
T = localizer;
_linkGenerator = linkGenerator;
_hca = hca;
_userManager = userManager;
_userService = userService;
}

public override IEnumerable<Outcome> GetPossibleOutcomes(
WorkflowExecutionContext workflowContext,
ActivityContext activityContext) =>
Outcomes(names: T["Done"]);

public override async Task<ActivityExecutionResult> ExecuteAsync(
WorkflowExecutionContext workflowContext,
ActivityContext activityContext)
{
var user = workflowContext.Input["User"] as User ?? workflowContext.Properties["User"] as User;
if (user == null && _hca.HttpContext.User.Identity.IsAuthenticated)
{
user = await _userService.GetAuthenticatedUserAsync(_hca.HttpContext.User) as User;
}
dministro marked this conversation as resolved.
Show resolved Hide resolved

if (user == null) return Outcomes("Done", "Done");
dministro marked this conversation as resolved.
Show resolved Hide resolved

var generatedToken = await _userManager.GeneratePasswordResetTokenAsync(user);
user.ResetToken = Convert.ToBase64String(Encoding.UTF8.GetBytes(generatedToken));
workflowContext.Properties["ResetPasswordToken"] = user.ResetToken;

var resetPasswordUrl = _linkGenerator.GetUriByAction(
_hca.HttpContext,
"ResetPassword",
"ResetPassword",
new { area = "OrchardCore.Users", code = user.ResetToken });
workflowContext.Properties["ResetPasswordUrl"] = resetPasswordUrl;
dministro marked this conversation as resolved.
Show resolved Hide resolved

return Outcomes("Done", "Done");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Lombiq.HelpfulExtensions.Extensions.Workflows.Activities;
using Lombiq.HelpfulExtensions.Extensions.Workflows.ViewModels;
using OrchardCore.Workflows.Display;

namespace Lombiq.HelpfulExtensions.Extensions.Workflows.Drivers;

public class GenerateResetPasswordTokenTaskDisplayDriver : ActivityDisplayDriver<
GenerateResetPasswordTokenTask,
GenerateResetPasswordTokenTaskViewModel>
{
}
14 changes: 14 additions & 0 deletions Lombiq.HelpfulExtensions/Extensions/Workflows/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Lombiq.HelpfulExtensions.Extensions.Workflows.Activities;
using Lombiq.HelpfulExtensions.Extensions.Workflows.Drivers;
using Microsoft.Extensions.DependencyInjection;
using OrchardCore.Modules;
using OrchardCore.Workflows.Helpers;

namespace Lombiq.HelpfulExtensions.Extensions.Workflows;

[Feature(FeatureIds.Workflows)]
public class Startup : StartupBase
{
public override void ConfigureServices(IServiceCollection services) =>
services.AddActivity<GenerateResetPasswordTokenTask, GenerateResetPasswordTokenTaskDisplayDriver>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace Lombiq.HelpfulExtensions.Extensions.Workflows.ViewModels;

public class GenerateResetPasswordTokenTaskViewModel
{
}
1 change: 1 addition & 0 deletions Lombiq.HelpfulExtensions/FeatureIds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ public static class FeatureIds
public const string Security = FeatureIdPrefix + nameof(Security);
public const string TargetBlank = FeatureIdPrefix + nameof(TargetBlank);
public const string SiteTexts = FeatureIdPrefix + nameof(SiteTexts);
public const string Workflows = FeatureIdPrefix + nameof(Workflows);
dministro marked this conversation as resolved.
Show resolved Hide resolved
}
1 change: 1 addition & 0 deletions Lombiq.HelpfulExtensions/Lombiq.HelpfulExtensions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<PackageReference Include="OrchardCore.Markdown" Version="1.5.0" />
<PackageReference Include="OrchardCore.MetaWeblog.Abstractions" Version="1.5.0" />
<PackageReference Include="OrchardCore.Module.Targets" Version="1.5.0" />
<PackageReference Include="OrchardCore.Workflows" Version="1.5.0" />
</ItemGroup>

<ItemGroup Condition="'$(NuGetBuild)' != 'true'">
Expand Down
11 changes: 11 additions & 0 deletions Lombiq.HelpfulExtensions/Manifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,14 @@
"OrchardCore.Markdown",
}
)]

[assembly: Feature(
Id = Workflows,
Name = "Lombiq Helpful Extensions - Workflows",
Category = "Workflows",
Description = "Adds useful workflow activities (e.g., generate reset password token).",
Dependencies = new[]
{
"OrchardCore.Workflows",
}
)]
dministro marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@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>
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>
6 changes: 6 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ Use the `ShellScope.Current.SendEmailDeferred()` for sending emails. It'll send

Gives all external links the `target="_blank"` attribute.

### Workflows

Adds useful Workflows activities.

- GenerateResetPasswordTask: Generates a reset password token for the user found in the Workflow context or the current user. The token is set to the Workflow properties to the `ResetPasswordToken` key. It also generates a URL for the built-in reset password page; it'll be set to the `ResetPasswordUrl` key. If you want to use the URL, make sure you have the Reset Password feature enabled and you have allowed users to reset their password by going to Admin UI > Security > Settings > Reset Password > tick "Allow the users to reset their password".
dministro marked this conversation as resolved.
Show resolved Hide resolved

## Contributing and support

Bug reports, feature requests, comments, questions, code contributions and love letters are warmly welcome. You can send them to us via GitHub issues and pull requests. Please adhere to our [open-source guidelines](https://lombiq.com/open-source-guidelines) while doing so.
Expand Down