Esempio n. 1
0
        public virtual async Task <List <SettingValue> > GetAllAsync()
        {
            var settingValues      = new Dictionary <string, SettingValue>();
            var settingDefinitions = SettingDefinitionManager.GetAll();

            foreach (var provider in SettingValueProviderManager.Providers)
            {
                foreach (var setting in settingDefinitions)
                {
                    var value = await provider.GetOrNullAsync(setting);

                    if (value != null)
                    {
                        if (setting.IsEncrypted)
                        {
                            value = SettingEncryptionService.Decrypt(setting, value);
                        }

                        settingValues[setting.Name] = new SettingValue(setting.Name, value);
                    }
                }
            }

            return(settingValues.Values.ToList());
        }
Esempio n. 2
0
        public virtual async Task <List <SettingValue> > GetAllAsync(string[] names)
        {
            var result             = new Dictionary <string, SettingValue>();
            var settingDefinitions = SettingDefinitionManager.GetAll().Where(x => names.Contains(x.Name)).ToList();

            foreach (var definition in settingDefinitions)
            {
                result.Add(definition.Name, new SettingValue(definition.Name, null));
            }

            foreach (var provider in Enumerable.Reverse(SettingValueProviderManager.Providers))
            {
                var settingValues = await provider.GetAllAsync(settingDefinitions.Where(x => !x.Providers.Any() || x.Providers.Contains(provider.Name)).ToArray());

                var notNullValues = settingValues.Where(x => x.Value != null).ToList();
                foreach (var settingValue in notNullValues)
                {
                    var settingDefinition = settingDefinitions.First(x => x.Name == settingValue.Name);
                    if (settingDefinition.IsEncrypted)
                    {
                        settingValue.Value = SettingEncryptionService.Decrypt(settingDefinition, settingValue.Value);
                    }

                    if (result.ContainsKey(settingValue.Name) && result[settingValue.Name].Value == null)
                    {
                        result[settingValue.Name].Value = settingValue.Value;
                    }
                }

                settingDefinitions.RemoveAll(x => notNullValues.Any(v => v.Name == x.Name));
            }

            return(result.Values.ToList());
        }
Esempio n. 3
0
        public virtual async Task <List <SettingValue> > GetAllAsync(string providerName, string providerKey, bool fallback = true)
        {
            Check.NotNull(providerName, nameof(providerName));

            var settingDefinitions = SettingDefinitionManager.GetAll();
            var providers          = Enumerable.Reverse(Providers.Value)
                                     .SkipWhile(c => c.Name != providerName);

            if (!fallback)
            {
                providers = providers.TakeWhile(c => c.Name == providerName);
            }

            var providerList = providers.Reverse().ToList();

            if (!providerList.Any())
            {
                return(new List <SettingValue>());
            }

            var settingValues = new Dictionary <string, SettingValue>();

            foreach (var setting in settingDefinitions)
            {
                string value = null;

                if (setting.IsInherited)
                {
                    foreach (var provider in providerList)
                    {
                        var providerValue = await provider.GetOrNullAsync(setting, providerKey);

                        if (providerValue != null)
                        {
                            value = providerValue;
                        }
                    }
                }
                else
                {
                    value = await providerList[0].GetOrNullAsync(setting, providerKey);
                }

                if (setting.IsEncrypted)
                {
                    value = SettingEncryptionService.Decrypt(setting, value);
                }

                if (value != null)
                {
                    settingValues[setting.Name] = new SettingValue(setting.Name, value);
                }
            }

            return(settingValues.Values.ToList());
        }
Esempio n. 4
0
        public virtual async Task SetAsync(string name, string value, string providerName, string providerKey, bool forceToSet = false)
        {
            Check.NotNull(name, nameof(name));
            Check.NotNull(providerName, nameof(providerName));

            var setting = SettingDefinitionManager.Get(name);

            var providers = Enumerable
                            .Reverse(Providers.Value)
                            .SkipWhile(p => p.Name != providerName)
                            .ToList();

            if (!providers.Any())
            {
                return;
            }

            if (setting.IsEncrypted)
            {
                value = SettingEncryptionService.Encrypt(setting, value);
            }

            if (providers.Count > 1 && !forceToSet && setting.IsInherited && value != null)
            {
                //Clear the value if it's same as it's fallback value
                var fallbackValue = await GetOrNullInternalAsync(name, providers[1].Name, providerKey);

                if (fallbackValue == value)
                {
                    value = null;
                }
            }

            providers = providers
                        .TakeWhile(p => p.Name == providerName)
                        .ToList(); //Getting list for case of there are more than one provider with same EntityType

            if (value == null)
            {
                foreach (var provider in providers)
                {
                    await provider.ClearAsync(setting, providerKey);
                }
            }
            else
            {
                foreach (var provider in providers)
                {
                    await provider.SetAsync(setting, value, providerKey);
                }
            }
        }
Esempio n. 5
0
        public virtual async Task <string> GetOrNullAsync(string name)
        {
            var setting   = SettingDefinitionManager.Get(name);
            var providers = Enumerable
                            .Reverse(Providers.Value);

            if (setting.Providers.Any())
            {
                providers = providers.Where(p => setting.Providers.Contains(p.Name));
            }

            //TODO: How to implement setting.IsInherited?

            var value = await GetOrNullValueFromProvidersAsync(providers, setting);

            if (setting.IsEncrypted)
            {
                value = SettingEncryptionService.Decrypt(setting, value);
            }

            return(value);
        }