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

Add helpful SiteService extension #16193

Merged
merged 14 commits into from
May 31, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public async Task DoWorkAsync(IServiceProvider serviceProvider, CancellationToke
{
var siteService = serviceProvider.GetRequiredService<ISiteService>();

var settings = (await siteService.GetSiteSettingsAsync()).As<AuditTrailTrimmingSettings>();
var settings = await siteService.GetSettingsAsync<AuditTrailTrimmingSettings>();
if (settings.Disabled)
{
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public override async Task<ProviderCultureResult> DetermineProviderCultureResult

if (localization != null)
{
var settings = (await siteService.GetSiteSettingsAsync()).As<ContentRequestCultureProviderSettings>();
var settings = await siteService.GetSettingsAsync<ContentRequestCultureProviderSettings>();
if (settings.SetCookie)
{
culturePickerService.SetContentCulturePickerCookie(localization.Culture);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public async Task<IActionResult> RedirectToLocalizedContent(string targetCulture

if (supportedCultures.Any(t => t == targetCulture))
{
var settings = (await _siteService.GetSiteSettingsAsync()).As<ContentCulturePickerSettings>();
var settings = await _siteService.GetSettingsAsync<ContentCulturePickerSettings>();
if (settings.SetCookie)
{
_culturePickerService.SetContentCulturePickerCookie(targetCulture);
Expand Down
2 changes: 1 addition & 1 deletion src/OrchardCore.Modules/OrchardCore.Contents/AdminMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ await content.AddAsync(S["Content Items"], S["Content Items"].PrefixPosition(),
});
});

var adminSettings = (await _siteService.GetSiteSettingsAsync()).As<AdminSettings>();
var adminSettings = await _siteService.GetSettingsAsync<AdminSettings>();

if (adminSettings.DisplayNewMenu && contentTypes.Any())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@ private async Task RecordAuditTrailEventAsync(string name, ContentItem content)
return;
}

var siteSettings = await _siteService.GetSiteSettingsAsync();
var settings = await _siteService.GetSettingsAsync<ContentAuditTrailSettings>();

var settings = siteSettings.As<ContentAuditTrailSettings>();
if (!settings.AllowedContentTypes.Contains(content.ContentItem.ContentType))
{
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public override IDisplayResult Display(ContentItem model)
{
if (await _deploymentPlanService.DoesUserHaveExportPermissionAsync())
{
var siteSettings = await _siteService.GetSiteSettingsAsync();
var exportContentToDeploymentTargetSettings = siteSettings.As<ExportContentToDeploymentTargetSettings>();
var exportContentToDeploymentTargetSettings = await _siteService.GetSettingsAsync<ExportContentToDeploymentTargetSettings>();

if (exportContentToDeploymentTargetSettings.ExportContentToDeploymentTargetPlanId != 0)
{
return true;
Expand All @@ -45,8 +45,8 @@ public override IDisplayResult Display(ContentItem model)
{
if (await _deploymentPlanService.DoesUserHaveExportPermissionAsync())
{
var siteSettings = await _siteService.GetSiteSettingsAsync();
var exportContentToDeploymentTargetSettings = siteSettings.As<ExportContentToDeploymentTargetSettings>();
var exportContentToDeploymentTargetSettings = await _siteService.GetSettingsAsync<ExportContentToDeploymentTargetSettings>();

if (exportContentToDeploymentTargetSettings.ExportContentToDeploymentTargetPlanId != 0)
{
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public override async Task<IDisplayResult> DisplayAsync(ContentOptionsViewModel
{
if (await _deploymentPlanService.DoesUserHaveExportPermissionAsync())
{
var siteSettings = await _siteService.GetSiteSettingsAsync();
var exportContentToDeploymentTargetSettings = siteSettings.As<ExportContentToDeploymentTargetSettings>();
var exportContentToDeploymentTargetSettings = await _siteService.GetSettingsAsync<ExportContentToDeploymentTargetSettings>();

if (exportContentToDeploymentTargetSettings.ExportContentToDeploymentTargetPlanId != 0)
{
return Combine(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ public CorsService(ISiteService siteService)

public async Task<CorsSettings> GetSettingsAsync()
{
var siteSettings = await _siteService.GetSiteSettingsAsync();
return siteSettings.As<CorsSettings>();
return await _siteService.GetSettingsAsync<CorsSettings>();
}

internal async Task UpdateSettingsAsync(CorsSettings corsSettings)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ public AzureEmailOptionsConfiguration(

public void Configure(AzureEmailOptions options)
{
var settings = _siteService.GetSiteSettingsAsync()
var settings = _siteService.GetSettingsAsync<AzureEmailSettings>()
.GetAwaiter()
.GetResult()
.As<AzureEmailSettings>();
.GetResult();

options.IsEnabled = settings.IsEnabled;
options.DefaultSender = settings.DefaultSender;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public SmtpOptionsConfiguration(

public void Configure(SmtpOptions options)
{
var settings = _siteService.GetSiteSettingsAsync()
.GetAwaiter().GetResult()
.As<SmtpSettings>();
var settings = _siteService.GetSettingsAsync<SmtpSettings>()
.GetAwaiter()
.GetResult();
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved

options.DefaultSender = settings.DefaultSender;
options.DeliveryMethod = settings.DeliveryMethod;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ public int Create()
}

var siteService = scope.ServiceProvider.GetRequiredService<ISiteService>();
var site = await siteService.GetSiteSettingsAsync();

var smtpSettings = site.As<SmtpSettings>();
var smtpSettings = await siteService.GetSettingsAsync<SmtpSettings>();
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved

if (!string.IsNullOrEmpty(smtpSettings.DefaultSender) ||
scope.ServiceProvider.GetService<IOptions<SmtpOptions>>()?.Value.ConfigurationExists() == true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultE

if (_scriptsCache == null && canTrack)
{
var settings = (await _siteService.GetSiteSettingsAsync()).As<FacebookPixelSettings>();
var settings = await _siteService.GetSettingsAsync<FacebookPixelSettings>();

if (!string.IsNullOrWhiteSpace(settings?.PixelId))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@ public FacebookLoginService(ISiteService siteService)
_siteService = siteService;
}

public async Task<FacebookLoginSettings> GetSettingsAsync()
{
var container = await _siteService.GetSiteSettingsAsync();
return container.As<FacebookLoginSettings>();
}
public Task<FacebookLoginSettings> GetSettingsAsync()
=> _siteService.GetSettingsAsync<FacebookLoginSettings>();

public async Task<FacebookLoginSettings> LoadSettingsAsync()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public async Task Invoke(HttpContext httpContext)
if (httpContext.Request.Path.StartsWithSegments("/OrchardCore.Facebook/sdk", StringComparison.OrdinalIgnoreCase))
{
var script = default(string);
var site = (await _siteService.GetSiteSettingsAsync());
var settings = site.As<FacebookSettings>();
var settings = await _siteService.GetSettingsAsync<FacebookSettings>();

if (Path.GetFileName(httpContext.Request.Path.Value) == "fbsdk.js")
{
var locale = CultureInfo.CurrentUICulture.Name.Replace('-', '_');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,8 @@ public FacebookService(
S = stringLocalizer;
}

public async Task<FacebookSettings> GetSettingsAsync()
{
var container = await _siteService.GetSiteSettingsAsync();
return container.As<FacebookSettings>();
}
public Task<FacebookSettings> GetSettingsAsync()
=> _siteService.GetSettingsAsync<FacebookSettings>();

public async Task UpdateSettingsAsync(FacebookSettings settings)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
@using OrchardCore.Settings
@using OrchardCore.Facebook.Settings
@model OrchardCore.Facebook.Widgets.ViewModels.FacebookPluginPartViewModel
@inject ISiteService _site
@inject ISiteService SiteService
@{
var fbInit = (await _site.GetSiteSettingsAsync()).As<FacebookSettings>().FBInit;
var fbInit = (await SiteService.GetSettingsAsync<FacebookSettings>()).FBInit;
}

@Html.Raw(Model.Html)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
@using OrchardCore.Settings
@using OrchardCore.Facebook.Settings
@model OrchardCore.Facebook.Widgets.ViewModels.FacebookPluginPartViewModel
@inject ISiteService _site
@inject ISiteService SiteService
@{
var fbInit = (await _site.GetSiteSettingsAsync()).As<FacebookSettings>().FBInit;
var fbInit = (await SiteService.GetSettingsAsync<FacebookSettings>()).FBInit;
}

@Html.Raw(Model.Html)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@ public GitHubAuthenticationService(
S = stringLocalizer;
}

public async Task<GitHubAuthenticationSettings> GetSettingsAsync()
{
var container = await _siteService.GetSiteSettingsAsync();
return container.As<GitHubAuthenticationSettings>();
}
public Task<GitHubAuthenticationSettings> GetSettingsAsync()
=> _siteService.GetSettingsAsync<GitHubAuthenticationSettings>();

public async Task<GitHubAuthenticationSettings> LoadSettingsAsync()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultE

if (_scriptsCache == null && canTrack)
{
var settings = (await _siteService.GetSiteSettingsAsync()).As<GoogleAnalyticsSettings>();
var settings = await _siteService.GetSettingsAsync<GoogleAnalyticsSettings>();

if (!string.IsNullOrWhiteSpace(settings?.TrackingID))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ public GoogleAnalyticsService(
_siteService = siteService;
}

public async Task<GoogleAnalyticsSettings> GetSettingsAsync()
{
var container = await _siteService.GetSiteSettingsAsync();
return container.As<GoogleAnalyticsSettings>();
}
public Task<GoogleAnalyticsSettings> GetSettingsAsync()
=> _siteService.GetSettingsAsync<GoogleAnalyticsSettings>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultE

if (_scriptsCache == null && canTrack)
{
var settings = (await _siteService.GetSiteSettingsAsync()).As<GoogleTagManagerSettings>();
var settings = await _siteService.GetSettingsAsync<GoogleTagManagerSettings>();

if (!string.IsNullOrWhiteSpace(settings?.ContainerID))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ public GoogleTagManagerService(ISiteService siteService)
_siteService = siteService;
}

public async Task<GoogleTagManagerSettings> GetSettingsAsync()
{
var container = await _siteService.GetSiteSettingsAsync();
return container.As<GoogleTagManagerSettings>();
}
public Task<GoogleTagManagerSettings> GetSettingsAsync()
=> _siteService.GetSettingsAsync<GoogleTagManagerSettings>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ public HttpsService(ISiteService siteService)
_siteService = siteService;
}

public async Task<HttpsSettings> GetSettingsAsync()
{
var siteSettings = await _siteService.GetSiteSettingsAsync();
return siteSettings.As<HttpsSettings>();
}
public Task<HttpsSettings> GetSettingsAsync()
=> _siteService.GetSettingsAsync<HttpsSettings>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,9 @@ public async Task<IActionResult> Index()

var model = new LayersIndexViewModel { Layers = layers.Layers.ToList() };

var siteSettings = await _siteService.GetSiteSettingsAsync();
var contentDefinitions = await _contentDefinitionManager.ListTypeDefinitionsAsync();

model.Zones = siteSettings.As<LayerSettings>().Zones ?? [];
model.Zones = (await _siteService.GetSettingsAsync<LayerSettings>()).Zones ?? [];
model.Widgets = [];

foreach (var widget in widgets.OrderBy(x => x.Position))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ public async Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlan
["Layers"] = JArray.FromObject(layers.Layers, _jsonSerializerOptions),
});

var siteSettings = await _siteService.GetSiteSettingsAsync();
var layerSettings = await _siteService.GetSettingsAsync<LayerSettings>();

// Adding Layer settings
result.Steps.Add(new JsonObject
{
["name"] = "Settings",
["LayerSettings"] = JObject.FromObject(siteSettings.As<LayerSettings>()),
["LayerSettings"] = JObject.FromObject(layerSettings),
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,7 @@ public async Task<string[]> GetSupportedCulturesAsync()

private async Task InitializeLocalizationSettingsAsync()
{
if (_localizationSettings == null)
{
var siteSettings = await _siteService.GetSiteSettingsAsync();
_localizationSettings = siteSettings.As<LocalizationSettings>();
}
_localizationSettings ??= await _siteService.GetSettingsAsync<LocalizationSettings>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public MediaTokenOptionsConfiguration(ISiteService siteService)

public void Configure(MediaTokenOptions options)
{
options.HashKey = _siteService.GetSiteSettingsAsync()
.GetAwaiter().GetResult()
.As<MediaTokenSettings>()
options.HashKey = _siteService.GetSettingsAsync<MediaTokenSettings>()
.GetAwaiter()
.GetResult()
.HashKey;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public async Task ActivatedAsync()
return;
}

var mediaTokenSettings = (await _siteService.GetSiteSettingsAsync()).As<MediaTokenSettings>();
var mediaTokenSettings = await _siteService.GetSettingsAsync<MediaTokenSettings>();

if (mediaTokenSettings.HashKey == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@ public AzureADService(
S = stringLocalizer;
}

public async Task<AzureADSettings> GetSettingsAsync()
{
var container = await _siteService.GetSiteSettingsAsync();
return container.As<AzureADSettings>();
}
public Task<AzureADSettings> GetSettingsAsync()
=> _siteService.GetSettingsAsync<AzureADSettings>();

public async Task<AzureADSettings> LoadSettingsAsync()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@ public MicrosoftAccountService(
S = stringLocalizer;
}

public async Task<MicrosoftAccountSettings> GetSettingsAsync()
{
var container = await _siteService.GetSiteSettingsAsync();
return container.As<MicrosoftAccountSettings>();
}
public Task<MicrosoftAccountSettings> GetSettingsAsync()
=> _siteService.GetSettingsAsync<MicrosoftAccountSettings>();

public async Task<MicrosoftAccountSettings> LoadSettingsAsync()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,8 @@ public OpenIdClientService(
S = stringLocalizer;
}

public async Task<OpenIdClientSettings> GetSettingsAsync()
{
var container = await _siteService.GetSiteSettingsAsync();
return container.As<OpenIdClientSettings>();
}
public Task<OpenIdClientSettings> GetSettingsAsync()
=> _siteService.GetSettingsAsync<OpenIdClientSettings>();

public async Task<OpenIdClientSettings> LoadSettingsAsync()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public ReCaptchaForgotPasswordFormDisplayDriver(ISiteService siteService)

public override async Task<IDisplayResult> EditAsync(ForgotPasswordForm model, BuildEditorContext context)
{
var _reCaptchaSettings = (await _siteService.GetSiteSettingsAsync()).As<ReCaptchaSettings>();
var _reCaptchaSettings = await _siteService.GetSettingsAsync<ReCaptchaSettings>();

if (!_reCaptchaSettings.IsValid())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public ReCaptchaLoginFormDisplayDriver(

public override async Task<IDisplayResult> EditAsync(LoginForm model, BuildEditorContext context)
{
var _reCaptchaSettings = (await _siteService.GetSiteSettingsAsync()).As<ReCaptchaSettings>();
var _reCaptchaSettings = await _siteService.GetSettingsAsync<ReCaptchaSettings>();

if (!_reCaptchaSettings.IsValid() || !_reCaptchaService.IsThisARobot())
{
Expand Down
Loading