Esempio n. 1
0
        public virtual IActionResult Register(RegisterModel model, string returnUrl, bool captchaValid)
        {
            //check whether registration is allowed
            if (_userSettings.UserRegistrationType == UserRegistrationType.Disabled)
            {
                return(RedirectToRoute("RegisterResult", new { resultId = (int)UserRegistrationType.Disabled }));
            }

            if (_workContext.CurrentUser.IsRegistered())
            {
                //Already registered user.
                _authenticationService.SignOut();

                //raise logged out event
                _eventPublisher.Publish(new UserLoggedOutEvent(_workContext.CurrentUser));

                //Save a new record
                _workContext.CurrentUser = _userService.InsertGuestUser();
            }
            var user = _workContext.CurrentUser;

            //custom user attributes
            var userAttributesXml     = ParseCustomUserAttributes(model.Form);
            var userAttributeWarnings = _userAttributeParser.GetAttributeWarnings(userAttributesXml);

            foreach (var error in userAttributeWarnings)
            {
                ModelState.AddModelError("", error);
            }

            //validate CAPTCHA
            if (_captchaSettings.Enabled && _captchaSettings.ShowOnRegistrationPage && !captchaValid)
            {
                ModelState.AddModelError("", _captchaSettings.GetWrongCaptchaMessage(_localizationService));
            }

            if (ModelState.IsValid)
            {
                if (_userSettings.UsernamesEnabled && model.Username != null)
                {
                    model.Username = model.Username.Trim();
                }

                var isApproved = _userSettings.UserRegistrationType == UserRegistrationType.Standard;
            }

            //If we got this far, something failed, redisplay form
            model = _userModelFactory.PrepareRegisterModel(model, true, userAttributesXml);
            return(View(model));
        }
        //available even when navigation is not allowed
        //[PublicStoreAllowNavigation(true)]
        public virtual ActionResult Register(RegisterModel model, string returnUrl, bool captchaValid, FormCollection form)
        {
            //check whether registration is allowed
            if (_userSettings.UserRegistrationType == UserRegistrationType.Disabled)
            {
                return(RedirectToRoute("RegisterResult", new { resultId = (int)UserRegistrationType.Disabled }));
            }

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

                //raise logged out event
                _eventPublisher.Publish(new UserLoggedOutEvent(_workContext.CurrentUser));

                //Save a new record
                // _workContext.CurrentUser = _userService.InsertGuestCustomer();
            }
            var customer = _workContext.CurrentUser;
            //customer.RegisteredInStoreId = _storeContext.CurrentStore.Id;

            //custom customer attributes
            var customerAttributesXml     = ParseCustomUserAttributes(form);
            var customerAttributeWarnings = _userAttributeParser.GetAttributeWarnings(customerAttributesXml);

            foreach (var error in customerAttributeWarnings)
            {
                ModelState.AddModelError("", error);
            }

            //validate CAPTCHA
            if (_captchaSettings.Enabled && _captchaSettings.ShowOnRegistrationPage && !captchaValid)
            {
                ModelState.AddModelError("", _captchaSettings.GetWrongCaptchaMessage(_localizationService));
            }

            if (ModelState.IsValid)
            {
                if (_userSettings.UsernamesEnabled && model.Username != null)
                {
                    model.Username = model.Username.Trim();
                }

                bool isApproved          = _userSettings.UserRegistrationType == UserRegistrationType.Standard;
                var  registrationRequest = new UserRegistrationRequest(customer,
                                                                       model.Email,
                                                                       _userSettings.UsernamesEnabled ? model.Username : model.Email,
                                                                       model.Password,
                                                                       _userSettings.DefaultPasswordFormat,
                                                                       0,
                                                                       isApproved);
                var registrationResult = _userRegistrationService.RegisterUser(registrationRequest);
                if (registrationResult.Success)
                {
                    //properties
                    if (_dateTimeSettings.AllowCustomersToSetTimeZone)
                    {
                        _genericAttributeService.SaveAttribute(customer, SystemUserAttributeNames.TimeZoneId, model.TimeZoneId);
                    }

                    //form fields
                    if (_userSettings.GenderEnabled)
                    {
                        _genericAttributeService.SaveAttribute(customer, SystemUserAttributeNames.Gender, model.Gender);
                    }
                    _genericAttributeService.SaveAttribute(customer, SystemUserAttributeNames.FirstName, model.FirstName);
                    _genericAttributeService.SaveAttribute(customer, SystemUserAttributeNames.LastName, model.LastName);
                    if (_userSettings.DateOfBirthEnabled)
                    {
                        DateTime?dateOfBirth = model.ParseDateOfBirth();
                        _genericAttributeService.SaveAttribute(customer, SystemUserAttributeNames.DateOfBirth, dateOfBirth);
                    }
                    if (_userSettings.CompanyEnabled)
                    {
                        _genericAttributeService.SaveAttribute(customer, SystemUserAttributeNames.Company, model.Company);
                    }
                    if (_userSettings.StreetAddressEnabled)
                    {
                        _genericAttributeService.SaveAttribute(customer, SystemUserAttributeNames.StreetAddress, model.StreetAddress);
                    }
                    if (_userSettings.StreetAddress2Enabled)
                    {
                        _genericAttributeService.SaveAttribute(customer, SystemUserAttributeNames.StreetAddress2, model.StreetAddress2);
                    }
                    if (_userSettings.ZipPostalCodeEnabled)
                    {
                        _genericAttributeService.SaveAttribute(customer, SystemUserAttributeNames.ZipPostalCode, model.ZipPostalCode);
                    }
                    if (_userSettings.CityEnabled)
                    {
                        _genericAttributeService.SaveAttribute(customer, SystemUserAttributeNames.City, model.City);
                    }
                    if (_userSettings.CountryEnabled)
                    {
                        _genericAttributeService.SaveAttribute(customer, SystemUserAttributeNames.CountryId, model.CountryId);
                    }
                    if (_userSettings.CountryEnabled && _userSettings.StateProvinceEnabled)
                    {
                        _genericAttributeService.SaveAttribute(customer, SystemUserAttributeNames.StateProvinceId,
                                                               model.StateProvinceId);
                    }
                    if (_userSettings.PhoneEnabled)
                    {
                        _genericAttributeService.SaveAttribute(customer, SystemUserAttributeNames.Phone, model.Phone);
                    }
                    if (_userSettings.FaxEnabled)
                    {
                        _genericAttributeService.SaveAttribute(customer, SystemUserAttributeNames.Fax, model.Fax);
                    }


                    //save customer attributes
                    _genericAttributeService.SaveAttribute(customer, SystemUserAttributeNames.CustomUserAttributes, customerAttributesXml);

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



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

                    //raise event
                    _eventPublisher.Publish(new UserRegisteredEvent(customer));

                    switch (_userSettings.UserRegistrationType)
                    {
                    case UserRegistrationType.EmailValidation:
                    {
                        //email validation message
                        _genericAttributeService.SaveAttribute(customer, SystemUserAttributeNames.AccountActivationToken, Guid.NewGuid().ToString());
                        //_workflowMessageService.SendCustomerEmailValidationMessage(customer, _workContext.WorkingLanguage.Id);

                        //result
                        return(RedirectToRoute("RegisterResult",
                                               new { resultId = (int)UserRegistrationType.EmailValidation }));
                    }

                    case UserRegistrationType.AdminApproval:
                    {
                        return(RedirectToRoute("RegisterResult",
                                               new { resultId = (int)UserRegistrationType.AdminApproval }));
                    }

                    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));
                    }

                    default:
                    {
                        return(RedirectToRoute("HomePage"));
                    }
                    }
                }

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

            //If we got this far, something failed, redisplay form
            model = _userModelFactory.PrepareRegisterModel(model, true, customerAttributesXml);
            return(View(model));
        }