From 33b6c721aab4b8e7b251b2608009828c94fafc21 Mon Sep 17 00:00:00 2001 From: support Date: Sun, 22 Nov 2026 19:27:05 +0100 Subject: [PATCH 1/2] Keep the system flag of a customer group out of the edit form The form posted IsSystem back through a hidden input and the profile mapped it onto the group. Posting false took the protection off a system group, after which its system name could be changed, the group deactivated or deleted; posting true made a new group undeletable. IsSystem is now never bound nor mapped; the installer seeds it. --- .../AdminShared/CustomerMappingTests.cs | 19 +++++++++++++++++++ .../Partials/CreateOrUpdate.TabInfo.cshtml | 4 +--- .../Controllers/CustomerGroupController.cs | 1 + .../Mapper/CustomerGroupProfile.cs | 3 ++- .../Models/Customers/CustomerGroupModel.cs | 6 ++++++ 5 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/Tests/Grand.Mapping.Tests/AdminShared/CustomerMappingTests.cs b/src/Tests/Grand.Mapping.Tests/AdminShared/CustomerMappingTests.cs index b35d2e78f..1610e6776 100644 --- a/src/Tests/Grand.Mapping.Tests/AdminShared/CustomerMappingTests.cs +++ b/src/Tests/Grand.Mapping.Tests/AdminShared/CustomerMappingTests.cs @@ -66,6 +66,25 @@ public Task CustomerGroupModel_ToDomain() return Verify(_mapper.Map(model)); } + [TestMethod] + public void CustomerGroupModel_ToDomain_KeepsStoredIsSystem() + { + var group = new CustomerGroup { Id = "cg-001", Name = "Administrators", SystemName = "Administrators", IsSystem = true }; + var model = new CustomerGroupModel { Id = "cg-001", Name = "Administrators", SystemName = "Administrators", IsSystem = false }; + + _mapper.Map(model, group); + + Assert.IsTrue(group.IsSystem); + } + + [TestMethod] + public void CustomerGroupModel_ToDomain_NewGroupIsNotSystem() + { + var model = new CustomerGroupModel { Name = "VIP Customers", SystemName = "vip", IsSystem = true }; + + Assert.IsFalse(_mapper.Map(model).IsSystem); + } + // ── CustomerAttribute ───────────────────────────────────────────────────── [TestMethod] diff --git a/src/Web/Grand.Web.Admin/Areas/Admin/Views/CustomerGroup/Partials/CreateOrUpdate.TabInfo.cshtml b/src/Web/Grand.Web.Admin/Areas/Admin/Views/CustomerGroup/Partials/CreateOrUpdate.TabInfo.cshtml index 927ee46ab..697a510e1 100644 --- a/src/Web/Grand.Web.Admin/Areas/Admin/Views/CustomerGroup/Partials/CreateOrUpdate.TabInfo.cshtml +++ b/src/Web/Grand.Web.Admin/Areas/Admin/Views/CustomerGroup/Partials/CreateOrUpdate.TabInfo.cshtml @@ -1,6 +1,5 @@ @model CustomerGroupModel -@* one card of two-column fields; IsSystem is not editable, so it reads as a fact and posts back - through its hidden input *@ +@* one card of two-column fields; IsSystem is not editable, so it reads as a fact and is never posted *@
@@ -18,5 +17,4 @@
@Model.IsSystem.ToString()
-
diff --git a/src/Web/Grand.Web.Admin/Controllers/CustomerGroupController.cs b/src/Web/Grand.Web.Admin/Controllers/CustomerGroupController.cs index b80f51ee6..b767fb30e 100644 --- a/src/Web/Grand.Web.Admin/Controllers/CustomerGroupController.cs +++ b/src/Web/Grand.Web.Admin/Controllers/CustomerGroupController.cs @@ -137,6 +137,7 @@ public async Task Edit(CustomerGroupModel model, bool continueEdi } //If we got this far, something failed, redisplay form + model.IsSystem = customerGroup.IsSystem; return View(model); } catch (Exception exc) diff --git a/src/Web/Grand.Web.AdminShared/Mapper/CustomerGroupProfile.cs b/src/Web/Grand.Web.AdminShared/Mapper/CustomerGroupProfile.cs index 8ee19541a..c17cf5170 100644 --- a/src/Web/Grand.Web.AdminShared/Mapper/CustomerGroupProfile.cs +++ b/src/Web/Grand.Web.AdminShared/Mapper/CustomerGroupProfile.cs @@ -11,7 +11,8 @@ public CustomerGroupProfile() { CreateMap(); CreateMap() - .ForMember(dest => dest.Id, mo => mo.Ignore()); + .ForMember(dest => dest.Id, mo => mo.Ignore()) + .ForMember(dest => dest.IsSystem, mo => mo.Ignore()); } public int Order => 0; diff --git a/src/Web/Grand.Web.AdminShared/Models/Customers/CustomerGroupModel.cs b/src/Web/Grand.Web.AdminShared/Models/Customers/CustomerGroupModel.cs index 86d716dbc..82e5eae42 100644 --- a/src/Web/Grand.Web.AdminShared/Models/Customers/CustomerGroupModel.cs +++ b/src/Web/Grand.Web.AdminShared/Models/Customers/CustomerGroupModel.cs @@ -1,5 +1,6 @@ using Grand.Infrastructure.ModelBinding; using Grand.Infrastructure.Models; +using Microsoft.AspNetCore.Mvc.ModelBinding; using System.ComponentModel.DataAnnotations; namespace Grand.Web.AdminShared.Models.Customers; @@ -20,6 +21,11 @@ public class CustomerGroupModel : BaseEntityModel [GrandResourceDisplayName("Admin.Customers.CustomerGroups.Fields.Active")] public bool Active { get; set; } + /// + /// Shown, never edited: the system groups are seeded by the installer. Never bound, so a form + /// cannot take the protection off a system group or give it to a new one. + /// + [BindNever] [GrandResourceDisplayName("Admin.Customers.CustomerGroups.Fields.IsSystem")] public bool IsSystem { get; set; } From 8f8040e7d9ced19ce9b97015b1304ba6a6938557 Mon Sep 17 00:00:00 2001 From: support Date: Sun, 22 Nov 2026 20:22:18 +0100 Subject: [PATCH 2/2] Keep the system flag of a customer group out of the API too The API profile mapped IsSystem from the request onto the stored group, and Patch checked the flag after applying the patch, so the request could clear it. IsSystem is now ignored when mapping a DTO onto a group, and Patch refuses a system group before applying the patch. --- .../Controllers/CustomerGroupController.cs | 11 ++++------- .../Mapper/Profiles/CustomerGroupProfile.cs | 3 ++- .../Api/ApiMappingTests.cs | 19 +++++++++++++++++++ 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/Modules/Grand.Module.Api/Controllers/CustomerGroupController.cs b/src/Modules/Grand.Module.Api/Controllers/CustomerGroupController.cs index 64862fc8c..da5cf06a7 100644 --- a/src/Modules/Grand.Module.Api/Controllers/CustomerGroupController.cs +++ b/src/Modules/Grand.Module.Api/Controllers/CustomerGroupController.cs @@ -104,14 +104,11 @@ public async Task Patch([FromRoute] string key, [FromBody] JsonPa if (!customerGroup.Any()) return NotFound(); var cr = customerGroup.FirstOrDefault(); - model.ApplyTo(cr); - if (cr is { IsSystem: false }) - { - await _mediator.Send(new UpdateCustomerGroupCommand { Model = cr }); - return Ok(); - } + if (cr!.IsSystem) return BadRequest(ModelState); - return BadRequest(ModelState); + model.ApplyTo(cr); + await _mediator.Send(new UpdateCustomerGroupCommand { Model = cr }); + return Ok(); } [EndpointDescription("Delete entity in CustomerGroup")] diff --git a/src/Modules/Grand.Module.Api/Infrastructure/Mapper/Profiles/CustomerGroupProfile.cs b/src/Modules/Grand.Module.Api/Infrastructure/Mapper/Profiles/CustomerGroupProfile.cs index b5bc43bf0..45e2cb8b9 100644 --- a/src/Modules/Grand.Module.Api/Infrastructure/Mapper/Profiles/CustomerGroupProfile.cs +++ b/src/Modules/Grand.Module.Api/Infrastructure/Mapper/Profiles/CustomerGroupProfile.cs @@ -10,7 +10,8 @@ public class CustomerGroupProfile : Profile, IAutoMapperProfile public CustomerGroupProfile() { CreateMap() - .ForMember(dest => dest.UserFields, mo => mo.Ignore()); + .ForMember(dest => dest.UserFields, mo => mo.Ignore()) + .ForMember(dest => dest.IsSystem, mo => mo.Ignore()); CreateMap(); } diff --git a/src/Tests/Grand.Mapping.Tests/Api/ApiMappingTests.cs b/src/Tests/Grand.Mapping.Tests/Api/ApiMappingTests.cs index 4ad226175..6661c40a2 100644 --- a/src/Tests/Grand.Mapping.Tests/Api/ApiMappingTests.cs +++ b/src/Tests/Grand.Mapping.Tests/Api/ApiMappingTests.cs @@ -200,6 +200,25 @@ public Task CustomerGroupDto_ToEntity() return Verify(_mapper.Map(dto)); } + [TestMethod] + public void CustomerGroupDto_ToEntity_KeepsStoredIsSystem() + { + var group = new CustomerGroup { Id = "cg-1", Name = "Administrators", SystemName = "Administrators", IsSystem = true }; + var dto = new CustomerGroupDto { Id = "cg-1", Name = "Administrators", SystemName = "Administrators", IsSystem = false }; + + _mapper.Map(dto, group); + + Assert.IsTrue(group.IsSystem); + } + + [TestMethod] + public void CustomerGroupDto_ToEntity_NewGroupIsNotSystem() + { + var dto = new CustomerGroupDto { Name = "VIP Customers", SystemName = "Vip", IsSystem = true }; + + Assert.IsFalse(_mapper.Map(dto).IsSystem); + } + [TestMethod] public Task CustomerGroup_ToDto() {