public void ConfigureRequest_Returns_False_When_IsRedirect()
        {
            var routeCtx = GetRoutingContext("/test");

            var pcr = new PublishedContentRequest(routeCtx.UmbracoContext.CleanedUmbracoUrl, routeCtx, Mock.Of <IWebRoutingSection>(), s => new string[] { });
            var pc  = GetPublishedContentMock();

            pcr.PublishedContent = pc.Object;
            pcr.Culture          = new CultureInfo("en-AU");
            pcr.SetRedirect("/hello");
            var pcre = new PublishedContentRequestEngine(
                Mock.Of <IWebRoutingSection>(),
                pcr);

            var result = pcre.ConfigureRequest();

            Assert.IsFalse(result);
        }
Exemple #2
0
        private static bool TryRedirect(PublishedContentRequest contentRequest, bool includeQuery)
        {
            bool   redirect = false;
            string path     = string.Format("{0}{1}{2}", contentRequest.Uri.Authority
                                            , contentRequest.Uri.AbsolutePath
                                            , includeQuery ? contentRequest.Uri.Query : String.Empty
                                            );

            RedirectInfo redirectInfo;

            if (RedirectFromTo.TryGetValue(path, out redirectInfo) && !string.IsNullOrWhiteSpace(redirectInfo.To))
            {
                contentRequest.SetRedirect(redirectInfo.To, redirectInfo.StatusCode);
                redirect = true;
            }

            return(redirect);
        }
