Esempio n. 1
0
        public void Delete(string id)
        {
            QueryStringId queryStringId = new QueryStringId(id);

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

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

            QueryStringRule queryString = QueryStringsHelper.GetQueryStrings(site, queryStringId.Path).FirstOrDefault(r => r.QueryString.ToString().Equals(queryStringId.QueryString));

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

                QueryStringsHelper.DeleteQueryString(queryString, section);
                ManagementUnit.Current.Commit();
            }

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

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

            if (queryStringId.SiteId != null && site == null)
            {
                return(new StatusCodeResult((int)HttpStatusCode.NotFound));
            }

            QueryStringRule queryString = QueryStringsHelper.GetQueryStrings(site, queryStringId.Path).FirstOrDefault(s => s.QueryString.ToString().Equals(queryStringId.QueryString));

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

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

            QueryStringsHelper.UpdateQueryString(queryString, model, site, queryStringId.Path, configPath);

            ManagementUnit.Current.Commit();

            dynamic qs = QueryStringsHelper.ToJsonModel(queryString, site, queryStringId.Path);

            if (qs.id != id)
            {
                return(LocationChanged(QueryStringsHelper.GetLocation(qs.id), qs));
            }

            return(qs);
        }
        public static void DeleteQueryString(QueryStringRule queryString, RequestFilteringSection section)
        {
            if (string.IsNullOrEmpty(queryString.QueryString))
            {
                throw new ArgumentNullException("queryString.QueryString");
            }
            if (queryString == null)
            {
                return;
            }

            if (queryString.Allow)
            {
                var collection = section.AlwaysAllowedQueryStrings;

                var elem = collection.FirstOrDefault(s => s.QueryString.Equals(queryString.QueryString));
                if (elem != null)
                {
                    try {
                        collection.Remove(elem);
                    }
                    catch (FileLoadException e) {
                        throw new LockedException(section.SectionPath, e);
                    }
                    catch (DirectoryNotFoundException e) {
                        throw new ConfigScopeNotFoundException(e);
                    }
                }
            }
            else
            {
                var collection = section.DenyQueryStringSequences;

                var elem = collection.FirstOrDefault(s => s.Sequence.Equals(queryString.QueryString));
                if (elem != null)
                {
                    try {
                        collection.Remove(elem);
                    }
                    catch (FileLoadException e) {
                        throw new LockedException(section.SectionPath, e);
                    }
                    catch (DirectoryNotFoundException e) {
                        throw new ConfigScopeNotFoundException(e);
                    }
                }
            }
        }
        public static object ToJsonModelRef(QueryStringRule queryString, Site site, string path)
        {
            if (queryString == null)
            {
                return(null);
            }

            QueryStringId id = new QueryStringId(site?.Id, path, queryString.QueryString);

            var obj = new {
                query_string = queryString.QueryString,
                id           = id.Uuid,
                allow        = queryString.Allow
            };

            return(Core.Environment.Hal.Apply(Defines.QueryStringResource.Guid, obj, false));
        }
        internal static object ToJsonModel(QueryStringRule queryString, Site site, string path)
        {
            if (queryString == null)
            {
                return(null);
            }

            QueryStringId id = new QueryStringId(site?.Id, path, queryString.QueryString);

            var obj = new {
                query_string      = queryString.QueryString,
                id                = id.Uuid,
                allow             = queryString.Allow,
                request_filtering = RequestFilteringHelper.ToJsonModelRef(site, path)
            };

            return(Core.Environment.Hal.Apply(Defines.QueryStringResource.Guid, obj));
        }
Esempio n. 6
0
        public object Post([FromBody] dynamic model)
        {
            QueryStringRule    queryString = null;
            Site               site        = null;
            RequestFilteringId reqId       = null;

            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("request_filtering");
            }
            string reqUuid = DynamicHelper.Value(model.request_filtering.id);

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

            reqId = new RequestFilteringId(reqUuid);

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

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

            queryString = QueryStringsHelper.CreateQueryString(model);

            QueryStringsHelper.AddQueryString(queryString, section);

            ManagementUnit.Current.Commit();

            //
            // Create response
            dynamic qs = QueryStringsHelper.ToJsonModel(queryString, site, reqId.Path);

            return(Created(QueryStringsHelper.GetLocation(qs.id), qs));
        }
