public virtual CustomerRegistrationResult RegisterCustomer(CustomerRegistrationRequest request) { Guard.NotNull(request, nameof(request)); Guard.NotNull(request.Customer, nameof(request.Customer)); var result = new CustomerRegistrationResult(); if (request.Customer.IsSearchEngineAccount()) { result.AddError(T("Account.Register.Errors.CannotRegisterSearchEngine")); return(result); } if (request.Customer.IsBackgroundTaskAccount()) { result.AddError(T("Account.Register.Errors.CannotRegisterTaskAccount")); return(result); } if (request.Customer.IsRegistered()) { result.AddError(T("Account.Register.Errors.AlreadyRegistered")); return(result); } if (String.IsNullOrEmpty(request.Email)) { result.AddError(T("Account.Register.Errors.EmailIsNotProvided")); return(result); } if (!request.Email.IsEmail()) { result.AddError(T("Common.WrongEmail")); return(result); } if (String.IsNullOrWhiteSpace(request.Password)) { result.AddError(T("Account.Register.Errors.PasswordIsNotProvided")); return(result); } if (_customerSettings.UsernamesEnabled) { if (String.IsNullOrEmpty(request.Username)) { result.AddError(T("Account.Register.Errors.UsernameIsNotProvided")); return(result); } } // Validate unique user if (_customerService.GetCustomerByEmail(request.Email) != null) { result.AddError(T("Account.Register.Errors.EmailAlreadyExists")); return(result); } if (_customerSettings.UsernamesEnabled) { if (_customerService.GetCustomerByUsername(request.Username) != null) { result.AddError(T("Account.Register.Errors.UsernameAlreadyExists")); return(result); } } // At this point request is valid request.Customer.Username = request.Username; request.Customer.Email = request.Email; request.Customer.PasswordFormat = request.PasswordFormat; switch (request.PasswordFormat) { case PasswordFormat.Clear: request.Customer.Password = request.Password; break; case PasswordFormat.Encrypted: request.Customer.Password = _encryptionService.EncryptText(request.Password); break; case PasswordFormat.Hashed: string saltKey = _encryptionService.CreateSaltKey(5); request.Customer.PasswordSalt = saltKey; request.Customer.Password = _encryptionService.CreatePasswordHash(request.Password, saltKey, _customerSettings.HashedPasswordFormat); break; } request.Customer.Active = request.IsApproved; if (_customerSettings.RegisterCustomerRoleId != 0) { var customerRole = _customerService.GetCustomerRoleById(_customerSettings.RegisterCustomerRoleId); request.Customer.CustomerRoles.Add(customerRole); } // Add to 'Registered' role var registeredRole = _customerService.GetCustomerRoleBySystemName(SystemCustomerRoleNames.Registered); if (registeredRole == null) { throw new SmartException(T("Admin.Customers.CustomerRoles.CannotFoundRole", "Registered")); } request.Customer.CustomerRoles.Add(registeredRole); // Remove from 'Guests' role var guestRole = request.Customer.CustomerRoles.FirstOrDefault(cr => cr.SystemName == SystemCustomerRoleNames.Guests); if (guestRole != null) { request.Customer.CustomerRoles.Remove(guestRole); } // Add reward points for customer registration (if enabled) if (_rewardPointsSettings.Enabled && _rewardPointsSettings.PointsForRegistration > 0) { request.Customer.AddRewardPointsHistoryEntry(_rewardPointsSettings.PointsForRegistration, T("RewardPoints.Message.RegisteredAsCustomer")); } if (request.IsAgent) { var agentRole = _customerService.GetCustomerRoleBySystemName(SystemCustomerRoleNames.Agents); if (agentRole == null) { throw new SmartException(T("Admin.Customers.CustomerRoles.CannotFoundRole", "Agent")); } request.Customer.CustomerRoles.Add(agentRole); } _customerService.UpdateCustomer(request.Customer); _eventPublisher.Publish(new CustomerRegisteredEvent { Customer = request.Customer }); return(result); }
/// <summary> /// Register customer /// </summary> /// <param name="request">Request</param> /// <returns>Result</returns> public virtual CustomerRegistrationResult RegisterCustomer(CustomerRegistrationRequest request) { if (request == null) { throw new ArgumentNullException("request"); } if (request.Customer == null) { throw new ArgumentException("Can't load current customer"); } var result = new CustomerRegistrationResult(); if (request.Customer.IsSearchEngineAccount()) { result.AddError("Search engine can't be registered"); return(result); } if (request.Customer.IsBackgroundTaskAccount()) { result.AddError("Background task account can't be registered"); return(result); } if (request.Customer.IsRegistered()) { result.AddError("Current customer is already registered"); return(result); } if (String.IsNullOrEmpty(request.Email)) { result.AddError(_localizationService.GetResource("Account.Register.Errors.EmailIsNotProvided")); return(result); } if (!request.Email.IsEmail()) { result.AddError(_localizationService.GetResource("Common.WrongEmail")); return(result); } if (String.IsNullOrWhiteSpace(request.Password)) { result.AddError(_localizationService.GetResource("Account.Register.Errors.PasswordIsNotProvided")); return(result); } if (_customerSettings.UsernamesEnabled) { if (String.IsNullOrEmpty(request.Username)) { result.AddError(_localizationService.GetResource("Account.Register.Errors.UsernameIsNotProvided")); return(result); } } //validate unique user if (_customerService.GetCustomerByEmail(request.Email) != null) { result.AddError(_localizationService.GetResource("Account.Register.Errors.EmailAlreadyExists")); return(result); } if (_customerSettings.UsernamesEnabled) { if (_customerService.GetCustomerByUsername(request.Username) != null) { result.AddError(_localizationService.GetResource("Account.Register.Errors.UsernameAlreadyExists")); return(result); } } //at this point request is valid request.Customer.Username = request.Username; request.Customer.Email = request.Email; request.Customer.PasswordFormat = request.PasswordFormat; switch (request.PasswordFormat) { case PasswordFormat.Clear: { request.Customer.Password = request.Password; } break; case PasswordFormat.Encrypted: { request.Customer.Password = _encryptionService.EncryptText(request.Password); } break; case PasswordFormat.Hashed: { string saltKey = _encryptionService.CreateSaltKey(5); request.Customer.PasswordSalt = saltKey; request.Customer.Password = _encryptionService.CreatePasswordHash(request.Password, saltKey, _customerSettings.HashedPasswordFormat); } break; default: break; } request.Customer.Active = request.IsApproved; //add to 'Registered' role var registeredRole = _customerService.GetCustomerRoleBySystemName(SystemCustomerRoleNames.Registered); if (registeredRole == null) { throw new SmartException("'Registered' role could not be loaded"); } request.Customer.CustomerRoles.Add(registeredRole); //remove from 'Guests' role var guestRole = request.Customer.CustomerRoles.FirstOrDefault(cr => cr.SystemName == SystemCustomerRoleNames.Guests); if (guestRole != null) { request.Customer.CustomerRoles.Remove(guestRole); } //Add reward points for customer registration (if enabled) if (_rewardPointsSettings.Enabled && _rewardPointsSettings.PointsForRegistration > 0) { request.Customer.AddRewardPointsHistoryEntry(_rewardPointsSettings.PointsForRegistration, _localizationService.GetResource("RewardPoints.Message.RegisteredAsCustomer")); } _customerService.UpdateCustomer(request.Customer); return(result); }
/// <summary> /// Register customer /// </summary> /// <param name="request">Request</param> /// <returns>Result</returns> public virtual CustomerRegistrationResult RegisterCustomer(CustomerRegistrationRequest request) { if (request == null) throw new ArgumentNullException("request"); if (request.Customer == null) throw new ArgumentException("Can't load current customer"); var result = new CustomerRegistrationResult(); if (request.Customer.IsSearchEngineAccount()) { result.AddError("Search engine can't be registered"); return result; } if (request.Customer.IsBackgroundTaskAccount()) { result.AddError("Background task account can't be registered"); return result; } if (request.Customer.IsRegistered()) { result.AddError("Current customer is already registered"); return result; } if (String.IsNullOrEmpty(request.Email)) { result.AddError(_localizationService.GetResource("Account.Register.Errors.EmailIsNotProvided")); return result; } if (!request.Email.IsEmail()) { result.AddError(_localizationService.GetResource("Common.WrongEmail")); return result; } if (String.IsNullOrWhiteSpace(request.Password)) { result.AddError(_localizationService.GetResource("Account.Register.Errors.PasswordIsNotProvided")); return result; } if (_customerSettings.UsernamesEnabled) { if (String.IsNullOrEmpty(request.Username)) { result.AddError(_localizationService.GetResource("Account.Register.Errors.UsernameIsNotProvided")); return result; } } //validate unique user if (_customerService.GetCustomerByEmail(request.Email) != null) { result.AddError(_localizationService.GetResource("Account.Register.Errors.EmailAlreadyExists")); return result; } if (_customerSettings.UsernamesEnabled) { if (_customerService.GetCustomerByUsername(request.Username) != null) { result.AddError(_localizationService.GetResource("Account.Register.Errors.UsernameAlreadyExists")); return result; } } //at this point request is valid request.Customer.Username = request.Username; request.Customer.Email = request.Email; request.Customer.PasswordFormat = request.PasswordFormat; switch (request.PasswordFormat) { case PasswordFormat.Clear: { request.Customer.Password = request.Password; } break; case PasswordFormat.Encrypted: { request.Customer.Password = _encryptionService.EncryptText(request.Password); } break; case PasswordFormat.Hashed: { string saltKey = _encryptionService.CreateSaltKey(5); request.Customer.PasswordSalt = saltKey; request.Customer.Password = _encryptionService.CreatePasswordHash(request.Password, saltKey, _customerSettings.HashedPasswordFormat); } break; default: break; } request.Customer.Active = request.IsApproved; //add to 'Registered' role var registeredRole = _customerService.GetCustomerRoleBySystemName(SystemCustomerRoleNames.Registered); if (registeredRole == null) throw new SmartException("'Registered' role could not be loaded"); request.Customer.CustomerRoles.Add(registeredRole); //remove from 'Guests' role var guestRole = request.Customer.CustomerRoles.FirstOrDefault(cr => cr.SystemName == SystemCustomerRoleNames.Guests); if (guestRole != null) request.Customer.CustomerRoles.Remove(guestRole); //Add reward points for customer registration (if enabled) if (_rewardPointsSettings.Enabled && _rewardPointsSettings.PointsForRegistration > 0) request.Customer.AddRewardPointsHistoryEntry(_rewardPointsSettings.PointsForRegistration, _localizationService.GetResource("RewardPoints.Message.RegisteredAsCustomer")); _customerService.UpdateCustomer(request.Customer); _eventPublisher.Publish(new CustomerRegisteredEvent{ Customer = request.Customer }); return result; }
public virtual CustomerRegistrationResult RegisterCustomer(CustomerRegistrationRequest request) { Guard.NotNull(request, nameof(request)); Guard.NotNull(request.Customer, nameof(request.Customer)); var result = new CustomerRegistrationResult(); if (request.Customer.IsSearchEngineAccount()) { result.AddError(T("Account.Register.Errors.CannotRegisterSearchEngine")); return(result); } if (request.Customer.IsBackgroundTaskAccount()) { result.AddError(T("Account.Register.Errors.CannotRegisterTaskAccount")); return(result); } if (request.Customer.IsRegistered()) { result.AddError(T("Account.Register.Errors.AlreadyRegistered")); return(result); } if (!request.Email.HasValue()) { result.AddError(T("Account.Register.Errors.EmailIsNotProvided")); return(result); } if (!request.Email.IsEmail()) { result.AddError(T("Common.WrongEmail")); return(result); } if (!request.Password.HasValue()) { result.AddError(T("Account.Register.Errors.PasswordIsNotProvided")); return(result); } if (_customerSettings.CustomerLoginType != CustomerLoginType.Email && !request.Username.HasValue()) { result.AddError(T("Account.Register.Errors.UsernameIsNotProvided")); return(result); } // Validate unique user //if (_customerService.GetCustomerByEmail(request.Email) != null) //{ // result.AddError(T("Account.Register.Errors.EmailAlreadyExists")); // return result; //} //if (_customerSettings.CustomerLoginType != CustomerLoginType.Email && _customerService.GetCustomerByUsername(request.Username) != null) //{ // result.AddError(T("Account.Register.Errors.UsernameAlreadyExists")); // return result; //} // At this point request is valid request.Customer.Username = request.Username; request.Customer.Email = request.Email; request.Customer.PasswordFormat = request.PasswordFormat; switch (request.PasswordFormat) { case PasswordFormat.Clear: request.Customer.Password = request.Password; break; case PasswordFormat.Encrypted: request.Customer.Password = _encryptionService.EncryptText(request.Password); break; case PasswordFormat.Hashed: string saltKey = _encryptionService.CreateSaltKey(5); request.Customer.PasswordSalt = saltKey; request.Customer.Password = _encryptionService.CreatePasswordHash(request.Password, saltKey, _customerSettings.HashedPasswordFormat); break; } request.Customer.Active = request.IsApproved; var registeredRole = _customerService.GetCustomerRoleBySystemName(SystemCustomerRoleNames.Registered); if (registeredRole == null) { throw new SmartException(T("Admin.Customers.CustomerRoles.CannotFoundRole", "Registered")); } if (_customerSettings.RegisterCustomerRoleId != 0) { var customerRole = _customerService.GetCustomerRoleById(_customerSettings.RegisterCustomerRoleId); if (customerRole != null && customerRole.Id != registeredRole.Id) { _customerService.InsertCustomerRoleMapping(new CustomerRoleMapping { CustomerId = request.Customer.Id, CustomerRoleId = customerRole.Id }); } } // Add to 'Registered' role. _customerService.InsertCustomerRoleMapping(new CustomerRoleMapping { CustomerId = request.Customer.Id, CustomerRoleId = registeredRole.Id }); // Remove from 'Guests' role. var mappings = request.Customer.CustomerRoleMappings.Where(x => !x.IsSystemMapping && x.CustomerRole.SystemName == SystemCustomerRoleNames.Guests).ToList(); mappings.Each(x => _customerService.DeleteCustomerRoleMapping(x)); // Add reward points for customer registration (if enabled) if (_rewardPointsSettings.Enabled && _rewardPointsSettings.PointsForRegistration > 0) { request.Customer.AddRewardPointsHistoryEntry(_rewardPointsSettings.PointsForRegistration, T("RewardPoints.Message.RegisteredAsCustomer")); } _customerService.UpdateCustomer(request.Customer); _eventPublisher.Publish(new CustomerRegisteredEvent { Customer = request.Customer }); return(result); }