-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAdminController.cs
117 lines (103 loc) · 4.33 KB
/
AdminController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using Lombiq.HelpfulLibraries.OrchardCore.Contents;
using Lombiq.HelpfulLibraries.OrchardCore.DependencyInjection;
using Lombiq.JsonEditor.ViewModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.Extensions.Localization;
using Newtonsoft.Json;
using OrchardCore.ContentManagement;
using OrchardCore.Contents;
using OrchardCore.DisplayManagement;
using OrchardCore.DisplayManagement.Layout;
using OrchardCore.DisplayManagement.Title;
using OrchardCore.Title.ViewModels;
using System.Threading.Tasks;
using YesSql;
namespace Lombiq.JsonEditor.Controllers;
public class AdminController : Controller
{
private readonly IAuthorizationService _authorizationService;
private readonly IContentItemIdGenerator _contentItemIdGenerator;
private readonly IContentManager _contentManager;
private readonly ILayoutAccessor _layoutAccessor;
private readonly IPageTitleBuilder _pageTitleBuilder;
private readonly ISession _session;
private readonly IShapeFactory _shapeFactory;
private readonly IStringLocalizer<AdminController> T;
public AdminController(
IContentItemIdGenerator contentItemIdGenerator,
ILayoutAccessor layoutAccessor,
IPageTitleBuilder pageTitleBuilder,
IShapeFactory shapeFactory,
IOrchardServices<AdminController> services)
{
_authorizationService = services.AuthorizationService.Value;
_contentItemIdGenerator = contentItemIdGenerator;
_contentManager = services.ContentManager.Value;
_layoutAccessor = layoutAccessor;
_pageTitleBuilder = pageTitleBuilder;
_session = services.Session.Value;
_shapeFactory = shapeFactory;
T = services.StringLocalizer.Value;
}
public async Task<IActionResult> Edit(string contentItemId)
{
if (string.IsNullOrWhiteSpace(contentItemId) ||
await _contentManager.GetAsync(contentItemId, VersionOptions.Latest) is not { } contentItem ||
!await CanEditAsync(contentItem))
{
return NotFound();
}
var name = string.IsNullOrWhiteSpace(contentItem.DisplayText)
? contentItem.ContentType
: $"\"{contentItem.DisplayText}\"";
var title = T["Edit {0} as JSON", name].Value;
_pageTitleBuilder.AddSegment(new StringHtmlContent(title));
var titleShape = await _shapeFactory.CreateAsync<TitlePartViewModel>("TitlePart", model =>
{
model.Title = title;
model.ContentItem = contentItem;
});
await _layoutAccessor.AddShapeToZoneAsync("Title", titleShape);
return View(new EditContentItemViewModel(contentItem, JsonConvert.SerializeObject(contentItem)));
}
[ValidateAntiForgeryToken]
[HttpPost, ActionName(nameof(Edit))]
public async Task<IActionResult> EditPost(
string contentItemId,
string json,
string returnUrl,
[Bind(Prefix = "submit.Publish")] string submitPublish)
{
if (string.IsNullOrWhiteSpace(contentItemId) ||
string.IsNullOrWhiteSpace(json) ||
JsonConvert.DeserializeObject<ContentItem>(json) is not { } contentItem)
{
return NotFound();
}
if (string.IsNullOrWhiteSpace(contentItem.ContentItemId)) contentItem.ContentItemId = contentItemId;
if (!await CanEditAsync(contentItem))
{
return NotFound();
}
await _contentManager.LoadAsync(contentItem);
if (await _contentManager.GetAsync(contentItem.ContentItemId, VersionOptions.Latest) is { } existing)
{
existing.Latest = false;
_session.Save(existing);
contentItem.ContentItemVersionId = _contentItemIdGenerator.GenerateUniqueId(existing);
}
contentItem.Published = false;
await _contentManager.PublishAsync(contentItem);
if (!string.IsNullOrEmpty(returnUrl) &&
submitPublish != "submit.PublishAndContinue" &&
Url.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
return RedirectToAction(nameof(Edit), new { contentItemId, returnUrl });
}
private Task<bool> CanEditAsync(ContentItem contentItem) =>
_authorizationService.AuthorizeAsync(User, CommonPermissions.EditContent, contentItem);
}