Пример #1
0
        private int[] PopulateActiveLayers()
        {
            var populatedLayers = _layerRetrievalService.GetLayers();

            var activeLayerIds = new List <int>();

            foreach (var layer in populatedLayers)
            {
                // ignore the rule if it fails to execute
                try
                {
                    var currentLayer     = layer;
                    var layerRuleMatches = _performanceMonitor.PublishTimedAction(() => _ruleManager.Matches(currentLayer.Rule), (r, t) => new LayerMessage
                    {
                        Active   = r,
                        Name     = currentLayer.Name,
                        Rule     = currentLayer.Rule,
                        Duration = t.Duration
                    }, TimelineCategories.Layers, "Layer Evaluation", currentLayer.Name).ActionResult;

                    if (layerRuleMatches)
                    {
                        activeLayerIds.Add(currentLayer.Id);
                    }
                }
                catch (Exception e)
                {
                    Logger.Warning(e, T("An error occurred during layer evaluation on: {0}. The rule that was attempted was {1}.", layer.Name, layer.Rule).Text);
                }
            }

            return(activeLayerIds.ToArray());
        }
Пример #2
0
        public new bool Authorize(Permission permission, IContent content, LocalizedString message)
        {
            var authorizerMessage = new AuthorizerMessage {
                PermissionName = permission.Name
            };

            if (content != null)
            {
                authorizerMessage.ContentId   = content.Id;
                authorizerMessage.ContentType = content.ContentItem.ContentType;
                authorizerMessage.ContentName = content.GetContentName();
            }

            return(_performanceMonitor.PublishTimedAction(() =>
            {
                if (permission == StandardPermissions.AccessFrontEnd)
                {
                    return true;
                }

                return base.Authorize(permission, content, message);
            }, (r, t) => {
                authorizerMessage.Duration = t.Duration;
                authorizerMessage.UserIsAuthorized = r;

                return authorizerMessage;
            }, TimelineCategories.Authorization, "Authorize", permission.Name).ActionResult);
        }
Пример #3
0
        public override dynamic DisplayElement(
            Element element,
            IContent content,
            string displayType   = null,
            IUpdateModel updater = null)
        {
            var container = element as Container;

            return(_performanceMonitor.PublishTimedAction(() =>
            {
                return base.DisplayElement(element, content, displayType, updater);
            }, (r, t) => new ElementMessage()
            {
                Duration = t.Duration,
                Offset = t.Offset,
                ContentItem = content,
                Category = element.Category,
                DisplayText = element.DisplayText.Text,
                HtmlClass = element.HtmlClass,
                HtmlId = element.HtmlId,
                HtmlStyle = element.HtmlStyle,
                Index = element.Index,
                Rule = element.Rule,
                NumberOfChildElements = container == null ? 0 : container.Elements.Count(),
                IsContainer = container != null
            }, TimelineCategories.Layouts, "Element Displaying", element.DisplayText.Text).ActionResult);
        }
 public new ContentItem Get(int id, VersionOptions options, QueryHints hints)
 {
     return(_performanceMonitor.PublishTimedAction(() => base.Get(id, options, hints), (r, t) => new ContentManagerMessage
     {
         ContentId = id,
         ContentType = GetContentType(id, r, options),
         Name = r.GetContentName(),
         Duration = t.Duration,
         //VersionOptions = options
     }, TimelineCategories.ContentManagement, r => "Get: " + GetContentType(id, r, options), r => r.GetContentName()).ActionResult);
 }
        public int[] GetActiveLayerIds()
        {
            // Once the Rule Engine is done:
            // Get Layers and filter by zone and rule
            // NOTE: .ForType("Layer") is faster than .Query<LayerPart, LayerPartRecord>()
            var activeLayers = _orchardServices.ContentManager.Query <LayerPart>().WithQueryHints(new QueryHints().ExpandParts <LayerPart>()).ForType("Layer").List();

            var activeLayerIds = new List <int>();

            foreach (var activeLayer in activeLayers)
            {
                // ignore the rule if it fails to execute
                try
                {
                    var currentLayer     = activeLayer;
                    var layerRuleMatches = _performanceMonitor.PublishTimedAction(() => _ruleManager.Matches(currentLayer.Record.LayerRule), (r, t) => new LayerMessage
                    {
                        Active   = r,
                        Name     = currentLayer.Record.Name,
                        Rule     = currentLayer.Record.LayerRule,
                        Duration = t.Duration
                    }, TimelineCategories.Layers, "Layer Evaluation", currentLayer.Record.Name).ActionResult;

                    if (layerRuleMatches)
                    {
                        activeLayerIds.Add(activeLayer.ContentItem.Id);
                    }
                }
                catch (Exception e)
                {
                    Logger.Warning(e, T("An error occured during layer evaluation on: {0}", activeLayer.Name).Text);
                }
            }

            return(activeLayerIds.ToArray());
        }
