2022-01-26 12:12:59 +01:00
|
|
|
using System.Net;
|
2017-10-10 16:05:21 +01:00
|
|
|
using System.Runtime.Serialization;
|
2022-01-26 12:12:59 +01:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2020-09-21 09:52:58 +02:00
|
|
|
using Microsoft.Extensions.Logging;
|
2022-01-26 12:12:59 +01:00
|
|
|
using Microsoft.Extensions.Options;
|
2021-02-18 11:06:02 +01:00
|
|
|
using Newtonsoft.Json;
|
2022-06-20 08:37:17 +02:00
|
|
|
using Umbraco.Cms.Core;
|
2022-01-26 12:12:59 +01:00
|
|
|
using Umbraco.Cms.Core.Configuration.Models;
|
2022-11-29 11:22:57 +00:00
|
|
|
using Umbraco.Cms.Core.DependencyInjection;
|
2021-02-18 11:06:02 +01:00
|
|
|
using Umbraco.Cms.Web.Common.Attributes;
|
2017-10-10 16:05:21 +01:00
|
|
|
|
2022-06-20 08:37:17 +02:00
|
|
|
namespace Umbraco.Cms.Web.BackOffice.Controllers;
|
|
|
|
|
|
|
|
|
|
[PluginController(Constants.Web.Mvc.BackOfficeApiArea)]
|
|
|
|
|
public class HelpController : UmbracoAuthorizedJsonController
|
2017-10-10 16:05:21 +01:00
|
|
|
{
|
2022-06-20 08:37:17 +02:00
|
|
|
private static HttpClient? _httpClient;
|
|
|
|
|
private readonly ILogger<HelpController> _logger;
|
|
|
|
|
private HelpPageSettings? _helpPageSettings;
|
|
|
|
|
|
|
|
|
|
[ActivatorUtilitiesConstructor]
|
|
|
|
|
public HelpController(
|
|
|
|
|
ILogger<HelpController> logger,
|
|
|
|
|
IOptionsMonitor<HelpPageSettings> helpPageSettings)
|
2018-08-17 15:07:57 +02:00
|
|
|
{
|
2022-06-20 08:37:17 +02:00
|
|
|
_logger = logger;
|
2020-05-20 13:35:55 +02:00
|
|
|
|
2022-06-20 08:37:17 +02:00
|
|
|
ResetHelpPageSettings(helpPageSettings.CurrentValue);
|
|
|
|
|
helpPageSettings.OnChange(ResetHelpPageSettings);
|
|
|
|
|
}
|
2022-01-26 12:40:14 +01:00
|
|
|
|
2022-06-20 08:37:17 +02:00
|
|
|
private void ResetHelpPageSettings(HelpPageSettings settings) => _helpPageSettings = settings;
|
2022-01-26 12:40:14 +01:00
|
|
|
|
2022-06-20 08:37:17 +02:00
|
|
|
public async Task<List<HelpPage>> GetContextHelpForPage(string section, string tree,
|
|
|
|
|
string baseUrl = "https://our.umbraco.com")
|
|
|
|
|
{
|
|
|
|
|
if (IsAllowedUrl(baseUrl) is false)
|
2022-01-26 12:40:14 +01:00
|
|
|
{
|
2022-06-20 08:37:17 +02:00
|
|
|
_logger.LogError(
|
|
|
|
|
$"The following URL is not listed in the allowlist for HelpPage in HelpPageSettings: {baseUrl}");
|
|
|
|
|
HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;
|
|
|
|
|
|
|
|
|
|
// Ideally we'd want to return a BadRequestResult here,
|
|
|
|
|
// however, since we're not returning ActionResult this is not possible and changing it would be a breaking change.
|
|
|
|
|
return new List<HelpPage>();
|
2020-05-20 13:35:55 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-20 08:37:17 +02:00
|
|
|
var url = string.Format(
|
|
|
|
|
baseUrl + "/Umbraco/Documentation/Lessons/GetContextHelpDocs?sectionAlias={0}&treeAlias={1}", section,
|
|
|
|
|
tree);
|
2022-01-26 12:12:59 +01:00
|
|
|
|
2022-06-20 08:37:17 +02:00
|
|
|
try
|
2017-10-10 16:05:21 +01:00
|
|
|
{
|
2022-06-20 08:37:17 +02:00
|
|
|
if (_httpClient == null)
|
2022-01-26 10:57:49 +01:00
|
|
|
{
|
2022-06-20 08:37:17 +02:00
|
|
|
_httpClient = new HttpClient();
|
2022-01-26 10:57:49 +01:00
|
|
|
}
|
|
|
|
|
|
2022-06-20 08:37:17 +02:00
|
|
|
//fetch dashboard json and parse to JObject
|
|
|
|
|
var json = await _httpClient.GetStringAsync(url);
|
|
|
|
|
List<HelpPage>? result = JsonConvert.DeserializeObject<List<HelpPage>>(json);
|
|
|
|
|
if (result != null)
|
2019-12-19 09:05:47 +01:00
|
|
|
{
|
2022-06-20 08:37:17 +02:00
|
|
|
return result;
|
2019-12-19 09:05:47 +01:00
|
|
|
}
|
2017-10-10 16:05:21 +01:00
|
|
|
}
|
2022-06-20 08:37:17 +02:00
|
|
|
catch (HttpRequestException rex)
|
2022-01-26 10:57:49 +01:00
|
|
|
{
|
2022-06-20 08:37:17 +02:00
|
|
|
_logger.LogInformation($"Check your network connection, exception: {rex.Message}");
|
2022-01-26 10:57:49 +01:00
|
|
|
}
|
2022-06-20 08:37:17 +02:00
|
|
|
|
|
|
|
|
return new List<HelpPage>();
|
2017-10-10 16:05:21 +01:00
|
|
|
}
|
|
|
|
|
|
2022-06-20 08:37:17 +02:00
|
|
|
private bool IsAllowedUrl(string url)
|
2017-10-10 16:05:21 +01:00
|
|
|
{
|
2022-06-20 08:37:17 +02:00
|
|
|
if (_helpPageSettings?.HelpPageUrlAllowList is null ||
|
|
|
|
|
_helpPageSettings.HelpPageUrlAllowList.Contains(url))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2017-10-10 16:05:21 +01:00
|
|
|
|
2022-06-20 08:37:17 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-10 16:05:21 +01:00
|
|
|
|
2022-06-20 08:37:17 +02:00
|
|
|
[DataContract(Name = "HelpPage")]
|
|
|
|
|
public class HelpPage
|
|
|
|
|
{
|
|
|
|
|
[DataMember(Name = "name")] public string? Name { get; set; }
|
2017-10-10 16:05:21 +01:00
|
|
|
|
2022-06-20 08:37:17 +02:00
|
|
|
[DataMember(Name = "description")] public string? Description { get; set; }
|
2017-10-10 16:05:21 +01:00
|
|
|
|
2022-06-20 08:37:17 +02:00
|
|
|
[DataMember(Name = "url")] public string? Url { get; set; }
|
2017-10-10 16:05:21 +01:00
|
|
|
|
2022-06-20 08:37:17 +02:00
|
|
|
[DataMember(Name = "type")] public string? Type { get; set; }
|
2017-10-10 16:05:21 +01:00
|
|
|
}
|