Exemple #1
0
        /// <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 (!CommonHelper.IsValidEmail(request.Email))
            {
                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);
                }
            }

            //event notification
            _eventPublisher.CustomerRegistrationEvent(result, request);

            //return if exist errors
            if (result.Errors.Any())
            {
                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;
            }

            _customerService.InsertCustomerPassword(request.Customer);

            request.Customer.Active = request.IsApproved;
            _customerService.UpdateActive(request.Customer);
            //add to 'Registered' role
            var registeredRole = _customerService.GetCustomerRoleBySystemName(SystemCustomerRoleNames.Registered);

            if (registeredRole == null)
            {
                throw new GrandException("'Registered' role could not be loaded");
            }
            request.Customer.CustomerRoles.Add(registeredRole);
            registeredRole.CustomerId = request.Customer.Id;
            _customerService.InsertCustomerRoleInCustomer(registeredRole);
            //remove from 'Guests' role
            var guestRole = request.Customer.CustomerRoles.FirstOrDefault(cr => cr.SystemName == SystemCustomerRoleNames.Guests);

            if (guestRole != null)
            {
                request.Customer.CustomerRoles.Remove(guestRole);
                guestRole.CustomerId = request.Customer.Id;
                _customerService.DeleteCustomerRoleInCustomer(guestRole);
            }
            //Add reward points for customer registration (if enabled)
            if (_rewardPointsSettings.Enabled &&
                _rewardPointsSettings.PointsForRegistration > 0)
            {
                _rewardPointsService.AddRewardPointsHistory(request.Customer.Id, _rewardPointsSettings.PointsForRegistration,
                                                            request.StoreId,
                                                            _localizationService.GetResource("RewardPoints.Message.EarnedForRegistration"), "", 0);
            }
            request.Customer.PasswordChangeDateUtc = DateTime.UtcNow;
            _customerService.UpdateCustomer(request.Customer);

            return(result);
        }