Пример #6
0
        public void OnResultExecuting(ResultExecutingContext filterContext)
        {
            // layers and widgets should only run on a full view rendering result
            var viewResult = filterContext.Result as ViewResult;

            if (viewResult == null)
            {
                return;
            }

            var workContext = _workContextAccessor.GetContext(filterContext);

            if (workContext == null ||
                workContext.Layout == null ||
                workContext.CurrentSite == null ||
                AdminFilter.IsApplied(filterContext.RequestContext) ||
                !ThemeFilter.IsApplied(filterContext.RequestContext))
            {
                return;
            }

            // Once the Rule Engine is done:
            // Get Layers and filter by zone and rule
            IEnumerable <LayerPart> activeLayers = _orchardServices.ContentManager.Query <LayerPart, LayerPartRecord>().List();

            var activeLayerIds = new List <int>();

            foreach (var activeLayer in activeLayers)
            {
                // ignore the rule if it fails to execute
                try
                {
                    var currentLayer     = activeLayer;
                    var layerRuleMatches = _performanceMonitor.PublishTimedAction(() => _ruleManager.Matches(currentLayer.Record.LayerRule), (r, t) => new LayerMessage
                    {
                        Active   = r,
                        Name     = currentLayer.Record.Name,
                        Rule     = currentLayer.Record.LayerRule,
                        Duration = t.Duration
                    }, TimelineCategories.Layers, "Layer Evaluation", currentLayer.Record.Name).ActionResult;

                    if (layerRuleMatches)
                    {
                        activeLayerIds.Add(activeLayer.ContentItem.Id);
                    }
                }
                catch (Exception e)
                {
                    Logger.Warning(e, T("An error occured during layer evaluation on: {0}", activeLayer.Name).Text);
                }
            }

            IEnumerable <WidgetPart> widgetParts = _widgetsService.GetWidgets(layerIds: activeLayerIds.ToArray());

            // Build and add shape to zone.
            var zones          = workContext.Layout.Zones;
            var defaultCulture = workContext.CurrentSite.As <SiteSettingsPart>().SiteCulture;
            var currentCulture = workContext.CurrentCulture;

            foreach (var widgetPart in widgetParts)
            {
                var commonPart = widgetPart.As <ICommonPart>();
                if (commonPart == null || commonPart.Container == null)
                {
                    Logger.Warning("The widget '{0}' is has no assigned layer or the layer does not exist.", widgetPart.Title);
                    continue;
                }

                // ignore widget for different cultures
                var localizablePart = widgetPart.As <ILocalizableAspect>();
                if (localizablePart != null)
                {
                    // if localized culture is null then show if current culture is the default
                    // this allows a user to show a content item for the default culture only
                    if (localizablePart.Culture == null && defaultCulture != currentCulture)
                    {
                        continue;
                    }

                    // if culture is set, show only if current culture is the same
                    if (localizablePart.Culture != null && localizablePart.Culture != currentCulture)
                    {
                        continue;
                    }
                }

                // check permissions
                if (!_orchardServices.Authorizer.Authorize(global::Orchard.Core.Contents.Permissions.ViewContent, widgetPart))
                {
                    continue;
                }

                var scopedWidgetPart       = widgetPart;
                var widgetBuildDisplayTime = _performanceMonitor.Time(() => _orchardServices.ContentManager.BuildDisplay(scopedWidgetPart));
                var widgetShape            = widgetBuildDisplayTime.ActionResult;

                _performanceMonitor.PublishMessage(new WidgetMessage
                {
                    Title         = widgetPart.Title,
                    Type          = widgetPart.ContentItem.ContentType,
                    Zone          = widgetPart.Zone,
                    Layer         = widgetPart.LayerPart,
                    Position      = widgetPart.Position,
                    TechnicalName = widgetPart.Name,
                    Duration      = widgetBuildDisplayTime.TimerResult.Duration
                });

                zones[widgetPart.Record.Zone].Add(widgetShape, widgetPart.Record.Position);
            }
        }
