public async Task FilterAsync(ContentOptionsViewModel model, IQuery <ContentItem> query, IUpdateModel updater) { var viewModel = new LocalizationContentsAdminFilterViewModel(); if (await updater.TryUpdateModelAsync(viewModel, "Localization")) { // Show localization content items // This is intended to be used by adding ?Localization.ShowLocalizedContentTypes to an AdminMenu url. if (viewModel.ShowLocalizedContentTypes) { var localizedTypes = _contentDefinitionManager .ListTypeDefinitions() .Where(x => x.Parts.Any(p => p.PartDefinition.Name == nameof(LocalizationPart))) .Select(x => x.Name); query.With <ContentItemIndex>(x => x.ContentType.IsIn(localizedTypes)); } // Show contained elements for the specified culture else if (!String.IsNullOrEmpty(viewModel.SelectedCulture)) { query.With <LocalizedContentItemIndex>(i => (i.Published || i.Latest) && i.Culture == viewModel.SelectedCulture); } } }
public async Task FilterAsync(ContentOptionsViewModel model, IQuery <ContentItem> query, IUpdateModel updater) { var settings = (await _siteService.GetSiteSettingsAsync()).As <TaxonomyContentsAdminListSettings>(); foreach (var contentItemId in settings.TaxonomyContentItemIds) { var viewModel = new TaxonomyContentsAdminFilterViewModel(); if (await updater.TryUpdateModelAsync(viewModel, "Taxonomy" + contentItemId)) { // Show all items categorized by the taxonomy if (!String.IsNullOrEmpty(viewModel.SelectedContentItemId)) { if (viewModel.SelectedContentItemId.StartsWith("Taxonomy:", StringComparison.OrdinalIgnoreCase)) { viewModel.SelectedContentItemId = viewModel.SelectedContentItemId.Substring(9); query.All( x => query.With <TaxonomyIndex>(x => x.TaxonomyContentItemId == viewModel.SelectedContentItemId) ); } else if (viewModel.SelectedContentItemId.StartsWith("Term:", StringComparison.OrdinalIgnoreCase)) { viewModel.SelectedContentItemId = viewModel.SelectedContentItemId.Substring(5); query.All( x => query.With <TaxonomyIndex>(x => x.TermContentItemId == viewModel.SelectedContentItemId) ); } } } } }
public async Task FilterAsync(ContentOptionsViewModel model, IQuery <ContentItem> query, IUpdateModel updater) { var viewModel = new ListPartContentsAdminFilterViewModel(); if (await updater.TryUpdateModelAsync(viewModel, nameof(ListPart))) { // Show list content items if (viewModel.ShowListContentTypes) { var listableTypes = _contentDefinitionManager .ListTypeDefinitions() .Where(x => x.Parts.Any(p => p.PartDefinition.Name == nameof(ListPart))) .Select(x => x.Name); query.With <ContentItemIndex>(x => x.ContentType.IsIn(listableTypes)); } // Show contained elements for the specified list else if (viewModel.ListContentItemId != null) { query.With <ContainedPartIndex>(x => x.ListContentItemId == viewModel.ListContentItemId); } } }
public async Task <IQuery <ContentItem> > QueryAsync(ContentOptionsViewModel model, IUpdateModel updater) { // Because admin filters can add a different index to the query this must be added as a Query<ContentItem>() var query = _session.Query <ContentItem>(); await _contentsAdminListFilters.InvokeAsync((filter, model, query, updater) => filter.FilterAsync(model, query, updater), model, query, updater, _logger); return(query); }
public async Task <IQuery <ContentItem> > QueryAsync(ContentOptionsViewModel model, IUpdateModel updater) { // Because admin filters can add a different index to the query this must be added as a Query<ContentItem>() var query = _session.Query <ContentItem>(); query = await model.FilterResult.ExecuteAsync(new ContentQueryContext(_serviceProvider, query)); // After the q=xx filters have been applied, allow the secondary filter providers to also parse other values for filtering. await _contentsAdminListFilters.InvokeAsync((filter, model, query, updater) => filter.FilterAsync(model, query, updater), model, query, updater, _logger); return(query); }
public async Task <ActionResult> ListFilterPOST(ContentOptionsViewModel options) { // 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(List), new RouteValueDictionary { { "q", options.SearchText } })); } // Evaluate the values provided in the form post and map them to the filter result and route values. await _contentOptionsDisplayManager.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(List), options.RouteValues)); }
public async Task FilterAsync(ContentOptionsViewModel model, IQuery <ContentItem> query, IUpdateModel updater) { var viewModel = new LocalizationContentsAdminFilterViewModel(); if (await updater.TryUpdateModelAsync(viewModel, "Localization")) { // Show localization content items // This is intended to be used by adding ?Localization.ShowLocalizedContentTypes to an AdminMenu url. if (viewModel.ShowLocalizedContentTypes) { var localizedTypes = _contentDefinitionManager .ListTypeDefinitions() .Where(x => x.Parts.Any(p => p.PartDefinition.Name == nameof(LocalizationPart))) .Select(x => x.Name); query.With <ContentItemIndex>(x => x.ContentType.IsIn(localizedTypes)); } } }
public async Task <ActionResult> ListPOST(ContentOptionsViewModel options, IEnumerable <int> itemIds) { if (itemIds?.Count() > 0) { var checkedContentItems = await _session.Query <ContentItem, ContentItemIndex>().Where(x => x.DocumentId.IsIn(itemIds) && x.Latest).ListAsync(); switch (options.BulkAction) { case ContentsBulkAction.None: break; case ContentsBulkAction.PublishNow: foreach (var item in checkedContentItems) { if (!await _authorizationService.AuthorizeAsync(User, CommonPermissions.PublishContent, item)) { _notifier.Warning(H["Couldn't publish selected content."]); _session.Cancel(); return(Forbid()); } await _contentManager.PublishAsync(item); } _notifier.Success(H["Content successfully published."]); break; case ContentsBulkAction.Unpublish: foreach (var item in checkedContentItems) { if (!await _authorizationService.AuthorizeAsync(User, CommonPermissions.PublishContent, item)) { _notifier.Warning(H["Couldn't unpublish selected content."]); _session.Cancel(); return(Forbid()); } await _contentManager.UnpublishAsync(item); } _notifier.Success(H["Content successfully unpublished."]); break; case ContentsBulkAction.Remove: foreach (var item in checkedContentItems) { if (!await _authorizationService.AuthorizeAsync(User, CommonPermissions.DeleteContent, item)) { _notifier.Warning(H["Couldn't remove selected content."]); _session.Cancel(); return(Forbid()); } await _contentManager.RemoveAsync(item); } _notifier.Success(H["Content successfully removed."]); break; default: throw new ArgumentOutOfRangeException(); } } return(RedirectToAction("List")); }
public async Task <IActionResult> List( [ModelBinder(BinderType = typeof(ContentItemFilterEngineModelBinder), Name = "q")] QueryFilterResult <ContentItem> queryFilterResult, ContentOptionsViewModel options, PagerParameters pagerParameters, string contentTypeId = "") { var context = _httpContextAccessor.HttpContext; var contentTypeDefinitions = _contentDefinitionManager.ListTypeDefinitions() .Where(ctd => ctd.GetSettings <ContentTypeSettings>().Creatable) .OrderBy(ctd => ctd.DisplayName); if (!await _authorizationService.AuthorizeContentTypeDefinitionsAsync(User, CommonPermissions.EditContent, contentTypeDefinitions, _contentManager)) { return(Forbid()); } var siteSettings = await _siteService.GetSiteSettingsAsync(); var pager = new Pager(pagerParameters, siteSettings.PageSize); // This is used by the AdminMenus so needs to be passed into the options. if (!String.IsNullOrEmpty(contentTypeId)) { options.SelectedContentType = contentTypeId; } // The filter is bound seperately and mapped to the options. // The options must still be bound so that options that are not filters are still bound options.FilterResult = queryFilterResult; // Populate the creatable types. if (!String.IsNullOrEmpty(options.SelectedContentType)) { // When the selected content type is provided via the route or options a placeholder node is used to apply a filter. options.FilterResult.TryAddOrReplace(new ContentTypeFilterNode(options.SelectedContentType)); var contentTypeDefinition = _contentDefinitionManager.GetTypeDefinition(options.SelectedContentType); if (contentTypeDefinition == null) { return(NotFound()); } var creatableList = new List <SelectListItem>(); // Allows non creatable types to be created by another admin page. if (contentTypeDefinition.GetSettings <ContentTypeSettings>().Creatable || options.CanCreateSelectedContentType) { var contentItem = await _contentManager.NewAsync(contentTypeDefinition.Name); contentItem.Owner = context.User.FindFirstValue(ClaimTypes.NameIdentifier); if (await _authorizationService.AuthorizeAsync(context.User, CommonPermissions.EditContent, contentItem)) { creatableList.Add(new SelectListItem(new LocalizedString(contentTypeDefinition.DisplayName, contentTypeDefinition.DisplayName).Value, contentTypeDefinition.Name)); } } options.CreatableTypes = creatableList; } if (options.CreatableTypes == null) { var creatableList = new List <SelectListItem>(); if (contentTypeDefinitions.Any()) { foreach (var contentTypeDefinition in contentTypeDefinitions) { var contentItem = await _contentManager.NewAsync(contentTypeDefinition.Name); contentItem.Owner = context.User.FindFirstValue(ClaimTypes.NameIdentifier); if (await _authorizationService.AuthorizeAsync(context.User, CommonPermissions.EditContent, contentItem)) { creatableList.Add(new SelectListItem(new LocalizedString(contentTypeDefinition.DisplayName, contentTypeDefinition.DisplayName).Value, contentTypeDefinition.Name)); } } } options.CreatableTypes = creatableList; } // We populate the remaining SelectLists. options.ContentStatuses = new List <SelectListItem>() { new SelectListItem() { Text = S["Latest"], Value = nameof(ContentsStatus.Latest), Selected = (options.ContentsStatus == ContentsStatus.Latest) }, new SelectListItem() { Text = S["Published"], Value = nameof(ContentsStatus.Published), Selected = (options.ContentsStatus == ContentsStatus.Published) }, new SelectListItem() { Text = S["Unpublished"], Value = nameof(ContentsStatus.Draft), Selected = (options.ContentsStatus == ContentsStatus.Draft) }, new SelectListItem() { Text = S["All versions"], Value = nameof(ContentsStatus.AllVersions), Selected = (options.ContentsStatus == ContentsStatus.AllVersions) } }; if (await _authorizationService.AuthorizeAsync(context.User, Permissions.ListContent)) { options.ContentStatuses.Insert(1, new SelectListItem() { Text = S["Owned by me"], Value = nameof(ContentsStatus.Owner) }); } options.ContentSorts = new List <SelectListItem>() { new SelectListItem() { Text = S["Recently created"], Value = nameof(ContentsOrder.Created), Selected = (options.OrderBy == ContentsOrder.Created) }, new SelectListItem() { Text = S["Recently modified"], Value = nameof(ContentsOrder.Modified), Selected = (options.OrderBy == ContentsOrder.Modified) }, new SelectListItem() { Text = S["Recently published"], Value = nameof(ContentsOrder.Published), Selected = (options.OrderBy == ContentsOrder.Published) }, new SelectListItem() { Text = S["Title"], Value = nameof(ContentsOrder.Title), Selected = (options.OrderBy == ContentsOrder.Title) }, }; options.ContentsBulkAction = new List <SelectListItem>() { new SelectListItem() { Text = S["Publish Now"], Value = nameof(ContentsBulkAction.PublishNow) }, new SelectListItem() { Text = S["Unpublish"], Value = nameof(ContentsBulkAction.Unpublish) }, new SelectListItem() { Text = S["Delete"], Value = nameof(ContentsBulkAction.Remove) } }; if ((String.IsNullOrEmpty(options.SelectedContentType) || String.IsNullOrEmpty(contentTypeId)) && options.ContentTypeOptions == null) { var listableTypes = new List <ContentTypeDefinition>(); var userNameIdentifier = context.User.FindFirstValue(ClaimTypes.NameIdentifier); foreach (var ctd in _contentDefinitionManager.ListTypeDefinitions()) { if (ctd.GetSettings <ContentTypeSettings>().Listable) { var contentItem = await _contentManager.NewAsync(ctd.Name); contentItem.Owner = userNameIdentifier; var authorized = await _authorizationService.AuthorizeAsync(User, CommonPermissions.EditContent, contentItem); if (authorized) { listableTypes.Add(ctd); } } } var contentTypeOptions = listableTypes .Select(ctd => new KeyValuePair <string, string>(ctd.Name, ctd.DisplayName)) .ToList().OrderBy(kvp => kvp.Value); options.ContentTypeOptions = new List <SelectListItem> { new SelectListItem() { Text = S["All content types"], Value = "" } }; foreach (var option in contentTypeOptions) { options.ContentTypeOptions.Add(new SelectListItem() { Text = option.Value, Value = option.Key, Selected = (option.Value == options.SelectedContentType) }); } } // If ContentTypeOptions is not initialized by query string or by the code above, initialize it if (options.ContentTypeOptions == null) { options.ContentTypeOptions = new List <SelectListItem>(); } // With the options populated we filter the query, allowing the filters to alter the options. var query = await _contentsAdminListQueryService.QueryAsync(options, _updateModelAccessor.ModelUpdater); // 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 routeData = new RouteData(options.RouteValues); var maxPagedCount = siteSettings.MaxPagedCount; if (maxPagedCount > 0 && pager.PageSize > maxPagedCount) { pager.PageSize = maxPagedCount; } var pagerShape = (await New.Pager(pager)).TotalItemCount(maxPagedCount > 0 ? maxPagedCount : await query.CountAsync()).RouteData(routeData); // Load items so that loading handlers are invoked. var pageOfContentItems = await query.Skip(pager.GetStartIndex()).Take(pager.PageSize).ListAsync(_contentManager); // We prepare the content items SummaryAdmin shape var contentItemSummaries = new List <dynamic>(); foreach (var contentItem in pageOfContentItems) { contentItemSummaries.Add(await _contentItemDisplayManager.BuildDisplayAsync(contentItem, _updateModelAccessor.ModelUpdater, "SummaryAdmin")); } // Populate options pager summary values. var startIndex = (pagerShape.Page - 1) * (pagerShape.PageSize) + 1; options.StartIndex = startIndex; options.EndIndex = startIndex + contentItemSummaries.Count - 1; options.ContentItemsCount = contentItemSummaries.Count; options.TotalItemCount = pagerShape.TotalItemCount; var header = await _contentOptionsDisplayManager.BuildEditorAsync(options, _updateModelAccessor.ModelUpdater, false); var shapeViewModel = await _shapeFactory.CreateAsync <ListContentsViewModel>("ContentsAdminList", viewModel => { viewModel.ContentItems = contentItemSummaries; viewModel.Pager = pagerShape; viewModel.Options = options; viewModel.Header = header; }); return(View(shapeViewModel)); }
public async Task FilterAsync(ContentOptionsViewModel model, IQuery <ContentItem> query, IUpdateModel updater) { if (!String.IsNullOrEmpty(model.DisplayText)) { query.With <ContentItemIndex>(x => x.DisplayText.Contains(model.DisplayText)); } switch (model.ContentsStatus) { case ContentsStatus.Published: query.With <ContentItemIndex>(x => x.Published); break; case ContentsStatus.Draft: query.With <ContentItemIndex>(x => x.Latest && !x.Published); break; case ContentsStatus.AllVersions: query.With <ContentItemIndex>(x => x.Latest); break; default: query.With <ContentItemIndex>(x => x.Latest); break; } if (model.ContentsStatus == ContentsStatus.Owner) { query.With <ContentItemIndex>(x => x.Owner == _httpContextAccessor.HttpContext.User.Identity.Name); } // Filter the creatable types. if (!string.IsNullOrEmpty(model.SelectedContentType)) { var contentTypeDefinition = _contentDefinitionManager.GetTypeDefinition(model.SelectedContentType); if (contentTypeDefinition != null) { // We display a specific type even if it's not listable so that admin pages // can reuse the Content list page for specific types. query.With <ContentItemIndex>(x => x.ContentType == model.SelectedContentType); } } else { var listableTypes = new List <ContentTypeDefinition>(); foreach (var ctd in _contentDefinitionManager.ListTypeDefinitions()) { if (ctd.GetSettings <ContentTypeSettings>().Listable) { var authorized = await _authorizationService.AuthorizeAsync(_httpContextAccessor.HttpContext.User, Permissions.EditContent, await _contentManager.NewAsync(ctd.Name)); if (authorized) { listableTypes.Add(ctd); } } } if (listableTypes.Any()) { query.With <ContentItemIndex>(x => x.ContentType.IsIn(listableTypes.Select(t => t.Name).ToArray())); } } // Apply OrderBy filters. switch (model.OrderBy) { case ContentsOrder.Modified: query.With <ContentItemIndex>().OrderByDescending(x => x.ModifiedUtc); break; case ContentsOrder.Published: query.With <ContentItemIndex>().OrderByDescending(cr => cr.PublishedUtc); break; case ContentsOrder.Created: query.With <ContentItemIndex>().OrderByDescending(cr => cr.CreatedUtc); break; case ContentsOrder.Title: query.With <ContentItemIndex>().OrderBy(cr => cr.DisplayText); break; default: query.With <ContentItemIndex>().OrderByDescending(cr => cr.ModifiedUtc); break; } ; }
public async Task FilterAsync(ContentOptionsViewModel model, IQuery <ContentItem> query, IUpdateModel updater) { var user = _httpContextAccessor.HttpContext.User; var userNameIdentifier = user.FindFirstValue(ClaimTypes.NameIdentifier); if (!String.IsNullOrEmpty(model.DisplayText)) { query.With <ContentItemIndex>(x => x.DisplayText.Contains(model.DisplayText)); } switch (model.ContentsStatus) { case ContentsStatus.Published: query.With <ContentItemIndex>(x => x.Published); break; case ContentsStatus.Draft: query.With <ContentItemIndex>(x => x.Latest && !x.Published); break; case ContentsStatus.AllVersions: query.With <ContentItemIndex>(x => x.Latest); break; default: query.With <ContentItemIndex>(x => x.Latest); break; } var canListAllContent = await _authorizationService.AuthorizeAsync(user, Permissions.ListContent); // Filter the creatable types. if (!string.IsNullOrEmpty(model.SelectedContentType)) { var contentTypeDefinition = _contentDefinitionManager.GetTypeDefinition(model.SelectedContentType); if (contentTypeDefinition != null) { // We display a specific type even if it's not listable so that admin pages // can reuse the Content list page for specific types. var contentItem = await _contentManager.NewAsync(contentTypeDefinition.Name); contentItem.Owner = userNameIdentifier; var hasContentListPermission = await _authorizationService.AuthorizeAsync(user, ContentTypePermissionsHelper.CreateDynamicPermission(ContentTypePermissionsHelper.PermissionTemplates[CommonPermissions.ListContent.Name], contentTypeDefinition), contentItem); if (hasContentListPermission) { query.With <ContentItemIndex>(x => x.ContentType == model.SelectedContentType); } else { query.With <ContentItemIndex>(x => x.ContentType == model.SelectedContentType && x.Owner == userNameIdentifier); } } } else { var listableTypes = new List <ContentTypeDefinition>(); var authorizedContentTypes = new List <ContentTypeDefinition>(); var unauthorizedContentTypes = new List <ContentTypeDefinition>(); foreach (var ctd in _contentDefinitionManager.ListTypeDefinitions()) { if (ctd.GetSettings <ContentTypeSettings>().Listable) { // We want to list the content item if the user can edit their own items at least. // It might display content items the user won't be able to edit though. var contentItem = await _contentManager.NewAsync(ctd.Name); contentItem.Owner = userNameIdentifier; var hasEditPermission = await _authorizationService.AuthorizeAsync(user, CommonPermissions.EditContent, contentItem); if (hasEditPermission) { listableTypes.Add(ctd); } if (!canListAllContent) { var hasContentListPermission = await _authorizationService.AuthorizeAsync(user, ContentTypePermissionsHelper.CreateDynamicPermission(ContentTypePermissionsHelper.PermissionTemplates[CommonPermissions.ListContent.Name], ctd), contentItem); if (hasContentListPermission) { authorizedContentTypes.Add(ctd); } else { unauthorizedContentTypes.Add(ctd); } } } } if (authorizedContentTypes.Any() && !canListAllContent) { query.With <ContentItemIndex>().Where(x => (x.ContentType.IsIn(authorizedContentTypes.Select(t => t.Name).ToArray())) || (x.ContentType.IsIn(unauthorizedContentTypes.Select(t => t.Name).ToArray()) && x.Owner == userNameIdentifier)); } else { query.With <ContentItemIndex>(x => x.ContentType.IsIn(listableTypes.Select(t => t.Name).ToArray())); // If we set the ListContent permission // to false we can only view our own content and // we bypass the corresponding ContentsStatus by owned content filtering if (!canListAllContent) { query.With <ContentItemIndex>(x => x.Owner == userNameIdentifier); } else { if (model.ContentsStatus == ContentsStatus.Owner) { query.With <ContentItemIndex>(x => x.Owner == userNameIdentifier); } } } } // Apply OrderBy filters. switch (model.OrderBy) { case ContentsOrder.Modified: query.With <ContentItemIndex>().OrderByDescending(x => x.ModifiedUtc); break; case ContentsOrder.Published: query.With <ContentItemIndex>().OrderByDescending(cr => cr.PublishedUtc); break; case ContentsOrder.Created: query.With <ContentItemIndex>().OrderByDescending(cr => cr.CreatedUtc); break; case ContentsOrder.Title: query.With <ContentItemIndex>().OrderBy(cr => cr.DisplayText); break; default: query.With <ContentItemIndex>().OrderByDescending(cr => cr.ModifiedUtc); break; } ; }