Esempio n. 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.IsRegistered())
            {
                result.AddError("Current customer is already registered");
                return(result);
            }
            if (String.IsNullOrEmpty(request.Email))
            {
                result.AddError("Email Is Not Provided.");
                return(result);
            }
            if (!CommonHelper.IsValidEmail(request.Email))
            {
                result.AddError("Wrong Email.");
                return(result);
            }
            if (String.IsNullOrWhiteSpace(request.Password))
            {
                result.AddError("Password Is Not Provided");
                return(result);
            }


            // validate unique user
            if (GetCustomerByEmail(request.Email) != null)
            {
                result.AddError("Email Already Exists.");
                return(result);
            }


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

            request.Customer.Password = request.Password;

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

            default:
                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;

            //add to 'Registered' role
            var registeredRole = GetCustomerRoleBySystemName(SystemCustomerRoleNames.Registered);

            if (registeredRole == null)
            {
                throw new EGSWException("'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);
            }


            UpdateCustomer(request.Customer);
            return(result);
        }
        public ActionResult Register(RegisterViewModel model)
        {
            string returnUrl = string.Empty;

            if (ModelState.IsValid)
            {

                if (_workContext.CurrentCustomer.IsRegistered())
                {
                    //Already registered customer.
                    _authenticationService.SignOut();

                    //Save a new record
                    _workContext.CurrentCustomer = _customerService.InsertGuestCustomer();
                }
                var customer = _workContext.CurrentCustomer;
                customer.FirstName = model.FirstName;
                customer.LastName = model.LastName;

                bool isApproved = true;
                var registrationRequest = new CustomerRegistrationRequest(customer, model.Email, model.Password,isApproved);

                var registrationResult = _customerService.RegisterCustomer(registrationRequest);
                if (registrationResult.Success)
                {

                    //login customer now
                    if (isApproved)
                        _authenticationService.SignIn(customer, true);

                    //form fields

                    //notifications
                    //if (_customerSettings.NotifyNewCustomerRegistration)
                    //    _workflowMessageService.SendCustomerRegisteredNotificationMessage(customer, _localizationSettings.DefaultAdminLanguageId);

                    switch (UserRegistrationType.Standard)
                    {

                        case UserRegistrationType.Standard:
                            {
                                ////send customer welcome message
                                //_workflowMessageService.SendCustomerWelcomeMessage(customer, _workContext.WorkingLanguage.Id);

                                //var redirectUrl = Url.RouteUrl("RegisterResult", new { resultId = (int)UserRegistrationType.Standard });
                                //if (!String.IsNullOrEmpty(returnUrl) && Url.IsLocalUrl(returnUrl))
                                //    redirectUrl = _webHelper.ModifyQueryString(redirectUrl, "returnurl=" + HttpUtility.UrlEncode(returnUrl), null);
                                //return Redirect(redirectUrl);

                                //send email
                                _workflowMessageService.SendCustomerWelcomeMessage(customer);

                                GutterCleanRequestModel Requestmodel = _httpContext.Session["GutterCleanRequestModel"] as GutterCleanRequestModel;
                                if (Requestmodel == null)
                                {
                                    return RedirectToRoute("GutterCleanRequest");
                                }
                                else
                                {

                                    return RedirectToRoute("ProcessPayment");
                                }
                            }
                        default:
                            {
                                return RedirectToRoute("GutterCleanRequest");

                            }
                    }
                }

                //errors
                foreach (var error in registrationResult.Errors)
                    ModelState.AddModelError("", error);
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
        public ActionResult GutterCleanInfoRegister(GutterCleanInfoModel model, bool captchaValid)
        {
            GutterCleanRequestModel requestModel = _httpContext.Session["GutterCleanRequestModel"] as GutterCleanRequestModel;
            if (requestModel == null)
            {
                return RedirectToRoute("GutterCleanRequest");
            }

            string returnUrl = string.Empty;

            if (!captchaValid)
            {
                ModelState.AddModelError("", "Wrong Captcha code.");
            }

            var zipcodeResult = _zipCodeService.GetZipCodeDetailByZipcode(model.ZipCode);
            if (zipcodeResult != null)
            {

            }
            else
            {
                ModelState.AddModelError("", "Zipcode is not valid.");

            }

            if (ModelState.IsValid)
            {

                if (!_workContext.CurrentCustomer.IsRegistered())
                {
                    //Already registered customer.
                    _authenticationService.SignOut();

                    //Save a new record
                    _workContext.CurrentCustomer = _customerService.InsertGuestCustomer();

                    var customer = _workContext.CurrentCustomer;
                    customer.FirstName = model.FirstName;
                    customer.LastName = model.LastName;

                    customer.Address1 = model.Address;
                    customer.ZipPostalCode = model.ZipCode;

                    bool isApproved = true;
                    string password = Guid.NewGuid().ToString();
                    var registrationRequest = new CustomerRegistrationRequest(customer, model.Email, password.Substring(0, 8), isApproved);

                    var registrationResult = _customerService.RegisterCustomer(registrationRequest);
                    if (registrationResult.Success)
                    {

                        //login customer now
                        if (isApproved)
                            _authenticationService.SignIn(customer, true);

                        switch (UserRegistrationType.Standard)
                        {

                            case UserRegistrationType.Standard:
                                {

                                    var address = new Address();
                                    address.Address1 = model.Address;
                                    address.Address2 = "";
                                    address.City = zipcodeResult.CityName;
                                    address.Email = model.Email;
                                    address.State = zipcodeResult.StateName;
                                    address.PhoneNumber = "";
                                    address.ZipPostalCode = model.ZipCode;
                                    address.CreatedOnUtc = DateTime.UtcNow;

                                    customer.Addresses.Add(address);
                                    _customerService.UpdateCustomer(customer);

                                    //send email
                                    _workflowMessageService.SendCustomerWelcomeMessage(customer);

                                    GutterCleanRequestModel Requestmodel = _httpContext.Session["GutterCleanRequestModel"] as GutterCleanRequestModel;
                                    if (Requestmodel == null)
                                    {
                                        return RedirectToRoute("GutterCleanRequest");
                                    }
                                    else
                                    {

                                        return RedirectToRoute("ProcessPayment");
                                    }
                                }
                            default:
                                {
                                    return RedirectToRoute("GutterCleanRequest");

                                }
                        }
                    }

                    //errors
                    foreach (var error in registrationResult.Errors)
                        ModelState.AddModelError("", error);
                }
                else
                {
                    /// user is already registered
                    return RedirectToRoute("ProcessPayment");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
        /// <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("Email Is Not Provided.");
                return result;
            }
            if (!CommonHelper.IsValidEmail(request.Email))
            {
                result.AddError("Wrong Email.");
                return result;
            }
            if (String.IsNullOrWhiteSpace(request.Password))
            {
                result.AddError("Password Is Not Provided");
                return result;
            }

               // validate unique user
            if (GetCustomerByEmail(request.Email) != null)
            {
                result.AddError("Email Already Exists.");
                return result;
            }

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

            request.Customer.Password = request.Password;

            switch (PasswordFormat.Clear)
            {
                case PasswordFormat.Clear:
                    {
                        request.Customer.Password = request.Password;
                    }
                    break;
                default:
                    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;

            //add to 'Registered' role
            var registeredRole = GetCustomerRoleBySystemName(SystemCustomerRoleNames.Registered);
            if (registeredRole == null)
                throw new EGSWException("'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);

            UpdateCustomer(request.Customer);
            return result;
        }