Beispiel #1
0
        public async Task <bool> Handle(UpdateCustomerInfoCommand request, CancellationToken cancellationToken)
        {
            //username
            if (_customerSettings.UsernamesEnabled && _customerSettings.AllowUsersToChangeUsernames)
            {
                if (!request.Customer.Username.Equals(request.Model.Username.Trim(), StringComparison.OrdinalIgnoreCase))
                {
                    //change username
                    await _customerRegistrationService.SetUsername(request.Customer, request.Model.Username.Trim());

                    //re-authenticate
                    if (request.OriginalCustomerIfImpersonated == null)
                    {
                        await _authenticationService.SignIn(request.Customer, true);
                    }
                }
            }
            //email
            if (!request.Customer.Email.Equals(request.Model.Email.Trim(), StringComparison.OrdinalIgnoreCase))
            {
                //change email
                await _customerRegistrationService.SetEmail(request.Customer, request.Model.Email.Trim());

                //re-authenticate (if usernames are disabled)
                //do not authenticate users in impersonation mode
                if (request.OriginalCustomerIfImpersonated == null)
                {
                    //re-authenticate (if usernames are disabled)
                    if (!_customerSettings.UsernamesEnabled)
                    {
                        await _authenticationService.SignIn(request.Customer, true);
                    }
                }
            }

            //VAT number
            if (_taxSettings.EuVatEnabled)
            {
                await UpdateTax(request);
            }
            //form fields
            await UpdateFormFields(request);

            //newsletter
            if (_customerSettings.NewsletterEnabled)
            {
                await UpdateNewsletter(request);
            }

            if (_forumSettings.ForumsEnabled && _forumSettings.SignaturesEnabled)
            {
                await _genericAttributeService.SaveAttribute(request.Customer, SystemCustomerAttributeNames.Signature, request.Model.Signature);
            }

            //save customer attributes
            await _genericAttributeService.SaveAttribute(request.Customer, SystemCustomerAttributeNames.CustomCustomerAttributes, request.CustomerAttributesXml);

            return(true);
        }
Beispiel #2
0
        public HttpResponseMessage PostUpdateCustomer(CustomerModel customer)
        {
            if (!CheckTokenUser(customer.Id))
            {
                return(ReturnResult(string.Empty, 1, "数据读取方式不合规"));
            }

            if (customer == null)
            {
                return(ReturnResult(string.Empty, 1, "不能传入空的数据"));
            }
            if (string.IsNullOrWhiteSpace(customer.Username))
            {
                return(ReturnResult(string.Empty, 1, "手机号不能为空"));
            }

            try
            {
                var oldCustomer = _customerService.GetCustomerById(customer.Id);
                //form fields
                //username
                if (_customerSettings.UsernamesEnabled && this._customerSettings.AllowUsersToChangeUsernames)
                {
                    if (!oldCustomer.Username.Equals(customer.Username.Trim(), StringComparison.InvariantCultureIgnoreCase))
                    {
                        //change username
                        _customerRegistrationService.SetUsername(oldCustomer, customer.Username.Trim());
                    }
                }



                _genericAttributeService.SaveAttribute(oldCustomer, SystemCustomerAttributeNames.FirstName, customer.FirstName);
                _genericAttributeService.SaveAttribute(oldCustomer, SystemCustomerAttributeNames.LastName, customer.LastName);

                var result = new
                {
                    id        = oldCustomer.Id,
                    username  = oldCustomer.Username,
                    email     = oldCustomer.Email.ConvertToString(),
                    firstname = oldCustomer.GetAttribute <string>(SystemCustomerAttributeNames.FirstName).ConvertToString(),
                    lastname  = oldCustomer.GetAttribute <string>(SystemCustomerAttributeNames.LastName).ConvertToString(),
                    imageurl  = _pictureService.GetPictureUrl(
                        oldCustomer.GetAttribute <int>(SystemCustomerAttributeNames.AvatarPictureId),
                        _mediaSettings.AvatarPictureSize,
                        false).ConvertToString()
                };
                return(ReturnResult(result, 0, string.Empty));
            }
            catch (Exception ex)
            {
                LogException(ex);
                return(ReturnResult(string.Empty, 1, ex.Message));
            }
        }
