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

JSON Content Editor #54

Merged
merged 34 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
2958e73
Add model, viewmodel and controller.
sarahelsaig Nov 27, 2023
cb6f93a
Post action.
sarahelsaig Nov 27, 2023
3d4bdfe
Rename files and fix route mapping.
sarahelsaig Dec 2, 2023
8eb3c6c
Add actions menu.
sarahelsaig Dec 2, 2023
2321bde
Add title part that works the same way as regular Edit.
sarahelsaig Dec 3, 2023
4587141
Handle returnUrl as expected.
sarahelsaig Dec 3, 2023
9263150
Add UI test to verify content editing.
sarahelsaig Dec 3, 2023
efc865d
Don't use the first item.
sarahelsaig Dec 3, 2023
be3047c
Add documentation in readme.
sarahelsaig Dec 5, 2023
3a9687b
Update Lombiq.JsonEditor/Drivers/EditJsonActionsMenuContentDisplayDri…
sarahelsaig Dec 5, 2023
40665db
Fix readme.
sarahelsaig Dec 5, 2023
0cf11d4
Add warning note.
sarahelsaig Dec 5, 2023
df79fd3
Make the title show up.
sarahelsaig Dec 5, 2023
3735468
Menu item only if authorized.
sarahelsaig Dec 5, 2023
daf2434
Split into separate feature.
sarahelsaig Dec 5, 2023
e476a59
Only if feature is enabled.
sarahelsaig Dec 5, 2023
0f34c5e
Ensure content is published.
sarahelsaig Dec 5, 2023
9879110
Generate new version ID.
sarahelsaig Dec 5, 2023
d8c4f83
Better title text.
sarahelsaig Dec 5, 2023
d943930
Remove unnecessary line.
sarahelsaig Dec 5, 2023
c00d589
call LoadAsync
sarahelsaig Dec 8, 2023
9e9287f
Clean up call order.
sarahelsaig Dec 8, 2023
979afa5
Use ApiController.Post to update the content item.
sarahelsaig Dec 8, 2023
a8424da
Add draft feature.
sarahelsaig Dec 8, 2023
25c2106
Don't fail if the user doesn't have AccessContentApi permission.
sarahelsaig Dec 8, 2023
156e741
Merge branch 'Lombiq:dev' into content-editor
sarahelsaig Dec 8, 2023
7e95d84
Bug fix.
sarahelsaig Dec 8, 2023
79490b9
Merge branch 'content-editor' of https://github.com/sarahelsaig/Orcha…
sarahelsaig Dec 8, 2023
bbfceff
Apply suggestions from code review
sarahelsaig Dec 12, 2023
aaf6574
Not needed.
sarahelsaig Dec 12, 2023
a4c119e
Inject the API controller.
sarahelsaig Dec 12, 2023
427b7f9
Add comments about AdminController.UpdateContentAsync.
sarahelsaig Dec 12, 2023
7fe7193
Code styling.
sarahelsaig Dec 12, 2023
8061dab
Typos
Piedone Dec 20, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public static class TestCaseUITestContextExtensions

