Skip to content

Commit 52cfb7f

Browse files
committed
fix: Added PrincipalId to the runcontext to hold an id of the user that executed the workflow
1 parent 98eb6f4 commit 52cfb7f

8 files changed

Lines changed: 22 additions & 6 deletions

File tree

src/WorkflowEngine.Core/Action.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class Action : IAction, IFormattable
1414
public int Index { get; set; }
1515
public bool ScopeMoveNext { get; set; }
1616

17+
public string PrincipalId { get; set; }
1718
public string ToString(string format, IFormatProvider formatProvider)
1819
{
1920
if (format == "Type")

src/WorkflowEngine.Core/Expressions/OutputsFunction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public override async ValueTask<ValueContainer> ExecuteFunction(params ValueCont
2424

2525
var key = parameters[0].ToString();
2626
if (scopeContext.Scope.Contains("."))
27-
{
27+
{
2828
key = $"{scopeContext.Scope.Substring(0, scopeContext.Scope.LastIndexOf('.'))}.{key}";
2929
}
3030

src/WorkflowEngine.Core/IWorkflowExecutor.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ namespace WorkflowEngine.Core
66
public interface IRunContext
77
{
88
Guid RunId { get; set; }
9+
10+
string PrincipalId { get; set; }
11+
12+
public T CopyTo<T>(T other)
13+
where T : IRunContext
14+
{
15+
other.RunId = RunId;
16+
other.PrincipalId = PrincipalId;
17+
return other;
18+
}
919
}
1020
public interface IWorkflowExecutor
1121
{

src/WorkflowEngine.Core/TriggerContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ public class TriggerContext : ITriggerContext, IFormattable
66
{
77
public IWorkflow Workflow { get; set; }
88
public ITrigger Trigger { get; set; }
9+
public string PrincipalId { get; set; }
910

1011
public Guid RunId { get; set; }
1112

src/WorkflowEngine.Core/WorkflowExecutor.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public ValueTask<IAction> GetNextAction(IRunContext context, IWorkflow workflow,
3939

4040
if (next.Value.ShouldRun(priorResult.Key,priorResult.Status)) // .RunAfter[priorResult.Key].Contains(priorResult.Status))
4141
{
42-
return new ValueTask<IAction>(new Action { RunId=context.RunId, Type = next.Value.Type, Key=next.Key, ScheduledTime=DateTimeOffset.UtcNow });
42+
return new ValueTask<IAction>(context.CopyTo(new Action { Type = next.Value.Type, Key=next.Key, ScheduledTime=DateTimeOffset.UtcNow }));
4343
}
4444

4545
return new ValueTask<IAction>();
@@ -58,7 +58,9 @@ public async ValueTask<IAction> Trigger(ITriggerContext context)
5858
if (action.IsDefault())
5959
return null;
6060

61-
return new Action { Type = action.Value.Type, Key=action.Key, ScheduledTime = DateTimeOffset.UtcNow, RunId = context.RunId };
61+
return context.CopyTo(
62+
new Action { Type = action.Value.Type, Key=action.Key, ScheduledTime = DateTimeOffset.UtcNow }
63+
);
6264
}
6365
}
6466

src/WorkflowEngine.Hangfire/ForloopAction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public async ValueTask<object> ExecuteAsync(IRunContext context, IWorkflow workf
4545
// var nextAction = new Action { Type = action.Type, Key=action.Key, ScheduledTime = DateTimeOffset.UtcNow, RunId = context.RunId, Index = action.Index+1 };
4646

4747
var nextactionmetadata = loop.Actions.SingleOrDefault(c => c.Value.RunAfter?.Count == 0);
48-
var nextaction = new Action { Type = nextactionmetadata.Value.Type, Key= $"{action.Key}.{nextactionmetadata.Key}", ScheduledTime = DateTimeOffset.UtcNow, RunId = context.RunId, Index=action.Index };
48+
var nextaction = context.CopyTo( new Action { Type = nextactionmetadata.Value.Type, Key= $"{action.Key}.{nextactionmetadata.Key}", ScheduledTime = DateTimeOffset.UtcNow, Index=action.Index });
4949

5050
var a = backgroundJobClient.ContinueJobWith<IHangfireActionExecutor>(arrayContext.JobId,
5151
(executor) => executor.ExecuteAsync(context, workflow, nextaction,null));

src/WorkflowEngine.Hangfire/HangfireWorkflowExecutor.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public async ValueTask<object> ExecuteAsync(IRunContext run, IWorkflow workflow,
5757
}else if(workflow.Manifest.Actions.FindParentAction(action.Key) is ForLoopActionMetadata scope)
5858
{
5959

60-
var scopeaction= new Action {ScopeMoveNext=true, RunId=run.RunId, Type = scope.Type, Key=action.Key.Substring(0, action.Key.LastIndexOf('.')), ScheduledTime=DateTimeOffset.UtcNow };
60+
var scopeaction= run.CopyTo( new Action {ScopeMoveNext=true, Type = scope.Type, Key=action.Key.Substring(0, action.Key.LastIndexOf('.')), ScheduledTime=DateTimeOffset.UtcNow });
6161

6262

6363
var a = backgroundJobClient.Enqueue<IHangfireActionExecutor>(
@@ -76,7 +76,9 @@ public async ValueTask<object> ExecuteAsync(IRunContext run, IWorkflow workflow,
7676
/// <returns></returns>
7777
public async ValueTask<object> TriggerAsync(ITriggerContext context)
7878
{
79+
7980
context.RunId = context.RunId == Guid.Empty? Guid.NewGuid() : context.RunId;
81+
8082
runContextAccessor.RunContext = context;
8183
var action = await executor.Trigger(context);
8284

src/WorkflowEngine.Hangfire/WorkflowStarterBackgroundJob.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
3636

3737
if (!trigger.Equals(default(KeyValuePair<string, TriggerMetadata>)))
3838
{
39-
39+
4040
jobs.AddOrUpdate<IHangfireWorkflowExecutor>(workflow.Id.ToString(),
4141
(executor) => executor.TriggerAsync(new TriggerContext
4242
{

0 commit comments

Comments
 (0)