Esempio n. 7
0
        public object Get(string id)
        {
            QueryStringId queryStringId = new QueryStringId(id);

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

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

            QueryStringRule queryString = QueryStringsHelper.GetQueryStrings(site, queryStringId.Path).FirstOrDefault(s => s.QueryString.ToString().Equals(queryStringId.QueryString));

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

            return(QueryStringsHelper.ToJsonModel(queryString, site, queryStringId.Path));
        }
        public static QueryStringRule CreateQueryString(dynamic model)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

            string queryString = DynamicHelper.Value(model.query_string);

            if (string.IsNullOrEmpty(queryString))
            {
                throw new ApiArgumentException("query_string");
            }

            QueryStringRule queryStringRule = new QueryStringRule();

            queryStringRule.QueryString = queryString;

            queryStringRule.Allow = DynamicHelper.To <bool>(model.allow) ?? default(bool);

            return(queryStringRule);
        }
        public static void AddQueryString(QueryStringRule queryString, RequestFilteringSection section)
        {
            if (queryString == null)
            {
                throw new ArgumentNullException("queryString");
            }
            if (string.IsNullOrEmpty(queryString.QueryString))
            {
                throw new ArgumentNullException("queryString.QueryString");
            }

            AlwaysAllowedQueryStringCollection allowCollection = section.AlwaysAllowedQueryStrings;
            DenyQueryStringSequenceCollection  denyCollection  = section.DenyQueryStringSequences;

            if (allowCollection.Any(s => s.QueryString.Equals(queryString.QueryString)) ||
                denyCollection.Any(s => s.Sequence.Equals(queryString.QueryString)))
            {
                throw new AlreadyExistsException("query_string");
            }

            try {
                if (queryString.Allow)
                {
                    allowCollection.Add(queryString.QueryString);
                }
                else
                {
                    denyCollection.Add(queryString.QueryString);
                }
            }
            catch (FileLoadException e) {
                throw new LockedException(section.SectionPath, e);
            }
            catch (DirectoryNotFoundException e) {
                throw new ConfigScopeNotFoundException(e);
            }
        }
        public static void UpdateQueryString(QueryStringRule queryString, dynamic model, Site site, string path, string configPath = null)
        {
            if (queryString == null)
            {
                throw new ArgumentNullException("queryString");
            }
            if (queryString.QueryString == null)
            {
                throw new ArgumentNullException("queryString.QueryString");
            }
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

            bool?  allow           = DynamicHelper.To <bool>(model.allow);
            string queryStringName = DynamicHelper.Value(model.query_string);

            // Empty change set
            if (string.IsNullOrEmpty(queryStringName) && allow == null)
            {
                return;
            }

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

            try {
                // Remove the old query string

                if (queryString.Allow)
                {
                    // We have to retrieve the configuration element from the allow collection
                    var allowCollection = section.AlwaysAllowedQueryStrings;
                    var allowElement    = allowCollection.First(s => s.QueryString.Equals(queryString.QueryString));

                    // Remove the query string from the allow collection
                    allowCollection.Remove(allowElement);
                }
                else
                {
                    var denyCollection = section.DenyQueryStringSequences;
                    var denyElement    = denyCollection.First(s => s.Sequence.Equals(queryString.QueryString));

                    denyCollection.Remove(denyElement);
                }
            }
            catch (FileLoadException e) {
                throw new LockedException(section.SectionPath, e);
            }
            catch (DirectoryNotFoundException e) {
                throw new ConfigScopeNotFoundException(e);
            }


            // Update the query string to its new state
            queryString.Allow       = allow == null ? queryString.Allow : allow.Value;
            queryString.QueryString = string.IsNullOrEmpty(queryStringName) ? queryString.QueryString : queryStringName;


            try {
                // Add the updated query string back into its respective collection

                if (queryString.Allow)
                {
                    // Insert the query string into the allow collection
                    section.AlwaysAllowedQueryStrings.Add(queryString.QueryString);
                }
                else
                {
                    // Insert the query string into the deny collection
                    section.DenyQueryStringSequences.Add(queryString.QueryString);
                }
            }
            catch (FileLoadException e) {
                throw new LockedException(section.SectionPath, e);
            }
            catch (DirectoryNotFoundException e) {
                throw new ConfigScopeNotFoundException(e);
            }
        }