Exemplo n.º 1
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);
            }
        }
Exemplo n.º 2
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);
            }
        }