public new void SetUp()
        {
            _customerSettings = new CustomerSettings();
            _securitySettings = new SecuritySettings()
            {
                EncryptionKey = "273ece6f97dd844d"
            };
            _rewardPointsSettings = new RewardPointsSettings()
            {
                Enabled = false,
            };

            _encryptionService = new EncryptionService(_securitySettings);
            _customerRepo = MockRepository.GenerateMock<IRepository<Customer>>();
            var customer1 = new Customer()
            {
                Username = "******",
                Email = "*****@*****.**",
                PasswordFormat = PasswordFormat.Hashed,
                Active = true
            };

            string saltKey = _encryptionService.CreateSaltKey(5);
            string password = _encryptionService.CreatePasswordHash("password", saltKey);
            customer1.PasswordSalt = saltKey;
            customer1.Password = password;
            AddCustomerToRegisteredRole(customer1);

            var customer2 = new Customer()
            {
                Username = "******",
                Email = "*****@*****.**",
                PasswordFormat = PasswordFormat.Clear,
                Password = "******",
                Active = true
            };
            AddCustomerToRegisteredRole(customer2);

            var customer3 = new Customer()
            {
                Username = "******",
                Email = "*****@*****.**",
                PasswordFormat = PasswordFormat.Encrypted,
                Password = _encryptionService.EncryptText("password"),
                Active = true
            };
            AddCustomerToRegisteredRole(customer3);

            var customer4 = new Customer()
            {
                Username = "******",
                Email = "*****@*****.**",
                PasswordFormat = PasswordFormat.Clear,
                Password = "******",
                Active = true
            };
            AddCustomerToRegisteredRole(customer4);

            var customer5 = new Customer()
            {
                Username = "******",
                Email = "*****@*****.**",
                PasswordFormat = PasswordFormat.Clear,
                Password = "******",
                Active = true
            };

            _eventPublisher = MockRepository.GenerateMock<IEventPublisher>();
            _eventPublisher.Expect(x => x.Publish(Arg<object>.Is.Anything));

            _customerRepo.Expect(x => x.Table).Return(new List<Customer>() { customer1, customer2, customer3, customer4, customer5 }.AsQueryable());

            _customerRoleRepo = MockRepository.GenerateMock<IRepository<CustomerRole>>();
            _genericAttributeRepo = MockRepository.GenerateMock<IRepository<GenericAttribute>>();

            _genericAttributeService = MockRepository.GenerateMock<IGenericAttributeService>();
            _newsLetterSubscriptionService = MockRepository.GenerateMock<INewsLetterSubscriptionService>();

            _localizationService = MockRepository.GenerateMock<ILocalizationService>();
            _customerService = new CustomerService(new NopNullCache(), _customerRepo, _customerRoleRepo,
                _genericAttributeRepo, _genericAttributeService, _eventPublisher, _customerSettings);
            _customerRegistrationService = new CustomerRegistrationService(_customerService,
                _encryptionService, _newsLetterSubscriptionService, _localizationService,
                _rewardPointsSettings, _customerSettings);
        }
