示例#1
0
        public virtual IActionResult Edit(UserModel model, bool continueEditing, IFormCollection form)
        {
            //try to get a user with the specified id
            var user = _userService.GetUserById(model.Id);

            if (user == null || user.Deleted)
            {
                return(RedirectToAction("List"));
            }

            // Ensure that valid email address is entered to avoid registered users with empty email address
            if (!CommonHelper.IsValidEmail(model.Email))
            {
                ModelState.AddModelError(string.Empty, "Valid Email is required for user");
                _notificationService.ErrorNotification("Valid Email is required for user");
            }

            if (ModelState.IsValid)
            {
                try
                {
                    //email
                    if (!string.IsNullOrWhiteSpace(model.Email))
                    {
                        _userRegistrationService.SetEmail(user, model.Email, false);
                    }
                    else
                    {
                        user.Email = model.Email;
                    }

                    _genericAttributeService.SaveAttribute(user, AldanUserDefaults.FirstNameAttribute, model.FirstName);
                    _genericAttributeService.SaveAttribute(user, AldanUserDefaults.LastNameAttribute, model.LastName);

                    user.Role = (Role)model.SelectedUserRoleId;

                    _userService.UpdateUser(user);

                    _notificationService.SuccessNotification("The user has been updated successfully.");

                    if (!continueEditing)
                    {
                        return(RedirectToAction("List"));
                    }

                    return(RedirectToAction("Edit", new { id = user.Id }));
                }
                catch (Exception exc)
                {
                    _notificationService.ErrorNotification(exc.Message);
                }
            }

            //prepare model
            model = _userModelFactory.PrepareUserModel(model, user, true);

            //if we got this far, something failed, redisplay form
            return(View(model));
        }
        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));
        }