2016-08-08 11:57:48 +02:00
|
|
|
|
using System;
|
2016-08-03 20:01:43 +02:00
|
|
|
|
using System.Xml;
|
2016-11-03 10:31:44 +01:00
|
|
|
|
using System.Security;
|
2020-05-28 07:12:53 +02:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2016-09-04 11:44:16 +02:00
|
|
|
|
using Umbraco.Core.Logging;
|
|
|
|
|
|
using Umbraco.Core.Models;
|
2016-08-08 14:40:53 +02:00
|
|
|
|
using Umbraco.Web.Models.ContentEditing;
|
2018-09-26 22:16:54 +01:00
|
|
|
|
using Umbraco.Core;
|
2020-01-21 17:03:46 -08:00
|
|
|
|
using Umbraco.Core.Configuration.UmbracoSettings;
|
2020-05-28 07:12:53 +02:00
|
|
|
|
using Umbraco.Core.Hosting;
|
|
|
|
|
|
using Umbraco.Core.Mapping;
|
|
|
|
|
|
using Umbraco.Core.Services;
|
|
|
|
|
|
using Umbraco.Web.Common.Attributes;
|
2020-06-04 13:53:06 +02:00
|
|
|
|
using Umbraco.Web.Security;
|
2020-09-08 15:50:52 +02:00
|
|
|
|
using Umbraco.Core.Configuration;
|
2020-08-21 14:52:47 +01:00
|
|
|
|
using Umbraco.Core.Configuration.Models;
|
|
|
|
|
|
using Microsoft.Extensions.Options;
|
2016-08-03 17:57:01 +02:00
|
|
|
|
|
2020-05-28 07:12:53 +02:00
|
|
|
|
namespace Umbraco.Web.BackOffice.Controllers
|
2016-08-03 17:57:01 +02:00
|
|
|
|
{
|
2020-06-09 13:01:05 +10:00
|
|
|
|
[PluginController(Constants.Web.Mvc.BackOfficeApiArea)]
|
2016-08-03 17:57:01 +02:00
|
|
|
|
public class RedirectUrlManagementController : UmbracoAuthorizedApiController
|
|
|
|
|
|
{
|
2020-09-16 10:55:41 +02:00
|
|
|
|
private readonly ILogger<RedirectUrlManagementController> _logger;
|
2020-08-21 14:52:47 +01:00
|
|
|
|
private readonly WebRoutingSettings _webRoutingSettings;
|
2020-06-04 13:53:06 +02:00
|
|
|
|
private readonly IWebSecurity _webSecurity;
|
2020-05-28 07:12:53 +02:00
|
|
|
|
private readonly IRedirectUrlService _redirectUrlService;
|
|
|
|
|
|
private readonly UmbracoMapper _umbracoMapper;
|
|
|
|
|
|
private readonly IHostingEnvironment _hostingEnvironment;
|
2020-09-08 15:50:52 +02:00
|
|
|
|
private readonly IConfigManipulator _configManipulator;
|
2020-05-28 07:12:53 +02:00
|
|
|
|
|
2020-09-08 15:50:52 +02:00
|
|
|
|
public RedirectUrlManagementController(
|
2020-09-16 10:55:41 +02:00
|
|
|
|
ILogger<RedirectUrlManagementController> logger,
|
2020-08-23 23:36:48 +02:00
|
|
|
|
IOptions<WebRoutingSettings> webRoutingSettings,
|
2020-06-04 13:53:06 +02:00
|
|
|
|
IWebSecurity webSecurity,
|
2020-05-28 07:12:53 +02:00
|
|
|
|
IRedirectUrlService redirectUrlService,
|
|
|
|
|
|
UmbracoMapper umbracoMapper,
|
2020-09-08 15:50:52 +02:00
|
|
|
|
IHostingEnvironment hostingEnvironment,
|
|
|
|
|
|
IConfigManipulator configManipulator)
|
2016-11-03 10:31:44 +01:00
|
|
|
|
{
|
2020-05-28 07:12:53 +02:00
|
|
|
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
2020-08-21 14:52:47 +01:00
|
|
|
|
_webRoutingSettings = webRoutingSettings.Value ?? throw new ArgumentNullException(nameof(webRoutingSettings));
|
2020-06-04 13:53:06 +02:00
|
|
|
|
_webSecurity = webSecurity ?? throw new ArgumentNullException(nameof(webSecurity));
|
2020-05-28 07:12:53 +02:00
|
|
|
|
_redirectUrlService = redirectUrlService ?? throw new ArgumentNullException(nameof(redirectUrlService));
|
|
|
|
|
|
_umbracoMapper = umbracoMapper ?? throw new ArgumentNullException(nameof(umbracoMapper));
|
|
|
|
|
|
_hostingEnvironment = hostingEnvironment ?? throw new ArgumentNullException(nameof(hostingEnvironment));
|
2020-09-08 15:50:52 +02:00
|
|
|
|
_configManipulator = configManipulator ?? throw new ArgumentNullException(nameof(configManipulator));
|
2016-11-03 10:31:44 +01:00
|
|
|
|
}
|
2016-08-08 14:40:53 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Returns true/false of whether redirect tracking is enabled or not
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[HttpGet]
|
2020-05-28 07:12:53 +02:00
|
|
|
|
public IActionResult GetEnableState()
|
2016-08-08 14:40:53 +02:00
|
|
|
|
{
|
2020-03-12 09:52:34 +01:00
|
|
|
|
var enabled = _webRoutingSettings.DisableRedirectUrlTracking == false;
|
2020-06-04 13:53:06 +02:00
|
|
|
|
var userIsAdmin = _webSecurity.CurrentUser.IsAdmin();
|
2016-09-04 11:44:16 +02:00
|
|
|
|
return Ok(new { enabled, userIsAdmin });
|
2016-08-08 14:40:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-08-03 17:57:01 +02:00
|
|
|
|
//add paging
|
|
|
|
|
|
[HttpGet]
|
2016-08-03 20:01:43 +02:00
|
|
|
|
public RedirectUrlSearchResult SearchRedirectUrls(string searchTerm, int page = 0, int pageSize = 10)
|
2016-08-03 17:57:01 +02:00
|
|
|
|
{
|
2016-08-08 14:40:53 +02:00
|
|
|
|
var searchResult = new RedirectUrlSearchResult();
|
|
|
|
|
|
long resultCount;
|
2016-08-04 11:21:42 +02:00
|
|
|
|
|
2017-07-20 11:21:28 +02:00
|
|
|
|
var redirects = string.IsNullOrWhiteSpace(searchTerm)
|
2020-05-28 07:12:53 +02:00
|
|
|
|
? _redirectUrlService.GetAllRedirectUrls(page, pageSize, out resultCount)
|
|
|
|
|
|
: _redirectUrlService.SearchRedirectUrls(searchTerm, page, pageSize, out resultCount);
|
2016-08-03 17:57:01 +02:00
|
|
|
|
|
2020-05-28 07:12:53 +02:00
|
|
|
|
searchResult.SearchResults = _umbracoMapper.MapEnumerable<IRedirectUrl, ContentRedirectUrl>(redirects);
|
2016-08-03 17:57:01 +02:00
|
|
|
|
searchResult.TotalCount = resultCount;
|
2016-08-04 11:21:42 +02:00
|
|
|
|
searchResult.CurrentPage = page;
|
2016-08-03 17:57:01 +02:00
|
|
|
|
searchResult.PageCount = ((int)resultCount + pageSize - 1) / pageSize;
|
2017-07-20 11:21:28 +02:00
|
|
|
|
|
2016-08-03 17:57:01 +02:00
|
|
|
|
return searchResult;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2018-09-26 22:16:54 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// This lists the RedirectUrls for a particular content item
|
|
|
|
|
|
/// Do we need to consider paging here?
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="contentUdi">Udi of content item to retrieve RedirectUrls for</param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
|
public RedirectUrlSearchResult RedirectUrlsForContentItem(string contentUdi)
|
|
|
|
|
|
{
|
|
|
|
|
|
var redirectsResult = new RedirectUrlSearchResult();
|
2019-11-13 16:33:40 +11:00
|
|
|
|
if (UdiParser.TryParse(contentUdi, out GuidUdi guidIdi))
|
2018-09-26 22:16:54 +01:00
|
|
|
|
{
|
2020-05-28 07:12:53 +02:00
|
|
|
|
|
|
|
|
|
|
var redirects = _redirectUrlService.GetContentRedirectUrls(guidIdi.Guid);
|
|
|
|
|
|
var mapped = _umbracoMapper.MapEnumerable<IRedirectUrl, ContentRedirectUrl>(redirects);
|
2019-04-08 16:38:18 +02:00
|
|
|
|
redirectsResult.SearchResults = mapped;
|
2018-09-26 22:16:54 +01:00
|
|
|
|
//not doing paging 'yet'
|
2020-05-28 07:12:53 +02:00
|
|
|
|
redirectsResult.TotalCount = mapped.Count;
|
2018-09-26 22:16:54 +01:00
|
|
|
|
redirectsResult.CurrentPage = 1;
|
|
|
|
|
|
redirectsResult.PageCount = 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
return redirectsResult;
|
|
|
|
|
|
}
|
2016-08-03 17:57:01 +02:00
|
|
|
|
[HttpPost]
|
2020-05-28 07:12:53 +02:00
|
|
|
|
public IActionResult DeleteRedirectUrl(Guid id)
|
2016-08-03 17:57:01 +02:00
|
|
|
|
{
|
2020-05-28 07:12:53 +02:00
|
|
|
|
_redirectUrlService.Delete(id);
|
2016-08-04 17:48:50 +02:00
|
|
|
|
return Ok();
|
2016-08-03 17:57:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-08-03 20:01:43 +02:00
|
|
|
|
[HttpPost]
|
2020-05-28 07:12:53 +02:00
|
|
|
|
public IActionResult ToggleUrlTracker(bool disable)
|
2016-08-03 20:01:43 +02:00
|
|
|
|
{
|
2020-06-04 13:53:06 +02:00
|
|
|
|
var userIsAdmin = _webSecurity.CurrentUser.IsAdmin();
|
2016-09-04 11:44:16 +02:00
|
|
|
|
if (userIsAdmin == false)
|
|
|
|
|
|
{
|
2017-09-12 16:22:16 +02:00
|
|
|
|
var errorMessage = "User is not a member of the administrators group and so is not allowed to toggle the URL tracker";
|
2020-09-16 10:24:05 +02:00
|
|
|
|
_logger.LogDebug(errorMessage);
|
2016-11-03 10:31:44 +01:00
|
|
|
|
throw new SecurityException(errorMessage);
|
2016-09-04 11:44:16 +02:00
|
|
|
|
}
|
2016-08-03 20:01:43 +02:00
|
|
|
|
|
2016-08-04 17:48:50 +02:00
|
|
|
|
var action = disable ? "disable" : "enable";
|
|
|
|
|
|
|
2020-09-08 15:50:52 +02:00
|
|
|
|
_configManipulator.SaveDisableRedirectUrlTracking(disable);
|
2016-08-03 20:01:43 +02:00
|
|
|
|
|
2016-11-03 10:31:44 +01:00
|
|
|
|
return Ok($"URL tracker is now {action}d.");
|
2016-08-03 20:01:43 +02:00
|
|
|
|
}
|
2016-08-03 17:57:01 +02:00
|
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
|
}
|