예제 #1
0
        /// <summary>
        /// Load the Preflight settings from the JSON file in app_plugins
        /// </summary>
        public PreflightSettings Get()
        {
            MemoryCache cache     = MemoryCache.Default;
            object      fromCache = cache.Get(KnownStrings.SettingsCacheKey);

            if (fromCache != null)
            {
                return(fromCache as PreflightSettings);
            }

            // only get here when nothing is cached
            List <SettingsModel> settings;

            // json initially stores the core checks only
            // once it has been saved in the backoffice, settings store all current plugins, with alias
            using (var file = new StreamReader(KnownStrings.SettingsFilePath))
            {
                string json = file.ReadToEnd();
                settings = JsonConvert.DeserializeObject <List <SettingsModel> >(json);
            }

            // add tabs for core items
            List <SettingsTab> tabs = new List <SettingsTab>();

            foreach (SettingsModel s in settings)
            {
                if (!s.Alias.HasValue())
                {
                    s.Alias = s.Label.Camel();
                }

                tabs.Add(new SettingsTab(s.Tab));
            }

            // get any plugins and add their settings
            // once settings have been saved from the backoffice, need to check that plugins aren't added twice
            var pluginProvider = new PluginProvider();

            foreach (IPreflightPlugin plugin in pluginProvider.Get())
            {
                foreach (SettingsModel setting in plugin.Settings)
                {
                    setting.Tab = plugin.Name;
                    settings.Add(setting);
                }

                // generate a tab from the setting - this list is filtered later
                // send back the summary and description for the plugin as part of the tab object for display in the settings view
                var pluginTab = new SettingsTab(plugin.Name)
                {
                    Summary     = plugin.Summary,
                    Description = plugin.Description
                };

                tabs.Add(pluginTab);
            }

            // tabs are sorted alpha, with general first
            var response = new PreflightSettings
            {
                Settings = settings.DistinctBy(s => new { s.Tab, s.Label }).ToList(),
                Tabs     = tabs.GroupBy(x => x.Alias)
                           .Select(y => y.First())
                           .OrderBy(i => i.Name != SettingsTabNames.General)
                           .ThenBy(i => i.Name).ToList()
            };

            // if we are here, cache should be set
            cache.Set(KnownStrings.SettingsCacheKey, response, DateTimeOffset.UtcNow.AddMinutes(120));

            return(response);
        }
예제 #2
0
        private PreflightSettings GetSettings()
        {
            // only get here when nothing is cached
            List <SettingsModel> settings;

            // json initially stores the core checks only
            // once it has been saved in the backoffice, settings store all current plugins, with alias
            using (var file = new StreamReader(KnownStrings.SettingsFilePath))
            {
                string json = file.ReadToEnd();
                settings = JsonConvert.DeserializeObject <List <SettingsModel> >(json);
            }

            // populate prevalues for the groups setting
            // intersect ensures any removed groups aren't kept as settings values
            // since group name and alias can differ, needs to store both in the prevalue, and manage rebuilding this on the client side
            var allGroups    = _userService.GetAllUserGroups();
            var groupSetting = settings.FirstOrDefault(x => string.Equals(x.Label, KnownSettings.UserGroupOptIn, StringComparison.InvariantCultureIgnoreCase));

            if (groupSetting != null)
            {
                var groupNames = allGroups.Select(x => new { value = x.Name, key = x.Alias });
                groupSetting.Prevalues = groupNames;

                if (groupSetting.Value.HasValue())
                {
                    var groupSettingValue = groupSetting.Value.Split(',').Intersect(groupNames.Select(x => x.value));
                    groupSetting.Value = string.Join(",", groupSettingValue);
                }
            }

            // populate prevlaues for subsetting testable properties, default value to all if nothing exists
            var testablePropertiesProp = settings.FirstOrDefault(x => string.Equals(x.Label, KnownSettings.PropertiesToTest, StringComparison.InvariantCultureIgnoreCase));

            if (testablePropertiesProp != null)
            {
                testablePropertiesProp.Prevalues = KnownPropertyAlias.All.Select(x => new { value = x, key = x });

                if (!testablePropertiesProp.Value.HasValue())
                {
                    testablePropertiesProp.Value = string.Join(",", KnownPropertyAlias.All);
                }
            }

            // add tabs for core items
            List <SettingsTab> tabs = new List <SettingsTab>();

            // get any plugins and add their settings
            // once settings have been saved from the backoffice, need to check that plugins aren't added twice
            var pluginProvider = new PluginProvider();
            var plugins        = pluginProvider.Get();

            foreach (IPreflightPlugin plugin in plugins)
            {
                foreach (SettingsModel setting in plugin.Settings)
                {
                    if (!settings.Any(x => x.Alias == setting.Alias))
                    {
                        setting.Tab = plugin.Name;
                        settings.Add(setting);
                    }
                }

                // generate a tab from the plugin if not added already
                // send back the summary and description for the plugin as part of the tab object for display in the settings view
                tabs.Add(new SettingsTab
                {
                    Name        = plugin.Name,
                    Description = plugin.Description,
                    Summary     = plugin.Summary
                });
            }

            foreach (SettingsModel s in settings)
            {
                if (!s.Alias.HasValue())
                {
                    s.Alias = s.Label.Camel();
                }

                if (!tabs.Any(x => x.Name == s.Tab))
                {
                    tabs.Add(new SettingsTab
                    {
                        Name = s.Tab
                    });
                }
            }

            // tabs are sorted alpha, with general first
            return(new PreflightSettings
            {
                Settings = settings.DistinctBy(s => (s.Tab, s.Label)).ToList(),
                Tabs = tabs.GroupBy(x => x.Name)
                       .Select(y => y.First())
                       .OrderBy(i => i.Name != SettingsTabNames.General)
                       .ThenBy(i => i.Name).ToList()
            });
예제 #3
0
        /// <summary>
        /// Runs the set of plugins against the given string
        /// </summary>
        /// <param name="name"></param>
        /// <param name="val"></param>
        /// <returns></returns>
        private PreflightPropertyResponseModel RunPluginsAgainstValue(string name, string val)
        {
            var model = new PreflightPropertyResponseModel
            {
                Label = name,
                Name  = name
            };

            if (val == null)
            {
                return(model);
            }

            var pluginProvider = new PluginProvider();

            foreach (IPreflightPlugin plugin in pluginProvider.Get())
            {
                // settings on the plugin are the defaults - set to correct values from _settings
                IEnumerable <SettingsModel> pluginSettings = _settings.Where(s => s.Tab == plugin.Name).ToList();
                plugin.Settings = pluginSettings;

                // ignore disabled plugins
                if (plugin.IsDisabled())
                {
                    continue;
                }
                if (!_fromSave && plugin.IsOnSaveOnly())
                {
                    continue;
                }

                try
                {
                    Type pluginType = plugin.GetType();
                    if (pluginType.GetMethod("Check") == null)
                    {
                        continue;
                    }

                    plugin.Check(_id, val, _settings);

                    if (plugin.Result != null)
                    {
                        if (plugin.FailedCount == 0)
                        {
                            plugin.FailedCount = plugin.Failed ? 1 : 0;
                        }

                        model.Plugins.Add(plugin);
                    }
                }
                catch (Exception e)
                {
                    // todo => log
                    string m = e.Message;
                }
            }

            // mark as failed if any sub-tests have failed
            model.FailedCount = model.Plugins.Sum(x => x.FailedCount);
            model.Failed      = model.FailedCount > 0;

            model.Plugins = model.Plugins.OrderBy(p => p.SortOrder).ToList();

            return(model);
        }