2017-07-20 11:21:28 +02:00
|
|
|
|
using System;
|
2015-04-13 15:06:40 +10:00
|
|
|
|
using System.Web;
|
|
|
|
|
|
using System.Web.Http;
|
2020-10-30 11:16:17 +00:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2015-04-13 15:06:40 +10:00
|
|
|
|
using Microsoft.Owin;
|
|
|
|
|
|
using Umbraco.Core;
|
2016-09-01 19:06:08 +02:00
|
|
|
|
using Umbraco.Core.Cache;
|
2019-12-19 10:43:00 +01:00
|
|
|
|
using Umbraco.Web.Composing;
|
2020-09-14 14:12:38 +02:00
|
|
|
|
using Umbraco.Core.Configuration.Models;
|
2015-04-13 15:06:40 +10:00
|
|
|
|
using Umbraco.Core.Logging;
|
2019-03-19 19:16:24 +01:00
|
|
|
|
using Umbraco.Core.Mapping;
|
2016-12-16 17:56:10 +01:00
|
|
|
|
using Umbraco.Core.Persistence;
|
2015-04-13 15:06:40 +10:00
|
|
|
|
using Umbraco.Core.Services;
|
2020-02-17 14:58:34 +01:00
|
|
|
|
using Umbraco.Web.Features;
|
2020-02-14 13:04:49 +01:00
|
|
|
|
using Umbraco.Web.Routing;
|
2018-03-27 10:04:07 +02:00
|
|
|
|
using Umbraco.Web.WebApi.Filters;
|
2020-10-23 14:18:53 +11:00
|
|
|
|
using Umbraco.Core.Security;
|
2015-04-13 15:06:40 +10:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.WebApi
|
|
|
|
|
|
{
|
2020-08-23 13:55:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
2015-04-13 15:06:40 +10:00
|
|
|
|
/// <summary>
|
2016-09-01 19:06:08 +02:00
|
|
|
|
/// Provides a base class for Umbraco API controllers.
|
2015-04-13 15:06:40 +10:00
|
|
|
|
/// </summary>
|
2016-09-01 19:06:08 +02:00
|
|
|
|
/// <remarks>These controllers are NOT auto-routed.</remarks>
|
2018-03-27 10:04:07 +02:00
|
|
|
|
[FeatureAuthorize]
|
2020-02-17 14:58:34 +01:00
|
|
|
|
public abstract class UmbracoApiControllerBase : ApiController, IUmbracoFeature
|
2015-04-13 15:06:40 +10:00
|
|
|
|
{
|
2020-08-23 13:55:36 +02:00
|
|
|
|
|
2018-11-28 16:19:50 +01:00
|
|
|
|
// note: all Umbraco controllers have two constructors: one with all dependencies, which should be used,
|
|
|
|
|
|
// and one with auto dependencies, ie no dependencies - and then dependencies are automatically obtained
|
|
|
|
|
|
// here from the Current service locator - this is obviously evil, but it allows us to add new dependencies
|
|
|
|
|
|
// without breaking compatibility.
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initializes a new instance of the <see cref="UmbracoApiControllerBase"/> class with auto dependencies.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <remarks>Dependencies are obtained from the <see cref="Current"/> service locator.</remarks>
|
|
|
|
|
|
protected UmbracoApiControllerBase()
|
|
|
|
|
|
: this(
|
2020-10-27 10:53:01 +00:00
|
|
|
|
Current.Factory.GetRequiredService<GlobalSettings>(),
|
|
|
|
|
|
Current.Factory.GetRequiredService<IUmbracoContextAccessor>(),
|
2020-11-24 12:52:48 +01:00
|
|
|
|
Current.Factory.GetRequiredService<IBackOfficeSecurityAccessor>(),
|
2020-10-27 10:53:01 +00:00
|
|
|
|
Current.Factory.GetRequiredService<ISqlContext>(),
|
|
|
|
|
|
Current.Factory.GetRequiredService<ServiceContext>(),
|
|
|
|
|
|
Current.Factory.GetRequiredService<AppCaches>(),
|
|
|
|
|
|
Current.Factory.GetRequiredService<IProfilingLogger>(),
|
|
|
|
|
|
Current.Factory.GetRequiredService<IRuntimeState>(),
|
|
|
|
|
|
Current.Factory.GetRequiredService<UmbracoMapper>(),
|
|
|
|
|
|
Current.Factory.GetRequiredService<IPublishedUrlProvider>()
|
2018-11-28 16:19:50 +01:00
|
|
|
|
)
|
|
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initializes a new instance of the <see cref="UmbracoApiControllerBase"/> class with all its dependencies.
|
|
|
|
|
|
/// </summary>
|
2020-11-24 12:52:48 +01:00
|
|
|
|
protected UmbracoApiControllerBase(GlobalSettings globalSettings, IUmbracoContextAccessor umbracoContextAccessor, IBackOfficeSecurityAccessor backOfficeSecurityAccessor, ISqlContext sqlContext, ServiceContext services, AppCaches appCaches, IProfilingLogger logger, IRuntimeState runtimeState, UmbracoMapper umbracoMapper, IPublishedUrlProvider publishedUrlProvider)
|
2018-11-28 16:19:50 +01:00
|
|
|
|
{
|
2019-02-14 12:40:45 +01:00
|
|
|
|
UmbracoContextAccessor = umbracoContextAccessor;
|
2020-11-24 12:52:48 +01:00
|
|
|
|
BackOfficeSecurityAccessor = backOfficeSecurityAccessor;
|
2018-11-28 16:19:50 +01:00
|
|
|
|
SqlContext = sqlContext;
|
|
|
|
|
|
Services = services;
|
2019-01-18 15:05:20 +01:00
|
|
|
|
AppCaches = appCaches;
|
2018-11-28 16:19:50 +01:00
|
|
|
|
Logger = logger;
|
|
|
|
|
|
RuntimeState = runtimeState;
|
2019-10-18 14:18:19 +02:00
|
|
|
|
Mapper = umbracoMapper;
|
2020-02-14 13:04:49 +01:00
|
|
|
|
PublishedUrlProvider = publishedUrlProvider;
|
2018-11-28 16:19:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets a unique instance identifier.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <remarks>For debugging purposes.</remarks>
|
2016-09-01 19:06:08 +02:00
|
|
|
|
internal Guid InstanceId { get; } = Guid.NewGuid();
|
2015-04-13 15:06:40 +10:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2018-11-28 16:19:50 +01:00
|
|
|
|
/// Gets the Umbraco context.
|
2015-04-13 15:06:40 +10:00
|
|
|
|
/// </summary>
|
2020-02-09 18:53:37 +01:00
|
|
|
|
public virtual IUmbracoContext UmbracoContext => UmbracoContextAccessor.UmbracoContext;
|
2019-02-14 12:40:45 +01:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the Umbraco context accessor.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public virtual IUmbracoContextAccessor UmbracoContextAccessor { get; }
|
|
|
|
|
|
|
2020-11-24 12:52:48 +01:00
|
|
|
|
public IBackOfficeSecurityAccessor BackOfficeSecurityAccessor { get; }
|
|
|
|
|
|
|
2019-02-14 12:40:45 +01:00
|
|
|
|
|
2015-04-13 15:06:40 +10:00
|
|
|
|
/// <summary>
|
2018-11-28 16:19:50 +01:00
|
|
|
|
/// Gets the sql context.
|
2015-04-13 15:06:40 +10:00
|
|
|
|
/// </summary>
|
2018-06-19 18:53:25 +02:00
|
|
|
|
public ISqlContext SqlContext { get; }
|
2015-04-13 15:06:40 +10:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2018-11-28 16:19:50 +01:00
|
|
|
|
/// Gets the services context.
|
2015-04-13 15:06:40 +10:00
|
|
|
|
/// </summary>
|
2018-06-19 18:53:25 +02:00
|
|
|
|
public ServiceContext Services { get; }
|
2015-04-13 15:06:40 +10:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2018-11-28 16:19:50 +01:00
|
|
|
|
/// Gets the application cache.
|
2015-04-13 15:06:40 +10:00
|
|
|
|
/// </summary>
|
2019-01-18 15:05:20 +01:00
|
|
|
|
public AppCaches AppCaches { get; }
|
2015-04-13 15:06:40 +10:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2018-11-28 16:19:50 +01:00
|
|
|
|
/// Gets the logger.
|
2015-04-13 15:06:40 +10:00
|
|
|
|
/// </summary>
|
2018-11-28 16:19:50 +01:00
|
|
|
|
public IProfilingLogger Logger { get; }
|
2015-04-13 15:06:40 +10:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2018-11-28 16:19:50 +01:00
|
|
|
|
/// Gets the runtime state.
|
2015-04-13 15:06:40 +10:00
|
|
|
|
/// </summary>
|
2018-06-19 18:53:25 +02:00
|
|
|
|
internal IRuntimeState RuntimeState { get; }
|
2015-04-13 15:06:40 +10:00
|
|
|
|
|
2019-03-19 19:16:24 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the mapper.
|
|
|
|
|
|
/// </summary>
|
2019-04-03 10:39:49 +02:00
|
|
|
|
public UmbracoMapper Mapper { get; }
|
2019-03-19 19:16:24 +01:00
|
|
|
|
|
2020-02-14 13:04:49 +01:00
|
|
|
|
protected IPublishedUrlProvider PublishedUrlProvider { get; }
|
|
|
|
|
|
|
2015-04-13 15:06:40 +10:00
|
|
|
|
/// <summary>
|
2016-09-01 19:06:08 +02:00
|
|
|
|
/// Gets the web security helper.
|
2015-04-13 15:06:40 +10:00
|
|
|
|
/// </summary>
|
2020-11-24 12:52:48 +01:00
|
|
|
|
public IBackOfficeSecurity Security => BackOfficeSecurityAccessor.BackOfficeSecurity;
|
2015-04-13 15:06:40 +10:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2016-09-01 19:06:08 +02:00
|
|
|
|
/// Tries to get the current HttpContext.
|
2015-04-13 15:06:40 +10:00
|
|
|
|
/// </summary>
|
2016-09-01 19:06:08 +02:00
|
|
|
|
protected Attempt<HttpContextBase> TryGetHttpContext()
|
2018-11-28 16:19:50 +01:00
|
|
|
|
=> Request.TryGetHttpContext();
|
2015-04-13 15:06:40 +10:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2016-09-01 19:06:08 +02:00
|
|
|
|
/// Tries to get the current OWIN context.
|
2015-04-13 15:06:40 +10:00
|
|
|
|
/// </summary>
|
2016-09-01 19:06:08 +02:00
|
|
|
|
protected Attempt<IOwinContext> TryGetOwinContext()
|
2018-11-28 16:19:50 +01:00
|
|
|
|
=> Request.TryGetOwinContext();
|
2015-04-13 15:06:40 +10:00
|
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
|
}
|