Skip to content

Instantly share code, notes, and snippets.

@sfmskywalker
Last active November 2, 2019 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sfmskywalker/19e5e3d7ee8604b925b5b856cbc6acc3 to your computer and use it in GitHub Desktop.
Save sfmskywalker/19e5e3d7ee8604b925b5b856cbc6acc3 to your computer and use it in GitHub Desktop.
CreateUser.cs - Building Workflow Driven .NET Core Applications with Elsa
using System.Threading;
using System.Threading.Tasks;
using Elsa.Attributes;
using Elsa.Expressions;
using Elsa.Extensions;
using Elsa.Results;
using Elsa.Samples.UserRegistration.Web.Models;
using Elsa.Samples.UserRegistration.Web.Services;
using Elsa.Services;
using Elsa.Services.Models;
using MongoDB.Driver;
namespace Elsa.Samples.UserRegistration.Web.Activities
{
[ActivityDefinition(Category = "Users", Description = "Create a User", Icon = "fas fa-user-plus", Outcomes = new[] { OutcomeNames.Done })]
public class CreateUser : Activity
{
private readonly IMongoCollection<User> _store;
private readonly IIdGenerator _idGenerator;
private readonly IPasswordHasher _passwordHasher;
public CreateUser(
IMongoCollection<User> store,
IIdGenerator idGenerator,
IPasswordHasher passwordHasher)
{
_store = store;
_idGenerator = idGenerator;
_passwordHasher = passwordHasher;
}
[ActivityProperty(Hint = "Enter an expression that evaluates to the name of the user to create.")]
public WorkflowExpression<string> UserName
{
get => GetState<WorkflowExpression<string>>();
set => SetState(value);
}
[ActivityProperty(Hint = "Enter an expression that evaluates to the email address of the user to create.")]
public WorkflowExpression<string> Email
{
get => GetState<WorkflowExpression<string>>();
set => SetState(value);
}
[ActivityProperty(Hint = "Enter an expression that evaluates to the password of the user to create.")]
public WorkflowExpression<string> Password
{
get => GetState<WorkflowExpression<string>>();
set => SetState(value);
}
protected override async Task<ActivityExecutionResult> OnExecuteAsync(WorkflowExecutionContext context, CancellationToken cancellationToken)
{
var password = await context.EvaluateAsync(Password, cancellationToken);
var hashedPassword = _passwordHasher.HashPassword(password);
var user = new User
{
Id = _idGenerator.Generate(),
Name = await context.EvaluateAsync(UserName, cancellationToken),
Email = await context.EvaluateAsync(Email, cancellationToken),
Password = hashedPassword.Hashed,
PasswordSalt = hashedPassword.Salt,
IsActive = false
};
await _store.InsertOneAsync(user, cancellationToken: cancellationToken);
Output.SetVariable("User", user);
return Done();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment