Ejemplo n.º 1
0
        /// <summary>
        /// States whether an Url is handled by Umbraco
        /// </summary>
        /// <param name="umbracoUrl">The umbraco url object from the app's config</param>
        /// <returns>Url is an Ubraco Url</returns>
        public bool IsUmbracoUrl(UmbracoUrl umbracoUrl)
        {
            if (string.IsNullOrEmpty(umbracoUrl.Value))
            {
                LogHelper.Error <UmbracoUrlService>("Error: No Unauthorized URL set in configuration", null);
                return(false);
            }

            if (umbracoUrl.Type == UmbracoUrlTypes.XPath || umbracoUrl.Type == UmbracoUrlTypes.ContentPicker)
            {
                return(true);
            }
            if (cache == null)
            {
                cache = ApplicationContext.Current.ApplicationCache.RuntimeCache;
            }

            return(cache.GetCacheItem <bool>(CacheKeyIsUmbracoUrl + umbracoUrl.Value, () =>
            {
                EnsureUmbracoContext(HttpContext.Current);
                var umbContext = UmbracoContext.Current;

                return umbContext.ContentCache.GetByRoute(umbracoUrl.Value) != null;
            }, CacheDuration));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets the Url from the UmbracoUrl type
        /// </summary>
        /// <param name="umbracoUrl">The umbraco url object from the app's config</param>
        /// <returns>The Unauthorised Url, or null</returns>
        public string Url(UmbracoUrl umbracoUrl)
        {
            if (string.IsNullOrEmpty(umbracoUrl.Value))
            {
                LogHelper.Error <UmbracoUrlService>("Error: No Unauthorized URL set in configuration", null);
                return(null);
            }

            if (umbracoUrl.Type == UmbracoUrlTypes.Url)
            {
                return(umbracoUrl.Value);
            }
            if (cache == null)
            {
                cache = ApplicationContext.Current.ApplicationCache.RuntimeCache;
            }

            return(cache.GetCacheItem <string>(CacheKeyUrl + umbracoUrl.Value, () =>
            {
                EnsureUmbracoContext(HttpContext.Current);
                var umbContext = UmbracoContext.Current;
                if (umbContext == null)
                {
                    LogHelper.Error <UmbracoUrlService>("Need to run this method from within a valid HttpContext request", null);
                    return null;
                }

                var umbracoContentService = new UmbracoContentService(umbContext);
                switch (umbracoUrl.Type)
                {
                case UmbracoUrlTypes.XPath:
                    var xpathId = umbracoContentService.XPath(umbracoUrl.Value);
                    if (xpathId != null)
                    {
                        return umbracoContentService.Url((int)xpathId);
                    }

                    LogHelper.Error <UmbracoUrlService>($"Error: Unable to find content using xpath of '{umbracoUrl.Value}'", null);
                    break;

                case UmbracoUrlTypes.ContentPicker:
                    if (int.TryParse(umbracoUrl.Value, out var id))
                    {
                        return umbracoContentService.Url(id);
                    }

                    LogHelper.Error <UmbracoUrlService>("Error: Unable to parse the selected unauthorized URL content picker item. Please ensure a valid content node is selected", null);
                    break;

                default:
                    LogHelper.Error <UmbracoUrlService>("Error: Unable to determine which method to use to get the unauthorized URL. Please ensure URL, XPath or Content Picker is selected", null);
                    break;
                }
                return null;
            }, CacheDuration));
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Create an exception Url, that won't be redirected. Usually a service or error page that you want displayed, regardless or what watches want
 /// </summary>
 /// <param name="job">The job that created this Exception</param>
 /// <param name="regex">The url / url rule that can be shown, try and match with or without trailing slash</param>
 /// <param name="url"></param>
 /// <returns>A Unique id for this Exception, or -1 if we failed to create it</returns>
 public static int Exception(IJob job, Regex regex = null, UmbracoUrl url = null)
 {
     return(UrlExceptionLock.Write(() =>
     {
         var count = Interlocked.Increment(ref _requestCount);
         UrlExceptions.Add(new UrlException
         {
             EnvironmentId = job.Environment.Id,
             AppId = job.App.Id,
             Regex = regex,
             Url = url
         });
         return count;
     }));
 }
Ejemplo n.º 4
0
        public static int Unexception(int?environmentId = null, string appId = null, Regex regex = null, UmbracoUrl url = null)
        {
            return(UrlExceptionLock.Write(() =>
            {
                var regy = regex?.ToString();

                return UrlExceptions.RemoveAll(x =>
                                               (environmentId == null || x.EnvironmentId == environmentId) &&
                                               (appId == null || x.AppId == appId) ||
                                               regex == null || x.Regex.ToString() == regy ||
                                               url == null || x.Url.Equals(url));
            }));
        }
Ejemplo n.º 5
0
 // ReSharper disable once MethodOverloadWithOptionalParameter
 public static int Unexception(IJob job, UmbracoUrl url = null) => Unexception(job.Environment.Id, job.App.Id, null, url);