public static async Task TestJsonEditorBehaviorAsync(this UITestContext context)
{
await context.EnableJsonEditorFeatureAsync();
await context.EnableJsonContentEditorFeatureAsync();

await context.ExecuteJsonEditorSampleRecipeDirectlyAsync();
Expand Down
18 changes: 10 additions & 8 deletions Lombiq.JsonEditor/Controllers/AdminController.cs
Piedone marked this conversation as resolved.
Show resolved Hide resolved
Piedone marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class AdminController : Controller
private readonly INotifier _notifier;
private readonly IPageTitleBuilder _pageTitleBuilder;
private readonly IShapeFactory _shapeFactory;
private readonly IStringLocalizer<ApiController> _apiStringLocalizer;
private readonly Lazy<ApiController> _contentApiControllerLazy;
private readonly IStringLocalizer<AdminController> T;
private readonly IHtmlLocalizer<AdminController> H;

Expand All @@ -43,7 +43,7 @@ public AdminController(
IPageTitleBuilder pageTitleBuilder,
IShapeFactory shapeFactory,
IOrchardServices<AdminController> services,
IStringLocalizer<ApiController> apiStringLocalizer)
Lazy<ApiController> contentApiControllerLazy)
{
_authorizationService = services.AuthorizationService.Value;
_contentManager = services.ContentManager.Value;
Expand All @@ -52,7 +52,7 @@ public AdminController(
_notifier = notifier;
_pageTitleBuilder = pageTitleBuilder;
_shapeFactory = shapeFactory;
_apiStringLocalizer = apiStringLocalizer;
_contentApiControllerLazy = contentApiControllerLazy;
T = services.StringLocalizer.Value;
H = services.HtmlLocalizer.Value;
}
Expand Down Expand Up @@ -132,21 +132,23 @@ private Task<bool> CanEditAsync(ContentItem contentItem) =>

private async Task<IActionResult> UpdateContentAsync(ContentItem contentItem, bool isDraft)
{
// The Content API Controller requires the AccessContentApi permission. As this isn't an external API request it
// doesn't make sense to require this permission. So we create a temporary claims principal and explicitly grant
// the permission
Piedone marked this conversation as resolved.
Show resolved Hide resolved
var currentUser = User;
HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity(User.Claims.Concat(Permissions.AccessContentApi)));

try
{
using var contentApiController = new ApiController(
_contentManager,
_contentDefinitionManager,
_authorizationService,
_apiStringLocalizer);
// Here the API controller is called directly. The behavior is the same as if we sent a POST request using a
// HTTP client (except the permission bypass above), but it's faster and more resource efficient.
Piedone marked this conversation as resolved.
Show resolved Hide resolved
var contentApiController = _contentApiControllerLazy.Value;
contentApiController.ControllerContext.HttpContext = HttpContext;
return await contentApiController.Post(contentItem, isDraft);
}
finally
{
// Ensure that the original claims principal is restored, just in case.
HttpContext.User = currentUser;
}
}
Expand Down
5 changes: 4 additions & 1 deletion Lombiq.JsonEditor/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Lombiq.HelpfulLibraries.OrchardCore.DependencyInjection;
using Lombiq.JsonEditor.Constants;
using Lombiq.JsonEditor.Controllers;
using Lombiq.JsonEditor.Drivers;
using Lombiq.JsonEditor.Fields;
using Lombiq.JsonEditor.Settings;
Expand All @@ -12,12 +11,15 @@
using OrchardCore.Admin;
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Display.ContentDisplay;
using OrchardCore.Contents.Controllers;
using OrchardCore.ContentTypes.Editors;
using OrchardCore.Modules;
using OrchardCore.Mvc.Core.Utilities;
using OrchardCore.ResourceManagement;
using System;

using AdminController = Lombiq.JsonEditor.Controllers.AdminController;

namespace Lombiq.JsonEditor;

public class Startup : StartupBase
Expand All @@ -43,6 +45,7 @@ public override void ConfigureServices(IServiceCollection services)
{
services.AddScoped<IContentDisplayDriver, EditJsonActionsMenuContentDisplayDriver>();
services.AddOrchardServices();
services.AddScoped<ApiController>();
}

public override void Configure(IApplicationBuilder app, IEndpointRouteBuilder routes, IServiceProvider serviceProvider) =>
Expand Down
2 changes: 1 addition & 1 deletion Lombiq.JsonEditor/Views/Admin/Edit.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

var warning = new HtmlString(" ").Join(
T["Be careful while editing a content item as any typo can lead to a loss of functionality."],
T["The submitted JSON will be deserialized and published so a properties may be altered or regenerated at that step."]);
T["The submitted JSON will be deserialized and published so properties may be altered or regenerated at that step."]);
}

<p class="alert alert-warning">@warning</p>
Expand Down
4 changes: 2 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Do you want to quickly try out this project and see it in action? Check it out i

## Documentation

### JSON editor
### JSON Editor

You can use the JSON editor either as a content field by adding a _Json Field_ to your content type, or by invoking the "JsonEditor" shape with the below tag helper:

Expand Down Expand Up @@ -39,7 +39,7 @@ The properties are:

All attributes are optional. If neither content nor json is set, an empty object is taken as the content.

### JSON content editor
### JSON Content Editor

The module also provides an editor for content items. This can be used to directly edit a content item as JSON data. This tool can be useful to inspect how the content item is serialized in the YesSql database without directly accessing the database or exporting the content item via deployment. It can also be used to edit properties that currently don't have an editor.

Expand Down