-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
SmsSettingsDisplayDriver.cs
78 lines (63 loc) · 2.84 KB
/
SmsSettingsDisplayDriver.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
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Options;
using OrchardCore.DisplayManagement.Entities;
using OrchardCore.DisplayManagement.Handlers;
using OrchardCore.DisplayManagement.Views;
using OrchardCore.Environment.Shell;
using OrchardCore.Settings;
using OrchardCore.Sms.ViewModels;
namespace OrchardCore.Sms.Drivers;
public sealed class SmsSettingsDisplayDriver : SiteDisplayDriver<SmsSettings>
{
private readonly IShellReleaseManager _shellReleaseManager;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IAuthorizationService _authorizationService;
internal readonly IStringLocalizer S;
private readonly SmsProviderOptions _smsProviderOptions;
protected override string SettingsGroupId
=> SmsSettings.GroupId;
public SmsSettingsDisplayDriver(
IShellReleaseManager shellReleaseManager,
IHttpContextAccessor httpContextAccessor,
IAuthorizationService authorizationService,
IOptions<SmsProviderOptions> smsProviders,
IStringLocalizer<SmsSettingsDisplayDriver> stringLocalizer)
{
_shellReleaseManager = shellReleaseManager;
_httpContextAccessor = httpContextAccessor;
_authorizationService = authorizationService;
_smsProviderOptions = smsProviders.Value;
S = stringLocalizer;
}
public override IDisplayResult Edit(ISite site, SmsSettings settings, BuildEditorContext context)
=> Initialize<SmsSettingsViewModel>("SmsSettings_Edit", model =>
{
model.DefaultProvider = settings.DefaultProviderName;
model.Providers = _smsProviderOptions.Providers
.Where(entry => entry.Value.IsEnabled)
.Select(entry => new SelectListItem(entry.Key, entry.Key))
.OrderBy(item => item.Text)
.ToArray();
}).Location("Content:1#Providers")
.RenderWhen(() => _authorizationService.AuthorizeAsync(_httpContextAccessor.HttpContext?.User, SmsPermissions.ManageSmsSettings))
.OnGroup(SettingsGroupId);
public override async Task<IDisplayResult> UpdateAsync(ISite site, SmsSettings settings, UpdateEditorContext context)
{
var user = _httpContextAccessor.HttpContext?.User;
if (!await _authorizationService.AuthorizeAsync(user, SmsPermissions.ManageSmsSettings))
{
return null;
}
var model = new SmsSettingsViewModel();
await context.Updater.TryUpdateModelAsync(model, Prefix);
if (settings.DefaultProviderName != model.DefaultProvider)
{
settings.DefaultProviderName = model.DefaultProvider;
_shellReleaseManager.RequestRelease();
}
return Edit(site, settings, context);
}
}