Beispiel #1
0
        /// <summary>
        /// Prepare user attribute model
        /// </summary>
        /// <param name="model">User attribute model</param>
        /// <param name="userAttribute">User attribute</param>
        /// <param name="excludeProperties">Whether to exclude populating of some properties of model</param>
        /// <returns>User attribute model</returns>
        public virtual UserAttributeModel PrepareUserAttributeModel(UserAttributeModel model,
                                                                    UserAttribute userAttribute, bool excludeProperties = false)
        {
            Action <UserAttributeLocalizedModel, int> localizedModelConfiguration = null;

            if (userAttribute != null)
            {
                //fill in model values from the entity
                model = model ?? userAttribute.ToModel <UserAttributeModel>();

                //prepare nested search model
                PrepareUserAttributeValueSearchModel(model.UserAttributeValueSearchModel, userAttribute);

                //define localized model configuration action
                localizedModelConfiguration = (locale, languageId) =>
                {
                    locale.Name = _localizationService.GetLocalized(userAttribute, entity => entity.Name, languageId, false, false);
                };
            }

            //prepare localized models
            if (!excludeProperties)
            {
                model.Locales = _localizedModelFactory.PrepareLocalizedModels(localizedModelConfiguration);
            }

            return(model);
        }
 protected virtual void UpdateAttributeLocales(UserAttribute userAttribute, UserAttributeModel model)
 {
     foreach (var localized in model.Locales)
     {
         _localizedEntityService.SaveLocalizedValue(userAttribute,
                                                    x => x.Name,
                                                    localized.Name,
                                                    localized.LanguageId);
     }
 }
