Skip to content

Commit

Permalink
Admin - GetActiveStore - Minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
KrzysztofPajak committed Jan 11, 2022
1 parent 6d40a58 commit d141950
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
14 changes: 8 additions & 6 deletions src/Web/Grand.Web.Admin/Components/StoreScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using System.Threading.Tasks;
using Grand.Business.Common.Interfaces.Directory;
using System.Linq;
using System.Collections.Generic;
using Grand.Domain.Stores;

namespace Grand.Web.Admin.Components
{
Expand Down Expand Up @@ -52,22 +54,22 @@ public async Task<IViewComponentResult> InvokeAsync()
Name = s.Shortcut
});
}
model.StoreId = await GetActiveStore(_storeService, _workContext);
model.StoreId = await GetActiveStore(allStores);
return View(model);
}

#endregion

#region Methods

private async Task<string> GetActiveStore(IStoreService storeService, IWorkContext workContext)
private async Task<string> GetActiveStore(IList<Store> stores)
{
//ensure that we have 2 (or more) stores
if ((await storeService.GetAllStores()).Count < 2)
return string.Empty;
if (stores.Count < 2)
return stores.FirstOrDefault().Id;

var storeId = workContext.CurrentCustomer.GetUserFieldFromEntity<string>(SystemCustomerFieldNames.AdminAreaStoreScopeConfiguration);
var store = await storeService.GetStoreById(storeId);
var storeId = _workContext.CurrentCustomer.GetUserFieldFromEntity<string>(SystemCustomerFieldNames.AdminAreaStoreScopeConfiguration);
var store = await _storeService.GetStoreById(storeId);

return store != null ? store.Id : "";
}
Expand Down
17 changes: 14 additions & 3 deletions src/Web/Grand.Web.Admin/Controllers/BaseAdminController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Grand.Business.Common.Interfaces.Directory;

namespace Grand.Web.Admin.Controllers
{
Expand Down Expand Up @@ -66,15 +67,25 @@ protected virtual async Task<string> GetActiveStore()
{
var storeService = HttpContext.RequestServices.GetRequiredService<IStoreService>();
var workContext = HttpContext.RequestServices.GetRequiredService<IWorkContext>();
var groupService = HttpContext.RequestServices.GetRequiredService<IGroupService>();

var stores = await storeService.GetAllStores();
if (stores.Count < 2)
return stores.FirstOrDefault().Id;

var storeId = workContext.CurrentCustomer.GetUserFieldFromEntity<string>(SystemCustomerFieldNames.AdminAreaStoreScopeConfiguration);
var store = await storeService.GetStoreById(storeId);
if (await groupService.IsStaff(workContext.CurrentCustomer))
{
return workContext.CurrentCustomer.StaffStoreId;
}

return store != null ? store.Id : workContext.CurrentStore.Id;
var storeId = workContext.CurrentCustomer.GetUserFieldFromEntity<string>(SystemCustomerFieldNames.AdminAreaStoreScopeConfiguration);
if (!string.IsNullOrEmpty(storeId))
{
var store = await storeService.GetStoreById(storeId);
if (store != null)
return store.Id;
}
return stores.FirstOrDefault().Id;
}
/// <summary>
/// Creates a <see cref="T:System.Web.Mvc.JsonResult"/> object that serializes the specified object to JavaScript Object Notation (JSON) format using the content type, content encoding, and the JSON request behavior.
Expand Down

0 comments on commit d141950

Please sign in to comment.