Exemple #3
0
        /// <summary>
        /// Publisheds the content request prepared.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private static void PublishedContentRequestPrepared(
            object sender,
            EventArgs e)
        {
            LogHelper.Info(typeof(ApplicationConfiguration), "PublishedContentRequestPrepared Started");

            PublishedContentRequest request = (PublishedContentRequest)sender;

            //// Check to make sure the request is valid
            if (request == null ||
                request.HasPublishedContent == false)
            {
                return;
            }

            if (ConfigurationHelper.IsBackOfficeUserLoggedIn())
            {
                return;
            }

            try
            {
                if (request.PublishedContent != null)
                {
                    string url = GetRedirectUrl(request);

                    if (string.IsNullOrEmpty(url) == false)
                    {
                        request.SetRedirect(url);
                    }
                }
            }
            catch (Exception exception)
            {
                LogHelper.Error(typeof(ApplicationConfiguration), "ApplicationConfiguration PublishedContentRequestPrepared", exception);
            }
        }
        protected override void ApplicationStarted(
            UmbracoApplicationBase umbracoApplication,
            ApplicationContext applicationContext)
        {
            umbracoApplication.BeginRequest += (sender, args) =>
            {
                CultureInfo newCulture = (CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
                newCulture.DateTimeFormat.ShortDatePattern = "dd MMMM yyyy";
                newCulture.DateTimeFormat.FirstDayOfWeek   = DayOfWeek.Monday;
                newCulture.DateTimeFormat.LongTimePattern  = "HH:mm";
                Thread.CurrentThread.CurrentCulture        = newCulture;
            };

            //The Umbraco implementation of Public Access restriction does an internal redirect to the login page PublishedContent.
            //We want that to be a redirect with returnUrl.
            PublishedContentRequest.Prepared += delegate(object sender, EventArgs args)
            {
                PublishedContentRequest pcr = (PublishedContentRequest)sender;
                if (pcr.HasPublishedContent && !pcr.IsInitialPublishedContent && !pcr.Is404 &&
                    !pcr.IsInternalRedirectPublishedContent && pcr.PublishedContent.DocumentTypeAlias == "Login")
                {
                    string redirectUrl = string.Format(
                        "{0}?returnUrl={1}",
                        pcr.PublishedContent.UrlWithDomain(),
                        HttpUtility.UrlEncode(pcr.RoutingContext.UmbracoContext.HttpContext.Request.RawUrl));

                    pcr.SetRedirect(redirectUrl);
                }
            };

            ContentService.Saving += (sender, args) =>
            {
                AddOrUpdateLecture(args.SavedEntities, x => x.Published && (x.Parent()?.Published ?? false));
                UpdateCourse(args.SavedEntities);
            };

            //Adding a Save post-event because during the Saving event, new items still have no Id so syncing won't work.
            ContentService.Saved += (sender, args) =>
            {
                AddOrUpdateLecture(args.SavedEntities, x => x.Published && (x.Parent()?.Published ?? false));
            };

            ContentService.UnPublishing += (sender, args) =>
            {
                AddOrUpdateLecture(args.PublishedEntities, x => false);
            };

            ContentService.RollingBack += (sender, args) =>
            {
                AddOrUpdateLecture(Enumerable.Repeat(args.Entity, 1), x => x.Published && (x.Parent()?.Published ?? false));
                UpdateCourse(Enumerable.Repeat(args.Entity, 1));
            };

            ContentService.Moving += (sender, args) =>
            {
                AddOrUpdateLecture(args.MoveInfoCollection.Select(x => x.Entity), x => x.Published && (x.Parent()?.Published ?? false));
            };

            ContentService.Trashing += (sender, args) =>
            {
                AddOrUpdateLecture(args.MoveInfoCollection.Select(x => x.Entity), x => false);
            };

            ContentService.Deleting += (sender, args) =>
            {
                AddOrUpdateLecture(args.DeletedEntities, x => false);
            };
        }
        /// <inheritdoc/>
        public bool TryFindContent(PublishedContentRequest contentRequest)
        {
            if (!contentRequest.Is404)
            {
                // TODO: use contentRequest.Uri?
                string url    = HttpContext.Current.Request.Url.ToString();
                string domain = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority);

                try
                {
                    Tuple <IPublishedContent, int> result;
                    IPublishedContent rerouteContent = null;
                    int statusCode = 0;

                    // Check the cache first
                    object cached = UrlRewriteCache.GetItem(url);
                    if (cached != null)
                    {
                        result         = (Tuple <IPublishedContent, int>)cached;
                        rerouteContent = result.Item1;
                        statusCode     = result.Item2;
                    }
                    else
                    {
                        // Nothing in the cache so let's taverse through each level of the site until we find it.
                        IPublishedContent root = contentRequest.RoutingContext.UmbracoContext.ContentCache.GetAtRoot()
                                                 .FirstOrDefault(p => p.UrlAbsolute().StartsWith(domain));

                        // Start looking for a match, this is recursive.
                        result = this.TryFindRewriteNode(root, BloodhoundPropertyEditor.PropertyEditorAlias, url);

                        if (result != null)
                        {
                            rerouteContent = result.Item1;
                            statusCode     = result.Item2;
                        }
                    }

                    // We have a result!
                    if (statusCode > 0 && rerouteContent != null)
                    {
                        if (statusCode == 301)
                        {
                            contentRequest.SetRedirectPermanent(rerouteContent.Url);
                        }
                        else
                        {
                            // 302
                            contentRequest.SetRedirect(rerouteContent.Url);
                        }

                        contentRequest.PublishedContent = rerouteContent;
                        return(true);
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Error <BloodhoundApplicationEvents>($"An error occured attempting to provide a rewrite for the following request {url}", ex);
                }
            }

            return(contentRequest.PublishedContent != null);
        }
        public bool TryFindContent(PublishedContentRequest contentRequest)
        {
            var stores = StoreHelper.GetAllStores();

            if (!stores.Any())
            {
                return(false);
            }

            var uwebshopRequest = UwebshopRequest.Current;
            var content         = uwebshopRequest.Product ?? uwebshopRequest.Category ?? uwebshopRequest.PaymentProvider ??     // in case ResolveUwebshopEntityUrl was already called from the module
                                  IO.Container.Resolve <IUrlRewritingService>().ResolveUwebshopEntityUrl().Entity;

            if (content is PaymentProvider)
            {
                var paymentProvider = content as PaymentProvider;

                Log.Instance.LogDebug("UmbracoDefaultAfterRequestInit paymentProvider: " + paymentProvider.Name);

                new PaymentRequestHandler().HandleuWebshopPaymentRequest(paymentProvider);

                var publishedContent = contentRequest.RoutingContext.UmbracoContext.ContentCache.GetById(paymentProvider.Id);
                if (publishedContent == null)
                {
                    return(false);
                }
                contentRequest.PublishedContent = publishedContent;

                SetRequestCulture(contentRequest);
                return(true);
            }

            if (content is Category)
            {
                var categoryFromUrl = content as Category;

                if (categoryFromUrl.Disabled)
                {
                    return(false);
                }

                if (Access.HasAccess(categoryFromUrl.Id, categoryFromUrl.Path, Membership.GetUser()))
                {
                    var doc = contentRequest.RoutingContext.UmbracoContext.ContentCache.GetById(content.Id);
                    if (doc != null)
                    {
                        contentRequest.PublishedContent = doc;
                        var altTemplate = HttpContext.Current.Request["altTemplate"];
                        contentRequest.TrySetTemplate(altTemplate);

                        SetRequestCulture(contentRequest);
                        return(true);
                    }
                }
                else
                {
                    if (HttpContext.Current.User.Identity.IsAuthenticated)
                    {
                        contentRequest.SetRedirect(library.NiceUrl(Access.GetErrorPage(categoryFromUrl.Path)));
                    }
                    contentRequest.SetRedirect(library.NiceUrl(Access.GetLoginPage(categoryFromUrl.Path)));
                    return(true);
                }
            }

            else if (content is Product)
            {
                var productFromUrl = content as Product;
                if (productFromUrl.Disabled)
                {
                    return(false);
                }

                if (Access.HasAccess(productFromUrl.Id, productFromUrl.Path, Membership.GetUser()))
                {
                    var doc = contentRequest.RoutingContext.UmbracoContext.ContentCache.GetById(content.Id);
                    if (doc != null)
                    {
                        contentRequest.PublishedContent = doc;
                        var altTemplate = HttpContext.Current.Request["altTemplate"];
                        contentRequest.TrySetTemplate(altTemplate);

                        SetRequestCulture(contentRequest);
                        return(true);
                    }
                }
                else
                {
                    if (HttpContext.Current.User.Identity.IsAuthenticated)
                    {
                        contentRequest.SetRedirect(library.NiceUrl(Access.GetErrorPage(productFromUrl.Path)));
                    }
                    contentRequest.SetRedirect(library.NiceUrl(Access.GetLoginPage(productFromUrl.Path)));
                    return(true);
                }
            }
            return(false);
        }