Files
Umbraco-CMS/src/Umbraco.Web.BackOffice/Controllers/HelpController.cs

106 lines
3.5 KiB
C#
Raw Normal View History

2022-01-26 12:12:59 +01:00
using System;
2022-01-26 10:57:49 +01:00
using System.Collections.Generic;
2022-01-26 12:12:59 +01:00
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Runtime.Serialization;
using System.Threading.Tasks;
2022-01-26 12:12:59 +01:00
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
2022-01-26 12:12:59 +01:00
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
2022-01-26 12:12:59 +01:00
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Web.Common.Attributes;
2022-01-26 12:12:59 +01:00
using Umbraco.Cms.Web.Common.DependencyInjection;
using Constants = Umbraco.Cms.Core.Constants;
namespace Umbraco.Cms.Web.BackOffice.Controllers
{
[PluginController(Constants.Web.Mvc.BackOfficeApiArea)]
public class HelpController : UmbracoAuthorizedJsonController
{
private readonly ILogger<HelpController> _logger;
2022-01-26 12:12:59 +01:00
private readonly HelpPageSettings _helpPageSettings;
2022-01-26 12:12:59 +01:00
[Obsolete("Use constructor that takes IOptions<HelpPageSettings>")]
public HelpController(ILogger<HelpController> logger)
: this(logger, StaticServiceProvider.Instance.GetRequiredService<IOptions<HelpPageSettings>>())
{
}
[ActivatorUtilitiesConstructor]
public HelpController(
ILogger<HelpController> logger,
IOptions<HelpPageSettings> helpPageSettings)
{
_logger = logger;
2022-01-26 12:12:59 +01:00
_helpPageSettings = helpPageSettings.Value;
}
2018-09-25 16:15:37 +02:00
private static HttpClient _httpClient;
2022-01-26 12:12:59 +01:00
2018-07-27 16:41:57 +01:00
public async Task<List<HelpPage>> GetContextHelpForPage(string section, string tree, string baseUrl = "https://our.umbraco.com")
{
2022-01-26 10:57:49 +01:00
if (IsAllowedUrl(baseUrl) is false)
{
2022-01-26 12:12:59 +01:00
_logger.LogError($"The following URL is not listed in the allowlist for HelpPage in web.config: {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>();
2022-01-26 10:57:49 +01:00
}
var url = string.Format(baseUrl + "/Umbraco/Documentation/Lessons/GetContextHelpDocs?sectionAlias={0}&treeAlias={1}", section, tree);
2018-09-25 16:15:37 +02:00
try
{
if (_httpClient == null)
_httpClient = new HttpClient();
//fetch dashboard json and parse to JObject
var json = await _httpClient.GetStringAsync(url);
var result = JsonConvert.DeserializeObject<List<HelpPage>>(json);
if (result != null)
return result;
}
catch (HttpRequestException rex)
{
2020-09-14 14:25:59 +02:00
_logger.LogInformation($"Check your network connection, exception: {rex.Message}");
}
return new List<HelpPage>();
}
2022-01-26 10:57:49 +01:00
private bool IsAllowedUrl(string url)
{
2022-01-26 12:12:59 +01:00
if (_helpPageSettings.HelpPageUrlAllowList is null ||
2022-01-26 10:57:49 +01:00
_helpPageSettings.HelpPageUrlAllowList.Contains(url))
{
return true;
}
return false;
}
}
[DataContract(Name = "HelpPage")]
public class HelpPage
{
[DataMember(Name = "name")]
public string Name { get; set; }
[DataMember(Name = "description")]
public string Description { get; set; }
[DataMember(Name = "url")]
public string Url { get; set; }
[DataMember(Name = "type")]
public string Type { get; set; }
}
}