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
Add new address settings DisallowUsersToChangeEmail
KrzysztofPajak125c26745d67
diff --git a/src/Core/Grand.Domain/Common/AddressSettings.cs b/src/Core/Grand.Domain/Common/AddressSettings.csindex 68a7870c9..d6a6a22b2 100644--- a/src/Core/Grand.Domain/Common/AddressSettings.cs+++ b/src/Core/Grand.Domain/Common/AddressSettings.cs@@ -95,5 +95,9 @@ namespace Grand.Domain.Common/// Gets or sets a value indicating whether 'AddressType' is enabled/// </summary>public bool AddressTypeEnabled { get; set; }+ /// <summary>+ /// Gets or sets a value indicating whether users are allowed to change their email on the address+ /// </summary>+ public bool DisallowUsersToChangeEmail { get; set; }}}\ No newline at end of filediff --git a/src/Web/Grand.Web.Admin/Areas/Admin/Views/Setting/Customer.TabAddressFormFields.cshtml b/src/Web/Grand.Web.Admin/Areas/Admin/Views/Setting/Customer.TabAddressFormFields.cshtmlindex 18ea7e409..694a0acdc 100644--- a/src/Web/Grand.Web.Admin/Areas/Admin/Views/Setting/Customer.TabAddressFormFields.cshtml+++ b/src/Web/Grand.Web.Admin/Areas/Admin/Views/Setting/Customer.TabAddressFormFields.cshtml@@ -328,5 +328,17 @@<span asp-validation-for="AddressSettings.NoteEnabled"></span></div></div>+ <div class="form-group">+ <div class="col-8 col-md-4 col-sm-4 text-right">+ <admin-label asp-for="AddressSettings.DisallowUsersToChangeEmail" class="control-label" />+ </div>+ <div class="col-4 col-md-8 col-sm-8">+ <label class="mt-checkbox mt-checkbox-outline control control-checkbox">+ <admin-input asp-for="AddressSettings.DisallowUsersToChangeEmail" />+ <div class="control__indicator"></div>+ </label>+ <span asp-validation-for="AddressSettings.DisallowUsersToChangeEmail"></span>+ </div>+ </div></div></div>diff --git a/src/Web/Grand.Web.Admin/Models/Settings/CustomerSettingsModel.cs b/src/Web/Grand.Web.Admin/Models/Settings/CustomerSettingsModel.csindex bbaeffc19..62664735f 100644--- a/src/Web/Grand.Web.Admin/Models/Settings/CustomerSettingsModel.cs+++ b/src/Web/Grand.Web.Admin/Models/Settings/CustomerSettingsModel.cs@@ -245,6 +245,9 @@ namespace Grand.Web.Admin.Models.Settings[GrandResourceDisplayName("Admin.Settings.Customer.AddressFormFields.NoteEnabled")]public bool NoteEnabled { get; set; }++ [GrandResourceDisplayName("Admin.Settings.Customer.AddressFormFields.DisallowUsersToChangeEmail")]+ public bool DisallowUsersToChangeEmail { get; set; }}diff --git a/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml b/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xmlindex 2ee6171d2..3b915f753 100644--- a/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml+++ b/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml@@ -10903,6 +10903,9 @@<Resource Name="Admin.Settings.Customer.AddressFormFields.Description"><Value>You can create and manage the address form fields available during checkout.</Value></Resource>+ <Resource Name="Admin.Settings.Customer.AddressFormFields.DisallowUsersToChangeEmail">+ <Value>Disallow user change email on the address fields</Value>+ </Resource><Resource Name="Admin.Settings.Customer.AddressFormFields.FaxEnabled"><Value>'Fax number' enabled</Value></Resource>diff --git a/src/Web/Grand.Web/App_Data/Resources/Upgrade/en_110.xml b/src/Web/Grand.Web/App_Data/Resources/Upgrade/en_110.xmlindex 2d6da1bbf..cb8b18e20 100644--- a/src/Web/Grand.Web/App_Data/Resources/Upgrade/en_110.xml+++ b/src/Web/Grand.Web/App_Data/Resources/Upgrade/en_110.xml@@ -297,4 +297,7 @@<Resource Name="Admin.Configuration.RobotsTxt.Fields.Text.Required"><Value>Field content is required</Value></Resource>+ <Resource Name="Admin.Settings.Customer.AddressFormFields.DisallowUsersToChangeEmail">+ <Value>Disallow user change email on the address fields</Value>+ </Resource></Language>\ No newline at end of filediff --git a/src/Web/Grand.Web/Controllers/AccountController.cs b/src/Web/Grand.Web/Controllers/AccountController.csindex b61378171..0617a3c08 100644--- a/src/Web/Grand.Web/Controllers/AccountController.cs+++ b/src/Web/Grand.Web/Controllers/AccountController.cs@@ -764,6 +764,8 @@ namespace Grand.Web.ControllersModel = null,Address = null,ExcludeProperties = false,+ PrePopulateWithCustomerFields = true,+ Customer = _workContext.CurrentCustomer,LoadCountries = () => countries})};@@ -774,6 +776,7 @@ namespace Grand.Web.Controllers[HttpPost][AutoValidateAntiforgeryToken]public virtual async Task<IActionResult> AddressAdd(CustomerAddressEditModel model, IFormCollection form,+ [FromServices] AddressSettings addressSettings,[FromServices] IAddressAttributeParser addressAttributeParser){if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))@@ -791,7 +794,7 @@ namespace Grand.Web.Controllersif (ModelState.IsValid && ModelState.ErrorCount == 0){- var address = model.Address.ToEntity();+ var address = model.Address.ToEntity(_workContext.CurrentCustomer, addressSettings);address.Attributes = customAttributes;address.CreatedOnUtc = DateTime.UtcNow;customer.Addresses.Add(address);@@ -808,6 +811,7 @@ namespace Grand.Web.ControllersModel = model.Address,Address = null,ExcludeProperties = true,+ Customer = _workContext.CurrentCustomer,LoadCountries = () => countries});@@ -834,6 +838,7 @@ namespace Grand.Web.ControllersModel = model.Address,Address = address,ExcludeProperties = false,+ Customer = _workContext.CurrentCustomer,LoadCountries = () => countries});@@ -843,6 +848,7 @@ namespace Grand.Web.Controllers[HttpPost][AutoValidateAntiforgeryToken]public virtual async Task<IActionResult> AddressEdit(CustomerAddressEditModel model, string addressId, IFormCollection form,+ [FromServices] AddressSettings addressSettings,[FromServices] IAddressAttributeParser addressAttributeParser){if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))@@ -865,8 +871,8 @@ namespace Grand.Web.Controllersif (ModelState.IsValid && ModelState.ErrorCount == 0){- address = model.Address.ToEntity(address);- address.Attributes = customAttributes;+ address = model.Address.ToEntity(address, _workContext.CurrentCustomer, addressSettings);+ address.Attributes = customAttributes;await _customerService.UpdateAddress(address, customer.Id);if (customer.BillingAddress?.Id == address.Id)@@ -884,6 +890,7 @@ namespace Grand.Web.ControllersModel = model.Address,Address = address,ExcludeProperties = true,+ Customer = _workContext.CurrentCustomer,LoadCountries = () => countries});diff --git a/src/Web/Grand.Web/Controllers/CheckoutController.cs b/src/Web/Grand.Web/Controllers/CheckoutController.csindex 44b6b6a9b..abc997062 100644--- a/src/Web/Grand.Web/Controllers/CheckoutController.cs+++ b/src/Web/Grand.Web/Controllers/CheckoutController.cs@@ -487,7 +487,7 @@ namespace Grand.Web.Controllersreturn View(model);}- public virtual async Task<IActionResult> SaveBilling(IFormCollection form)+ public virtual async Task<IActionResult> SaveBilling([FromServices] AddressSettings addressSettings, IFormCollection form){try{@@ -558,7 +558,11 @@ namespace Grand.Web.Controllersif (address == null){//address is not found. create a new one- address = model.NewAddress.ToEntity();+ address =+ await _groupService.IsGuest(_workContext.CurrentCustomer) ?+ model.NewAddress.ToEntity() :+ model.NewAddress.ToEntity(_workContext.CurrentCustomer, addressSettings);+address.Attributes = customAttributes;address.CreatedOnUtc = DateTime.UtcNow;address.AddressType = _addressSettings.AddressTypeEnabled ? AddressType.Billing : AddressType.Any;@@ -589,7 +593,8 @@ namespace Grand.Web.Controllers}}- public virtual async Task<IActionResult> SaveShipping(CheckoutShippingAddressModel model, IFormCollection form)+ public virtual async Task<IActionResult> SaveShipping([FromServices] AddressSettings addressSettings,+ CheckoutShippingAddressModel model, IFormCollection form){try{@@ -711,7 +716,11 @@ namespace Grand.Web.Controllersmodel.NewAddress.CountryId);if (address == null){- address = model.NewAddress.ToEntity();+ address =+ await _groupService.IsGuest(_workContext.CurrentCustomer) ?+ model.NewAddress.ToEntity() :+ model.NewAddress.ToEntity(_workContext.CurrentCustomer, addressSettings);+address.Attributes = customAttributes;address.CreatedOnUtc = DateTime.UtcNow;address.AddressType = _addressSettings.AddressTypeEnabled ? (model.BillToTheSameAddress ? AddressType.Any : AddressType.Shipping) : AddressType.Any;diff --git a/src/Web/Grand.Web/Controllers/MerchandiseReturnController.cs b/src/Web/Grand.Web/Controllers/MerchandiseReturnController.csindex a9b65b017..303bb1b8d 100644--- a/src/Web/Grand.Web/Controllers/MerchandiseReturnController.cs+++ b/src/Web/Grand.Web/Controllers/MerchandiseReturnController.cs@@ -32,7 +32,7 @@ namespace Grand.Web.Controllersprivate readonly IGroupService _groupService;private readonly ITranslationService _translationService;private readonly IMediator _mediator;-+ private readonly AddressSettings _addressSettings;private readonly OrderSettings _orderSettings;#endregion@@ -45,6 +45,7 @@ namespace Grand.Web.ControllersIGroupService groupService,ITranslationService translationService,IMediator mediator,+ AddressSettings addressSettings,OrderSettings orderSettings){_merchandiseReturnService = merchandiseReturnService;@@ -53,6 +54,7 @@ namespace Grand.Web.Controllers_groupService = groupService;_translationService = translationService;_mediator = mediator;+ _addressSettings = addressSettings;_orderSettings = orderSettings;}@@ -97,7 +99,7 @@ namespace Grand.Web.ControllersModelState.AddModelError("", error);}await TryUpdateModelAsync(model.NewAddress, "MerchandiseReturnNewAddress");- address = model.NewAddress.ToEntity();+ address = model.NewAddress.ToEntity(_workContext.CurrentCustomer, _addressSettings);model.NewAddressPreselected = true;address.Attributes = customAttributes;address.CreatedOnUtc = DateTime.UtcNow;diff --git a/src/Web/Grand.Web/Extensions/MappingExtensions.cs b/src/Web/Grand.Web/Extensions/MappingExtensions.csindex 65071ada9..fe79cad1a 100644--- a/src/Web/Grand.Web/Extensions/MappingExtensions.cs+++ b/src/Web/Grand.Web/Extensions/MappingExtensions.cs@@ -3,6 +3,7 @@ using Grand.Business.Common.Interfaces.Directory;using Grand.Domain.Catalog;using Grand.Domain.Common;using Grand.Domain.Courses;+using Grand.Domain.Customers;using Grand.Domain.Localization;using Grand.Domain.Pages;using Grand.Web.Models.Catalog;@@ -24,8 +25,7 @@ namespace Grand.Web.Extensionsif (entity == null)return null;- var model = new CategoryModel- {+ var model = new CategoryModel {Id = entity.Id,ParentCategoryId = entity.ParentCategoryId,Name = entity.GetTranslation(x => x.Name, language.Id),@@ -48,8 +48,7 @@ namespace Grand.Web.Extensionsif (entity == null)return null;- var model = new BrandModel- {+ var model = new BrandModel {Id = entity.Id,Name = entity.GetTranslation(x => x.Name, language.Id),Description = entity.GetTranslation(x => x.Description, language.Id),@@ -69,8 +68,7 @@ namespace Grand.Web.Extensionsif (entity == null)return null;- var model = new CollectionModel- {+ var model = new CollectionModel {Id = entity.Id,Name = entity.GetTranslation(x => x.Name, language.Id),Description = entity.GetTranslation(x => x.Description, language.Id),@@ -91,8 +89,7 @@ namespace Grand.Web.Extensionsif (entity == null)return null;- var model = new CourseModel- {+ var model = new CourseModel {Id = entity.Id,Name = entity.GetTranslation(x => x.Name, language.Id),Description = entity.GetTranslation(x => x.Description, language.Id),@@ -110,8 +107,7 @@ namespace Grand.Web.Extensions//pagepublic static PageModel ToModel(this Page entity, Language language, IDateTimeService dateTimeService, string password = ""){- var model = new PageModel- {+ var model = new PageModel {Id = entity.Id,SystemName = entity.SystemName,IncludeInSitemap = entity.IncludeInSitemap,@@ -132,6 +128,20 @@ namespace Grand.Web.Extensions}+ public static Address ToEntity(this AddressModel model, Customer customer, AddressSettings addressSettings, bool trimFields = true)+ {+ if (model == null)+ return null;++ var entity = new Address();+ entity = ToEntity(model, entity, trimFields);+ if (addressSettings.DisallowUsersToChangeEmail)+ {+ entity.Email = customer.Email;+ }+ return entity;+ }+public static Address ToEntity(this AddressModel model, bool trimFields = true){if (model == null)@@ -141,6 +151,16 @@ namespace Grand.Web.Extensionsreturn ToEntity(model, entity, trimFields);}+ public static Address ToEntity(this AddressModel model, Address destination, Customer customer, AddressSettings addressSettings, bool trimFields = true)+ {+ var entity = ToEntity(model, destination, trimFields);+ if (addressSettings.DisallowUsersToChangeEmail)+ {+ entity.Email = customer.Email;+ }+ return entity;+ }+public static Address ToEntity(this AddressModel model, Address destination, bool trimFields = true){if (model == null)diff --git a/src/Web/Grand.Web/Features/Handlers/Common/GetAddressModelHandler.cs b/src/Web/Grand.Web/Features/Handlers/Common/GetAddressModelHandler.csindex 1b95fdb52..98a37a297 100644--- a/src/Web/Grand.Web/Features/Handlers/Common/GetAddressModelHandler.cs+++ b/src/Web/Grand.Web/Features/Handlers/Common/GetAddressModelHandler.cs@@ -26,7 +26,7 @@ namespace Grand.Web.Features.Handlers.Commonprivate readonly ITranslationService _translationService;private readonly IAddressAttributeService _addressAttributeService;private readonly IAddressAttributeParser _addressAttributeParser;-+ private readonly IGroupService _groupService;private readonly AddressSettings _addressSettings;public GetAddressModelHandler(@@ -34,12 +34,14 @@ namespace Grand.Web.Features.Handlers.CommonITranslationService translationService,IAddressAttributeService addressAttributeService,IAddressAttributeParser addressAttributeParser,+ IGroupService groupService,AddressSettings addressSettings){_countryService = countryService;_translationService = translationService;_addressAttributeService = addressAttributeService;_addressAttributeParser = addressAttributeParser;+ _groupService = groupService;_addressSettings = addressSettings;}@@ -171,6 +173,10 @@ namespace Grand.Web.Features.Handlers.Commonmodel.FaxEnabled = _addressSettings.FaxEnabled;model.FaxRequired = _addressSettings.FaxRequired;model.NoteEnabled = _addressSettings.NoteEnabled;+ if (customer != null && !await _groupService.IsGuest(customer))+ {+ model.DisallowUsersToChangeEmail = _addressSettings.DisallowUsersToChangeEmail;+ }model.AddressTypeEnabled = _addressSettings.AddressTypeEnabled;}diff --git a/src/Web/Grand.Web/Models/Common/AddressModel.cs b/src/Web/Grand.Web/Models/Common/AddressModel.csindex ab15a6320..f233a2088 100644--- a/src/Web/Grand.Web/Models/Common/AddressModel.cs+++ b/src/Web/Grand.Web/Models/Common/AddressModel.cs@@ -94,5 +94,7 @@ namespace Grand.Web.Models.Commonpublic int AddressTypeId { get; set; }public bool HideAddressType { get; set; }++ public bool DisallowUsersToChangeEmail { get; set; }}}\ No newline at end of filediff --git a/src/Web/Grand.Web/Views/Shared/_CreateOrUpdateAddress.cshtml b/src/Web/Grand.Web/Views/Shared/_CreateOrUpdateAddress.cshtmlindex bf277eadb..567c767c2 100644--- a/src/Web/Grand.Web/Views/Shared/_CreateOrUpdateAddress.cshtml+++ b/src/Web/Grand.Web/Views/Shared/_CreateOrUpdateAddress.cshtml@@ -64,7 +64,7 @@<validation-provider tag="div" rules="required|email" :name="'@prefix' + '.Email'" v-slot="{ errors, classes }"><label asp-for="Email" class="col-form-label">@Loc["Address.Fields.Email"]:</label><span class="required">*</span>- <input asp-for="Email" v-model="createorupdateaddress@{@prefix}.Email" v-bind:class="[classes , 'form-control']" />+ <input asp-for="Email" v-model="createorupdateaddress@{@prefix}.Email" v-bind:class="[classes , 'form-control']" asp-readonly=@(Model.DisallowUsersToChangeEmail)/><span class="field-validation-error">{{ errors[0] }}</span><span asp-validation-for="Email"></span></validation-provider>