Ejemplo n.º 1
0
        public async Task <ActionResult> IndexFilterPOST(AuditTrailIndexOptions options)
        {
            await _auditTrailOptionsDisplayManager.UpdateEditorAsync(options, _updateModelAccessor.ModelUpdater, false);

            // When the user has typed something into the search input no further evaluation of the form post is required.
            if (!String.Equals(options.SearchText, options.OriginalSearchText, StringComparison.OrdinalIgnoreCase))
            {
                return(RedirectToAction(nameof(Index), new RouteValueDictionary {
                    { "q", options.SearchText }
                }));
            }

            // Evaluate the values provided in the form post and map them to the filter result and route values.
            await _auditTrailOptionsDisplayManager.UpdateEditorAsync(options, _updateModelAccessor.ModelUpdater, false);

            // The route value must always be added after the editors have updated the models.
            options.RouteValues.TryAdd("q", options.FilterResult.ToString());

            return(RedirectToAction(nameof(Index), options.RouteValues));
        }
Ejemplo n.º 2
0
        public async Task <AuditTrailEventQueryResult> QueryAsync(int page, int pageSize, AuditTrailIndexOptions options)
        {
            // Because admin filters can add a different index to the query this must be added as a Query<AuditTrailEvent>()
            var query = _session.Query <AuditTrailEvent>(collection: AuditTrailEvent.Collection);

            query = await options.FilterResult.ExecuteAsync(new AuditTrailQueryContext(_serviceProvider, query));

            var totalCount = await query.CountAsync();

            if (pageSize > 0)
            {
                if (page > 1)
                {
                    query = query.Skip((page - 1) * pageSize);
                }

                query = query.Take(pageSize);
            }

            var events = await query.ListAsync();

            var result = new AuditTrailEventQueryResult
            {
                Events     = events,
                TotalCount = totalCount
            };

            options.AuditTrailSorts = _adminListOptions.SortOptions.Values.Where(x => x.SelectListItem != null).Select(opt => opt.SelectListItem(_serviceProvider, opt, options)).ToList();

            var categories = _auditTrailManager.DescribeCategories();

            options.Categories = categories
                                 .Select(category => new SelectListItem(category.LocalizedName(_serviceProvider), category.Name, category.Name == options.Category))
                                 .ToList();

            options.Categories.Insert(0, new SelectListItem(S["All categories"], String.Empty, String.IsNullOrEmpty(options.Category)));

            if (options.CorrelationIdFromRoute)
            {
                var firstEvent = result.Events.FirstOrDefault();
                if (firstEvent != null)
                {
                    var currentCategory = categories.FirstOrDefault(x => x.Name == firstEvent.Category);
                    if (currentCategory != null)
                    {
                        options.Events = currentCategory.Events.Values.Select(category =>
                                                                              new SelectListItem(category.LocalizedName(_serviceProvider), category.Name, category.Name == options.Category)).ToList();
                    }
                }
            }

            var localNow    = await _localClock.LocalNowAsync;
            var startOfWeek = CultureInfo.CurrentUICulture.DateTimeFormat.FirstDayOfWeek;

            options.AuditTrailDates = new List <SelectListItem>()
            {
                new SelectListItem(S["Any date"], String.Empty, options.Date == String.Empty),
            };

            var dateTimeValue = ">@now-1";

            options.AuditTrailDates.Add(new SelectListItem(S["Last 24 hours"], dateTimeValue, options.Date == dateTimeValue));

            dateTimeValue = "@now-2..@now-1";
            options.AuditTrailDates.Add(new SelectListItem(S["Previous 48 hours"], dateTimeValue, options.Date == dateTimeValue));

            dateTimeValue = $">{localNow.StartOfWeek(CultureInfo.CurrentUICulture.DateTimeFormat.FirstDayOfWeek).LocalDateTime.ToString(DateFormat)}";
            options.AuditTrailDates.Add(new SelectListItem(S["This week"], dateTimeValue, options.Date == dateTimeValue));

            var start = localNow.StartOfWeek(CultureInfo.CurrentUICulture.DateTimeFormat.FirstDayOfWeek).AddDays(-7).LocalDateTime;
            var end   = start.AddDays(7);

            dateTimeValue = $"{start.ToString(DateFormat)}..{end.ToString(DateFormat)}";
            options.AuditTrailDates.Add(new SelectListItem(S["Last week"], dateTimeValue, options.Date == dateTimeValue));

            start         = new DateTime(localNow.LocalDateTime.Year, localNow.LocalDateTime.Month, 1);
            dateTimeValue = $">{start.ToString(DateFormat)}";
            options.AuditTrailDates.Add(new SelectListItem(S["This month"], dateTimeValue, options.Date == dateTimeValue));

            start         = new DateTime(localNow.LocalDateTime.Year, localNow.LocalDateTime.Month, 1).AddMonths(-1);
            end           = start.AddMonths(1);
            dateTimeValue = $"{start.ToString(DateFormat)}..{end.ToString(DateFormat)}";
            options.AuditTrailDates.Add(new SelectListItem(S["Last month"], dateTimeValue, options.Date == dateTimeValue));
            options.AuditTrailDates.Add(new SelectListItem(S["Custom date range"], dateTimeValue, options.Date == dateTimeValue));

            dateTimeValue = $">{localNow.AddHours(-1).ToString("o")}";
            options.AuditTrailDates.Add(new SelectListItem(S["Last hour"], dateTimeValue, options.Date == dateTimeValue));

            dateTimeValue = $"{localNow.AddHours(-2).ToString("o")}..{localNow.AddHours(-1).ToString("o")}";
            options.AuditTrailDates.Add(new SelectListItem(S["Previous hour"], dateTimeValue, options.Date == dateTimeValue));
            options.AuditTrailDates.Add(new SelectListItem(S["Custom time range"], dateTimeValue, options.Date == dateTimeValue));

            return(result);
        }
