|
| 1 | +// Copyright (c) Umbraco. |
| 2 | +// See LICENSE for more details. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Microsoft.Extensions.Options; |
| 8 | +using Umbraco.Cms.Core.Configuration.Models; |
| 9 | +using Umbraco.Cms.Core.Services; |
| 10 | +using Umbraco.Extensions; |
| 11 | + |
| 12 | +namespace Umbraco.Cms.Core.HealthChecks.Checks.Security |
| 13 | +{ |
| 14 | + [HealthCheck( |
| 15 | + "6708CA45-E96E-40B8-A40A-0607C1CA7F28", |
| 16 | + "Application URL Configuration", |
| 17 | + Description = "Checks if the Umbraco application URL is configured for your site.", |
| 18 | + Group = "Security")] |
| 19 | + public class UmbracoApplicationUrlCheck : HealthCheck |
| 20 | + { |
| 21 | + private readonly ILocalizedTextService _textService; |
| 22 | + private readonly IOptionsMonitor<WebRoutingSettings> _webRoutingSettings; |
| 23 | + |
| 24 | + public UmbracoApplicationUrlCheck(ILocalizedTextService textService, IOptionsMonitor<WebRoutingSettings> webRoutingSettings) |
| 25 | + { |
| 26 | + _textService = textService; |
| 27 | + _webRoutingSettings = webRoutingSettings; |
| 28 | + } |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Executes the action and returns its status |
| 32 | + /// </summary> |
| 33 | + public override HealthCheckStatus ExecuteAction(HealthCheckAction action) => throw new InvalidOperationException("UmbracoApplicationUrlCheck has no executable actions"); |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Get the status for this health check |
| 37 | + /// </summary> |
| 38 | + public override Task<IEnumerable<HealthCheckStatus>> GetStatus() => |
| 39 | + Task.FromResult(CheckUmbracoApplicationUrl().Yield()); |
| 40 | + |
| 41 | + private HealthCheckStatus CheckUmbracoApplicationUrl() |
| 42 | + { |
| 43 | + var url = _webRoutingSettings.CurrentValue.UmbracoApplicationUrl; |
| 44 | + |
| 45 | + string resultMessage; |
| 46 | + StatusResultType resultType; |
| 47 | + var success = false; |
| 48 | + |
| 49 | + if (url.IsNullOrWhiteSpace()) |
| 50 | + { |
| 51 | + resultMessage = _textService.Localize("healthcheck", "umbracoApplicationUrlCheckResultFalse"); |
| 52 | + resultType = StatusResultType.Warning; |
| 53 | + } |
| 54 | + else |
| 55 | + { |
| 56 | + resultMessage = _textService.Localize("healthcheck", "umbracoApplicationUrlCheckResultTrue", new[] { url }); |
| 57 | + resultType = StatusResultType.Success; |
| 58 | + success = true; |
| 59 | + } |
| 60 | + |
| 61 | + return new HealthCheckStatus(resultMessage) |
| 62 | + { |
| 63 | + ResultType = resultType, |
| 64 | + ReadMoreLink = success ? null : Constants.HealthChecks.DocumentationLinks.Security.UmbracoApplicationUrlCheck |
| 65 | + }; |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments