Skip to content

Commit

Permalink
Add Async method to ContentDefinitionManager to prevent possible thre…
Browse files Browse the repository at this point in the history
…ad starvation

Fix #14667
  • Loading branch information
MikeAlhayek committed Nov 11, 2023
1 parent b10dd08 commit 62341f3
Show file tree
Hide file tree
Showing 99 changed files with 1,634 additions and 601 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public async Task<IActionResult> Index()
if (model.CanManageDashboard || await _authorizationService.AuthorizeAsync(User, Permissions.AccessAdminDashboard))
{
var wrappers = new List<DashboardWrapper>();
var widgetContentTypes = GetDashboardWidgets();
var widgetContentTypes = await GetDashboardWidgetsAsync();

var widgets = await _adminDashboardService.GetWidgetsAsync(x => x.Published);
foreach (var widget in widgets)
Expand Down Expand Up @@ -99,7 +99,7 @@ public async Task<IActionResult> Manage()
});

var dashboardCreatable = new List<SelectListItem>();
var widgetContentTypes = GetDashboardWidgets();
var widgetContentTypes = await GetDashboardWidgetsAsync();

var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);

Expand Down Expand Up @@ -202,8 +202,8 @@ public async Task<IActionResult> Update([FromForm] DashboardPartViewModel[] part
return RedirectToAction(nameof(Manage));
}

private Dictionary<string, ContentTypeDefinition> GetDashboardWidgets()
=> _contentDefinitionManager.ListTypeDefinitions()
private async Task<Dictionary<string, ContentTypeDefinition>> GetDashboardWidgetsAsync()
=> (await _contentDefinitionManager.ListTypeDefinitionsAsync())
.Where(t => t.StereotypeEquals("DashboardWidget"))
.ToDictionary(ctd => ctd.Name, ctd => ctd);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public AliasPartIndexProvider(IServiceProvider serviceProvider)
_serviceProvider = serviceProvider;
}

public override Task UpdatedAsync(UpdateContentContext context)
public override async Task UpdatedAsync(UpdateContentContext context)
{
var part = context.ContentItem.As<AliasPart>();

Expand All @@ -43,19 +43,19 @@ public override Task UpdatedAsync(UpdateContentContext context)
_contentDefinitionManager ??= _serviceProvider.GetRequiredService<IContentDefinitionManager>();

// Search for this part.
var contentTypeDefinition = _contentDefinitionManager.GetTypeDefinition(context.ContentItem.ContentType);
if (!contentTypeDefinition.Parts.Any(ctpd => ctpd.Name == nameof(AliasPart)))
var contentTypeDefinition = await _contentDefinitionManager.GetTypeDefinitionAsync(context.ContentItem.ContentType);
if (!contentTypeDefinition.Parts.Any(ctd => ctd.Name == nameof(AliasPart)))
{
context.ContentItem.Remove<AliasPart>();
_partRemoved.Add(context.ContentItem.ContentItemId);
}
}

return Task.CompletedTask;
}

public string CollectionName { get; set; }

public Type ForType() => typeof(ContentItem);

public void Describe(IDescriptor context) => Describe((DescribeContext<ContentItem>)context);

public void Describe(DescribeContext<ContentItem> context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,21 @@ public ArchiveLaterPartIndexProvider(IServiceProvider serviceProvider)
_serviceProvider = serviceProvider;
}

public override Task UpdatedAsync(UpdateContentContext context)
public override async Task UpdatedAsync(UpdateContentContext context)
{
var part = context.ContentItem.As<ArchiveLaterPart>();

if (part != null)
{
_contentDefinitionManager ??= _serviceProvider.GetRequiredService<IContentDefinitionManager>();

var contentTypeDefinition = _contentDefinitionManager.GetTypeDefinition(context.ContentItem.ContentType);
var contentTypeDefinition = await _contentDefinitionManager.GetTypeDefinitionAsync(context.ContentItem.ContentType);
if (!contentTypeDefinition.Parts.Any(pd => pd.Name == nameof(ArchiveLaterPart)))
{
context.ContentItem.Remove<ArchiveLaterPart>();
_partRemoved.Add(context.ContentItem.ContentItemId);
}
}

return Task.CompletedTask;
}

public string CollectionName { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;
using OrchardCore.ArchiveLater.Indexes;
using OrchardCore.ArchiveLater.Models;
using OrchardCore.ContentManagement.Metadata;
Expand All @@ -17,9 +18,9 @@ public Migrations(IContentDefinitionManager contentDefinitionManager)
_contentDefinitionManager = contentDefinitionManager;
}

