Skip to content

Commit 943e6d6

Browse files
authored
VCST-4673: Preserve System Context for Hangfire Background Jobs (#2982)
1 parent 81e3cb0 commit 943e6d6

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

src/VirtoCommerce.Platform.Data/Extensions/ApplicationBuilderExtensions.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using VirtoCommerce.Platform.Core.ChangeLog;
66
using VirtoCommerce.Platform.Core.Common;
77
using VirtoCommerce.Platform.Core.Security;
8-
using static VirtoCommerce.Platform.Data.Constants.DefaultEntityNames;
98

109
namespace VirtoCommerce.Platform.Data.Extensions
1110
{
@@ -33,13 +32,8 @@ public static IApplicationBuilder UseDbTriggers(this IApplicationBuilder appBuil
3332

3433
entry.Entity.CreatedDate = entry.Original.CreatedDate;
3534
entry.Entity.CreatedBy = entry.Original.CreatedBy;
36-
3735
entry.Entity.ModifiedDate = currentTime;
38-
39-
if (userName != UNKNOWN_USERNAME)
40-
{
41-
entry.Entity.ModifiedBy = userName;
42-
}
36+
entry.Entity.ModifiedBy = userName;
4337
};
4438

4539
Triggers<IEntity>.Inserting += entry =>

src/VirtoCommerce.Platform.Hangfire/Middleware/HangfireUserContextMiddleware.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ public HangfireUserContextMiddleware(IUserNameResolver userNameResolver)
2525
public void OnCreating(CreatingContext filterContext)
2626
{
2727
if (ContextUserName is null)
28+
{
2829
return;
30+
}
2931

3032
filterContext.SetJobParameter(USER_NAME, ContextUserName);
3133
}
@@ -41,7 +43,16 @@ public void OnCreated(CreatedContext filterContext)
4143

4244
public void OnPerforming(PerformingContext filterContext)
4345
{
44-
var userName = filterContext.GetJobParameter<string>(USER_NAME);
46+
string userName;
47+
48+
if (IsRecurringJob(filterContext, out var recurringJobId))
49+
{
50+
userName = $"system:{recurringJobId}";
51+
}
52+
else
53+
{
54+
userName = filterContext.GetJobParameter<string>(USER_NAME);
55+
}
4556

4657
_userNameResolver.SetCurrentUserName(userName);
4758
}
@@ -52,5 +63,11 @@ public void OnPerformed(PerformedContext filterContext)
5263
}
5364

5465
#endregion IServerFilter Members
66+
67+
private static bool IsRecurringJob(PerformingContext context, out string recurringJobId)
68+
{
69+
recurringJobId = context.GetJobParameter<string>("RecurringJobId");
70+
return !string.IsNullOrEmpty(recurringJobId);
71+
}
5572
}
5673
}

src/VirtoCommerce.Platform.Security/HttpContextUserResolver.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ public class HttpContextUserResolver : IUserNameResolver
1212
/// <summary>
1313
/// This header contains logined user name
1414
/// </summary>
15-
private const string CurrentUserNameHeader = "VirtoCommerce-User-Name";
15+
private const string _currentUserNameHeader = "VirtoCommerce-User-Name";
1616

1717
/// <summary>
1818
/// This header contains Login-on-behalf operator user name
1919
/// </summary>
20-
private const string OperatorUserNameHeader = "VirtoCommerce-Operator-User-Name";
20+
private const string _operatorUserNameHeader = "VirtoCommerce-Operator-User-Name";
21+
22+
private const string _anonymousUserName = "http:anonymous";
2123

2224
private static readonly AsyncLocal<string> _currentUserName = new AsyncLocal<string>();
2325

@@ -35,6 +37,8 @@ public string GetCurrentUserName()
3537
var context = _httpContextAccessor.HttpContext;
3638
if (context != null && context.Request != null && context.User != null)
3739
{
40+
result = _anonymousUserName;
41+
3842
var identity = context.User.Identity;
3943
if (identity != null && identity.IsAuthenticated)
4044
{
@@ -44,9 +48,9 @@ public string GetCurrentUserName()
4448
if (string.IsNullOrEmpty(result))
4549
{
4650
// Login-on-behalf operator user has a priority over an actual user
47-
var userHeader = context.Request.Headers.ContainsKey(OperatorUserNameHeader) ?
48-
OperatorUserNameHeader :
49-
CurrentUserNameHeader;
51+
var userHeader = context.Request.Headers.ContainsKey(_operatorUserNameHeader) ?
52+
_operatorUserNameHeader :
53+
_currentUserNameHeader;
5054

5155
result = context.Request.Headers[userHeader];
5256
if (string.IsNullOrEmpty(result))

0 commit comments

Comments
 (0)