Skip to content

Commit 68790b9

Browse files
Refactor/WorkContextAccessor (#549)
Refactor to use IWorkContextAccessor instead of IWorkContext - Removed `IWorkContextSetter` methods for setting individual properties; added `InitializeWorkContext` method. - Updated `BackgroundServiceTask` and `WorkContextMiddleware` to use `IWorkContextAccessor` and `InitializeWorkContext`. - Added `IWorkContextAccessor` interface and `WorkContextAccessor` implementation using `AsyncLocal`.
1 parent 97c6899 commit 68790b9

File tree

353 files changed

+3377
-3478
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

353 files changed

+3377
-3478
lines changed

src/Business/Grand.Business.Authentication/Services/ExternalAuthenticationService.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public ExternalAuthenticationService(
3131
IGroupService groupService,
3232
IMediator mediator,
3333
IRepository<ExternalAuthentication> externalAuthenticationRecordRepository,
34-
IWorkContext workContext,
34+
IWorkContextAccessor workContextAccessor,
3535
IEnumerable<IExternalAuthenticationProvider> externalAuthenticationProviders,
3636
CustomerSettings customerSettings,
3737
ExternalAuthenticationSettings externalAuthenticationSettings)
@@ -44,7 +44,7 @@ public ExternalAuthenticationService(
4444
_groupService = groupService;
4545
_mediator = mediator;
4646
_externalAuthenticationRecordRepository = externalAuthenticationRecordRepository;
47-
_workContext = workContext;
47+
_workContextAccessor = workContextAccessor;
4848
_externalAuthenticationProviders = externalAuthenticationProviders;
4949
}
5050

@@ -58,7 +58,7 @@ public ExternalAuthenticationService(
5858
private readonly IGroupService _groupService;
5959
private readonly IMediator _mediator;
6060
private readonly IRepository<ExternalAuthentication> _externalAuthenticationRecordRepository;
61-
private readonly IWorkContext _workContext;
61+
private readonly IWorkContextAccessor _workContextAccessor;
6262
private readonly IEnumerable<IExternalAuthenticationProvider> _externalAuthenticationProviders;
6363
private readonly CustomerSettings _customerSettings;
6464
private readonly ExternalAuthenticationSettings _externalAuthenticationSettings;
@@ -130,32 +130,32 @@ _customerSettings.UserRegistrationType is UserRegistrationType.Standard
130130
or UserRegistrationType.EmailValidation;
131131

132132
//create registration request
133-
var registrationRequest = new RegistrationRequest(_workContext.CurrentCustomer,
133+
var registrationRequest = new RegistrationRequest(_workContextAccessor.WorkContext.CurrentCustomer,
134134
parameters.Email, parameters.Email,
135135
CommonHelper.GenerateRandomDigitCode(20),
136136
PasswordFormat.Hashed,
137-
_workContext.CurrentStore.Id,
137+
_workContextAccessor.WorkContext.CurrentStore.Id,
138138
approved);
139139

140140
//whether registration request has been completed successfully
141141
await _customerManagerService.RegisterCustomer(registrationRequest);
142142

143143
//allow to save other customer values by consuming this event
144-
await _mediator.Publish(new RegisteredByExternalMethod(_workContext.CurrentCustomer, parameters));
144+
await _mediator.Publish(new RegisteredByExternalMethod(_workContextAccessor.WorkContext.CurrentCustomer, parameters));
145145

146146
//raise customer registered event
147-
await _mediator.Publish(new CustomerRegisteredEvent(_workContext.CurrentCustomer));
147+
await _mediator.Publish(new CustomerRegisteredEvent(_workContextAccessor.WorkContext.CurrentCustomer));
148148

149149
//associate external account with registered user
150-
await AssociateCustomer(_workContext.CurrentCustomer, parameters);
150+
await AssociateCustomer(_workContextAccessor.WorkContext.CurrentCustomer, parameters);
151151

152152
//authenticate
153153
if (!approved)
154154
return _customerSettings.UserRegistrationType == UserRegistrationType.AdminApproval
155155
? new RedirectToRouteResult("RegisterResult",
156156
new { resultId = (int)UserRegistrationType.AdminApproval })
157157
: Error(["Error on registration"]);
158-
await _authenticationService.SignIn(_workContext.CurrentCustomer, false);
158+
await _authenticationService.SignIn(_workContextAccessor.WorkContext.CurrentCustomer, false);
159159

160160
return new RedirectToRouteResult("RegisterResult", new { resultId = (int)UserRegistrationType.Standard });
161161
}
@@ -206,8 +206,8 @@ public virtual IList<IExternalAuthenticationProvider> LoadActiveAuthenticationPr
206206
return LoadAllAuthenticationProviders()
207207
.Where(provider =>
208208
provider.IsMethodActive(_externalAuthenticationSettings) &&
209-
provider.IsAuthenticateGroup(_workContext.CurrentCustomer) &&
210-
provider.IsAuthenticateStore(_workContext.CurrentStore)
209+
provider.IsAuthenticateGroup(_workContextAccessor.WorkContext.CurrentCustomer) &&
210+
provider.IsAuthenticateStore(_workContextAccessor.WorkContext.CurrentStore)
211211
).ToList();
212212
}
213213

@@ -242,8 +242,8 @@ public virtual bool AuthenticationProviderIsAvailable(string systemName)
242242

243243
return authenticationMethod != null &&
244244
authenticationMethod.IsMethodActive(_externalAuthenticationSettings) &&
245-
authenticationMethod.IsAuthenticateGroup(_workContext.CurrentCustomer) &&
246-
authenticationMethod.IsAuthenticateStore(_workContext.CurrentStore);
245+
authenticationMethod.IsAuthenticateGroup(_workContextAccessor.WorkContext.CurrentCustomer) &&
246+
authenticationMethod.IsAuthenticateStore(_workContextAccessor.WorkContext.CurrentStore);
247247
}
248248

