-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
ContentController.cs
101 lines (84 loc) · 3.33 KB
/
ContentController.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
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using OrchardCore.Admin;
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Display;
using OrchardCore.Contents;
using OrchardCore.DisplayManagement.ModelBinding;
using YesSql;
using IHttpContextAccessor = Microsoft.AspNetCore.Http.IHttpContextAccessor;
namespace OrchardCore.Demo.Controllers;
public sealed class ContentController : Controller
{
private readonly IContentItemDisplayManager _contentDisplay;
private readonly IContentManager _contentManager;
private readonly ISession _session;
private readonly IUpdateModelAccessor _updateModelAccessor;
private readonly IAuthorizationService _authorizationService;
private readonly IHttpContextAccessor _httpContextAccessor;
public ContentController(
IContentManager contentManager,
IContentItemDisplayManager contentDisplay,
ISession session,
IUpdateModelAccessor updateModelAccessor,
IAuthorizationService authorizationService,
IHttpContextAccessor httpContextAccessor)
{
_contentManager = contentManager;
_contentDisplay = contentDisplay;
_session = session;
_updateModelAccessor = updateModelAccessor;
_authorizationService = authorizationService;
_httpContextAccessor = httpContextAccessor;
}
public async Task<ActionResult> Display(string contentItemId)
{
var contentItem = await _contentManager.GetAsync(contentItemId);
if (contentItem == null)
{
return NotFound();
}
if (!await _authorizationService.AuthorizeAsync(_httpContextAccessor.HttpContext.User, CommonPermissions.ViewContent, contentItem))
{
return Forbid();
}
var shape = await _contentDisplay.BuildDisplayAsync(contentItem, _updateModelAccessor.ModelUpdater);
return View(shape);
}
[Admin("Demo/Content/Edit", "Demo.Content.Edit")]
public async Task<ActionResult> Edit(string contentItemId)
{
var contentItem = await _contentManager.GetAsync(contentItemId);
if (contentItem == null)
{
return NotFound();
}
if (!await _authorizationService.AuthorizeAsync(_httpContextAccessor.HttpContext.User, CommonPermissions.EditContent, contentItem))
{
return Forbid();
}
var shape = await _contentDisplay.BuildEditorAsync(contentItem, _updateModelAccessor.ModelUpdater, false);
return View(shape);
}
[Admin, HttpPost, ActionName("Edit")]
public async Task<ActionResult> EditPost(string contentItemId)
{
var contentItem = await _contentManager.GetAsync(contentItemId);
if (contentItem == null)
{
return NotFound();
}
if (!await _authorizationService.AuthorizeAsync(_httpContextAccessor.HttpContext.User, CommonPermissions.EditContent, contentItem))
{
return Forbid();
}
var shape = await _contentDisplay.UpdateEditorAsync(contentItem, _updateModelAccessor.ModelUpdater, false);
if (!ModelState.IsValid)
{
await _session.CancelAsync();
return View(nameof(Edit), shape);
}
await _session.SaveAsync(contentItem);
return RedirectToAction(nameof(Edit), contentItemId);
}
}