Ejemplo n.º 3
0
        public async Task <ActionResult> Index([ModelBinder(BinderType = typeof(AuditTrailFilterEngineModelBinder), Name = "q")] QueryFilterResult <AuditTrailEvent> queryFilterResult, PagerParameters pagerParameters, string correlationId = "")
        {
            if (!await _authorizationService.AuthorizeAsync(User, AuditTrailPermissions.ViewAuditTrail))
            {
                return(Forbid());
            }

            var options = new AuditTrailIndexOptions
            {
                FilterResult = queryFilterResult
            };

            // This is used by Contents feature for routing so needs to be passed into the options.
            if (!String.IsNullOrEmpty(correlationId))
            {
                options.CorrelationId          = correlationId;
                options.CorrelationIdFromRoute = true;
            }

            if (options.CorrelationIdFromRoute)
            {
                // When the correlation id is provided via the route or options a placeholder node is used to apply a filter.
                options.FilterResult.TryAddOrReplace(new CorrelationIdFilterNode(options.CorrelationId));
            }

            var siteSettings = await _siteService.GetSiteSettingsAsync();

            var pager = new Pager(pagerParameters, siteSettings.PageSize);

            // With the options populated we filter the query, allowing the filters to alter the options.
            var result = await _auditTrailAdminListQueryService.QueryAsync(pager.Page, pager.PageSize, options);

            // The search text is provided back to the UI.
            options.SearchText         = options.FilterResult.ToString();
            options.OriginalSearchText = options.SearchText;

            // Populate route values to maintain previous route data when generating page links.
            options.RouteValues.TryAdd("q", options.FilterResult.ToString());

            var pagerShape = await _shapeFactory.CreateAsync("Pager", Arguments.From(new
            {
                pager.Page,
                pager.PageSize,
                TotalItemCount = result.TotalCount
            }));

            var items = new List <IShape>();

            foreach (var auditTrailEvent in result.Events)
            {
                items.Add(
                    await _displayManager.BuildDisplayAsync(auditTrailEvent, updater: _updateModelAccessor.ModelUpdater, displayType: "SummaryAdmin")
                    );
            }

            var startIndex = (pager.Page - 1) * (pager.PageSize) + 1;

            options.StartIndex     = startIndex;
            options.EndIndex       = startIndex + items.Count - 1;
            options.EventsCount    = items.Count;
            options.TotalItemCount = result.TotalCount;

            var header = await _auditTrailOptionsDisplayManager.BuildEditorAsync(options, _updateModelAccessor.ModelUpdater, false);

            var shapeViewModel = await _shapeFactory.CreateAsync <AuditTrailListViewModel>("AuditTrailAdminList", viewModel =>
            {
                viewModel.Events  = items;
                viewModel.Pager   = pagerShape;
                viewModel.Options = options;
                viewModel.Header  = header;
            });

            return(View(shapeViewModel));
        }