Esempio n. 1
0
        public static void DeleteUrl(UrlRule url, RequestFilteringSection section)
        {
            if (url == null)
            {
                return;
            }

            try {
                if (url.Allow)
                {
                    var target = section.AlwaysAllowedUrls.FirstOrDefault(u => u.Url.Equals(url.Url, StringComparison.OrdinalIgnoreCase));

                    if (target != null)
                    {
                        section.AlwaysAllowedUrls.Remove(target);
                    }
                }

                else
                {
                    var target = section.DenyUrlSequences.FirstOrDefault(u => u.Sequence.Equals(url.Url, StringComparison.OrdinalIgnoreCase));

                    if (target != null)
                    {
                        section.DenyUrlSequences.Remove(target);
                    }
                }
            }
            catch (FileLoadException e) {
                throw new LockedException(section.SectionPath, e);
            }
            catch (DirectoryNotFoundException e) {
                throw new ConfigScopeNotFoundException(e);
            }
        }
Esempio n. 2
0
        public void Delete(string id)
        {
            UrlId urlId = new UrlId(id);

            Site site = urlId.SiteId == null ? null : SiteHelper.GetSite(urlId.SiteId.Value);

            if (urlId.SiteId != null && site == null)
            {
                Context.Response.StatusCode = (int)HttpStatusCode.NoContent;
                return;
            }

            UrlRule url = UrlsHelper.GetUrls(site, urlId.Path).Where(u => u.Url.ToString().Equals(urlId.Url)).FirstOrDefault();

            if (url != null)
            {
                var section = RequestFilteringHelper.GetRequestFilteringSection(site, urlId.Path, ManagementUnit.ResolveConfigScope());

                UrlsHelper.DeleteUrl(url, section);
                ManagementUnit.Current.Commit();
            }

            Context.Response.StatusCode = (int)HttpStatusCode.NoContent;
            return;
        }
Esempio n. 3
0
        public object Patch(string id, [FromBody] dynamic model)
        {
            UrlId urlId = new UrlId(id);

            Site site = urlId.SiteId == null ? null : SiteHelper.GetSite(urlId.SiteId.Value);

            if (urlId.SiteId != null && site == null)
            {
                return(NotFound());
            }

            UrlRule url = UrlsHelper.GetUrls(site, urlId.Path).FirstOrDefault(u => u.Url.Equals(urlId.Url, StringComparison.OrdinalIgnoreCase));

            if (url == null)
            {
                return(NotFound());
            }

            string configPath = model == null ? null : ManagementUnit.ResolveConfigScope(model);

            UrlsHelper.UpdateUrl(url, model, site, urlId.Path, configPath);

            ManagementUnit.Current.Commit();

            dynamic urlModel = UrlsHelper.ToJsonModel(url, site, urlId.Path);

            if (urlModel.id != id)
            {
                return(LocationChanged(UrlsHelper.GetLocation(urlModel.id), urlModel));
            }

            return(urlModel);
        }
Esempio n. 4
0
        public static void AddUrl(UrlRule url, RequestFilteringSection section)
        {
            if (url == null)
            {
                throw new ArgumentNullException("rule");
            }
            if (url.Url == null)
            {
                throw new ArgumentNullException("rule.Url");
            }

            try {
                if (url.Allow)
                {
                    var collection            = section.AlwaysAllowedUrls;
                    AlwaysAllowedUrl allowUrl = collection.CreateElement();

                    if (collection.Any(u => u.Url.Equals(url.Url)))
                    {
                        throw new AlreadyExistsException("url");
                    }

                    allowUrl.Url = url.Url;

                    collection.Add(allowUrl);
                }
                else
                {
                    var             collection   = section.DenyUrlSequences;
                    DenyUrlSequence denySequence = collection.CreateElement();

                    if (collection.Any(u => u.Sequence.Equals(url.Url)))
                    {
                        throw new AlreadyExistsException("url");
                    }

                    denySequence.Sequence = url.Url;

                    collection.Add(denySequence);
                }
            }
            catch (FileLoadException e) {
                throw new LockedException(section.SectionPath, e);
            }
            catch (DirectoryNotFoundException e) {
                throw new ConfigScopeNotFoundException(e);
            }
        }
Esempio n. 5
0
        public static object ToJsonModelRef(UrlRule url, Site site, string path)
        {
            if (url == null)
            {
                return(null);
            }

            UrlId urlId = new UrlId(site?.Id, path, url.Url);

            var obj = new {
                url   = url.Url,
                id    = urlId.Uuid,
                allow = url.Allow
            };

            return(Core.Environment.Hal.Apply(Defines.UrlsResource.Guid, obj, false));
        }
