Skip to content

Commit 545b456

Browse files
committed
Resolves smartstore#1636 Validate the username when creating a customer in backend
1 parent 9b22cbf commit 545b456

1 file changed

Lines changed: 17 additions & 11 deletions

File tree

src/Presentation/SmartStore.Web/Administration/Controllers/CustomerController.cs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -558,19 +558,21 @@ public ActionResult Create()
558558
public ActionResult Create(CustomerModel model, bool continueEditing, FormCollection form)
559559
{
560560
if (!_permissionService.Authorize(StandardPermissionProvider.ManageCustomers))
561+
{
561562
return AccessDeniedView();
563+
}
562564

563-
if (model.Email.HasValue())
565+
/// Validate unique user. <see cref="ICustomerRegistrationService.RegisterCustomer(CustomerRegistrationRequest)"/>
566+
if (model.Email.HasValue() && _customerService.GetCustomerByEmail(model.Email) != null)
564567
{
565-
var tempCust = _customerService.GetCustomerByEmail(model.Email);
566-
if (tempCust != null)
567-
ModelState.AddModelError("", "Email is already registered");
568+
ModelState.AddModelError("", T("Account.Register.Errors.EmailAlreadyExists"));
568569
}
569-
if (model.Username.HasValue() && _customerSettings.CustomerLoginType != CustomerLoginType.Email)
570+
571+
if (model.Username.HasValue() &&
572+
_customerSettings.CustomerLoginType != CustomerLoginType.Email &&
573+
_customerService.GetCustomerByUsername(model.Username) != null)
570574
{
571-
var tempCust = _customerService.GetCustomerByEmail(model.Username);
572-
if (tempCust != null)
573-
ModelState.AddModelError("", "Username is already registered");
575+
ModelState.AddModelError("", T("Account.Register.Errors.UsernameAlreadyExists"));
574576
}
575577

576578
// Validate customer roles.
@@ -581,15 +583,19 @@ public ActionResult Create(CustomerModel model, bool continueEditing, FormCollec
581583
{
582584
foreach (var customerRole in allCustomerRoles)
583585
{
584-
if (model.SelectedCustomerRoleIds.Contains(customerRole.Id))
585-
newCustomerRoles.Add(customerRole);
586+
if (model.SelectedCustomerRoleIds.Contains(customerRole.Id))
587+
{
588+
newCustomerRoles.Add(customerRole);
589+
}
586590
}
587591
}
588592

589593
var customerRolesError = ValidateCustomerRoles(newCustomerRoles);
590594

591595
if (customerRolesError.HasValue())
596+
{
592597
ModelState.AddModelError("", customerRolesError);
598+
}
593599

594600
var allowManagingCustomerRoles = _permissionService.Authorize(StandardPermissionProvider.ManageCustomerRoles);
595601

@@ -660,7 +666,7 @@ public ActionResult Create(CustomerModel model, bool continueEditing, FormCollec
660666
_genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.Fax, model.Fax);
661667

662668
// Password.
663-
if (!String.IsNullOrWhiteSpace(model.Password))
669+
if (model.Password.HasValue())
664670
{
665671
var changePassRequest = new ChangePasswordRequest(model.Email, false, _customerSettings.DefaultPasswordFormat, model.Password);
666672
var changePassResult = _customerRegistrationService.ChangePassword(changePassRequest);

0 commit comments

Comments
 (0)