/
GithubAuthenticationSettingsDisplayDriver.cs
90 lines (82 loc) · 3.87 KB
/
GithubAuthenticationSettingsDisplayDriver.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
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Http;
using OrchardCore.DisplayManagement.Entities;
using OrchardCore.DisplayManagement.Handlers;
using OrchardCore.DisplayManagement.Views;
using OrchardCore.Environment.Shell;
using OrchardCore.Github.Settings;
using OrchardCore.Github.ViewModels;
using OrchardCore.Settings;
namespace OrchardCore.Github.Drivers
{
public class GithubAuthenticationSettingsDisplayDriver : SectionDisplayDriver<ISite, GithubAuthenticationSettings>
{
private readonly IAuthorizationService _authorizationService;
private readonly IDataProtectionProvider _dataProtectionProvider;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IShellHost _shellHost;
private readonly ShellSettings _shellSettings;
public GithubAuthenticationSettingsDisplayDriver(
IAuthorizationService authorizationService,
IDataProtectionProvider dataProtectionProvider,
IHttpContextAccessor httpContextAccessor,
IShellHost shellHost,
ShellSettings shellSettings)
{
_authorizationService = authorizationService;
_dataProtectionProvider = dataProtectionProvider;
_httpContextAccessor = httpContextAccessor;
_shellHost = shellHost;
_shellSettings = shellSettings;
}
public override async Task<IDisplayResult> EditAsync(GithubAuthenticationSettings settings, BuildEditorContext context)
{
var user = _httpContextAccessor.HttpContext?.User;
if (user == null || !await _authorizationService.AuthorizeAsync(user, Permissions.ManageGithubAuthentication))
{
return null;
}
return Initialize<GithubAuthenticationSettingsViewModel>("GithubAuthenticationSettings_Edit", model =>
{
model.ClientID = settings.ClientID;
if (!string.IsNullOrWhiteSpace(settings.ClientSecret))
{
var protector = _dataProtectionProvider.CreateProtector(GithubConstants.Features.GithubAuthentication);
model.ClientSecret = protector.Unprotect(settings.ClientSecret);
}
else
{
model.ClientSecret = string.Empty;
}
if (settings.CallbackPath.HasValue)
{
model.CallbackUrl = settings.CallbackPath;
}
}).Location("Content:5").OnGroup(GithubConstants.Features.GithubAuthentication);
}
public override async Task<IDisplayResult> UpdateAsync(GithubAuthenticationSettings settings, BuildEditorContext context)
{
if (context.GroupId == GithubConstants.Features.GithubAuthentication)
{
var user = _httpContextAccessor.HttpContext?.User;
if (user == null || !await _authorizationService.AuthorizeAsync(user, Permissions.ManageGithubAuthentication))
{
return null;
}
var model = new GithubAuthenticationSettingsViewModel();
await context.Updater.TryUpdateModelAsync(model, Prefix);
if (context.Updater.ModelState.IsValid)
{
var protector = _dataProtectionProvider.CreateProtector(GithubConstants.Features.GithubAuthentication);
settings.ClientID = model.ClientID;
settings.ClientSecret = protector.Protect(model.ClientSecret);
settings.CallbackPath = model.CallbackUrl;
await _shellHost.ReloadShellContextAsync(_shellSettings);
}
}
return await EditAsync(settings, context);
}
}
}