コード例 #1
0
        public async Task <IActionResult> Info(UserInfoModel model)
        {
            if (!_workContext.CurrentUser.IsRegistered())
            {
                return(Challenge());
            }

            var user = _workContext.CurrentUser;

            try
            {
                if (ModelState.IsValid)
                {
                    //username
                    if (_userSettings.UsernamesEnabled && _userSettings.AllowUsersToChangeUsernames)
                    {
                        if (!user.Username.Equals(model.Username.Trim(), StringComparison.InvariantCultureIgnoreCase))
                        {
                            //change username
                            await _userRegistrationService.SetUsernameAsync(user, model.Username.Trim());

                            //re-authenticate
                            await _authenticationService.SignInAsync(user, true);
                        }
                    }

                    //email
                    if (!user.Email.Equals(model.Email.Trim(), StringComparison.InvariantCultureIgnoreCase))
                    {
                        //change email
                        await _userRegistrationService.SetEmailAsync(user, model.Email.Trim());

                        //re-authenticate (if usernames are disabled)
                        if (!_userSettings.UsernamesEnabled)
                        {
                            await _authenticationService.SignInAsync(user, true);
                        }
                    }

                    //properties
                    if (_dateTimeSettings.AllowUsersToSetTimeZone)
                    {
                        await _genericAttributeService.SaveAttributeAsync(user, UserDefaults.TimeZoneIdAttribute, model.TimeZoneId);
                    }

                    //form fields
                    if (_userSettings.GenderEnabled)
                    {
                        await _genericAttributeService.SaveAttributeAsync(user, UserDefaults.GenderAttribute, model.Gender);
                    }

                    await _genericAttributeService.SaveAttributeAsync(user, UserDefaults.FirstNameAttribute, model.FirstName);

                    await _genericAttributeService.SaveAttributeAsync(user, UserDefaults.LastNameAttribute, model.LastName);

                    if (_userSettings.DateOfBirthEnabled)
                    {
                        var dateOfBirth = model.ParseDateOfBirth();
                        await _genericAttributeService.SaveAttributeAsync(user, UserDefaults.DateOfBirthAttribute, dateOfBirth);
                    }

                    if (_userSettings.PhoneEnabled)
                    {
                        await _genericAttributeService.SaveAttributeAsync(user, UserDefaults.PhoneAttribute, model.Phone);
                    }

                    return(RedirectToRoute("UserInfo"));
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
            }

            model = await _userAccountModelFactory.PrepareUserInfoModel(model, user, true);

            return(View(model));
        }
コード例 #2
0
        public virtual IActionResult Info(UserInfoModel model)
        {
            if (!_workContext.CurrentUser.IsRegistered())
            {
                return(Challenge());
            }

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

            try
            {
                if (ModelState.IsValid)
                {
                    //username
                    if (_userSettings.UsernamesEnabled && this._userSettings.AllowUsersToChangeUsernames)
                    {
                        if (
                            !user.Username.Equals(model.Username.Trim(), StringComparison.InvariantCultureIgnoreCase))
                        {
                            //change username
                            _userRegistrationService.SetUsername(user, model.Username.Trim());

                            //re-authenticate
                            //do not authenticate users in impersonation mode
                            _authenticationService.SignIn(user, true);
                        }
                    }
                    //email
                    if (!user.Email.Equals(model.Email.Trim(), StringComparison.InvariantCultureIgnoreCase))
                    {
                        //change email
                        var requireValidation = _userSettings.UserRegistrationType == UserRegistrationType.EmailValidation;
                        _userRegistrationService.SetEmail(user, model.Email.Trim(), requireValidation);
                    }

                    //properties
                    if (_dateTimeSettings.AllowUsersToSetTimeZone)
                    {
                        _genericAttributeService.SaveAttribute(user, NopUserDefaults.TimeZoneIdAttribute,
                                                               model.TimeZoneId);
                    }

                    //form fields
                    if (_userSettings.GenderEnabled)
                    {
                        _genericAttributeService.SaveAttribute(user, NopUserDefaults.GenderAttribute, model.Gender);
                    }
                    _genericAttributeService.SaveAttribute(user, NopUserDefaults.FirstNameAttribute, model.FirstName);
                    _genericAttributeService.SaveAttribute(user, NopUserDefaults.LastNameAttribute, model.LastName);
                    if (_userSettings.DateOfBirthEnabled)
                    {
                        var dateOfBirth = model.ParseDateOfBirth();
                        _genericAttributeService.SaveAttribute(user, NopUserDefaults.DateOfBirthAttribute, dateOfBirth);
                    }
                    if (_userSettings.CompanyEnabled)
                    {
                        _genericAttributeService.SaveAttribute(user, NopUserDefaults.CompanyAttribute, model.Company);
                    }
                    if (_userSettings.StreetAddressEnabled)
                    {
                        _genericAttributeService.SaveAttribute(user, NopUserDefaults.StreetAddressAttribute, model.StreetAddress);
                    }
                    if (_userSettings.StreetAddress2Enabled)
                    {
                        _genericAttributeService.SaveAttribute(user, NopUserDefaults.StreetAddress2Attribute, model.StreetAddress2);
                    }
                    if (_userSettings.ZipPostalCodeEnabled)
                    {
                        _genericAttributeService.SaveAttribute(user, NopUserDefaults.ZipPostalCodeAttribute, model.ZipPostalCode);
                    }
                    if (_userSettings.CityEnabled)
                    {
                        _genericAttributeService.SaveAttribute(user, NopUserDefaults.CityAttribute, model.City);
                    }
                    if (_userSettings.CountyEnabled)
                    {
                        _genericAttributeService.SaveAttribute(user, NopUserDefaults.CountyAttribute, model.County);
                    }
                    if (_userSettings.CountryEnabled)
                    {
                        _genericAttributeService.SaveAttribute(user, NopUserDefaults.CountryIdAttribute, model.CountryId);
                    }
                    if (_userSettings.CountryEnabled && _userSettings.StateProvinceEnabled)
                    {
                        _genericAttributeService.SaveAttribute(user, NopUserDefaults.StateProvinceIdAttribute, model.StateProvinceId);
                    }
                    if (_userSettings.PhoneEnabled)
                    {
                        _genericAttributeService.SaveAttribute(user, NopUserDefaults.PhoneAttribute, model.Phone);
                    }
                    if (_userSettings.FaxEnabled)
                    {
                        _genericAttributeService.SaveAttribute(user, NopUserDefaults.FaxAttribute, model.Fax);
                    }

                    //save user attributes
                    _genericAttributeService.SaveAttribute(_workContext.CurrentUser,
                                                           NopUserDefaults.CustomUserAttributes, userAttributesXml);

                    return(RedirectToRoute("UserInfo"));
                }
            }
            catch (Exception exc)
            {
                ModelState.AddModelError("", exc.Message);
            }

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