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 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 void Delete(string id) { RuleId ruleId = new RuleId(id); Site site = ruleId.SiteId == null ? null : SiteHelper.GetSite(ruleId.SiteId.Value); if (ruleId.SiteId != null && site == null) { // The rule id specified a site but we couldn't find it, therefore we can't get the rule Context.Response.StatusCode = (int)HttpStatusCode.NoContent; return; } Rule rule = IPRestrictionsHelper.GetRules(site, ruleId.Path).Where(r => r.IpAddress.ToString().Equals(ruleId.IpAddress)).FirstOrDefault(); if (rule != null) { var section = IPRestrictionsHelper.GetSection(site, ruleId.Path, ManagementUnit.ResolveConfigScope()); IPRestrictionsHelper.DeleteRule(rule, section); ManagementUnit.Current.Commit(); } Context.Response.StatusCode = (int)HttpStatusCode.NoContent; }
public object Patch(string id, [FromBody] dynamic model) { RuleId ruleId = new RuleId(id); Site site = ruleId.SiteId == null ? null : SiteHelper.GetSite(ruleId.SiteId.Value); if (ruleId.SiteId != null && site == null) { // The rule id specified a site but we couldn't find it, therefore we can't get the rule return(NotFound()); } string configPath = model == null ? null : ManagementUnit.ResolveConfigScope(model); var section = IPRestrictionsHelper.GetSection(site, ruleId.Path, configPath); Rule rule = section.IpAddressFilters.Where(r => r.IpAddress.ToString().Equals(ruleId.IpAddress)).FirstOrDefault(); if (rule == null) { return(NotFound()); } rule = IPRestrictionsHelper.SetRule(rule, model, section); ManagementUnit.Current.Commit(); dynamic rle = IPRestrictionsHelper.RuleToJsonModel(rule, site, ruleId.Path); if (rle.id != id) { return(LocationChanged(IPRestrictionsHelper.GetRuleLocation(rle.id), rle)); } return(rle); }
private void ConfigureIPRestrictions() { // Register controller routes in mvc framework Environment.Host.RouteBuilder.MapWebApiRoute(Defines.Resource.Guid, $"{Defines.PATH}/{{id?}}", new { controller = "iprestriction" }); // Self Environment.Hal.ProvideLink(Defines.Resource.Guid, "self", ipRes => new { href = IPRestrictionsHelper.GetLocation(ipRes.id) }); // Web Server Environment.Hal.ProvideLink(WebServer.Defines.Resource.Guid, Defines.Resource.Name, _ => { var id = GetIPRestrictionId(null, null); return(new { href = IPRestrictionsHelper.GetLocation(id.Uuid) }); }); // Site Environment.Hal.ProvideLink(Sites.Defines.Resource.Guid, Defines.Resource.Name, site => { var siteId = new SiteId((string)site.id); Site s = SiteHelper.GetSite(siteId.Id); var id = GetIPRestrictionId(s, "/"); return(new { href = IPRestrictionsHelper.GetLocation(id.Uuid) }); }); // Application Environment.Hal.ProvideLink(Applications.Defines.Resource.Guid, Defines.Resource.Name, app => { var appId = new ApplicationId((string)app.id); Site s = SiteHelper.GetSite(appId.SiteId); var id = GetIPRestrictionId(s, appId.Path); return(new { href = IPRestrictionsHelper.GetLocation(id.Uuid) }); }); }
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 async Task <object> Post() { if (IPRestrictionsHelper.IsFeatureEnabled()) { throw new AlreadyExistsException(IPRestrictionsHelper.FEATURE_NAME); } await IPRestrictionsHelper.SetFeatureEnabled(true); dynamic settings = IPRestrictionsHelper.ToJsonModel(null, null); return(Created(IPRestrictionsHelper.GetLocation(settings.id), settings)); }
public object Get() { // Check if the scope of the request is for site or application Site site = ApplicationHelper.ResolveSite(); string path = ApplicationHelper.ResolvePath(); if (path == null) { return(NotFound()); } dynamic d = IPRestrictionsHelper.ToJsonModel(site, path); return(LocationChanged(IPRestrictionsHelper.GetLocation(d.id), d)); }
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)); }
public object Get(string id) { RuleId ruleId = new RuleId(id); Site site = ruleId.SiteId == null ? null : SiteHelper.GetSite(ruleId.SiteId.Value); if (ruleId.SiteId != null && site == null) { // The rule id specified a site but we couldn't find it, therefore we can't get the rule return(NotFound()); } Rule rule = IPRestrictionsHelper.GetRules(site, ruleId.Path).Where(r => r.IpAddress.ToString().Equals(ruleId.IpAddress)).FirstOrDefault(); if (rule == null) { return(NotFound()); } return(IPRestrictionsHelper.RuleToJsonModel(rule, site, ruleId.Path)); }
private IPRestrictionId GetIPRestrictionId(Site s, string path) { return(new IPRestrictionId(s?.Id, path, IPRestrictionsHelper.IsSectionLocal(s, path))); }
private void ConfigureRules() { Environment.Host.RouteBuilder.MapWebApiRoute(Defines.RulesResource.Guid, $"{Defines.RULES_PATH}/{{id?}}", new { controller = "iprestrictionrules" }); // Provide self links for plugin resources Environment.Hal.ProvideLink(Defines.RulesResource.Guid, "self", rule => new { href = IPRestrictionsHelper.GetRuleLocation(rule.id) }); // Provide link for the rules sub resource Environment.Hal.ProvideLink(Defines.Resource.Guid, "entries", ipRes => new { href = $"/{Defines.RULES_PATH}?{Defines.IDENTIFIER}={ipRes.id}" }); }