Beispiel #3
0
        /// <summary>
        /// Prepare the custom user attribute models
        /// </summary>
        /// <param name="user">User</param>
        /// <param name="overrideAttributesXml">Overridden user attributes in XML format; pass null to use CustomUserAttributes of user</param>
        /// <returns>List of the user attribute model</returns>
        public virtual IList <UserAttributeModel> PrepareCustomUserAttributes(User user, string overrideAttributesXml = "")
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            var result = new List <UserAttributeModel>();

            var userAttributes = _userAttributeService.GetAllUserAttributes();

            foreach (var attribute in userAttributes)
            {
                var attributeModel = new UserAttributeModel
                {
                    Id         = attribute.Id,
                    Name       = _localizationService.GetLocalized(attribute, x => x.Name),
                    IsRequired = attribute.IsRequired,
                };

                if (attribute.ShouldHaveValues())
                {
                    //values
                    var attributeValues = _userAttributeService.GetUserAttributeValues(attribute.Id);
                    foreach (var attributeValue in attributeValues)
                    {
                        var valueModel = new UserAttributeValueModel
                        {
                            Id            = attributeValue.Id,
                            Name          = _localizationService.GetLocalized(attributeValue, x => x.Name),
                            IsPreSelected = attributeValue.IsPreSelected
                        };
                        attributeModel.Values.Add(valueModel);
                    }
                }

                //set already selected attributes
                var selectedAttributesXml = !string.IsNullOrEmpty(overrideAttributesXml) ?
                                            overrideAttributesXml :
                                            _genericAttributeService.GetAttribute <string>(user, NopUserDefaults.CustomUserAttributes);

                result.Add(attributeModel);
            }

            return(result);
        }
        public virtual IActionResult Edit(UserAttributeModel model, bool continueEditing)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageSettings))
            {
                return(AccessDeniedView());
            }

            var userAttribute = _userAttributeService.GetUserAttributeById(model.Id);

            if (userAttribute == null)
            {
                //no user attribute found with the specified id
                return(RedirectToAction("List"));
            }

            if (!ModelState.IsValid)
            {
                //if we got this far, something failed, redisplay form
                return(View(model));
            }

            userAttribute = model.ToEntity(userAttribute);
            _userAttributeService.UpdateUserAttribute(userAttribute);

            //activity log
            _userActivityService.InsertActivity("EditUserAttribute",
                                                string.Format(_localizationService.GetResource("ActivityLog.EditUserAttribute"), userAttribute.Id),
                                                userAttribute);

            //locales
            UpdateAttributeLocales(userAttribute, model);

            SuccessNotification(_localizationService.GetResource("Admin.Users.UserAttributes.Updated"));

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

            //selected tab
            SaveSelectedTabName();

            return(RedirectToAction("Edit", new { id = userAttribute.Id }));
        }
        public virtual IActionResult Create(UserAttributeModel model, bool continueEditing)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageSettings))
            {
                return(AccessDeniedView());
            }

            if (ModelState.IsValid)
            {
                var userAttribute = model.ToEntity <UserAttribute>();
                _userAttributeService.InsertUserAttribute(userAttribute);

                //activity log
                _userActivityService.InsertActivity("AddNewUserAttribute",
                                                    string.Format(_localizationService.GetResource("ActivityLog.AddNewUserAttribute"), userAttribute.Id),
                                                    userAttribute);

                //locales
                UpdateAttributeLocales(userAttribute, model);

                SuccessNotification(_localizationService.GetResource("Admin.Users.UserAttributes.Added"));

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

                //selected tab
                SaveSelectedTabName();

                return(RedirectToAction("Edit", new { id = userAttribute.Id }));
            }

            //prepare model
            model = _userAttributeModelFactory.PrepareUserAttributeModel(model, null, true);

            //if we got this far, something failed, redisplay form
            return(View(model));
        }
        /// <summary>
        /// Prepare the custom customer attribute models
        /// </summary>
        /// <param name="customer">Customer</param>
        /// <param name="overrideAttributesXml">Overridden customer attributes in XML format; pass null to use CustomCustomerAttributes of customer</param>
        /// <returns>List of the customer attribute model</returns>
        public virtual IList <UserAttributeModel> PrepareCustomCustomerAttributes(User customer, string overrideAttributesXml = "")
        {
            if (customer == null)
            {
                throw new ArgumentNullException("customer");
            }

            var result = new List <UserAttributeModel>();

            var customerAttributes = _customerAttributeService.GetAllUserAttributes();

            foreach (var attribute in customerAttributes)
            {
                var attributeModel = new UserAttributeModel
                {
                    Id                   = attribute.Id,
                    Name                 = attribute.GetLocalized(x => x.Name),
                    IsRequired           = attribute.IsRequired,
                    AttributeControlType = attribute.AttributeControlType,
                };

                if (attribute.ShouldHaveValues())
                {
                    //values
                    var attributeValues = _customerAttributeService.GetUserAttributeValues(attribute.Id);
                    foreach (var attributeValue in attributeValues)
                    {
                        var valueModel = new CustomerAttributeValueModel
                        {
                            Id            = attributeValue.Id,
                            Name          = attributeValue.GetLocalized(x => x.Name),
                            IsPreSelected = attributeValue.IsPreSelected
                        };
                        attributeModel.Values.Add(valueModel);
                    }
                }

                //set already selected attributes
                var selectedAttributesXml = !String.IsNullOrEmpty(overrideAttributesXml) ?
                                            overrideAttributesXml :
                                            customer.GetAttribute <string>(SystemUserAttributeNames.CustomUserAttributes, _genericAttributeService);
                switch (attribute.AttributeControlType)
                {
                case AttributeControlType.DropdownList:
                case AttributeControlType.RadioList:
                case AttributeControlType.Checkboxes:
                {
                    if (!String.IsNullOrEmpty(selectedAttributesXml))
                    {
                        //clear default selection
                        foreach (var item in attributeModel.Values)
                        {
                            item.IsPreSelected = false;
                        }

                        //select new values
                        var selectedValues = _customerAttributeParser.ParseUserAttributeValues(selectedAttributesXml);
                        foreach (var attributeValue in selectedValues)
                        {
                            foreach (var item in attributeModel.Values)
                            {
                                if (attributeValue.Id == item.Id)
                                {
                                    item.IsPreSelected = true;
                                }
                            }
                        }
                    }
                }
                break;

                case AttributeControlType.ReadonlyCheckboxes:
                {
                    //do nothing
                    //values are already pre-set
                }
                break;

                case AttributeControlType.TextBox:
                case AttributeControlType.MultilineTextbox:
                {
                    if (!String.IsNullOrEmpty(selectedAttributesXml))
                    {
                        var enteredText = _customerAttributeParser.ParseValues(selectedAttributesXml, attribute.Id);
                        if (enteredText.Any())
                        {
                            attributeModel.DefaultValue = enteredText[0];
                        }
                    }
                }
                break;

                case AttributeControlType.ColorSquares:
                case AttributeControlType.ImageSquares:
                case AttributeControlType.Datepicker:
                case AttributeControlType.FileUpload:
                default:
                    //not supported attribute control types
                    break;
                }

                result.Add(attributeModel);
            }


            return(result);
        }
        public async Task <UserAttributeModel> GetUserAttribute()
        {
            try
            {
                var client = GraphClientUtility.GetGraphServiceClient();

                if (client == null)
                {
                    _logger.LogError("UserAttributeService-GetUserAttribute: Unable to create proxy for the Azure AD B2C graph client");
                    return(null);
                }
                _logger.LogInfo("UserAttributeService-GetUserAttribute: [Started] to fetch user attribute in Azure AD B2C");

                var extensionName = GraphClientUtility.ExtensionName;

                var schemaExtensions = await client.SchemaExtensions.Request().GetAsync();


                var userAttributeModel = new UserAttributeModel();
                var extensionSchemas   = new List <ExtensionSchema>();
                while (schemaExtensions.NextPageRequest != null)
                {
                    foreach (SchemaExtension extension in schemaExtensions.CurrentPage)
                    {
                        try
                        {
                            if (extension.Id.Contains(extensionName))
                            {
                                userAttributeModel.Id = extension.Id;

                                foreach (var item in extension.Properties)
                                {
                                    try
                                    {
                                        ExtensionSchema extensionSchema = new ExtensionSchema();

                                        extensionSchema.Name     = item.Name;
                                        extensionSchema.DataType = item.Type;
                                        extensionSchemas.Add(extensionSchema);
                                    }
                                    catch (Exception ex)
                                    {
                                        _logger.LogError($"ExtensionController-GetExtensions: fail to add extension [name:{item.Name}] to collection ");
                                        _logger.LogError(ex);
                                    }
                                }
                                userAttributeModel.ExtensionSchemas = extensionSchemas;
                            }
                        }
                        catch (Exception ex)
                        {
                            _logger.LogError($"ExtensionController-GetExtensions: fail to add extension [name:{extension.Id}] to collection ");
                            _logger.LogError(ex);
                        }
                    }


                    schemaExtensions = await schemaExtensions.NextPageRequest.GetAsync();
                }

                return(userAttributeModel);
            }
            catch (Exception ex)
            {
                _logger.LogError("ExtensionController-GetExtensions: Exception occured....");
                _logger.LogError(ex);
            }
            return(null);
        }