249249
#endregion
@@ -264,8 +264,8 @@ public virtual async Task<IActionResult> Authenticate(ExternalAuthParam paramete
264264
return Error(["External authentication method cannot be loaded"]);
265265

266266
//get current logged-in user
267-
var currentLoggedInUser = await _groupService.IsRegistered(_workContext.CurrentCustomer)
268-
? _workContext.CurrentCustomer
267+
var currentLoggedInUser = await _groupService.IsRegistered(_workContextAccessor.WorkContext.CurrentCustomer)
268+
? _workContextAccessor.WorkContext.CurrentCustomer
269269
: null;
270270

271271
//authenticate associated user if already exists

src/Business/Grand.Business.Authentication/Services/TwoFactorAuthenticationService.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ public class TwoFactorAuthenticationService : ITwoFactorAuthenticationService
1414
private readonly IEnumerable<ISMSVerificationService> _smsVerificationService;
1515
private readonly TwoFactorAuthenticator _twoFactorAuthentication;
1616
private readonly ICustomerService _customerService;
17-
private readonly IWorkContext _workContext;
17+
private readonly IWorkContextAccessor _workContextAccessor;
1818

1919
public TwoFactorAuthenticationService(
20-
IWorkContext workContext,
20+
IWorkContextAccessor workContextAccessor,
2121
ICustomerService customerService,
2222
IEnumerable<ISMSVerificationService> smsVerificationService)
2323
{
24-
_workContext = workContext;
24+
_workContextAccessor = workContextAccessor;
2525
_customerService = customerService;
2626
_smsVerificationService = smsVerificationService;
2727
_twoFactorAuthentication = new TwoFactorAuthenticator();
@@ -62,7 +62,7 @@ public virtual async Task<TwoFactorCodeSetup> GenerateCodeSetup(string secretKey
6262
switch (twoFactorAuthenticationType)
6363
{
6464
case TwoFactorAuthenticationType.AppVerification:
65-
var setupInfo = _twoFactorAuthentication.GenerateSetupCode(_workContext.CurrentStore.CompanyName,
65+
var setupInfo = _twoFactorAuthentication.GenerateSetupCode(_workContextAccessor.WorkContext.CurrentStore.CompanyName,
6666
customer.Email, secretKey, false);
6767
model.CustomValues.Add("QrCodeImageUrl", setupInfo.QrCodeSetupImageUrl);
6868
model.CustomValues.Add("ManualEntryQrCode", setupInfo.ManualEntryKey);

src/Business/Grand.Business.Catalog/Services/Brands/BrandService.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ public class BrandService : IBrandService
2424
/// </summary>
2525
public BrandService(ICacheBase cacheBase,
2626
IRepository<Brand> brandRepository,
27-
IWorkContext workContext,
27+
IWorkContextAccessor workContextAccessor,
2828
IMediator mediator, AccessControlConfig accessControlConfig)
2929
{
3030
_cacheBase = cacheBase;
3131
_brandRepository = brandRepository;
32-
_workContext = workContext;
32+
_workContextAccessor = workContextAccessor;
3333
_mediator = mediator;
3434
_accessControlConfig = accessControlConfig;
3535
}
@@ -39,7 +39,7 @@ public BrandService(ICacheBase cacheBase,
3939
#region Fields
4040

4141
private readonly IRepository<Brand> _brandRepository;
42-
private readonly IWorkContext _workContext;
42+
private readonly IWorkContextAccessor _workContextAccessor;
4343
private readonly IMediator _mediator;
4444
private readonly ICacheBase _cacheBase;
4545
private readonly AccessControlConfig _accessControlConfig;
@@ -77,7 +77,7 @@ public virtual async Task<IPagedList<Brand>> GetAllBrands(string brandName = "",
7777
if (!showHidden && !_accessControlConfig.IgnoreAcl)
7878
{
7979
//Limited to customer groups rules
80-
var allowedCustomerGroupsIds = _workContext.CurrentCustomer.GetCustomerGroupIds();
80+
var allowedCustomerGroupsIds = _workContextAccessor.WorkContext.CurrentCustomer.GetCustomerGroupIds();
8181
query = from p in query
8282
where !p.LimitedToGroups || allowedCustomerGroupsIds.Any(x => p.CustomerGroups.Contains(x))
8383
select p;

src/Business/Grand.Business.Catalog/Services/Categories/CategoryService.cs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ public class CategoryService : ICategoryService
3232
/// <param name="accessControlConfig"></param>
3333
public CategoryService(ICacheBase cacheBase,
3434
IRepository<Category> categoryRepository,
35-
IWorkContext workContext,
35+
IWorkContextAccessor workContextAccessor,
3636
IMediator mediator,
3737
IAclService aclService,
3838
AccessControlConfig accessControlConfig)
3939
{
4040
_cacheBase = cacheBase;
4141
_categoryRepository = categoryRepository;
42-
_workContext = workContext;
42+
_workContextAccessor = workContextAccessor;
4343
_mediator = mediator;
4444
_aclService = aclService;
4545
_accessControlConfig = accessControlConfig;
@@ -50,7 +50,7 @@ public CategoryService(ICacheBase cacheBase,
5050
#region Fields
5151

5252
private readonly IRepository<Category> _categoryRepository;
53-
private readonly IWorkContext _workContext;
53+
private readonly IWorkContextAccessor _workContextAccessor;
5454
private readonly IMediator _mediator;
5555
private readonly ICacheBase _cacheBase;
5656
private readonly IAclService _aclService;
@@ -91,7 +91,7 @@ public virtual async Task<IPagedList<Category>> GetAllCategories(string parentId
9191
if (!showHidden && !_accessControlConfig.IgnoreAcl)
9292
{
9393
//Limited to customer group (access control list)
94-
var allowedCustomerGroupsIds = _workContext.CurrentCustomer.GetCustomerGroupIds();
94+
var allowedCustomerGroupsIds = _workContextAccessor.WorkContext.CurrentCustomer.GetCustomerGroupIds();
9595
query = from p in query
9696
where !p.LimitedToGroups || allowedCustomerGroupsIds.Any(x => p.CustomerGroups.Contains(x))
9797
select p;
@@ -124,23 +124,23 @@ public virtual async Task<IList<Category>> GetMenuCategories()
124124
switch (_accessControlConfig.IgnoreAcl)
125125
{
126126
case true when
127-
string.IsNullOrEmpty(_workContext.CurrentStore.Id) || _accessControlConfig.IgnoreStoreLimitations:
127+
string.IsNullOrEmpty(_workContextAccessor.WorkContext.CurrentStore.Id) || _accessControlConfig.IgnoreStoreLimitations:
128128
return await Task.FromResult(query.ToList());
129129
case false:
130130
{
131131
//Limited to customer group (access control list)
132-
var allowedCustomerGroupsIds = _workContext.CurrentCustomer.GetCustomerGroupIds();
132+
var allowedCustomerGroupsIds = _workContextAccessor.WorkContext.CurrentCustomer.GetCustomerGroupIds();
133133
query = from p in query
134134
where !p.LimitedToGroups || allowedCustomerGroupsIds.Any(x => p.CustomerGroups.Contains(x))
135135
select p;
136136
break;
137137
}
138138
}
139139

140-
if (!string.IsNullOrEmpty(_workContext.CurrentStore.Id) && !_accessControlConfig.IgnoreStoreLimitations)
140+
if (!string.IsNullOrEmpty(_workContextAccessor.WorkContext.CurrentStore.Id) && !_accessControlConfig.IgnoreStoreLimitations)
141141
//Limited to stores rule
142142
query = from p in query
143-
where !p.LimitedToStores || p.Stores.Contains(_workContext.CurrentStore.Id)
143+
where !p.LimitedToStores || p.Stores.Contains(_workContextAccessor.WorkContext.CurrentStore.Id)
144144
select p;
145145
return await Task.FromResult(query.ToList());
146146
}
@@ -155,8 +155,8 @@ public virtual async Task<IList<Category>> GetMenuCategories()
155155
public virtual async Task<IList<Category>> GetAllCategoriesByParentCategoryId(string parentCategoryId = "",
156156
bool showHidden = false, bool includeAllLevels = false)
157157
{
158-
var storeId = _workContext.CurrentStore.Id;
159-
var customer = _workContext.CurrentCustomer;
158+
var storeId = _workContextAccessor.WorkContext.CurrentStore.Id;
159+
var customer = _workContextAccessor.WorkContext.CurrentCustomer;
160160
var key = string.Format(CacheKey.CATEGORIES_BY_PARENT_CATEGORY_ID_KEY, parentCategoryId, showHidden,
161161
customer.Id, storeId, includeAllLevels);
162162
return await _cacheBase.GetAsync(key, async () =>
@@ -170,7 +170,7 @@ public virtual async Task<IList<Category>> GetAllCategoriesByParentCategoryId(st
170170
if (!_accessControlConfig.IgnoreAcl)
171171
{
172172
//Limited to customer groups rules
173-
var allowedCustomerGroupsIds = _workContext.CurrentCustomer.GetCustomerGroupIds();
173+
var allowedCustomerGroupsIds = _workContextAccessor.WorkContext.CurrentCustomer.GetCustomerGroupIds();
174174
query = from p in query
175175
where !p.LimitedToGroups || allowedCustomerGroupsIds.Any(x => p.CustomerGroups.Contains(x))
176176
select p;
@@ -207,8 +207,8 @@ public virtual async Task<IList<Category>> GetAllCategoriesDisplayedOnHomePage(b
207207
var categories = await Task.FromResult(query.ToList());
208208
if (!showHidden)
209209
categories = categories
210-
.Where(c => _aclService.Authorize(c, _workContext.CurrentCustomer) &&
211-
_aclService.Authorize(c, _workContext.CurrentStore.Id))
210+
.Where(c => _aclService.Authorize(c, _workContextAccessor.WorkContext.CurrentCustomer) &&
211+
_aclService.Authorize(c, _workContextAccessor.WorkContext.CurrentStore.Id))
212212
.ToList();
213213

214214
return categories;
@@ -228,8 +228,8 @@ public virtual async Task<IList<Category>> GetAllCategoriesFeaturedProductsOnHom
228228
var categories = await Task.FromResult(query.ToList());
229229
if (!showHidden)
230230
categories = categories
231-
.Where(c => _aclService.Authorize(c, _workContext.CurrentCustomer) &&
232-
_aclService.Authorize(c, _workContext.CurrentStore.Id))
231+
.Where(c => _aclService.Authorize(c, _workContextAccessor.WorkContext.CurrentCustomer) &&
232+
_aclService.Authorize(c, _workContextAccessor.WorkContext.CurrentStore.Id))
233233
.ToList();
234234
return categories;
235235
}
@@ -245,8 +245,8 @@ public virtual async Task<IList<Category>> GetAllCategoriesSearchBox()
245245
.OrderBy(x => x.SearchBoxDisplayOrder);
246246

247247
var categories = (await Task.FromResult(query.ToList()))
248-
.Where(c => _aclService.Authorize(c, _workContext.CurrentCustomer) &&
249-
_aclService.Authorize(c, _workContext.CurrentStore.Id))
248+
.Where(c => _aclService.Authorize(c, _workContextAccessor.WorkContext.CurrentCustomer) &&
249+
_aclService.Authorize(c, _workContextAccessor.WorkContext.CurrentStore.Id))
250250
.ToList();
251251

252252
return categories;
@@ -268,8 +268,8 @@ public virtual async Task<IList<Category>> GetCategoryBreadCrumb(Category catego
268268
while (category != null && //not null
269269
(showHidden || category.Published) && //published
270270
(showHidden ||
271-
_aclService.Authorize(category, _workContext.CurrentCustomer)) && //limited to customer groups
272-
(showHidden || _aclService.Authorize(category, _workContext.CurrentStore.Id)) && //limited to store
271+
_aclService.Authorize(category, _workContextAccessor.WorkContext.CurrentCustomer)) && //limited to customer groups
272+
(showHidden || _aclService.Authorize(category, _workContextAccessor.WorkContext.CurrentStore.Id)) && //limited to store
273273
!alreadyProcessedCategoryIds.Contains(category.Id))
274274
{
275275
result.Add(category);
@@ -301,8 +301,8 @@ public virtual IList<Category> GetCategoryBreadCrumb(Category category, IList<Ca
301301
while (category != null && //not null
302302
(showHidden || category.Published) && //published
303303
(showHidden ||
304-
_aclService.Authorize(category, _workContext.CurrentCustomer)) && //limited to customer groups
305-
(showHidden || _aclService.Authorize(category, _workContext.CurrentStore.Id)) && //limited to store
304+
_aclService.Authorize(category, _workContextAccessor.WorkContext.CurrentCustomer)) && //limited to customer groups
305+
(showHidden || _aclService.Authorize(category, _workContextAccessor.WorkContext.CurrentStore.Id)) && //limited to store
306306
!alreadyProcessedCategoryIds.Contains(category.Id)) //avoid circular references
307307
{
308308
result.Add(category);

src/Business/Grand.Business.Catalog/Services/Categories/ProductCategoryService.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ public class ProductCategoryService : IProductCategoryService
1919
private readonly ICacheBase _cacheBase;
2020
private readonly IMediator _mediator;
2121
private readonly IRepository<Product> _productRepository;
22-
private readonly IWorkContext _workContext;
22+
private readonly IWorkContextAccessor _workContextAccessor;
2323

2424
public ProductCategoryService(
2525
IRepository<Product> productRepository,
2626
ICacheBase cacheBase,
27-
IWorkContext workContext,
27+
IWorkContextAccessor workContextAccessor,
2828
IMediator mediator, AccessControlConfig accessControlConfig)
2929
{
3030
_productRepository = productRepository;
3131
_cacheBase = cacheBase;
32-
_workContext = workContext;
32+
_workContextAccessor = workContextAccessor;
3333
_mediator = mediator;
3434
_accessControlConfig = accessControlConfig;
3535
}
@@ -49,7 +49,7 @@ public virtual async Task<IPagedList<ProductsCategory>> GetProductCategoriesByCa
4949
return new PagedList<ProductsCategory>(new List<ProductsCategory>(), pageIndex, pageSize);
5050

5151
var key = string.Format(CacheKey.PRODUCTCATEGORIES_ALLBYCATEGORYID_KEY, showHidden, categoryId, pageIndex,
52-
pageSize, _workContext.CurrentCustomer.Id, _workContext.CurrentStore.Id);
52+
pageSize, _workContextAccessor.WorkContext.CurrentCustomer.Id, _workContextAccessor.WorkContext.CurrentStore.Id);
5353
return await _cacheBase.GetAsync(key, () =>
5454
{
5555
var query = _productRepository.Table.Where(x => x.ProductCategories.Any(y => y.CategoryId == categoryId));
@@ -59,7 +59,7 @@ public virtual async Task<IPagedList<ProductsCategory>> GetProductCategoriesByCa
5959
if (!_accessControlConfig.IgnoreAcl)
6060
{
6161
//Limited to customer groups
62-
var allowedCustomerGroupsIds = _workContext.CurrentCustomer.GetCustomerGroupIds();
62+
var allowedCustomerGroupsIds = _workContextAccessor.WorkContext.CurrentCustomer.GetCustomerGroupIds();
6363
query = from p in query
6464
where !p.LimitedToGroups || allowedCustomerGroupsIds.Any(x => p.CustomerGroups.Contains(x))
6565
select p;
@@ -68,7 +68,7 @@ public virtual async Task<IPagedList<ProductsCategory>> GetProductCategoriesByCa
6868
if (!_accessControlConfig.IgnoreStoreLimitations)
6969
{
7070
//Limited to stores
71-
var currentStoreId = _workContext.CurrentStore.Id;
71+
var currentStoreId = _workContextAccessor.WorkContext.CurrentStore.Id;
7272
query = from p in query
7373
where !p.LimitedToStores || p.Stores.Contains(currentStoreId)
7474
select p;

0 commit comments

Comments
 (0)