public static object ToJsonModelRef(Site site, string path)
        {
            var hasDynamic = ManagementUnit.ServerManager.GetApplicationHostConfiguration().HasSection(IPRestrictionsGlobals.DynamicIPSecuritySectionName);

            var section = GetSection(site, path);
            DynamicIPSecuritySection dynamicSection = null;

            if (hasDynamic)
            {
                dynamicSection = GetDynamicSecuritySection(site, path);
            }

            bool isLocal;

            if (hasDynamic)
            {
                isLocal = section.IsLocallyStored || dynamicSection.IsLocallyStored;
            }
            else
            {
                isLocal = section.IsLocallyStored;
            }

            // Construct id passing possible site and application associated
            IPRestrictionId ipId = new IPRestrictionId(site?.Id, path, isLocal);

            var obj = new {
                id    = ipId.Uuid,
                scope = site == null ? string.Empty : site.Name + path
            };

            return(Core.Environment.Hal.Apply(Defines.Resource.Guid, obj, false));
        }
        internal static object ToJsonModel(Site site, string path)
        {
            // Dynamic ip security section added in iis 8.0
            var hasDynamic = ManagementUnit.ServerManager.GetApplicationHostConfiguration().HasSection(IPRestrictionsGlobals.DynamicIPSecuritySectionName);

            var section = GetSection(site, path);
            DynamicIPSecuritySection dynamicSection = null;

            bool isLocal;
            bool isLocked;
            bool isEnabled;

            OverrideMode overrideMode;
            OverrideMode overrideModeEffective;

            isLocal   = section.IsLocallyStored;
            isLocked  = section.IsLocked;
            isEnabled = section.IpAddressFilters.Count != 0 ||
                        section.EnableReverseDns ||
                        !section.AllowUnlisted;

            if (section.Schema.HasAttribute(EnableProxyModeAttribute))
            {
                isEnabled = isEnabled || section.EnableProxyMode;
            }

            overrideMode          = section.OverrideMode;
            overrideModeEffective = section.OverrideModeEffective;


            if (hasDynamic)
            {
                dynamicSection = GetDynamicSecuritySection(site, path);

                isLocal   = isLocal || dynamicSection.IsLocallyStored;
                isLocked  = isLocked || dynamicSection.IsLocked;
                isEnabled = isEnabled || dynamicSection.DenyByConcurrentRequests.Enabled ||
                            dynamicSection.DenyByRequestRate.Enabled;

                overrideMode          = section.OverrideMode != dynamicSection.OverrideMode ? OverrideMode.Unknown : section.OverrideMode;
                overrideModeEffective = section.OverrideModeEffective != dynamicSection.OverrideModeEffective ? OverrideMode.Unknown : section.OverrideModeEffective;
            }

            // Construct id passing possible site and application associated
            IPRestrictionId ipId = new IPRestrictionId(site?.Id, path, isLocal);

            dynamic obj = new ExpandoObject();

            obj.id                 = ipId.Uuid;
            obj.scope              = site == null ? string.Empty : site.Name + path;
            obj.enabled            = isEnabled;
            obj.metadata           = ConfigurationUtility.MetadataToJson(isLocal, isLocked, overrideMode, overrideModeEffective);
            obj.allow_unlisted     = section.AllowUnlisted;
            obj.enable_reverse_dns = section.EnableReverseDns;

            if (section.Schema.HasAttribute(EnableProxyModeAttribute))
            {
                obj.enable_proxy_mode = section.EnableProxyMode;
            }

            if (section.Schema.HasAttribute(DenyActionAttribute))
            {
                obj.deny_action = Enum.GetName(typeof(DenyActionType), section.DenyAction);
            }

            if (hasDynamic)
            {
                obj.deny_by_concurrent_requests = new
                {
                    enabled = dynamicSection.DenyByConcurrentRequests.Enabled,
                    max_concurrent_requests = dynamicSection.DenyByConcurrentRequests.MaxConcurrentRequests
                };
                obj.deny_by_request_rate = new
                {
                    enabled      = dynamicSection.DenyByRequestRate.Enabled,
                    max_requests = dynamicSection.DenyByRequestRate.MaxRequests,
                    time_period  = dynamicSection.DenyByRequestRate.TimePeriod
                };
                obj.logging_only_mode = dynamicSection.LoggingOnlyMode;
            }
            obj.website = SiteHelper.ToJsonModelRef(site);


            return(Core.Environment.Hal.Apply(Defines.Resource.Guid, obj));
        }
        public static void SetFeatureSettings(dynamic model, Site site, string path, string configPath = null)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

            var hasDynamic = ManagementUnit.ServerManager.GetApplicationHostConfiguration().HasSection(IPRestrictionsGlobals.DynamicIPSecuritySectionName);

            var mainSection = GetSection(site, path, configPath);
            DynamicIPSecuritySection dynamicSection = null;

            if (hasDynamic)
            {
                dynamicSection = GetDynamicSecuritySection(site, path, configPath);
            }

            try {
                DynamicHelper.If <bool>((object)model.allow_unlisted, v => mainSection.AllowUnlisted        = v);
                DynamicHelper.If <bool>((object)model.enable_reverse_dns, v => mainSection.EnableReverseDns = v);

                if (mainSection.Schema.HasAttribute(EnableProxyModeAttribute))
                {
                    DynamicHelper.If <bool>((object)model.enable_proxy_mode, v => mainSection.EnableProxyMode = v);
                }
                if (mainSection.Schema.HasAttribute(DenyActionAttribute))
                {
                    DynamicHelper.If <DenyActionType>((object)model.deny_action, v => mainSection.DenyAction = v);
                }

                if (hasDynamic)
                {
                    DynamicHelper.If <bool>((object)model.logging_only_mode, v => dynamicSection.LoggingOnlyMode = v);

                    // Concurrent request restrictions
                    if (model.deny_by_concurrent_requests != null)
                    {
                        if (!(model.deny_by_concurrent_requests is JObject))
                        {
                            throw new ApiArgumentException("deny_by_concurrent_requests");
                        }

                        dynamic denyConcurrent = model.deny_by_concurrent_requests;

                        DynamicHelper.If <bool>((object)denyConcurrent.enabled, v => dynamicSection.DenyByConcurrentRequests.Enabled = v);
                        DynamicHelper.If((object)denyConcurrent.max_concurrent_requests, 1, 4294967295, v => dynamicSection.DenyByConcurrentRequests.MaxConcurrentRequests = v);
                    }

                    // Request rate restrictions
                    if (model.deny_by_request_rate != null)
                    {
                        if (!(model.deny_by_request_rate is JObject))
                        {
                            throw new ApiArgumentException("deny_by_request_rate");
                        }

                        dynamic denyRequestRate = model.deny_by_request_rate;

                        DynamicHelper.If <bool>((object)denyRequestRate.enabled, v => dynamicSection.DenyByRequestRate.Enabled = v);
                        DynamicHelper.If((object)denyRequestRate.max_requests, 1, 4294967295, v => dynamicSection.DenyByRequestRate.MaxRequests = v);
                        DynamicHelper.If((object)denyRequestRate.time_period, 1, 4294967295, v => dynamicSection.DenyByRequestRate.TimePeriod   = v);
                    }
                }

                // Enabled
                DynamicHelper.If <bool>((object)model.enabled, v => {
                    if (!v)
                    {
                        mainSection.AllowUnlisted    = true;
                        mainSection.EnableReverseDns = false;

                        if (mainSection.Schema.HasAttribute(EnableProxyModeAttribute))
                        {
                            mainSection.EnableProxyMode = false;
                        }

                        if (hasDynamic)
                        {
                            dynamicSection.DenyByConcurrentRequests.Enabled = false;
                            dynamicSection.DenyByRequestRate.Enabled        = false;
                        }

                        mainSection.IpAddressFilters.Clear();
                    }
                });

                if (model.metadata != null)
                {
                    DynamicHelper.If <OverrideMode>((object)model.metadata.override_mode, v => {
                        mainSection.OverrideMode = v;

                        if (hasDynamic)
                        {
                            dynamicSection.OverrideMode = v;
                        }
                    });
                }
            }
            catch (FileLoadException e) {
                throw new LockedException(mainSection.SectionPath + "|" + dynamicSection.SectionPath, e);
            }
            catch (DirectoryNotFoundException e) {
                throw new ConfigScopeNotFoundException(e);
            }
        }