Beispiel #3
0
        public virtual ActionResult Edit(CustomerModel model, bool continueEditing, FormCollection form)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageCustomers))
            {
                return(AccessDeniedView());
            }

            var customer = _customerService.GetCustomerById(model.Id);

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

            //验证用户角色
            var allCustomerRoles = _customerService.GetAllCustomerRoles(true);
            var newCustomerRoles = new List <CustomerRole>();

            foreach (var customerRole in allCustomerRoles)
            {
                if (model.SelectedCustomerRoleIds.Contains(customerRole.Id))
                {
                    newCustomerRoles.Add(customerRole);
                }
            }
            var customerRolesError = ValidateCustomerRoles(newCustomerRoles);

            if (!string.IsNullOrEmpty(customerRolesError))
            {
                ModelState.AddModelError("", customerRolesError);
                ErrorNotification(customerRolesError, false);
            }

            //如果用户角色为"注册用户",需要确保输入的电子邮箱有效性
            if (newCustomerRoles.Any() && newCustomerRoles.FirstOrDefault(c => c.SystemName == SystemCustomerRoleNames.Registered) != null && !CommonHelper.IsValidEmail(model.Email))
            {
                ModelState.AddModelError("", _localizationService.GetResource("Admin.Customers.Customers.ValidEmailRequiredRegisteredRole"));
                ErrorNotification(_localizationService.GetResource("Admin.Customers.Customers.ValidEmailRequiredRegisteredRole"), false);
            }

            if (ModelState.IsValid)
            {
                try
                {
                    customer.AdminComment = model.AdminComment;

                    //阻止最后一个"管理员"角色用户被禁用
                    if (!customer.IsAdmin() || model.Active || SecondAdminAccountExists(customer))
                    {
                        customer.Active = model.Active;
                    }
                    else
                    {
                        ErrorNotification(_localizationService.GetResource("Admin.Customers.Customers.AdminAccountShouldExists.Deactivate"));
                    }

                    if (!string.IsNullOrWhiteSpace(model.Email))
                    {
                        _customerRegistrationService.SetEmail(customer, model.Email, false);
                    }
                    else
                    {
                        //游客用户...
                        customer.Email = model.Email;
                    }

                    if (!string.IsNullOrWhiteSpace(model.Username))
                    {
                        _customerRegistrationService.SetUsername(customer, model.Username);
                    }
                    else
                    {
                        //游客用户...
                        customer.Username = model.Username;
                    }

                    _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.Name, model.Name);
                    if (_dateTimeSettings.AllowCustomersToSetTimeZone)
                    {
                        _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.TimeZoneId, model.TimeZoneId);
                    }
                    if (_customerSettings.GenderEnabled)
                    {
                        _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.Gender, model.Gender);
                    }
                    if (_customerSettings.DateOfBirthEnabled)
                    {
                        _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.DateOfBirth, model.DateOfBirth);
                    }
                    if (_customerSettings.CompanyEnabled)
                    {
                        _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.Company, model.Company);
                    }
                    if (_customerSettings.PhoneEnabled)
                    {
                        _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.Phone, model.Phone);
                    }

                    foreach (var customerRole in allCustomerRoles)
                    {
                        if (customerRole.SystemName == SystemCustomerRoleNames.Administrators &&
                            !_workContext.CurrentCustomer.IsAdmin())
                        {
                            continue;
                        }

                        if (model.SelectedCustomerRoleIds.Contains(customerRole.Id))
                        {
                            //没有该角色身份时则添加
                            if (customer.CustomerRoles.Count(cr => cr.Id == customerRole.Id) == 0)
                            {
                                customer.CustomerRoles.Add(customerRole);
                            }
                        }
                        else
                        {
                            //确保当删除管理员角色身份时存在第二个管理员角色对象
                            if (customerRole.SystemName == SystemCustomerRoleNames.Administrators &&
                                !SecondAdminAccountExists(customer))
                            {
                                ErrorNotification(_localizationService.GetResource("Admin.Customers.Customers.AdminAccountShouldExists.DeleteRole"));
                                continue;
                            }

                            //如果存在该角色身份时则清除
                            if (customer.CustomerRoles.Count(cr => cr.Id == customerRole.Id) > 0)
                            {
                                customer.CustomerRoles.Remove(customerRole);
                            }
                        }
                    }
                    _customerService.UpdateCustomer(customer);

                    _customerActivityService.InsertActivity("EditCustomer", _localizationService.GetResource("ActivityLog.EditCustomer"), customer.Id);

                    SuccessNotification(_localizationService.GetResource("Admin.Customers.Customers.Updated"));

                    if (continueEditing)
                    {
                        SaveSelectedTabName();
                        return(RedirectToAction("Edit", new { id = customer.Id }));
                    }
                    return(RedirectToAction("List"));
                }
                catch (Exception exc)
                {
                    ErrorNotification(exc.Message, false);
                }
            }

            PrepareCustomerModel(model, customer, true);
            return(View(model));
        }
 /// <summary>
 /// Sets a customer username
 /// </summary>
 /// <param name="customer">Customer</param>
 /// <param name="newUsername">New Username</param>
 public void SetUsername(Customer customer, string newUsername)
 {
     _customerRegistrationService.SetUsername(customer, newUsername);
 }
