Skip to content

Commit d69faea

Browse files
Improvements/code (#574)
* Renamed IWorkContextAccessor to IContextAccessor. * Added IStoreContext to IContextAccessor and moved CurrentStore/CurrentHost from IWorkContext. * Theme Moder plugin - Fix typo in CSS padding-left unit in home.css * Views - Refactor menu display logic into a private method * Fix loop variable scoping in public.common.js * Remove MigrationProcessTest.cs * Limit loop iterations in ProductController to 100 * Grand.Web.Admin - Generate unique file names for uploaded zip files * Grand.Web.Admin - Refactor plugin path handling and simplify deletions * Remove [DisableRequestSizeLimit] from AsyncUpload method * Created IAdminStoreService to retrieve the active store in the admin panel (for settings). * Updated validators in the admin panel. * Update plugins (AdminStoreService) * Admin - Update validators (add BaseStoreAccessValidator) * Refactor KnowledgebaseService with new helper methods * Fix redundant rendering of Selector_Store in Header.cshtml * Theme.Plugin - Update ShoppingCartLinks.cshtml * Remove duplicated code * Renamed `GetPushReceivers` to `GetAllowedPushReceivers` in the `IPushNotificationsService` interface and its implementation in `PushNotificationsService`.
1 parent 3852822 commit d69faea

File tree

378 files changed

+4037
-4074
lines changed

Some content is hidden

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

378 files changed

+4037
-4074
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-
IWorkContextAccessor workContextAccessor,
34+
IContextAccessor contextAccessor,
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-
_workContextAccessor = workContextAccessor;
47+
_contextAccessor = contextAccessor;
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 IWorkContextAccessor _workContextAccessor;
61+
private readonly IContextAccessor _contextAccessor;
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(_workContextAccessor.WorkContext.CurrentCustomer,
133+
var registrationRequest = new RegistrationRequest(_contextAccessor.WorkContext.CurrentCustomer,
134134
parameters.Email, parameters.Email,
135135
CommonHelper.GenerateRandomDigitCode(20),
136136
PasswordFormat.Hashed,
137-
_workContextAccessor.WorkContext.CurrentStore.Id,
137+
_contextAccessor.StoreContext.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(_workContextAccessor.WorkContext.CurrentCustomer, parameters));
144+
await _mediator.Publish(new RegisteredByExternalMethod(_contextAccessor.WorkContext.CurrentCustomer, parameters));
145145

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

149149
//associate external account with registered user
150-
await AssociateCustomer(_workContextAccessor.WorkContext.CurrentCustomer, parameters);
150+
await AssociateCustomer(_contextAccessor.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(_workContextAccessor.WorkContext.CurrentCustomer, false);
158+
await _authenticationService.SignIn(_contextAccessor.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(_workContextAccessor.WorkContext.CurrentCustomer) &&
210-
provider.IsAuthenticateStore(_workContextAccessor.WorkContext.CurrentStore)
209+
provider.IsAuthenticateGroup(_contextAccessor.WorkContext.CurrentCustomer) &&
210+
provider.IsAuthenticateStore(_contextAccessor.StoreContext.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(_workContextAccessor.WorkContext.CurrentCustomer) &&
246-
authenticationMethod.IsAuthenticateStore(_workContextAccessor.WorkContext.CurrentStore);
245+
authenticationMethod.IsAuthenticateGroup(_contextAccessor.WorkContext.CurrentCustomer) &&
246+
authenticationMethod.IsAuthenticateStore(_contextAccessor.StoreContext.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(_workContextAccessor.WorkContext.CurrentCustomer)
268-
? _workContextAccessor.WorkContext.CurrentCustomer
267+
var currentLoggedInUser = await _groupService.IsRegistered(_contextAccessor.WorkContext.CurrentCustomer)
268+
? _contextAccessor.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 IWorkContextAccessor _workContextAccessor;
17+
private readonly IContextAccessor _contextAccessor;
1818

1919
public TwoFactorAuthenticationService(
20-
IWorkContextAccessor workContextAccessor,
20+
IContextAccessor contextAccessor,
2121
ICustomerService customerService,
2222
IEnumerable<ISMSVerificationService> smsVerificationService)
2323
{
24-
_workContextAccessor = workContextAccessor;
24+
_contextAccessor = contextAccessor;
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(_workContextAccessor.WorkContext.CurrentStore.CompanyName,
65+
var setupInfo = _twoFactorAuthentication.GenerateSetupCode(_contextAccessor.StoreContext.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-
IWorkContextAccessor workContextAccessor,
27+
IContextAccessor contextAccessor,
2828
IMediator mediator, AccessControlConfig accessControlConfig)
2929
{
3030
_cacheBase = cacheBase;
3131
_brandRepository = brandRepository;
32-
_workContextAccessor = workContextAccessor;
32+
_contextAccessor = contextAccessor;
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 IWorkContextAccessor _workContextAccessor;
42+
private readonly IContextAccessor _contextAccessor;
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 = _workContextAccessor.WorkContext.CurrentCustomer.GetCustomerGroupIds();
80+
var allowedCustomerGroupsIds = _contextAccessor.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: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Grand.Domain;
66
using Grand.Domain.Catalog;
77
using Grand.Domain.Customers;
8+
using Grand.Domain.Stores;
89
using Grand.Infrastructure;
910
using Grand.Infrastructure.Caching;
1011
using Grand.Infrastructure.Caching.Constants;
@@ -32,14 +33,14 @@ public class CategoryService : ICategoryService
3233
/// <param name="accessControlConfig"></param>
3334
public CategoryService(ICacheBase cacheBase,
3435
IRepository<Category> categoryRepository,
35-
IWorkContextAccessor workContextAccessor,
36+
IContextAccessor contextAccessor,
3637
IMediator mediator,
3738
IAclService aclService,
3839
AccessControlConfig accessControlConfig)
3940
{
4041
_cacheBase = cacheBase;
4142
_categoryRepository = categoryRepository;
42-
_workContextAccessor = workContextAccessor;
43+
_contextAccessor = contextAccessor;
4344
_mediator = mediator;
4445
_aclService = aclService;
4546
_accessControlConfig = accessControlConfig;
@@ -50,12 +51,15 @@ public CategoryService(ICacheBase cacheBase,
5051
#region Fields
5152

5253
private readonly IRepository<Category> _categoryRepository;
53-
private readonly IWorkContextAccessor _workContextAccessor;
54+
private readonly IContextAccessor _contextAccessor;
5455
private readonly IMediator _mediator;
5556
private readonly ICacheBase _cacheBase;
5657
private readonly IAclService _aclService;
5758
private readonly AccessControlConfig _accessControlConfig;
5859

60+
private Customer CurrentCustomer => _contextAccessor.WorkContext.CurrentCustomer;
61+
private Store CurrentStore => _contextAccessor.StoreContext.CurrentStore;
62+
5963
#endregion
6064

6165
#region Methods
@@ -91,7 +95,7 @@ public virtual async Task<IPagedList<Category>> GetAllCategories(string parentId
9195
if (!showHidden && !_accessControlConfig.IgnoreAcl)
9296
{
9397
//Limited to customer group (access control list)
94-
var allowedCustomerGroupsIds = _workContextAccessor.WorkContext.CurrentCustomer.GetCustomerGroupIds();
98+
var allowedCustomerGroupsIds = CurrentCustomer.GetCustomerGroupIds();
9599
query = from p in query
96100
where !p.LimitedToGroups || allowedCustomerGroupsIds.Any(x => p.CustomerGroups.Contains(x))
97101
select p;
@@ -124,23 +128,23 @@ public virtual async Task<IList<Category>> GetMenuCategories()
124128
switch (_accessControlConfig.IgnoreAcl)
125129
{
126130
case true when
127-
string.IsNullOrEmpty(_workContextAccessor.WorkContext.CurrentStore.Id) || _accessControlConfig.IgnoreStoreLimitations:
131+
string.IsNullOrEmpty(CurrentStore.Id) || _accessControlConfig.IgnoreStoreLimitations:
128132
return await Task.FromResult(query.ToList());
129133
case false:
130134
{
131135
//Limited to customer group (access control list)
132-
var allowedCustomerGroupsIds = _workContextAccessor.WorkContext.CurrentCustomer.GetCustomerGroupIds();
136+
var allowedCustomerGroupsIds = CurrentCustomer.GetCustomerGroupIds();
133137
query = from p in query
134138
where !p.LimitedToGroups || allowedCustomerGroupsIds.Any(x => p.CustomerGroups.Contains(x))
135139
select p;
136140
break;
137141
}
138142
}
139143

140-
if (!string.IsNullOrEmpty(_workContextAccessor.WorkContext.CurrentStore.Id) && !_accessControlConfig.IgnoreStoreLimitations)
144+
if (!string.IsNullOrEmpty(CurrentStore.Id) && !_accessControlConfig.IgnoreStoreLimitations)
141145
//Limited to stores rule
142146
query = from p in query
143-
where !p.LimitedToStores || p.Stores.Contains(_workContextAccessor.WorkContext.CurrentStore.Id)
147+
where !p.LimitedToStores || p.Stores.Contains(CurrentStore.Id)
144148
select p;
145149
return await Task.FromResult(query.ToList());
146150
}
@@ -155,10 +159,8 @@ public virtual async Task<IList<Category>> GetMenuCategories()
155159
public virtual async Task<IList<Category>> GetAllCategoriesByParentCategoryId(string parentCategoryId = "",
156160
bool showHidden = false, bool includeAllLevels = false)
157161
{
158-
var storeId = _workContextAccessor.WorkContext.CurrentStore.Id;
159-
var customer = _workContextAccessor.WorkContext.CurrentCustomer;
160162
var key = string.Format(CacheKey.CATEGORIES_BY_PARENT_CATEGORY_ID_KEY, parentCategoryId, showHidden,
161-
customer.Id, storeId, includeAllLevels);
163+
CurrentCustomer.Id, CurrentStore.Id, includeAllLevels);
162164
return await _cacheBase.GetAsync(key, async () =>
163165
{
164166
var query = _categoryRepository.Table.Where(c => c.ParentCategoryId == parentCategoryId);
@@ -170,7 +172,7 @@ public virtual async Task<IList<Category>> GetAllCategoriesByParentCategoryId(st
170172
if (!_accessControlConfig.IgnoreAcl)
171173
{
172174
//Limited to customer groups rules
173-
var allowedCustomerGroupsIds = _workContextAccessor.WorkContext.CurrentCustomer.GetCustomerGroupIds();
175+
var allowedCustomerGroupsIds = CurrentCustomer.GetCustomerGroupIds();
174176
query = from p in query
175177
where !p.LimitedToGroups || allowedCustomerGroupsIds.Any(x => p.CustomerGroups.Contains(x))
176178
select p;
@@ -179,7 +181,7 @@ public virtual async Task<IList<Category>> GetAllCategoriesByParentCategoryId(st
179181
if (!_accessControlConfig.IgnoreStoreLimitations)
180182
//Limited to stores rules
181183
query = from p in query
182-
where !p.LimitedToStores || p.Stores.Contains(storeId)
184+
where !p.LimitedToStores || p.Stores.Contains(CurrentStore.Id)
183185
select p;
184186
}
185187

@@ -207,8 +209,8 @@ public virtual async Task<IList<Category>> GetAllCategoriesDisplayedOnHomePage(b
207209
var categories = await Task.FromResult(query.ToList());
208210
if (!showHidden)
209211
categories = categories
210-
.Where(c => _aclService.Authorize(c, _workContextAccessor.WorkContext.CurrentCustomer) &&
211-
_aclService.Authorize(c, _workContextAccessor.WorkContext.CurrentStore.Id))
212+
.Where(c => _aclService.Authorize(c, CurrentCustomer) &&
213+
_aclService.Authorize(c, CurrentStore.Id))
212214
.ToList();
213215

214216
return categories;
@@ -228,8 +230,8 @@ public virtual async Task<IList<Category>> GetAllCategoriesFeaturedProductsOnHom
228230
var categories = await Task.FromResult(query.ToList());
229231
if (!showHidden)
230232
categories = categories
231-
.Where(c => _aclService.Authorize(c, _workContextAccessor.WorkContext.CurrentCustomer) &&
232-
_aclService.Authorize(c, _workContextAccessor.WorkContext.CurrentStore.Id))
233+
.Where(c => _aclService.Authorize(c, CurrentCustomer) &&
234+
_aclService.Authorize(c, CurrentStore.Id))
233235
.ToList();
234236
return categories;
235237
}
@@ -245,8 +247,8 @@ public virtual async Task<IList<Category>> GetAllCategoriesSearchBox()
245247
.OrderBy(x => x.SearchBoxDisplayOrder);
246248

247249
var categories = (await Task.FromResult(query.ToList()))
248-
.Where(c => _aclService.Authorize(c, _workContextAccessor.WorkContext.CurrentCustomer) &&
249-
_aclService.Authorize(c, _workContextAccessor.WorkContext.CurrentStore.Id))
250+
.Where(c => _aclService.Authorize(c, CurrentCustomer) &&
251+
_aclService.Authorize(c, CurrentStore.Id))
250252
.ToList();
251253

252254
return categories;
@@ -268,8 +270,8 @@ public virtual async Task<IList<Category>> GetCategoryBreadCrumb(Category catego
268270
while (category != null && //not null
269271
(showHidden || category.Published) && //published
270272
(showHidden ||
271-
_aclService.Authorize(category, _workContextAccessor.WorkContext.CurrentCustomer)) && //limited to customer groups
272-
(showHidden || _aclService.Authorize(category, _workContextAccessor.WorkContext.CurrentStore.Id)) && //limited to store
273+
_aclService.Authorize(category, CurrentCustomer)) && //limited to customer groups
274+
(showHidden || _aclService.Authorize(category, CurrentStore.Id)) && //limited to store
273275
!alreadyProcessedCategoryIds.Contains(category.Id))
274276
{
275277
result.Add(category);
@@ -301,8 +303,8 @@ public virtual IList<Category> GetCategoryBreadCrumb(Category category, IList<Ca
301303
while (category != null && //not null
302304
(showHidden || category.Published) && //published
303305
(showHidden ||
304-
_aclService.Authorize(category, _workContextAccessor.WorkContext.CurrentCustomer)) && //limited to customer groups
305-
(showHidden || _aclService.Authorize(category, _workContextAccessor.WorkContext.CurrentStore.Id)) && //limited to store
306+
_aclService.Authorize(category, CurrentCustomer)) && //limited to customer groups
307+
(showHidden || _aclService.Authorize(category, CurrentStore.Id)) && //limited to store
306308
!alreadyProcessedCategoryIds.Contains(category.Id)) //avoid circular references
307309
{
308310
result.Add(category);

0 commit comments

Comments
 (0)