Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.
C# aspnet-corecsharpdockerdotnetecommerceecommerce-platformmongodbmulti-storemulti-vendornosqlonline-shopshoppingcart
refactor(arch001): reduce Admin/Store CategoryController to thin BaseCategoryController subclasses
KrzysztofPajakdc42231b6306
diff --git a/src/Web/Grand.Web.Admin/Controllers/CategoryController.cs b/src/Web/Grand.Web.Admin/Controllers/CategoryController.csindex 9b962438d..baf24ce90 100644--- a/src/Web/Grand.Web.Admin/Controllers/CategoryController.cs+++ b/src/Web/Grand.Web.Admin/Controllers/CategoryController.cs@@ -1,350 +1,33 @@-using Grand.Business.Core.Dto;-using Grand.Business.Core.Extensions;using Grand.Business.Core.Interfaces.Catalog.Categories;+using Grand.Business.Core.Interfaces.Catalog.Products;using Grand.Business.Core.Interfaces.Common.Localization;-using Grand.Business.Core.Interfaces.ExportImport;using Grand.Domain.Catalog;-using Grand.Domain.Permissions;-using Grand.Web.AdminShared.Extensions.Mapping;+using Grand.Web.Admin.Extensions;+using Grand.Web.AdminShared.Controllers;using Grand.Web.AdminShared.Interfaces;-using Grand.Web.AdminShared.Models.Catalog;-using Grand.Web.AdminShared.Models.Common;-using Grand.Web.Common.DataSource;using Grand.Web.Common.Filters;-using Grand.Web.Common.Security.Authorization;+using Grand.Web.Common.Localization;using Microsoft.AspNetCore.Mvc;namespace Grand.Web.Admin.Controllers;-[PermissionAuthorize(PermissionSystemName.Categories)]-public class CategoryController : BaseAdminController-{- #region Constructors-- public CategoryController(- ICategoryService categoryService,- ICategoryViewModelService categoryViewModelService,- ILanguageService languageService,- ITranslationService translationService,- IPictureViewModelService pictureViewModelService)- {- _categoryService = categoryService;- _categoryViewModelService = categoryViewModelService;- _languageService = languageService;- _translationService = translationService;- _pictureViewModelService = pictureViewModelService;- }-- #endregion-- #region Fields-- private readonly ICategoryService _categoryService;- private readonly ICategoryViewModelService _categoryViewModelService;- private readonly ILanguageService _languageService;- private readonly ITranslationService _translationService;- private readonly IPictureViewModelService _pictureViewModelService;-- #endregion-- #region List-- public IActionResult Index()- {- return RedirectToAction("List");- }-- public async Task<IActionResult> List()- {- var model = await _categoryViewModelService.PrepareCategoryListModel(string.Empty);- return View(model);- }-- [PermissionAuthorizeAction(PermissionActionName.List)]- [HttpPost]- public async Task<IActionResult> List(DataSourceRequest command, CategoryListModel model)- {- var categories = await _categoryViewModelService.PrepareCategoryListModel(model, command.Page, command.PageSize);- var gridModel = new DataSourceResult {- Data = categories.categoryListModel,- Total = categories.totalCount- };- return Json(gridModel);- }-- #endregion-- #region Create / Edit / Delete-- [PermissionAuthorizeAction(PermissionActionName.Create)]- public async Task<IActionResult> Create()- {- var model = await _categoryViewModelService.PrepareCategoryModel(string.Empty);- //locales- await AddLocales(_languageService, model.Locales);-- return View(model);- }-- [PermissionAuthorizeAction(PermissionActionName.Edit)]- [HttpPost]- [ArgumentNameFilter(KeyName = "save-continue", Argument = "continueEditing")]- public async Task<IActionResult> Create(CategoryModel model, bool continueEditing)- {- if (ModelState.IsValid)- {- var category = await _categoryViewModelService.InsertCategoryModel(model);- Success(_translationService.GetResource("Admin.Catalog.Categories.Added"));- return continueEditing ? RedirectToAction("Edit", new { id = category.Id }) : RedirectToAction("List");- }-- //If we got this far, something failed, redisplay form- model = await _categoryViewModelService.PrepareCategoryModel(model, null, string.Empty);-- return View(model);- }-- [PermissionAuthorizeAction(PermissionActionName.Preview)]- public async Task<IActionResult> Edit(string id)- {- var category = await _categoryService.GetCategoryById(id);- if (category == null)- //No category found with the specified id- return RedirectToAction("List");-- var model = category.ToModel();- //locales- await AddLocales(_languageService, model.Locales, (locale, languageId) =>- {- locale.Name = category.GetTranslation(x => x.Name, languageId, false);- locale.Description = category.GetTranslation(x => x.Description, languageId, false);- locale.BottomDescription = category.GetTranslation(x => x.BottomDescription, languageId, false);- locale.MetaKeywords = category.GetTranslation(x => x.MetaKeywords, languageId, false);- locale.MetaDescription = category.GetTranslation(x => x.MetaDescription, languageId, false);- locale.MetaTitle = category.GetTranslation(x => x.MetaTitle, languageId, false);- locale.SeName = category.GetSeName(languageId, false);- locale.Flag = category.GetTranslation(x => x.Flag, languageId, false);- });- model = await _categoryViewModelService.PrepareCategoryModel(model, category, string.Empty);-- return View(model);- }-- [PermissionAuthorizeAction(PermissionActionName.Edit)]- [HttpPost]- [ArgumentNameFilter(KeyName = "save-continue", Argument = "continueEditing")]- public async Task<IActionResult> Edit(CategoryModel model, bool continueEditing)- {- var category = await _categoryService.GetCategoryById(model.Id);- if (category == null)- //No category found with the specified id- return RedirectToAction("List");-- if (ModelState.IsValid)- {- category = await _categoryViewModelService.UpdateCategoryModel(category, model);- Success(_translationService.GetResource("Admin.Catalog.Categories.Updated"));- if (continueEditing)- {- //selected tab- await SaveSelectedTabIndex();- return RedirectToAction("Edit", new { id = category.Id });- }- return RedirectToAction("List");- }-- //If we got this far, something failed, redisplay form- model = await _categoryViewModelService.PrepareCategoryModel(model, category, string.Empty);-- return View(model);- }-- [PermissionAuthorizeAction(PermissionActionName.Delete)]- [HttpPost]- public async Task<IActionResult> Delete(string id)- {- var category = await _categoryService.GetCategoryById(id);- if (category == null)- //No category found with the specified id- return RedirectToAction("List");-- if (ModelState.IsValid)- {- await _categoryViewModelService.DeleteCategory(category);- Success(_translationService.GetResource("Admin.Catalog.Categories.Deleted"));- }-- return RedirectToAction("List");- }-- #endregion-- #region Picture-- [PermissionAuthorizeAction(PermissionActionName.Preview)]- public async Task<IActionResult> PicturePopup(string categoryId)- {- var category = await _categoryService.GetCategoryById(categoryId);- if (category == null)- return Content("Category not exist");-- if (string.IsNullOrEmpty(category.PictureId))- return Content("Picture not exist");-- return View("Partials/PicturePopup",- await _pictureViewModelService.PreparePictureModel(category.PictureId, category.Id));- }-- [PermissionAuthorizeAction(PermissionActionName.Edit)]- [HttpPost]- public async Task<IActionResult> PicturePopup(PictureModel model)- {- if (ModelState.IsValid)- {- var category = await _categoryService.GetCategoryById(model.ObjectId);- if (category == null)- throw new ArgumentException("No category found with the specified id");-- if (string.IsNullOrEmpty(category.PictureId))- throw new ArgumentException("No picture found with the specified id");-- if (category.PictureId != model.Id)- throw new ArgumentException("Picture ident doesn't fit with category");-- await _pictureViewModelService.UpdatePicture(model);-- return Content("");- }-- Error(ModelState);-- return View("Partials/PicturePopup", model);- }-- #endregion-- #region Export / Import-- [PermissionAuthorizeAction(PermissionActionName.Export)]- public async Task<IActionResult> ExportXlsx([FromServices] IExportManager<Category> exportManager)- {- try- {- var bytes = await exportManager.Export(await _categoryService.GetAllCategories(parentId: null, categoryName: "", storeId: "", showHidden: true));- return File(bytes, "text/xls", "categories.xlsx");- }- catch (Exception exc)- {- Error(exc);- return RedirectToAction("List");- }- }-- [PermissionAuthorizeAction(PermissionActionName.Import)]- [HttpPost]- public async Task<IActionResult> ImportFromXlsx(IFormFile importexcelfile,- [FromServices] IImportManager<CategoryDto> importManager)- {- try- {- if (importexcelfile is { Length: > 0 })- {- await importManager.Import(importexcelfile.OpenReadStream());- }- else- {- Error(_translationService.GetResource("Admin.Common.UploadFile"));- return RedirectToAction("List");- }-- Success(_translationService.GetResource("Admin.Catalog.Category.Imported"));- return RedirectToAction("List");- }- catch (Exception exc)- {- Error(exc);- return RedirectToAction("List");- }- }-- #endregion-- #region Products-- [PermissionAuthorizeAction(PermissionActionName.Preview)]- [HttpPost]- public async Task<IActionResult> ProductList(DataSourceRequest command, string categoryId)- {- var category = await _categoryService.GetCategoryById(categoryId);- var productCategories = await _categoryViewModelService.PrepareCategoryProductModel(categoryId, command.Page, command.PageSize);- var gridModel = new DataSourceResult {- Data = productCategories.categoryProductModels,- Total = productCategories.totalCount- };-- return Json(gridModel);- }-- [PermissionAuthorizeAction(PermissionActionName.Edit)]- public async Task<IActionResult> ProductUpdate(CategoryModel.CategoryProductModel model)- {- if (ModelState.IsValid)- {- await _categoryViewModelService.UpdateProductCategoryModel(model);- return new JsonResult("");- }-- return ErrorForKendoGridJson(ModelState);- }-- [PermissionAuthorizeAction(PermissionActionName.Edit)]- public async Task<IActionResult> ProductDelete(CategoryModel.CategoryProductModel model)- {- if (ModelState.IsValid)- {- await _categoryViewModelService.DeleteProductCategoryModel(model.Id, model.ProductId);- return new JsonResult("");- }-- return ErrorForKendoGridJson(ModelState);- }-- [PermissionAuthorizeAction(PermissionActionName.Edit)]- public async Task<IActionResult> ProductAddPopup(string categoryId)- {- var model = await _categoryViewModelService.PrepareAddCategoryProductModel(string.Empty);- model.CategoryId = categoryId;- return View(model);- }-- [PermissionAuthorizeAction(PermissionActionName.Edit)]- [HttpPost]- public async Task<IActionResult> ProductAddPopupList(DataSourceRequest command,- CategoryModel.AddCategoryProductModel model)- {- var gridModel = new DataSourceResult();-- var products = await _categoryViewModelService.PrepareProductModel(model, command.Page, command.PageSize);- gridModel.Data = products.products.ToList();- gridModel.Total = products.totalCount;-- return Json(gridModel);- }-- [PermissionAuthorizeAction(PermissionActionName.Edit)]- [HttpPost]- public async Task<IActionResult> ProductAddPopup(CategoryModel.AddCategoryProductModel model)- {- if (ModelState.IsValid)- {- if (model.SelectedProductIds != null) await _categoryViewModelService.InsertCategoryProductModel(model);-- return Content("");- }-- Error(ModelState);- return View(model);- }-- #endregion-}\ No newline at end of file+// Reduced to a thin subclass of BaseCategoryController (ARCH-001 Category consolidation). All+// regions of behavior live in the shared base; this class only supplies Admin's DI wiring plus the+// attributes that used to arrive transitively via BaseAdminController - BaseCategoryController+// can't inherit any single host's base controller (it's shared across Admin/Store, each with a+// different [Area]/[Authorize*] pair), so each subclass restates its own host's attribute set+// explicitly. Same pattern as ProductController (see that file).+[AuthorizeAdmin]+[AutoValidateAntiforgeryToken]+[Area(Constants.AreaAdmin)]+[AuthorizeMenu]+public class CategoryController(+ ICategoryService categoryService,+ ICategoryViewModelService categoryViewModelService,+ ILanguageService languageService,+ ITranslationService translationService,+ IPictureViewModelService pictureViewModelService,+ IProductService productService,+ IAdminDataScope<Category> scope)+ : BaseCategoryController(categoryService, categoryViewModelService, languageService,+ translationService, pictureViewModelService, productService, scope);diff --git a/src/Web/Grand.Web.Store/Controllers/CategoryController.cs b/src/Web/Grand.Web.Store/Controllers/CategoryController.csindex 22d220ba4..adeec3876 100644--- a/src/Web/Grand.Web.Store/Controllers/CategoryController.cs+++ b/src/Web/Grand.Web.Store/Controllers/CategoryController.cs@@ -1,366 +1,46 @@-using Grand.Business.Core.Extensions;using Grand.Business.Core.Interfaces.Catalog.Categories;using Grand.Business.Core.Interfaces.Catalog.Products;using Grand.Business.Core.Interfaces.Common.Localization;-using Grand.Domain.Permissions;-using Grand.Infrastructure;-using Grand.Web.AdminShared.Extensions;-using Grand.Web.AdminShared.Extensions.Mapping;+using Grand.Domain.Catalog;+using Grand.Web.AdminShared.Controllers;using Grand.Web.AdminShared.Interfaces;-using Grand.Web.AdminShared.Models.Catalog;-using Grand.Web.AdminShared.Models.Common;-using Grand.Web.Common.DataSource;using Grand.Web.Common.Filters;-using Grand.Web.Common.Security.Authorization;+using Grand.Web.Common.Localization;+using Grand.Web.Store.Extensions;using Microsoft.AspNetCore.Mvc;namespace Grand.Web.Store.Controllers;-[PermissionAuthorize(PermissionSystemName.Categories)]-public class CategoryController : BaseStoreController+// Reduced to a thin subclass of BaseCategoryController (ARCH-001 Category consolidation). All+// regions of behavior live in the shared base; this class only supplies Store's DI wiring, the+// EditWarningCheck hook, and the attributes that used to arrive transitively via+// BaseStoreController. Same pattern as ProductController (see that file).+[AutoValidateAntiforgeryToken]+[Area(Constants.AreaStore)]+[AuthorizeStore]+[AuthorizeMenu]+public class CategoryController(+ ICategoryService categoryService,+ ICategoryViewModelService categoryViewModelService,+ ILanguageService languageService,+ ITranslationService translationService,+ IPictureViewModelService pictureViewModelService,+ IProductService productService,+ IAdminDataScope<Category> scope)+ : BaseCategoryController(categoryService, categoryViewModelService, languageService,+ translationService, pictureViewModelService, productService, scope){- #region Constructors-- public CategoryController(- ICategoryService categoryService,- ICategoryViewModelService categoryViewModelService,- ILanguageService languageService,- ITranslationService translationService,- IContextAccessor contextAccessor,- IPictureViewModelService pictureViewModelService,- IProductService productService)- {- _categoryService = categoryService;- _categoryViewModelService = categoryViewModelService;- _languageService = languageService;- _translationService = translationService;- _contextAccessor = contextAccessor;- _pictureViewModelService = pictureViewModelService;- _productService = productService;- }-- #endregion-- #region Fields-- private readonly ICategoryService _categoryService;- private readonly ICategoryViewModelService _categoryViewModelService;- private readonly ILanguageService _languageService;- private readonly ITranslationService _translationService;- private readonly IContextAccessor _contextAccessor;- private readonly IPictureViewModelService _pictureViewModelService;- private readonly IProductService _productService;-- #endregion-- #region List-- public IActionResult Index()- {- return RedirectToAction("List");- }-- public async Task<IActionResult> List()- {- var model = await _categoryViewModelService.PrepareCategoryListModel(_contextAccessor.WorkContext.CurrentCustomer.StaffStoreId);- return View(model);- }-- [PermissionAuthorizeAction(PermissionActionName.List)]- [HttpPost]- public async Task<IActionResult> List(DataSourceRequest command, CategoryListModel model)- {- model.SearchStoreId = _contextAccessor.WorkContext.CurrentCustomer.StaffStoreId;- var categories = await _categoryViewModelService.PrepareCategoryListModel(model, command.Page, command.PageSize);- var gridModel = new DataSourceResult {- Data = categories.categoryListModel,- Total = categories.totalCount- };- return Json(gridModel);- }-- #endregion-- #region Create / Edit / Delete-- [PermissionAuthorizeAction(PermissionActionName.Create)]- public async Task<IActionResult> Create()- {- var model = await _categoryViewModelService.PrepareCategoryModel(_contextAccessor.WorkContext.CurrentCustomer.StaffStoreId);- //locales- await AddLocales(_languageService, model.Locales);-- return View(model);- }-- [PermissionAuthorizeAction(PermissionActionName.Edit)]- [HttpPost]- [ArgumentNameFilter(KeyName = "save-continue", Argument = "continueEditing")]- public async Task<IActionResult> Create(CategoryModel model, bool continueEditing)- {- if (ModelState.IsValid)- {- model.Stores = [_contextAccessor.WorkContext.CurrentCustomer.StaffStoreId];- var category = await _categoryViewModelService.InsertCategoryModel(model);- Success(_translationService.GetResource("Admin.Catalog.Categories.Added"));- return continueEditing ? RedirectToAction("Edit", new { id = category.Id }) : RedirectToAction("List");- }-- //If we got this far, something failed, redisplay form- model = await _categoryViewModelService.PrepareCategoryModel(model, null, _contextAccessor.WorkContext.CurrentCustomer.StaffStoreId);- return View(model);- }-- [PermissionAuthorizeAction(PermissionActionName.Preview)]- public async Task<IActionResult> Edit(string id)- {- var category = await _categoryService.GetCategoryById(id);- if (category == null)- //No category found with the specified id- return RedirectToAction("List");-- if (!category.LimitedToStores || (category.LimitedToStores &&- category.Stores.Contains(_contextAccessor.WorkContext.CurrentCustomer.StaffStoreId) &&- category.Stores.Count > 1))- {- Warning(_translationService.GetResource("Admin.Catalog.Categories.Permissions"));- }- else- {- if (!category.AccessToEntityByStore(_contextAccessor.WorkContext.CurrentCustomer.StaffStoreId))- return RedirectToAction("List");- }-- var model = category.ToModel();- //locales- await AddLocales(_languageService, model.Locales, (locale, languageId) =>- {- locale.Name = category.GetTranslation(x => x.Name, languageId, false);- locale.Description = category.GetTranslation(x => x.Description, languageId, false);- locale.BottomDescription = category.GetTranslation(x => x.BottomDescription, languageId, false);- locale.MetaKeywords = category.GetTranslation(x => x.MetaKeywords, languageId, false);- locale.MetaDescription = category.GetTranslation(x => x.MetaDescription, languageId, false);- locale.MetaTitle = category.GetTranslation(x => x.MetaTitle, languageId, false);- locale.SeName = category.GetSeName(languageId, false);- locale.Flag = category.GetTranslation(x => x.Flag, languageId, false);- });-- model = await _categoryViewModelService.PrepareCategoryModel(model, category, _contextAccessor.WorkContext.CurrentCustomer.StaffStoreId);- return View(model);- }-- [PermissionAuthorizeAction(PermissionActionName.Edit)]- [HttpPost]- [ArgumentNameFilter(KeyName = "save-continue", Argument = "continueEditing")]- public async Task<IActionResult> Edit(CategoryModel model, bool continueEditing)- {- var category = await _categoryService.GetCategoryById(model.Id);- if (category == null)- //No category found with the specified id- return RedirectToAction("List");-- if (!category.AccessToEntityByStore(_contextAccessor.WorkContext.CurrentCustomer.StaffStoreId))- return RedirectToAction("Edit", new { id = category.Id });-- if (ModelState.IsValid)- {- model.Stores = [_contextAccessor.WorkContext.CurrentCustomer.StaffStoreId];- category = await _categoryViewModelService.UpdateCategoryModel(category, model);-- Success(_translationService.GetResource("Admin.Catalog.Categories.Updated"));- if (continueEditing)- {- //selected tab- await SaveSelectedTabIndex();-- return RedirectToAction("Edit", new { id = category.Id });- }-- return RedirectToAction("List");- }-- //If we got this far, something failed, redisplay form- model = await _categoryViewModelService.PrepareCategoryModel(model, category, _contextAccessor.WorkContext.CurrentCustomer.StaffStoreId);- return View(model);- }-- [PermissionAuthorizeAction(PermissionActionName.Delete)]- [HttpPost]- public async Task<IActionResult> Delete(string id)- {- var category = await _categoryService.GetCategoryById(id);- if (category == null)- //No category found with the specified id- return RedirectToAction("List");-- if (!category.AccessToEntityByStore(_contextAccessor.WorkContext.CurrentCustomer.StaffStoreId))- return RedirectToAction("Edit", new { id = category.Id });-- if (ModelState.IsValid)- {- await _categoryViewModelService.DeleteCategory(category);- Success(_translationService.GetResource("Admin.Catalog.Categories.Deleted"));- }-- return RedirectToAction("List");- }-- #endregion-- #region Picture-- [PermissionAuthorizeAction(PermissionActionName.Preview)]- public async Task<IActionResult> PicturePopup(string categoryId)- {- var category = await _categoryService.GetCategoryById(categoryId);- if (category == null)- return Content("Category not exist");-- if (string.IsNullOrEmpty(category.PictureId))- return Content("Picture not exist");-- if (!category.AccessToEntityByStore(_contextAccessor.WorkContext.CurrentCustomer.StaffStoreId))- return Content("This is not your category");-- return View("Partials/PicturePopup",- await _pictureViewModelService.PreparePictureModel(category.PictureId, category.Id));- }-- [PermissionAuthorizeAction(PermissionActionName.Edit)]- [HttpPost]- public async Task<IActionResult> PicturePopup(PictureModel model)- {- if (ModelState.IsValid)- {- var category = await _categoryService.GetCategoryById(model.ObjectId);- if (category == null)- throw new ArgumentException("No category found with the specified id");-- if (!category.AccessToEntityByStore(_contextAccessor.WorkContext.CurrentCustomer.StaffStoreId))- return Content("This is not your category");-- if (string.IsNullOrEmpty(category.PictureId))- throw new ArgumentException("No picture found with the specified id");-- if (category.PictureId != model.Id)- throw new ArgumentException("Picture ident doesn't fit with category");-- await _pictureViewModelService.UpdatePicture(model);-- return Content("");- }-- Error(ModelState);-- return View("Partials/PicturePopup", model);- }-- #endregion-- #region Products-- [PermissionAuthorizeAction(PermissionActionName.Preview)]- [HttpPost]- public async Task<IActionResult> ProductList(DataSourceRequest command, string categoryId)- {- var category = await _categoryService.GetCategoryById(categoryId);-- if (!category.AccessToEntityByStore(_contextAccessor.WorkContext.CurrentCustomer.StaffStoreId))- return ErrorForKendoGridJson("This is not your category");-- var productCategories = await _categoryViewModelService.PrepareCategoryProductModel(categoryId, command.Page, command.PageSize);- var gridModel = new DataSourceResult {- Data = productCategories.categoryProductModels,- Total = productCategories.totalCount- };-- return Json(gridModel);- }-- [PermissionAuthorizeAction(PermissionActionName.Edit)]- public async Task<IActionResult> ProductUpdate(CategoryModel.CategoryProductModel model)- {- var product = await _productService.GetProductById(model.ProductId);- if (product == null || !product.AccessToEntityByStore(_contextAccessor.WorkContext.CurrentCustomer.StaffStoreId))- return ErrorForKendoGridJson("This is not your product");-- if (ModelState.IsValid)- {- await _categoryViewModelService.UpdateProductCategoryModel(model);- return new JsonResult("");- }-- return ErrorForKendoGridJson(ModelState);- }-- [PermissionAuthorizeAction(PermissionActionName.Edit)]- public async Task<IActionResult> ProductDelete(CategoryModel.CategoryProductModel model)- {- var product = await _productService.GetProductById(model.ProductId);- if (product == null || !product.AccessToEntityByStore(_contextAccessor.WorkContext.CurrentCustomer.StaffStoreId))- return ErrorForKendoGridJson("This is not your product");-- if (ModelState.IsValid)- {- await _categoryViewModelService.DeleteProductCategoryModel(model.Id, model.ProductId);- return new JsonResult("");- }-- return ErrorForKendoGridJson(ModelState);- }-- [PermissionAuthorizeAction(PermissionActionName.Edit)]- public async Task<IActionResult> ProductAddPopup(string categoryId)- {- var model = await _categoryViewModelService.PrepareAddCategoryProductModel(_contextAccessor.WorkContext.CurrentCustomer.StaffStoreId);- model.CategoryId = categoryId;- return View(model);- }-- [PermissionAuthorizeAction(PermissionActionName.Edit)]- [HttpPost]- public async Task<IActionResult> ProductAddPopupList(DataSourceRequest command, CategoryModel.AddCategoryProductModel model)- {- var gridModel = new DataSourceResult();- model.SearchStoreId = _contextAccessor.WorkContext.CurrentCustomer.StaffStoreId;- var products = await _categoryViewModelService.PrepareProductModel(model, command.Page, command.PageSize);- gridModel.Data = products.products.ToList();- gridModel.Total = products.totalCount;-- return Json(gridModel);- }-- [PermissionAuthorizeAction(PermissionActionName.Edit)]- [HttpPost]- public async Task<IActionResult> ProductAddPopup(CategoryModel.AddCategoryProductModel model)- {- var category = await _categoryService.GetCategoryById(model.CategoryId);- if (category == null || !category.AccessToEntityByStore(_contextAccessor.WorkContext.CurrentCustomer.StaffStoreId))- return Content("This is not your category");-- if (ModelState.IsValid)- {- //InsertCategoryProductModel mutates each selected product's ProductCategories collection,- //so every selected id must also belong to the current store.- if (model.SelectedProductIds != null)- {- var validIds = new List<string>();- foreach (var id in model.SelectedProductIds)- {- var selected = await _productService.GetProductById(id);- if (selected != null && selected.AccessToEntityByStore(_contextAccessor.WorkContext.CurrentCustomer.StaffStoreId))- validIds.Add(id);- }- model.SelectedProductIds = validIds.ToArray();- if (validIds.Any()) await _categoryViewModelService.InsertCategoryProductModel(model);- }-- return Content("");- }-- Error(ModelState);- return View(model);- }-- #endregion-}\ No newline at end of file+ // Re-derived from the original Store CategoryController.Edit(GET) (pre-cutover:+ // src/Web/Grand.Web.Store/Controllers/CategoryController.cs, lines ~122-132) - the condition is+ // unusual (warns when NOT limited to stores at all, or when limited AND the staff member's+ // store is one of several) and easy to get backwards. Scope.DefaultStoreId is exactly+ // StaffStoreId for Store (StoreAdminDataScope.DefaultStoreId => CurrentCustomer.StaffStoreId).+ protected override void EditWarningCheck(Category category)+ {+ if (!category.LimitedToStores ||+ (category.LimitedToStores &&+ category.Stores.Contains(Scope.DefaultStoreId) &&+ category.Stores.Count > 1))+ Warning(TranslationService.GetResource("Admin.Catalog.Categories.Permissions"));+ }+}