Beispiel #5
0
        public ActionResult OverrideCustomerEdit(CustomerModel model, bool continueEditing, FormCollection form)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageCustomers))
            {
                return(AccessDeniedView());
            }

            var customer = _customerService.GetCustomerById(model.Id);

            if (customer == null || customer.Deleted)
            {
                //No customer found with the specified id
                return(Redirect("/Admin/Customer/List"));
            }

            //validate customer roles
            var allCustomerRoles = _customerService.GetAllCustomerRoles(true);
            var newCustomerRoles = new List <CustomerRole>();

            foreach (var customerRole in allCustomerRoles)
            {
                if (model.SelectedCustomerRoleIds != null && model.SelectedCustomerRoleIds.Contains(customerRole.Id))
                {
                    newCustomerRoles.Add(customerRole);
                }
            }
            var customerRolesError = ValidateCustomerRoles(newCustomerRoles);

            if (!String.IsNullOrEmpty(customerRolesError))
            {
                ModelState.AddModelError("", customerRolesError);
                ErrorNotification(customerRolesError, false);
            }

            if (ModelState.IsValid)
            {
                try
                {
                    customer.AdminComment = model.AdminComment;
                    customer.IsTaxExempt  = model.IsTaxExempt;
                    customer.Active       = model.Active;
                    //email
                    if (!String.IsNullOrWhiteSpace(model.Email))
                    {
                        _customerRegistrationService.SetEmail(customer, model.Email);
                    }
                    else
                    {
                        customer.Email = model.Email;
                    }

                    //username
                    if (_customerSettings.UsernamesEnabled && _customerSettings.AllowUsersToChangeUsernames)
                    {
                        if (!String.IsNullOrWhiteSpace(model.Username))
                        {
                            _customerRegistrationService.SetUsername(customer, model.Username);
                        }
                        else
                        {
                            customer.Username = model.Username;
                        }
                    }

                    //VAT number
                    if (_taxSettings.EuVatEnabled)
                    {
                        var prevVatNumber = customer.GetAttribute <string>(SystemCustomerAttributeNames.VatNumber);

                        _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.VatNumber, model.VatNumber);
                        //set VAT number status
                        if (!String.IsNullOrEmpty(model.VatNumber))
                        {
                            if (!model.VatNumber.Equals(prevVatNumber, StringComparison.InvariantCultureIgnoreCase))
                            {
                                _genericAttributeService.SaveAttribute(customer,
                                                                       SystemCustomerAttributeNames.VatNumberStatusId,
                                                                       (int)_taxService.GetVatNumberStatus(model.VatNumber));
                            }
                        }
                        else
                        {
                            _genericAttributeService.SaveAttribute(customer,
                                                                   SystemCustomerAttributeNames.VatNumberStatusId,
                                                                   (int)VatNumberStatus.Empty);
                        }
                    }

                    //vendor
                    customer.VendorId = model.VendorId;

                    //form fields
                    if (_dateTimeSettings.AllowCustomersToSetTimeZone)
                    {
                        _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.TimeZoneId, model.TimeZoneId);
                    }
                    if (_customerSettings.GenderEnabled)
                    {
                        _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.Gender, model.Gender);
                    }
                    _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.FirstName, model.FirstName);
                    _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.LastName, model.LastName);
                    if (_customerSettings.DateOfBirthEnabled)
                    {
                        _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.DateOfBirth, model.DateOfBirth);
                    }
                    if (_customerSettings.CompanyEnabled)
                    {
                        _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.Company, model.Company);
                    }
                    if (_customerSettings.StreetAddressEnabled)
                    {
                        _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.StreetAddress, model.StreetAddress);
                    }
                    if (_customerSettings.StreetAddress2Enabled)
                    {
                        _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.StreetAddress2, model.StreetAddress2);
                    }
                    if (_customerSettings.ZipPostalCodeEnabled)
                    {
                        _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.ZipPostalCode, model.ZipPostalCode);
                    }
                    if (_customerSettings.CityEnabled)
                    {
                        _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.City, model.City);
                    }
                    if (_customerSettings.CountryEnabled)
                    {
                        _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.CountryId, model.CountryId);
                    }
                    if (_customerSettings.CountryEnabled && _customerSettings.StateProvinceEnabled)
                    {
                        _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.StateProvinceId, model.StateProvinceId);
                    }
                    if (_customerSettings.PhoneEnabled)
                    {
                        _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.Phone, model.Phone);
                    }
                    if (_customerSettings.FaxEnabled)
                    {
                        _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.Fax, model.Fax);
                    }

                    //custom customer attributes
                    var customerAttributes = ParseCustomCustomerAttributes(customer, form);
                    _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.CustomCustomerAttributes, customerAttributes);


                    //customer roles
                    foreach (var customerRole in allCustomerRoles)
                    {
                        //ensure that the current customer cannot add/remove to/from "Administrators" system role
                        //if he's not an admin himself
                        if (customerRole.SystemName == SystemCustomerRoleNames.Administrators &&
                            !_workContext.CurrentCustomer.IsAdmin())
                        {
                            continue;
                        }

                        if (model.SelectedCustomerRoleIds != null &&
                            model.SelectedCustomerRoleIds.Contains(customerRole.Id))
                        {
                            //new role
                            if (customer.CustomerRoles.Count(cr => cr.Id == customerRole.Id) == 0)
                            {
                                customer.CustomerRoles.Add(customerRole);
                            }
                        }
                        else
                        {
                            //remove role
                            if (customer.CustomerRoles.Count(cr => cr.Id == customerRole.Id) > 0)
                            {
                                customer.CustomerRoles.Remove(customerRole);
                            }
                        }
                    }
                    _customerService.UpdateCustomer(customer);


                    //ensure that a customer with a vendor associated is not in "Administrators" role
                    //otherwise, he won't have access to the other functionality in admin area
                    if (customer.IsAdmin() && customer.VendorId > 0)
                    {
                        customer.VendorId = 0;
                        _customerService.UpdateCustomer(customer);
                        ErrorNotification(_localizationService.GetResource("Admin.Customers.Customers.AdminCouldNotbeVendor"));
                    }

                    //ensure that a customer in the Vendors role has a vendor account associated.
                    //otherwise, he will have access to ALL products
                    if (customer.IsVendor() && customer.VendorId == 0)
                    {
                        var vendorRole = customer
                                         .CustomerRoles
                                         .FirstOrDefault(x => x.SystemName == SystemCustomerRoleNames.Vendors);
                        customer.CustomerRoles.Remove(vendorRole);
                        _customerService.UpdateCustomer(customer);
                        ErrorNotification(_localizationService.GetResource("Admin.Customers.Customers.CannotBeInVendoRoleWithoutVendorAssociated"));
                    }


                    //activity log
                    _customerActivityService.InsertActivity("EditCustomer", _localizationService.GetResource("ActivityLog.EditCustomer"), customer.Id);

                    SuccessNotification(_localizationService.GetResource("Admin.Customers.Customers.Updated"));
                    if (continueEditing)
                    {
                        //selected tab
                        SaveSelectedTabIndex();

                        return(RedirectToAction("OverrideCustomerEdit", new { id = customer.Id }));
                    }
                    return(Redirect("/Admin/Customer/List"));
                }
                catch (Exception exc)
                {
                    ErrorNotification(exc.Message, false);
                }
            }


            //If we got this far, something failed, redisplay form
            PrepareCustomerModel(model, customer, true);
            //return View("", model);
            var customerStateModel = (CustomerStateModel)model;

            PrepareCustomerStateModel(customerStateModel);
            return(View("~/Plugins/Misc.UserStateManagement/Views/UserStateManagement/Edit.cshtml", customerStateModel));
        }