Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OSOE-893: Update shape type in AddTenantReloadWarning #281

Merged
merged 15 commits into from
Aug 9, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@

<ItemGroup>
<PackageReference Include="linq2db" Version="5.4.0" />
<PackageReference Include="OrchardCore.Data.YesSql" Version="2.0.0-preview-18288" />
<PackageReference Include="OrchardCore.Data.YesSql" Version="2.0.0-preview-18297" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
using OrchardCore.DisplayManagement.Entities;
#nullable enable

using OrchardCore.DisplayManagement.Entities;
using System;
using System.Threading.Tasks;

namespace OrchardCore.DisplayManagement.Handlers;

public static class BuildEditorContextExtensions
{
/// <summary>
/// Creates a new instance of <typeparamref name="TViewModel"/> and tries to populate it from the <paramref
/// name="context"/> with the <paramref name="prefix"/>.
/// </summary>
/// <param name="context">The editor context of the current update request.</param>
/// <param name="prefix">The name prefix of the fields being edited.</param>
/// <inheritdoc cref="CreateModelAsync{TViewModel}"/>
/// <param name="groupId">
/// If not <see langword="null"/>, it's needed to check the group (e.g. in <see
/// cref="SectionDisplayDriver{TModel,TSection}"/>. The value is checked against the <see
Expand All @@ -21,26 +18,55 @@ public static class BuildEditorContextExtensions
/// If not <see langword="null"/> and the awaited result is <see langword="false"/>, then <see langword="null"/> is
/// returned.
/// </param>
/// <typeparam name="TViewModel">The expected view-model type.</typeparam>
/// <returns>
/// A new instance of <typeparamref name="TViewModel"/>, populated with data from <paramref name="context"/>. Unless
/// at least one of <paramref name="groupId"/> and <paramref name="authorizeAsync"/> are provided and the checks
/// failed, at which case <see langword="null"/> is returned.
/// </returns>
public static async Task<TViewModel> CreateModelMaybeAsync<TViewModel>(
[Obsolete($"Inherit your driver from {nameof(SiteDisplayDriver<object>)} instead, which does the group ID check implicitly.")]
public static Task<TViewModel?> CreateModelMaybeAsync<TViewModel>(
this BuildEditorContext context,
string prefix,
string groupId = null,
Func<Task<bool>> authorizeAsync = null)
where TViewModel : class, new()
{
if (!string.IsNullOrEmpty(groupId) && context.GroupId != groupId) return null;
string? prefix,
string groupId,
Func<Task<bool>>? authorizeAsync = null)
where TViewModel : class, new() =>
!string.IsNullOrEmpty(groupId) && context.GroupId != groupId
? Task.FromResult<TViewModel?>(null)
: context.CreateModelMaybeAsync<TViewModel>(prefix, authorizeAsync);

if (authorizeAsync != null && !await authorizeAsync())
{
return null;
}
/// <inheritdoc cref="CreateModelAsync{TViewModel}"/>
/// <param name="authorizeAsync">If <see langword="false"/>, then <see langword="null"/> is returned.</param>
/// <returns>
/// A new instance of <typeparamref name="TViewModel"/> populated with data from <paramref name="context"/>, or <see
/// langword="null"/> if <paramref name="authorizeAsync"/>'s check fails.
/// </returns>
/// <remarks><para>
/// If authorization is not needed, use <see cref="CreateModelAsync{TViewModel}"/> instead.
/// </para></remarks>
public static async Task<TViewModel?> CreateModelMaybeAsync<TViewModel>(
this BuildEditorContext context,
string? prefix,
Func<Task<bool>>? authorizeAsync)
where TViewModel : class, new() =>
authorizeAsync != null && !await authorizeAsync()
? null
: await context.CreateModelAsync<TViewModel>(prefix);

/// <summary>
/// Creates a new instance of <typeparamref name="TViewModel"/> and tries to populate it from the <paramref
/// name="context"/> with the <paramref name="prefix"/>.
/// </summary>
/// <param name="context">The editor context of the current update request.</param>
/// <param name="prefix">
/// The name prefix of the fields being edited, usually <see cref="DisplayDriverBase.Prefix"/>.
/// </param>
/// <typeparam name="TViewModel">The expected view-model type.</typeparam>
/// <returns>
/// A new instance of <typeparamref name="TViewModel"/>, populated with data from <paramref name="context"/>.
/// </returns>
public static async Task<TViewModel> CreateModelAsync<TViewModel>(this BuildEditorContext context, string? prefix)
where TViewModel : class, new()
{
var viewModel = new TViewModel();
await context.Updater.TryUpdateModelAsync(viewModel, prefix);
return viewModel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@

namespace Lombiq.HelpfulLibraries.OrchardCore.Contents;

public abstract class JsonSectionDisplayDriver<TSection, TAdditionalData> : SectionDisplayDriver<ISite, TSection>
public abstract class JsonSectionDisplayDriver<TSection, TAdditionalData> : SiteDisplayDriver<TSection>
where TSection : class, new()
{
protected abstract string GroupId { get; }
protected virtual Permission Permission => null;
protected virtual string ShapeType => $"{typeof(TSection).Name}_Edit";
protected virtual string Location => "Content:1";
protected virtual string Location => $"{CommonLocationNames.Content}:1";

protected readonly IAuthorizationService _authorizationService;
protected readonly IHttpContextAccessor _hca;
Expand All @@ -30,7 +30,7 @@ protected JsonSectionDisplayDriver(
_hca = hca;
}

public override async Task<IDisplayResult> EditAsync(TSection section, BuildEditorContext context) =>
public async override Task<IDisplayResult> EditAsync(ISite model, TSection section, BuildEditorContext context) =>
await AuthorizeAsync()
? Initialize<JsonViewModel<TAdditionalData>>(
ShapeType,
Expand All @@ -43,15 +43,15 @@ await AuthorizeAsync()
.OnGroup(GroupId)
: null;

public override async Task<IDisplayResult> UpdateAsync(TSection section, UpdateEditorContext context)
public override async Task<IDisplayResult> UpdateAsync(ISite model, TSection section, UpdateEditorContext context)
{
if (await context.CreateModelMaybeAsync<JsonViewModel<TAdditionalData>>(Prefix, GroupId, AuthorizeAsync) is { } viewModel &&
if (await context.CreateModelMaybeAsync<JsonViewModel<TAdditionalData>>(Prefix, AuthorizeAsync) is { } viewModel &&
TryParseJson(viewModel.Json, out var result))
{
await UpdateAsync(section, context, result);
}

return await EditAsync(section, context);
return await EditAsync(model, section, context);
}

protected abstract Task UpdateAsync(TSection section, BuildEditorContext context, TSection viewModel);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using OrchardCore.ContentManagement.Display.Models;
using OrchardCore.DisplayManagement.Handlers;
using OrchardCore.DisplayManagement.ModelBinding;
using OrchardCore.DisplayManagement.Views;
using System.Collections.Generic;
Expand Down Expand Up @@ -34,9 +35,8 @@ public override async Task<IDisplayResult> UpdateAsync(
IUpdateModel updater,
UpdatePartEditorContext context)
{
var viewModel = new TViewModel();
if (await updater.TryUpdateModelAsync(viewModel, Prefix) &&
await UpdateAsync(part, viewModel, context) is { } updateResults)
var viewModel = await context.CreateModelAsync<TViewModel>(Prefix);
if (await UpdateAsync(part, viewModel, context) is { } updateResults)
{
foreach (var (key, error) in updateResults)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ public static OrchardCoreBuilder ConfigureHostingDefaults(

builder
.AddDatabaseShellsConfigurationIfAvailable(webApplicationBuilder.Configuration)
.ConfigureSmtpSettings(overrideAdminSettings: false)
.ConfigureSecurityDefaultsWithStaticFiles(allowInlineStyle: true);

return builder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,31 @@

<ItemGroup>
<PackageReference Include="NodaTime" Version="3.1.11" />
<PackageReference Include="OrchardCore.Alias" Version="2.0.0-preview-18288" />
<PackageReference Include="OrchardCore.Autoroute" Version="2.0.0-preview-18288" />
<PackageReference Include="OrchardCore.ContentFields" Version="2.0.0-preview-18288" />
<PackageReference Include="OrchardCore.ContentManagement" Version="2.0.0-preview-18288" />
<PackageReference Include="OrchardCore.ContentManagement.Abstractions" Version="2.0.0-preview-18288" />
<PackageReference Include="OrchardCore.Email.Abstractions" Version="2.0.0-preview-18288" />
<PackageReference Include="OrchardCore.FileStorage.Abstractions" Version="2.0.0-preview-18288" />
<PackageReference Include="OrchardCore.Html" Version="2.0.0-preview-18288" />
<PackageReference Include="OrchardCore.Infrastructure" Version="2.0.0-preview-18288" />
<PackageReference Include="OrchardCore.Markdown" Version="2.0.0-preview-18288" />
<PackageReference Include="OrchardCore.Media.Core" Version="2.0.0-preview-18288" />
<PackageReference Include="OrchardCore.Queries" Version="2.0.0-preview-18288" />
<PackageReference Include="OrchardCore.Settings" Version="2.0.0-preview-18288" />
<PackageReference Include="OrchardCore.Setup.Abstractions" Version="2.0.0-preview-18288" />
<PackageReference Include="OrchardCore.Taxonomies" Version="2.0.0-preview-18288" />
<PackageReference Include="OrchardCore.Themes" Version="2.0.0-preview-18288" />
<PackageReference Include="OrchardCore.Title" Version="2.0.0-preview-18288" />
<PackageReference Include="OrchardCore.Users.Core" Version="2.0.0-preview-18288" />
<PackageReference Include="OrchardCore.Workflows.Abstractions" Version="2.0.0-preview-18288" />
<PackageReference Include="OrchardCore.Alias" Version="2.0.0-preview-18297" />
<PackageReference Include="OrchardCore.Autoroute" Version="2.0.0-preview-18297" />
<PackageReference Include="OrchardCore.ContentFields" Version="2.0.0-preview-18297" />
<PackageReference Include="OrchardCore.ContentManagement" Version="2.0.0-preview-18297" />
<PackageReference Include="OrchardCore.ContentManagement.Abstractions" Version="2.0.0-preview-18297" />
<PackageReference Include="OrchardCore.Email.Abstractions" Version="2.0.0-preview-18297" />
<PackageReference Include="OrchardCore.FileStorage.Abstractions" Version="2.0.0-preview-18297" />
<PackageReference Include="OrchardCore.Html" Version="2.0.0-preview-18297" />
<PackageReference Include="OrchardCore.Infrastructure" Version="2.0.0-preview-18297" />
<PackageReference Include="OrchardCore.Markdown" Version="2.0.0-preview-18297" />
<PackageReference Include="OrchardCore.Media.Core" Version="2.0.0-preview-18297" />
<PackageReference Include="OrchardCore.Queries" Version="2.0.0-preview-18297" />
<PackageReference Include="OrchardCore.Settings" Version="2.0.0-preview-18297" />
<PackageReference Include="OrchardCore.Setup.Abstractions" Version="2.0.0-preview-18297" />
<PackageReference Include="OrchardCore.Taxonomies" Version="2.0.0-preview-18297" />
<PackageReference Include="OrchardCore.Themes" Version="2.0.0-preview-18297" />
<PackageReference Include="OrchardCore.Title" Version="2.0.0-preview-18297" />
<PackageReference Include="OrchardCore.Users.Core" Version="2.0.0-preview-18297" />
<PackageReference Include="OrchardCore.Workflows.Abstractions" Version="2.0.0-preview-18297" />
</ItemGroup>

<!-- Necessary so tag helpers will work in the projects depending on this. -->
<ItemGroup>
<PackageReference Include="OrchardCore.DisplayManagement" Version="2.0.0-preview-18288" />
<PackageReference Include="OrchardCore.ResourceManagement" Version="2.0.0-preview-18288" />
<PackageReference Include="OrchardCore.DisplayManagement" Version="2.0.0-preview-18297" />
<PackageReference Include="OrchardCore.ResourceManagement" Version="2.0.0-preview-18297" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 4 additions & 2 deletions Lombiq.HelpfulLibraries.OrchardCore/Shapes/ShapeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,10 @@ public static async Task<IShape> CreateAdHocShapeForCurrentThemeAsync(
}

/// <summary>
/// Adds the warning to the screen which says "The current tenant will be reloaded when the settings are saved.".
/// Adds the warning to the screen which says "The website might be restarted upon saving the settings, potentially
/// leading to temporary unresponsiveness during the process.".
/// </summary>
[Obsolete("Use OC's AddTenantReloadWarningWrapper extension method instead.")]
public static void AddTenantReloadWarning(this IShape shape) =>
shape.Metadata.Wrappers.Add("Settings_Wrapper__General");
shape.Metadata.Wrappers.Add("Settings_Wrapper__Reload");
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public abstract class SimpleEventActivityDisplayDriverBase<TActivity> : DisplayD

private string IconHtml => string.IsNullOrEmpty(IconClass) ? string.Empty : $"<i class=\"fa {IconClass}\"></i>";

public override IDisplayResult Display(TActivity model) =>
public override IDisplayResult Display(TActivity model, BuildDisplayContext context) =>
Combine(
this.RawHtml(ThumbnailHtml(model)).Location(CommonContentDisplayTypes.Thumbnail, CommonLocationNames.Content),
this.RawHtml(DesignHtml(model)).Location(CommonContentDisplayTypes.Design, CommonLocationNames.Content));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="OrchardCore.ContentFields" Version="2.0.0-preview-18288" />
<PackageReference Include="OrchardCore.Module.Targets" Version="2.0.0-preview-18288" />
<PackageReference Include="OrchardCore.ContentManagement" Version="2.0.0-preview-18288" />
<PackageReference Include="OrchardCore.ContentTypes.Abstractions" Version="2.0.0-preview-18288" />
<PackageReference Include="OrchardCore.DisplayManagement" Version="2.0.0-preview-18288" />
<PackageReference Include="OrchardCore.Autoroute" Version="2.0.0-preview-18288" />
<PackageReference Include="OrchardCore.ContentFields" Version="2.0.0-preview-18297" />
<PackageReference Include="OrchardCore.Module.Targets" Version="2.0.0-preview-18297" />
<PackageReference Include="OrchardCore.ContentManagement" Version="2.0.0-preview-18297" />
<PackageReference Include="OrchardCore.ContentTypes.Abstractions" Version="2.0.0-preview-18297" />
<PackageReference Include="OrchardCore.DisplayManagement" Version="2.0.0-preview-18297" />
<PackageReference Include="OrchardCore.Autoroute" Version="2.0.0-preview-18297" />
</ItemGroup>

<ItemGroup>
Expand Down