Skip to content

Commit

Permalink
Merge pull request #281 from Lombiq/issue/OSOE-893
Browse files Browse the repository at this point in the history
OSOE-893: Update shape type in AddTenantReloadWarning
  • Loading branch information
sarahelsaig authored Aug 9, 2024
2 parents ec968a6 + c979481 commit af9a45f
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 80 deletions.
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-18300" />
</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 @@ -6,18 +6,21 @@
using OrchardCore.DisplayManagement.Views;
using OrchardCore.Security.Permissions;
using OrchardCore.Settings;
using System;
using System.Text.Json;
using System.Threading.Tasks;

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; }
[Obsolete($"Override {nameof(SettingsGroupId)} instead. This property will be removed in future versions.")]
protected virtual string GroupId => SettingsGroupId;

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 +33,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 @@ -40,18 +43,18 @@ await AuthorizeAsync()
settings.AdditionalData = await GetAdditionalDataAsync(section, context);
})
.Location(Location)
.OnGroup(GroupId)
.OnGroup(SettingsGroupId)
: 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 @@ -29,18 +30,14 @@ public abstract class SingleViewModelContentPartDisplayDriver<TPart, TViewModel>
/// Attempts to perform the update activity and adds model state errors if necessary, then redirects to the Edit
/// action.
/// </summary>
public override async Task<IDisplayResult> UpdateAsync(
TPart part,
IUpdateModel updater,
UpdatePartEditorContext context)
public override async Task<IDisplayResult> UpdateAsync(TPart part, 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)
{
updater.ModelState.AddModelError(key, error);
context.Updater.ModelState.AddModelError(key, error);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,14 @@ public static OrchardCoreBuilder ConfigureHostingDefaults(
.AddValueIfKeyNotExists("System", "Information")
.AddValueIfKeyNotExists("Microsoft", "Information");

// Orchard Core 1.8 and prior, this can be removed after an Orchard Core upgrade to 2.0.
// OrchardCore_Email_Smtp below is 2.0+.
// Orchard Core 1.8 and prior section. Keeping it here for leftover configs, because it keeps working under
// 2.0 too
var oc18SmtpSection = ocSection.GetSection("SmtpSettings");

if (oc18SmtpSection["Host"] == null)
{
oc18SmtpSection["Host"] = "127.0.0.1";
oc18SmtpSection["RequireCredentials"] = "false";
oc18SmtpSection["Port"] = "25";
}

oc18SmtpSection.AddValueIfKeyNotExists("DefaultSender", "[email protected]");

var smtpSection = ocSection.GetSection("OrchardCore_Email_Smtp");

if (smtpSection["Host"] == null)
if (oc18SmtpSection["Host"] == null && smtpSection["Host"] == null)
{
smtpSection["IsEnabled"] = "true";
smtpSection["Host"] = "127.0.0.1";
smtpSection["RequireCredentials"] = "false";
smtpSection["Port"] = "25";
Expand Down Expand Up @@ -105,7 +96,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-18300" />
<PackageReference Include="OrchardCore.Autoroute" Version="2.0.0-preview-18300" />
<PackageReference Include="OrchardCore.ContentFields" Version="2.0.0-preview-18300" />
<PackageReference Include="OrchardCore.ContentManagement" Version="2.0.0-preview-18300" />
<PackageReference Include="OrchardCore.ContentManagement.Abstractions" Version="2.0.0-preview-18300" />
<PackageReference Include="OrchardCore.Email.Abstractions" Version="2.0.0-preview-18300" />
<PackageReference Include="OrchardCore.FileStorage.Abstractions" Version="2.0.0-preview-18300" />
<PackageReference Include="OrchardCore.Html" Version="2.0.0-preview-18300" />
<PackageReference Include="OrchardCore.Infrastructure" Version="2.0.0-preview-18300" />
<PackageReference Include="OrchardCore.Markdown" Version="2.0.0-preview-18300" />
<PackageReference Include="OrchardCore.Media.Core" Version="2.0.0-preview-18300" />
<PackageReference Include="OrchardCore.Queries" Version="2.0.0-preview-18300" />
<PackageReference Include="OrchardCore.Settings" Version="2.0.0-preview-18300" />
<PackageReference Include="OrchardCore.Setup.Abstractions" Version="2.0.0-preview-18300" />
<PackageReference Include="OrchardCore.Taxonomies" Version="2.0.0-preview-18300" />
<PackageReference Include="OrchardCore.Themes" Version="2.0.0-preview-18300" />
<PackageReference Include="OrchardCore.Title" Version="2.0.0-preview-18300" />
<PackageReference Include="OrchardCore.Users.Core" Version="2.0.0-preview-18300" />
<PackageReference Include="OrchardCore.Workflows.Abstractions" Version="2.0.0-preview-18300" />
</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-18300" />
<PackageReference Include="OrchardCore.ResourceManagement" Version="2.0.0-preview-18300" />
</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-18300" />
<PackageReference Include="OrchardCore.Module.Targets" Version="2.0.0-preview-18300" />
<PackageReference Include="OrchardCore.ContentManagement" Version="2.0.0-preview-18300" />
<PackageReference Include="OrchardCore.ContentTypes.Abstractions" Version="2.0.0-preview-18300" />
<PackageReference Include="OrchardCore.DisplayManagement" Version="2.0.0-preview-18300" />
<PackageReference Include="OrchardCore.Autoroute" Version="2.0.0-preview-18300" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit af9a45f

Please sign in to comment.