Skip to content

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.

Admin panel - ApiUserController.cs - changes with validators

support0c3468c6bbf5
diff --git a/src/Web/Grand.Web.Admin/Controllers/ApiUserController.cs b/src/Web/Grand.Web.Admin/Controllers/ApiUserController.cs
index 43f92a7d1..dc77e8115 100644
--- a/src/Web/Grand.Web.Admin/Controllers/ApiUserController.cs
+++ b/src/Web/Grand.Web.Admin/Controllers/ApiUserController.cs
@@ -47,10 +47,6 @@ namespace Grand.Web.Admin.Controllers
[HttpPost]
public async Task<IActionResult> Update(UserApiModel model)
{
- if (!ModelState.IsValid)
- {
- return Json(new DataSourceResult { Errors = ModelState.SerializeErrors() });
- }
var userapi = await _userApiService.GetUserById(model.Id);
if (userapi == null)
throw new ArgumentException("No user api found with the specified id");
@@ -71,15 +67,8 @@ namespace Grand.Web.Admin.Controllers
}
[HttpPost]
- public async Task<IActionResult> Insert(UserApiModel model)
+ public async Task<IActionResult> Insert(UserApiCreateModel model)
{
- if (string.IsNullOrEmpty(model.Password))
- ModelState.AddModelError("", "Password is required");
-
- if (!ModelState.IsValid)
- {
- return Json(new DataSourceResult { Errors = ModelState.SerializeErrors() });
- }
if (ModelState.IsValid)
{
var userapi = model.ToEntity();
diff --git a/src/Web/Grand.Web.Admin/Extensions/Mapping/UserApiMappingExtensions.cs b/src/Web/Grand.Web.Admin/Extensions/Mapping/UserApiMappingExtensions.cs
index 87fd7b940..b8d9ff7fb 100644
--- a/src/Web/Grand.Web.Admin/Extensions/Mapping/UserApiMappingExtensions.cs
+++ b/src/Web/Grand.Web.Admin/Extensions/Mapping/UserApiMappingExtensions.cs
@@ -11,14 +11,15 @@ namespace Grand.Web.Admin.Extensions.Mapping
return entity.MapTo<UserApi, UserApiModel>();
}
- public static UserApi ToEntity(this UserApiModel model)
+ public static UserApi ToEntity(this UserApiCreateModel model)
{
- return model.MapTo<UserApiModel, UserApi>();
+ return model.MapTo<UserApiCreateModel, UserApi>();
}
public static UserApi ToEntity(this UserApiModel model, UserApi destination)
{
return model.MapTo(destination);
}
+
}
}
\ No newline at end of file
diff --git a/src/Web/Grand.Web.Admin/Mapper/UserApiProfile.cs b/src/Web/Grand.Web.Admin/Mapper/UserApiProfile.cs
index b679aa077..c380fcab4 100644
--- a/src/Web/Grand.Web.Admin/Mapper/UserApiProfile.cs
+++ b/src/Web/Grand.Web.Admin/Mapper/UserApiProfile.cs
@@ -14,6 +14,9 @@ namespace Grand.Web.Admin.Mapper
CreateMap<UserApiModel, UserApi>()
.ForMember(dest => dest.Password, mo => mo.Ignore())
.ForMember(dest => dest.Id, mo => mo.Ignore());
+
+ CreateMap<UserApiCreateModel, UserApi>();
+
}
public int Order => 0;
diff --git a/src/Web/Grand.Web.Admin/Models/Customers/UserApiCreateModel.cs b/src/Web/Grand.Web.Admin/Models/Customers/UserApiCreateModel.cs
new file mode 100644
index 000000000..3a8a3c941
--- /dev/null
+++ b/src/Web/Grand.Web.Admin/Models/Customers/UserApiCreateModel.cs
@@ -0,0 +1,18 @@
+using Grand.Infrastructure.ModelBinding;
+using Grand.Infrastructure.Models;
+
+namespace Grand.Web.Admin.Models.Customers
+{
+ public class UserApiCreateModel : BaseEntityModel
+ {
+ [GrandResourceDisplayName("Admin.System.UserApi.Email")]
+ public string Email { get; set; }
+
+ [GrandResourceDisplayName("Admin.System.UserApi.Password")]
+ public string Password { get; set; }
+
+ [GrandResourceDisplayName("Admin.System.UserApi.IsActive")]
+ public bool IsActive { get; set; }
+
+ }
+}
diff --git a/src/Web/Grand.Web.Admin/Validators/Customers/UserApiCreateValidator.cs b/src/Web/Grand.Web.Admin/Validators/Customers/UserApiCreateValidator.cs
new file mode 100644
index 000000000..8e5af7f70
--- /dev/null
+++ b/src/Web/Grand.Web.Admin/Validators/Customers/UserApiCreateValidator.cs
@@ -0,0 +1,30 @@
+using FluentValidation;
+using Grand.Infrastructure.Validators;
+using Grand.Business.Core.Interfaces.Customers;
+using Grand.Business.Core.Interfaces.Common.Localization;
+using Grand.Web.Admin.Models.Customers;
+
+namespace Grand.Web.Admin.Validators.Customers
+{
+ public class UserApiCreateValidator : BaseGrandValidator<UserApiCreateModel>
+ {
+ public UserApiCreateValidator(
+ IEnumerable<IValidatorConsumer<UserApiCreateModel>> validators,
+ ITranslationService translationService, ICustomerService customerService)
+ : base(validators)
+ {
+ RuleFor(x => x.Password).NotEmpty().WithMessage(translationService.GetResource("Admin.System.UserApi.Password.Required"));
+ RuleFor(x => x.Email).NotEmpty().WithMessage(translationService.GetResource("Admin.System.UserApi.Email.Required"));
+ RuleFor(x => x).MustAsync(async (x, _, _) =>
+ {
+ if (!string.IsNullOrEmpty(x.Email))
+ {
+ var customer = await customerService.GetCustomerByEmail(x.Email.ToLowerInvariant());
+ if (customer is { Active: true, IsSystemAccount: false })
+ return true;
+ }
+ return false;
+ }).WithMessage(translationService.GetResource("Admin.System.UserApi.Email.CustomerNotExist"));
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml b/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml
index e5d5c2269..27740a118 100644
--- a/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml
+++ b/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml
@@ -11922,6 +11922,9 @@
<Resource Name="Admin.System.UserApi.Password" Area="Admin">
<Value>Password</Value>
</Resource>
+ <Resource Name="Admin.System.UserApi.Password.Required" Area="Admin">
+ <Value>Password is required</Value>
+ </Resource>
<Resource Name="Admin.System.Warnings" Area="Admin">
<Value>Warnings</Value>
</Resource>