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)); }
public async Task Delete(string id) { IPRestrictionId ipId = new IPRestrictionId(id); Context.Response.StatusCode = (int)HttpStatusCode.NoContent; Site site = (ipId.SiteId != null) ? SiteHelper.GetSite(ipId.SiteId.Value) : null; if (site != null) { IPRestrictionsHelper.GetSection(site, ipId.Path, ManagementUnit.ResolveConfigScope()).RevertToParent(); if (ManagementUnit.ServerManager.GetApplicationHostConfiguration().HasSection(IPRestrictionsGlobals.DynamicIPSecuritySectionName)) { IPRestrictionsHelper.GetDynamicSecuritySection(site, ipId.Path, ManagementUnit.ResolveConfigScope()).RevertToParent(); } ManagementUnit.Current.Commit(); } if (ipId.SiteId == null && IPRestrictionsHelper.IsFeatureEnabled()) { await IPRestrictionsHelper.SetFeatureEnabled(false); } }
public object Get() { string ipRestrictionUuid = Context.Request.Query[Defines.IDENTIFIER]; if (string.IsNullOrEmpty(ipRestrictionUuid)) { return(new StatusCodeResult((int)HttpStatusCode.NotFound)); } IPRestrictionId ipId = new IPRestrictionId(ipRestrictionUuid); // Get site rule is for if applicable Site site = ipId.SiteId == null ? null : SiteHelper.GetSite(ipId.SiteId.Value); List <Rule> rules = IPRestrictionsHelper.GetRules(site, ipId.Path); // Set HTTP header for total count this.Context.Response.SetItemsCount(rules.Count()); Fields fields = Context.Request.GetFields(); return(new { entries = rules.Select(rule => IPRestrictionsHelper.RuleToJsonModelRef(rule, site, ipId.Path, fields)) }); }
public object Get(string id) { IPRestrictionId ipId = new IPRestrictionId(id); Site site = ipId.SiteId == null ? null : SiteHelper.GetSite(ipId.SiteId.Value); return(IPRestrictionsHelper.ToJsonModel(site, ipId.Path)); }
public object Post([FromBody] dynamic model) { if (model == null) { throw new ApiArgumentException("model"); } // Rule must be created for a specific ip restriction section if (model.ip_restriction == null) { throw new ApiArgumentException("ip_restriction"); } if (!(model.ip_restriction is JObject)) { throw new ApiArgumentException("ip_restriction"); } string ipUuid = DynamicHelper.Value(model.ip_restriction.id); if (ipUuid == null) { throw new ApiArgumentException("ip_restriction.id"); } // Get the ip restriction feature id IPRestrictionId ipId = new IPRestrictionId(ipUuid); // Get site Site site = ipId.SiteId == null ? null : SiteHelper.GetSite(ipId.SiteId.Value); string configPath = ManagementUnit.ResolveConfigScope(model); IPSecuritySection section = IPRestrictionsHelper.GetSection(site, ipId.Path, configPath); // Create ip restriction rule Rule rule = IPRestrictionsHelper.CreateRule(model, section); // Add it IPRestrictionsHelper.AddRule(rule, section); // Save ManagementUnit.Current.Commit(); // // Create response dynamic r = IPRestrictionsHelper.RuleToJsonModel(rule, site, ipId.Path); return(Created(IPRestrictionsHelper.GetRuleLocation(r.id), r)); }
public object Patch([FromBody] dynamic model, string id) { IPRestrictionId ipId = new IPRestrictionId(id); Site site = ipId.SiteId == null ? null : SiteHelper.GetSite(ipId.SiteId.Value); if (ipId.SiteId != null && site == null) { return(NotFound()); } string configPath = model == null ? null : ManagementUnit.ResolveConfigScope(model); IPRestrictionsHelper.SetFeatureSettings(model, site, ipId.Path, configPath); ManagementUnit.Current.Commit(); return(IPRestrictionsHelper.ToJsonModel(site, ipId.Path)); }
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)); }