public int Create()
public async Task<int> CreateAsync()
{
_contentDefinitionManager.AlterPartDefinition(nameof(ArchiveLaterPart), builder => builder
await _contentDefinitionManager.AlterPartDefinitionAsync(nameof(ArchiveLaterPart), builder => builder
.Attachable()
.WithDescription("Adds the ability to schedule content items to be archived at a given future date and time."));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ ILogger<ContentLocalizationPartHandlerCoordinator> logger

public override async Task LocalizingAsync(LocalizationContentContext context)
{
var contentTypeDefinition = _contentDefinitionManager.GetTypeDefinition(context.ContentItem.ContentType);
var contentTypeDefinition = await _contentDefinitionManager.GetTypeDefinitionAsync(context.ContentItem.ContentType);
if (contentTypeDefinition == null)
{
return;
}

foreach (var typePartDefinition in contentTypeDefinition.Parts)
{
Expand All @@ -49,9 +51,11 @@ public override async Task LocalizingAsync(LocalizationContentContext context)

public override async Task LocalizedAsync(LocalizationContentContext context)
{
var contentTypeDefinition = _contentDefinitionManager.GetTypeDefinition(context.ContentItem.ContentType);
var contentTypeDefinition = await _contentDefinitionManager.GetTypeDefinitionAsync(context.ContentItem.ContentType);
if (contentTypeDefinition == null)
{
return;
}

foreach (var typePartDefinition in contentTypeDefinition.Parts)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using OrchardCore.ContentLocalization.Models;
using OrchardCore.ContentManagement;
Expand All @@ -19,9 +20,9 @@ public Migrations(IContentDefinitionManager contentDefinitionManager)
_contentDefinitionManager = contentDefinitionManager;
}

public int Create()
public async Task<int> CreateAsync()
{
_contentDefinitionManager.AlterPartDefinition(nameof(LocalizationPart), builder => builder
await _contentDefinitionManager.AlterPartDefinitionAsync(nameof(LocalizationPart), builder => builder
.Attachable()
.WithDescription("Provides a way to create localized version of content."));

Expand Down Expand Up @@ -81,7 +82,7 @@ public int UpdateFrom2()
public int UpdateFrom3()
#pragma warning restore CA1822 // Mark members as static
{
// Defer this until after the subsequent migrations have succeded as the schema has changed.
// Defer this until after the subsequent migrations have succeeded as the schema has changed.
ShellScope.AddDeferredTask(async scope =>
{
var session = scope.ServiceProvider.GetRequiredService<ISession>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ public async Task FilterAsync(ContentOptionsViewModel model, IQuery<ContentItem>
// This is intended to be used by adding ?Localization.ShowLocalizedContentTypes to an AdminMenu url.
if (viewModel.ShowLocalizedContentTypes)
{
var localizedTypes = _contentDefinitionManager
.ListTypeDefinitions()
var localizedTypes = (await _contentDefinitionManager.ListTypeDefinitionsAsync())
.Where(x =>
x.Parts.Any(p =>
p.PartDefinition.Name == nameof(LocalizationPart)))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -29,9 +28,9 @@ public PreviewPartHandler(
/// <summary>
/// Get the pattern from the AutoroutePartSettings property for its type
/// </summary>
private string GetPattern(PreviewPart part)
private async Task<string> GetPatternAsync(PreviewPart part)
{
var contentTypeDefinition = _contentDefinitionManager.GetTypeDefinition(part.ContentItem.ContentType);
var contentTypeDefinition = await _contentDefinitionManager.GetTypeDefinitionAsync(part.ContentItem.ContentType);
var contentTypePartDefinition = contentTypeDefinition.Parts.FirstOrDefault(x => string.Equals(x.PartDefinition.Name, "PreviewPart"));
var pattern = contentTypePartDefinition.GetSettings<PreviewPartSettings>().Pattern;

Expand All @@ -40,7 +39,7 @@ private string GetPattern(PreviewPart part)

public override async Task GetContentItemAspectAsync(ContentItemAspectContext context, PreviewPart part)
{
var pattern = GetPattern(part);
var pattern = await GetPatternAsync(part);

if (!string.IsNullOrEmpty(pattern))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Threading.Tasks;
using OrchardCore.ContentManagement.Metadata;
using OrchardCore.ContentManagement.Metadata.Settings;
using OrchardCore.Data.Migration;
Expand All @@ -13,9 +14,9 @@ public Migrations(IContentDefinitionManager contentDefinitionManager)
_contentDefinitionManager = contentDefinitionManager;
}

public int Create()
public async Task<int> CreateAsync()
{
_contentDefinitionManager.AlterPartDefinition("PreviewPart", builder => builder
await _contentDefinitionManager.AlterPartDefinitionAsync("PreviewPart", builder => builder
.Attachable()
.WithDescription("Provides a way to define the url that is used to render your content item for preview. You only need to use this for the content preview feature when running the frontend decoupled from the admin."));

Expand Down
Loading

0 comments on commit 62341f3

Please sign in to comment.