示例#2
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.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);
                }
            }

            //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 NopException("'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, "Registered as customer");
            }

            _customerService.UpdateCustomer(request.Customer);
            return(result);
        }
        /// <summary>
        /// Register customer
        /// </summary>
        /// <param name="request">Request</param>
        /// <returns>
        /// A task that represents the asynchronous operation
        /// The task result contains the result
        /// </returns>
        public virtual async Task <CustomerRegistrationResult> RegisterCustomerAsync(CustomerRegistrationRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(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 (await _customerService.IsRegisteredAsync(request.Customer))
            {
                result.AddError("Current customer is already registered");
                return(result);
            }

            if (string.IsNullOrEmpty(request.Email))
            {
                result.AddError(await _localizationService.GetResourceAsync("Account.Register.Errors.EmailIsNotProvided"));
                return(result);
            }

            if (!CommonHelper.IsValidEmail(request.Email))
            {
                result.AddError(await _localizationService.GetResourceAsync("Common.WrongEmail"));
                return(result);
            }

            if (string.IsNullOrWhiteSpace(request.Password))
            {
                result.AddError(await _localizationService.GetResourceAsync("Account.Register.Errors.PasswordIsNotProvided"));
                return(result);
            }

            if (_customerSettings.UsernamesEnabled && string.IsNullOrEmpty(request.Username))
            {
                result.AddError(await _localizationService.GetResourceAsync("Account.Register.Errors.UsernameIsNotProvided"));
                return(result);
            }

            //validate unique user
            if (await _customerService.GetCustomerByEmailAsync(request.Email) != null)
            {
                result.AddError(await _localizationService.GetResourceAsync("Account.Register.Errors.EmailAlreadyExists"));
                return(result);
            }

            if (_customerSettings.UsernamesEnabled && await _customerService.GetCustomerByUsernameAsync(request.Username) != null)
            {
                result.AddError(await _localizationService.GetResourceAsync("Account.Register.Errors.UsernameAlreadyExists"));
                return(result);
            }

            //at this point request is valid
            request.Customer.Username = request.Username;
            request.Customer.Email    = request.Email;

            var customerPassword = new CustomerPassword
            {
                CustomerId     = request.Customer.Id,
                PasswordFormat = request.PasswordFormat,
                CreatedOnUtc   = DateTime.UtcNow
            };

            switch (request.PasswordFormat)
            {
            case PasswordFormat.Clear:
                customerPassword.Password = request.Password;
                break;

            case PasswordFormat.Encrypted:
                customerPassword.Password = _encryptionService.EncryptText(request.Password);
                break;

            case PasswordFormat.Hashed:
                var saltKey = _encryptionService.CreateSaltKey(NopCustomerServicesDefaults.PasswordSaltKeySize);
                customerPassword.PasswordSalt = saltKey;
                customerPassword.Password     = _encryptionService.CreatePasswordHash(request.Password, saltKey, _customerSettings.HashedPasswordFormat);
                break;
            }

            await _customerService.InsertCustomerPasswordAsync(customerPassword);

            request.Customer.Active = request.IsApproved;

            //add to 'Registered' role
            var registeredRole = await _customerService.GetCustomerRoleBySystemNameAsync(NopCustomerDefaults.RegisteredRoleName);

            if (registeredRole == null)
            {
                throw new NopException("'Registered' role could not be loaded");
            }

            await _customerService.AddCustomerRoleMappingAsync(new CustomerCustomerRoleMapping { CustomerId = request.Customer.Id, CustomerRoleId = registeredRole.Id });

            //remove from 'Guests' role
            if (await _customerService.IsGuestAsync(request.Customer))
            {
                var guestRole = await _customerService.GetCustomerRoleBySystemNameAsync(NopCustomerDefaults.GuestsRoleName);

                await _customerService.RemoveCustomerRoleMappingAsync(request.Customer, guestRole);
            }

            //add reward points for customer registration (if enabled)
            if (_rewardPointsSettings.Enabled && _rewardPointsSettings.PointsForRegistration > 0)
            {
                var endDate = _rewardPointsSettings.RegistrationPointsValidity > 0
                    ? (DateTime?)DateTime.UtcNow.AddDays(_rewardPointsSettings.RegistrationPointsValidity.Value) : null;
                await _rewardPointService.AddRewardPointsHistoryEntryAsync(request.Customer, _rewardPointsSettings.PointsForRegistration,
                                                                           request.StoreId, await _localizationService.GetResourceAsync("RewardPoints.Message.EarnedForRegistration"), endDate : endDate);
            }

            await _customerService.UpdateCustomerAsync(request.Customer);

            return(result);
        }
        public new void SetUp()
        {
            _customerSettings = new CustomerSettings
            {
                UnduplicatedPasswordsNumber = 1
            };
            _securitySettings = new SecuritySettings
            {
                EncryptionKey = "273ece6f97dd844d"
            };

            _encryptionService = new EncryptionService(_securitySettings);
            _customerRepo      = MockRepository.GenerateMock <IRepository <Customer> >();
            var customer1 = new Customer
            {
                Id       = 1,
                Username = "******",
                Email    = "*****@*****.**",
                Active   = true
            };

            AddCustomerToRegisteredRole(customer1);

            var customer2 = new Customer
            {
                Id       = 2,
                Username = "******",
                Email    = "*****@*****.**",
                Active   = true
            };

            AddCustomerToRegisteredRole(customer2);

            var customer3 = new Customer
            {
                Id       = 3,
                Username = "******",
                Email    = "*****@*****.**",
                Active   = true
            };

            AddCustomerToRegisteredRole(customer3);

            var customer4 = new Customer
            {
                Id       = 4,
                Username = "******",
                Email    = "*****@*****.**",
                Active   = true
            };

            AddCustomerToRegisteredRole(customer4);

            var customer5 = new Customer
            {
                Id       = 5,
                Username = "******",
                Email    = "*****@*****.**",
                Active   = true
            };

            _customerRepo.Expect(x => x.Table).Return(new List <Customer> {
                customer1, customer2, customer3, customer4, customer5
            }.AsQueryable());

            _customerPasswordRepo = MockRepository.GenerateMock <IRepository <CustomerPassword> >();
            string saltKey   = _encryptionService.CreateSaltKey(5);
            string password  = _encryptionService.CreatePasswordHash("password", saltKey);
            var    password1 = new CustomerPassword
            {
                CustomerId     = customer1.Id,
                PasswordFormat = PasswordFormat.Hashed,
                PasswordSalt   = saltKey,
                Password       = password,
                CreatedOnUtc   = DateTime.UtcNow
            };
            var password2 = new CustomerPassword
            {
                CustomerId     = customer2.Id,
                PasswordFormat = PasswordFormat.Clear,
                Password       = "******",
                CreatedOnUtc   = DateTime.UtcNow
            };
            var password3 = new CustomerPassword
            {
                CustomerId     = customer3.Id,
                PasswordFormat = PasswordFormat.Encrypted,
                Password       = _encryptionService.EncryptText("password"),
                CreatedOnUtc   = DateTime.UtcNow
            };
            var password4 = new CustomerPassword
            {
                CustomerId     = customer4.Id,
                PasswordFormat = PasswordFormat.Clear,
                Password       = "******",
                CreatedOnUtc   = DateTime.UtcNow
            };
            var password5 = new CustomerPassword
            {
                CustomerId     = customer5.Id,
                PasswordFormat = PasswordFormat.Clear,
                Password       = "******",
                CreatedOnUtc   = DateTime.UtcNow
            };

            _customerPasswordRepo.Expect(x => x.Table).Return(new[] { password1, password2, password3, password4, password5 }.AsQueryable());

            _eventPublisher = MockRepository.GenerateMock <IEventPublisher>();
            _eventPublisher.Expect(x => x.Publish(Arg <object> .Is.Anything));

            _storeService         = MockRepository.GenerateMock <IStoreService>();
            _customerRoleRepo     = MockRepository.GenerateMock <IRepository <CustomerRole> >();
            _genericAttributeRepo = MockRepository.GenerateMock <IRepository <GenericAttribute> >();
            _forumPostRepo        = MockRepository.GenerateMock <IRepository <ForumPost> >();
            _forumTopicRepo       = MockRepository.GenerateMock <IRepository <ForumTopic> >();

            _genericAttributeService       = MockRepository.GenerateMock <IGenericAttributeService>();
            _newsLetterSubscriptionService = MockRepository.GenerateMock <INewsLetterSubscriptionService>();

            _localizationService    = MockRepository.GenerateMock <ILocalizationService>();
            _workContext            = MockRepository.GenerateMock <IWorkContext>();
            _workflowMessageService = MockRepository.GenerateMock <IWorkflowMessageService>();

            _customerService = new CustomerService(new NopNullCache(), _customerRepo, _customerPasswordRepo, _customerRoleRepo,
                                                   _genericAttributeRepo, _forumPostRepo, _forumTopicRepo,
                                                   null, null, null, null, null,
                                                   _genericAttributeService, null, null, _eventPublisher, _customerSettings, null);
            _customerRegistrationService = new CustomerRegistrationService(_customerService,
                                                                           _encryptionService, _newsLetterSubscriptionService, _localizationService,
                                                                           _storeService, _workContext, _genericAttributeService,
                                                                           _workflowMessageService, _eventPublisher, _customerSettings);
        }
示例#5
0
        public new void SetUp()
        {
            _customerSettings = new CustomerSettings();
            _securitySettings = new SecuritySettings()
            {
                EncryptionKey = "273ece6f97dd844d"
            };
            _rewardPointsSettings = new RewardPointsSettings()
            {
                Enabled = false,
            };

            _encryptionService = new EncryptionService(_securitySettings);
            _customerRepo      = MockRepository.GenerateMock <IRepository <Customer> >();
            var customer1 = new Customer()
            {
                Username       = "******",
                Email          = "*****@*****.**",
                PasswordFormat = PasswordFormat.Hashed,
                Active         = true
            };

            string saltKey  = _encryptionService.CreateSaltKey(5);
            string password = _encryptionService.CreatePasswordHash("password", saltKey);

            customer1.PasswordSalt = saltKey;
            customer1.Password     = password;
            AddCustomerToRegisteredRole(customer1);

            var customer2 = new Customer()
            {
                Username       = "******",
                Email          = "*****@*****.**",
                PasswordFormat = PasswordFormat.Clear,
                Password       = "******",
                Active         = true
            };

            AddCustomerToRegisteredRole(customer2);

            var customer3 = new Customer()
            {
                Username       = "******",
                Email          = "*****@*****.**",
                PasswordFormat = PasswordFormat.Encrypted,
                Password       = _encryptionService.EncryptText("password"),
                Active         = true
            };

            AddCustomerToRegisteredRole(customer3);

            var customer4 = new Customer()
            {
                Username       = "******",
                Email          = "*****@*****.**",
                PasswordFormat = PasswordFormat.Clear,
                Password       = "******",
                Active         = true
            };

            AddCustomerToRegisteredRole(customer4);

            var customer5 = new Customer()
            {
                Username       = "******",
                Email          = "*****@*****.**",
                PasswordFormat = PasswordFormat.Clear,
                Password       = "******",
                Active         = true
            };

            _eventPublisher = MockRepository.GenerateMock <IEventPublisher>();
            _eventPublisher.Expect(x => x.Publish(Arg <object> .Is.Anything));

            _customerRepo.Expect(x => x.Table).Return(new List <Customer>()
            {
                customer1, customer2, customer3, customer4, customer5
            }.AsQueryable());

            _customerRoleRepo        = MockRepository.GenerateMock <IRepository <CustomerRole> >();
            _genericAttributeRepo    = MockRepository.GenerateMock <IRepository <GenericAttribute> >();
            _rewardPointsHistoryRepo = MockRepository.GenerateMock <IRepository <RewardPointsHistory> >();

            _genericAttributeService       = MockRepository.GenerateMock <IGenericAttributeService>();
            _newsLetterSubscriptionService = MockRepository.GenerateMock <INewsLetterSubscriptionService>();

            _storeContext = MockRepository.GenerateMock <IStoreContext>();

            _customerService = new CustomerService(new NullCache(), _customerRepo, _customerRoleRepo,
                                                   _genericAttributeRepo, _rewardPointsHistoryRepo, _genericAttributeService, _eventPublisher, _rewardPointsSettings);

            _customerRegistrationService = new CustomerRegistrationService(_customerService,
                                                                           _encryptionService, _newsLetterSubscriptionService, _rewardPointsSettings, _customerSettings, _storeContext, _eventPublisher);
        }
示例#6
0
        public void TestInitialize()
        {
            _securitySettings = new SecuritySettings
            {
                EncryptionKey = "273ece6f97dd844d97dd8f4d"
            };
            _rewardPointsSettings = new RewardPointsSettings
            {
                Enabled = false,
            };

            _encryptionService = new EncryptionService(_securitySettings);

            var customer1 = new Customer
            {
                Username       = "******",
                Email          = "*****@*****.**",
                PasswordFormat = PasswordFormat.Hashed,
                Active         = true
            };

            string saltKey  = _encryptionService.CreateSaltKey(5);
            string password = _encryptionService.CreatePasswordHash("password", saltKey);

            customer1.PasswordSalt = saltKey;
            customer1.Password     = password;
            AddCustomerToRegisteredRole(customer1);

            var customer2 = new Customer
            {
                Username       = "******",
                Email          = "*****@*****.**",
                PasswordFormat = PasswordFormat.Clear,
                Password       = "******",
                Active         = true
            };

            AddCustomerToRegisteredRole(customer2);

            var customer3 = new Customer
            {
                Username       = "******",
                Email          = "*****@*****.**",
                PasswordFormat = PasswordFormat.Encrypted,
                Password       = _encryptionService.EncryptText("password"),
                Active         = true
            };

            AddCustomerToRegisteredRole(customer3);

            var customer4 = new Customer
            {
                Username       = "******",
                Email          = "*****@*****.**",
                PasswordFormat = PasswordFormat.Clear,
                Password       = "******",
                Active         = true
            };

            AddCustomerToRegisteredRole(customer4);

            var customer5 = new Customer
            {
                Username       = "******",
                Email          = "*****@*****.**",
                PasswordFormat = PasswordFormat.Clear,
                Password       = "******",
                Active         = true
            };

            //trying to recreate

            var eventPublisher = new Mock <IMediator>();

            //eventPublisher.Setup(x => x.PublishAsync(new object()));
            _eventPublisher = eventPublisher.Object;

            _storeService = new Mock <IStoreService>().Object;

            _customerRepo = new Grand.Services.Tests.MongoDBRepositoryTest <Customer>();
            _customerRepo.Insert(customer1);
            _customerRepo.Insert(customer2);
            _customerRepo.Insert(customer3);
            _customerRepo.Insert(customer4);
            _customerRepo.Insert(customer5);

            _customerRoleRepo         = new Mock <IRepository <CustomerRole> >().Object;
            _orderRepo                = new Mock <IRepository <Order> >().Object;
            _forumPostRepo            = new Mock <IRepository <ForumPost> >().Object;
            _forumTopicRepo           = new Mock <IRepository <ForumTopic> >().Object;
            _customerProductPriceRepo = new Mock <IRepository <CustomerProductPrice> >().Object;
            _customerProductRepo      = new Mock <IRepository <CustomerProduct> >().Object;
            _customerHistoryRepo      = new Mock <IRepository <CustomerHistoryPassword> >().Object;
            _customerNoteRepo         = new Mock <IRepository <CustomerNote> >().Object;

            _genericAttributeService       = new Mock <IGenericAttributeService>().Object;
            _newsLetterSubscriptionService = new Mock <INewsLetterSubscriptionService>().Object;
            _localizationService           = new Mock <ILocalizationService>().Object;
            _rewardPointsService           = new Mock <IRewardPointsService>().Object;
            _customerRoleProductRepo       = new Mock <IRepository <CustomerRoleProduct> >().Object;
            _serviceProvider  = new Mock <IServiceProvider>().Object;
            _customerSettings = new CustomerSettings();
            _commonSettings   = new CommonSettings();
            _customerService  = new CustomerService(new TestMemoryCacheManager(new Mock <IMemoryCache>().Object, _eventPublisher), _customerRepo, _customerRoleRepo,
                                                    _customerHistoryRepo, _customerRoleProductRepo, _customerNoteRepo, null, _eventPublisher);

            _customerRegistrationService = new CustomerRegistrationService(
                _customerService,
                _encryptionService,
                _newsLetterSubscriptionService,
                _localizationService,
                _storeService,
                _eventPublisher,
                _rewardPointsSettings,
                _customerSettings,
                _rewardPointsService);
        }
示例#7
0
        public ChangePasswordResult ChangePassword(ChangePasswordRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            var result = new ChangePasswordResult();

            if (String.IsNullOrWhiteSpace(request.Email))
            {
                result.AddError(_localizationService.GetResource("Account.ChangePassword.Errors.EmailIsNotProvided"));
                return(result);
            }
            if (String.IsNullOrWhiteSpace(request.NewPassword))
            {
                result.AddError(_localizationService.GetResource("Account.ChangePassword.Errors.PasswordIsNotProvided"));
                return(result);
            }

            var customer = _customerService.GetCustomerByEmail(request.Email);

            if (customer == null)
            {
                result.AddError(_localizationService.GetResource("Account.ChangePassword.Errors.EmailNotFound"));
                return(result);
            }

            var requestIsValid = false;

            if (request.ValidateRequest)
            {
                string oldPwd;
                switch (customer.PasswordFormat)
                {
                case PasswordFormat.Encrypted:
                    oldPwd = _encryptionService.EncryptText(request.OldPassword);
                    break;

                case PasswordFormat.Hashed:
                    oldPwd = _encryptionService.CreatePasswordHash(request.OldPassword, customer.PasswordSalt, _customerSettings.HashedPasswordFormat);
                    break;

                default:
                    oldPwd = request.OldPassword;
                    break;
                }
                bool oldPasswordIsValid = oldPwd == customer.Password;
                if (!oldPasswordIsValid)
                {
                    result.AddError(_localizationService.GetResource("Account.ChangePassword.Errors.OldPasswordDoesntMatch"));
                }
                if (oldPasswordIsValid)
                {
                    requestIsValid = true;
                }
            }
            else
            {
                requestIsValid = true;
            }

            if (requestIsValid)
            {
                switch (customer.PasswordFormat)
                {
                case PasswordFormat.Clear:
                    customer.Password = request.NewPassword;
                    break;

                case PasswordFormat.Encrypted:
                    customer.Password = _encryptionService.EncryptText(request.NewPassword);
                    break;

                case PasswordFormat.Hashed:
                {
                    string saltKey = _encryptionService.CreateSaltKey(5);
                    customer.PasswordSalt = saltKey;
                    customer.Password     = _encryptionService.CreatePasswordHash(request.NewPassword, saltKey, _customerSettings.HashedPasswordFormat);
                }
                break;

                default:
                    break;
                }
                customer.PasswordFormat = request.NewPasswordFormat;
                _customerService.UpdateCustomer(customer);
            }

            return(result);
        }
        public void TestInitialize()
        {
            _securitySettings = new SecuritySettings {
                EncryptionKey = "273ece6f97dd844d"
            };
            _rewardPointsSettings = new RewardPointsSettings {
                Enabled = false,
            };

            _encryptionService = new EncryptionService(_securitySettings);

            var customer1 = new Customer {
                Username       = "******",
                Email          = "*****@*****.**",
                PasswordFormat = PasswordFormat.Hashed,
                Active         = true
            };

            string saltKey  = _encryptionService.CreateSaltKey(5);
            string password = _encryptionService.CreatePasswordHash("password", saltKey);

            customer1.PasswordSalt = saltKey;
            customer1.Password     = password;
            AddCustomerToRegisteredRole(customer1);

            var customer2 = new Customer {
                Username       = "******",
                Email          = "*****@*****.**",
                PasswordFormat = PasswordFormat.Clear,
                Password       = "******",
                Active         = true
            };

            AddCustomerToRegisteredRole(customer2);

            var customer3 = new Customer {
                Username       = "******",
                Email          = "*****@*****.**",
                PasswordFormat = PasswordFormat.Encrypted,
                Password       = _encryptionService.EncryptText("password"),
                Active         = true
            };

            AddCustomerToRegisteredRole(customer3);

            var customer4 = new Customer {
                Username       = "******",
                Email          = "*****@*****.**",
                PasswordFormat = PasswordFormat.Clear,
                Password       = "******",
                Active         = true
            };

            AddCustomerToRegisteredRole(customer4);

            var customer5 = new Customer {
                Username       = "******",
                Email          = "*****@*****.**",
                PasswordFormat = PasswordFormat.Clear,
                Password       = "******",
                Active         = true
            };

            _eventPublisher = MockRepository.GenerateMock <IEventPublisher>();
            _eventPublisher.Expect(x => x.Publish(Arg <object> .Is.Anything));
            _storeService = MockRepository.GenerateMock <IStoreService>();

            _customerRepo = new Grand.Services.Tests.MongoDBRepositoryTest <Customer>();
            _customerRepo.Insert(customer1);
            _customerRepo.Insert(customer2);
            _customerRepo.Insert(customer3);
            _customerRepo.Insert(customer4);
            _customerRepo.Insert(customer5);

            _customerRoleRepo = MockRepository.GenerateMock <IRepository <CustomerRole> >();
            _orderRepo        = MockRepository.GenerateMock <IRepository <Order> >();
            _forumPostRepo    = MockRepository.GenerateMock <IRepository <ForumPost> >();
            _forumTopicRepo   = MockRepository.GenerateMock <IRepository <ForumTopic> >();

            _genericAttributeService       = MockRepository.GenerateMock <IGenericAttributeService>();
            _newsLetterSubscriptionService = MockRepository.GenerateMock <INewsLetterSubscriptionService>();
            _localizationService           = MockRepository.GenerateMock <ILocalizationService>();
            _customerRoleProductRepo       = MockRepository.GenerateMock <IRepository <CustomerRoleProduct> >();
            _customerSettings = new CustomerSettings();

            _customerService = new CustomerService(new NopNullCache(), _customerRepo, _customerRoleRepo,
                                                   _customerRoleProductRepo, _orderRepo, _forumPostRepo, _forumTopicRepo,
                                                   null, null, _genericAttributeService, null,
                                                   _eventPublisher, _customerSettings, null);

            _customerRegistrationService = new CustomerRegistrationService(
                _customerService,
                _encryptionService,
                _newsLetterSubscriptionService,
                _localizationService,
                _storeService,
                _rewardPointsSettings,
                _customerSettings,
                null);
        }
示例#9
0
 public InstallTokenService(IEncryptionService encryptionService)
 {
     this.Token = encryptionService.CreateSaltKey(32);
 }
        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);
        }