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/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/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() { 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; }