Esempio n. 6
0
        internal static object ToJsonModel(UrlRule url, Site site, string path)
        {
            if (url == null)
            {
                return(null);
            }

            UrlId urlId = new UrlId(site?.Id, path, url.Url);

            var obj = new {
                url               = url.Url,
                id                = urlId.Uuid,
                allow             = url.Allow,
                request_filtering = RequestFilteringHelper.ToJsonModelRef(site, path)
            };

            return(Core.Environment.Hal.Apply(Defines.UrlsResource.Guid, obj));
        }
Esempio n. 7
0
        public object Get(string id)
        {
            UrlId urlId = new UrlId(id);

            Site site = urlId.SiteId == null ? null : SiteHelper.GetSite(urlId.SiteId.Value);

            if (urlId.SiteId != null && site == null)
            {
                return(NotFound());
            }

            UrlRule url = UrlsHelper.GetUrls(site, urlId.Path).FirstOrDefault(u => u.Url.Equals(urlId.Url, StringComparison.OrdinalIgnoreCase));

            if (url == null)
            {
                return(NotFound());
            }

            return(UrlsHelper.ToJsonModel(url, site, urlId.Path));
        }
Esempio n. 8
0
        public object Post([FromBody] dynamic model)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }
            if (model.request_filtering == null)
            {
                throw new ApiArgumentException("request_filtering");
            }
            if (!(model.request_filtering is JObject))
            {
                throw new ApiArgumentException(String.Empty, "request_filtering");
            }
            string reqUuid = DynamicHelper.Value(model.request_filtering.id);

            if (reqUuid == null)
            {
                throw new ApiArgumentException("request_filtering.id");
            }

            // Get the feature id
            RequestFilteringId reqId = new RequestFilteringId(reqUuid);

            Site site = reqId.SiteId == null ? null : SiteHelper.GetSite(reqId.SiteId.Value);

            string configPath = ManagementUnit.ResolveConfigScope(model);
            RequestFilteringSection section = RequestFilteringHelper.GetRequestFilteringSection(site, reqId.Path, configPath);

            UrlRule url = UrlsHelper.CreateUrl(model, section);

            UrlsHelper.AddUrl(url, section);

            ManagementUnit.Current.Commit();

            //
            // Create response
            dynamic u = UrlsHelper.ToJsonModel(url, site, reqId.Path);

            return(Created(UrlsHelper.GetLocation(u.id), u));
        }
Esempio n. 9
0
        public static void UpdateUrl(UrlRule url, dynamic model, Site site, string path, string configPath = null)
        {
            if (url == null)
            {
                throw new ArgumentNullException("url");
            }
            if (url.Url == null)
            {
                throw new ArgumentNullException("url.Url");
            }
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

            bool   allow  = DynamicHelper.To <bool>(model.allow) ?? url.Allow;
            string newUrl = DynamicHelper.Value(model.url);

            var section = RequestFilteringHelper.GetRequestFilteringSection(site, path, configPath);

            // Url is in as an allow url
            if (url.Allow)
            {
                AlwaysAllowedUrl targetUrl = section.AlwaysAllowedUrls.FirstOrDefault(u => u.Url.Equals(url.Url, StringComparison.OrdinalIgnoreCase));

                if (targetUrl == null)
                {
                    throw new NotFoundException("url");
                }

                section.AlwaysAllowedUrls.Remove(targetUrl);
            }
            // Url is in the configuration as a deny url sequence
            else
            {
                DenyUrlSequence denySequence = section.DenyUrlSequences.FirstOrDefault(u => u.Sequence.Equals(url.Url, StringComparison.OrdinalIgnoreCase));

                if (denySequence == null)
                {
                    throw new NotFoundException("url");
                }

                section.DenyUrlSequences.Remove(denySequence);
            }

            try {
                // The target url has been removed from either allow or deny collection.
                // Add updated url to proper collection

                if (allow)
                {
                    var elem = section.AlwaysAllowedUrls.CreateElement();
                    elem.Url = newUrl ?? url.Url;

                    section.AlwaysAllowedUrls.Add(elem);
                    url.Allow = true;
                }

                else
                {
                    var elem = section.DenyUrlSequences.CreateElement();
                    elem.Sequence = newUrl ?? url.Url;

                    section.DenyUrlSequences.Add(elem);
                    url.Allow = false;
                }

                url.Url = newUrl ?? url.Url;
            }
            catch (FileLoadException e) {
                throw new LockedException(section.SectionPath, e);
            }
            catch (DirectoryNotFoundException e) {
                throw new ConfigScopeNotFoundException(e);
            }
        }