Пример #7
0
        public void OnResultExecuting(ResultExecutingContext filterContext)
        {
            // layers and widgets should only run on a full view rendering result
            var viewResult = filterContext.Result as ViewResult;

            if (viewResult == null)
            {
                return;
            }

            var workContext = _workContextAccessor.GetContext(filterContext.HttpContext);

            if (workContext == null ||
                workContext.Layout == null ||
                workContext.CurrentSite == null ||
                AdminFilter.IsApplied(filterContext.RequestContext) ||
                !ThemeFilter.IsApplied(filterContext.RequestContext))
            {
                return;
            }

            IEnumerable <WidgetPart> widgetParts = _widgetsService.GetWidgets(_layerEvaluationService.GetActiveLayerIds().ToArray());

            // Build and add shape to zone.
            var zones          = workContext.Layout.Zones;
            var defaultCulture = workContext.CurrentSite.As <SiteSettingsPart>().SiteCulture;
            var currentCulture = workContext.CurrentCulture;

            foreach (var widgetPart in widgetParts)
            {
                var commonPart = widgetPart.As <ICommonPart>();
                if (commonPart == null || commonPart.Container == null)
                {
                    Logger.Warning("The widget '{0}' is has no assigned layer or the layer does not exist.", widgetPart.Title);
                    continue;
                }

                // ignore widget for different cultures
                var localizablePart = widgetPart.As <ILocalizableAspect>();
                if (localizablePart != null)
                {
                    // if localized culture is null then show if current culture is the default
                    // this allows a user to show a content item for the default culture only
                    if (localizablePart.Culture == null && defaultCulture != currentCulture)
                    {
                        continue;
                    }

                    // if culture is set, show only if current culture is the same
                    if (localizablePart.Culture != null && localizablePart.Culture != currentCulture)
                    {
                        continue;
                    }
                }

                // check permissions
                if (!_orchardServices.Authorizer.Authorize(global::Orchard.Core.Contents.Permissions.ViewContent, widgetPart))
                {
                    continue;
                }

                var scopedWidgetPart = widgetPart;
                var widgetShape      = _performanceMonitor.PublishTimedAction(() => _orchardServices.ContentManager.BuildDisplay(scopedWidgetPart), (r, t) =>
                                                                              new WidgetMessage
                {
                    Title         = scopedWidgetPart.Title,
                    Type          = scopedWidgetPart.ContentItem.ContentType,
                    Zone          = scopedWidgetPart.Zone,
                    Layer         = scopedWidgetPart.LayerPart,
                    Position      = scopedWidgetPart.Position,
                    TechnicalName = scopedWidgetPart.Name,
                    Duration      = t.Duration
                }, TimelineCategories.Widgets, string.Format("Build Display ({0})", scopedWidgetPart.ContentItem.ContentType), scopedWidgetPart.Title);

                zones[widgetPart.Record.Zone].Add(widgetShape.ActionResult, widgetPart.Record.Position);
            }
        }