Cleanup and document controller constructors

This commit is contained in:
Stephan
2018-11-28 16:19:50 +01:00
parent f9a19ada04
commit 8d49fb3e8b
6 changed files with 113 additions and 85 deletions

View File

@@ -40,27 +40,27 @@ namespace Umbraco.Web.Editors
[IsBackOffice]
public class AuthenticationController : UmbracoApiController
{
//fixme inject these
private BackOfficeUserManager<BackOfficeIdentityUser> _userManager;
private BackOfficeSignInManager _signInManager;
protected BackOfficeUserManager<BackOfficeIdentityUser> UserManager
{
get { return _userManager ?? (_userManager = TryGetOwinContext().Result.GetBackOfficeUserManager()); }
}
protected BackOfficeSignInManager SignInManager
{
get { return _signInManager ?? (_signInManager = TryGetOwinContext().Result.GetBackOfficeSignInManager()); }
}
/// <summary>
/// Initializes a new instance of the new <see cref="AuthenticationController"/> class with auto dependencies.
/// </summary>
public AuthenticationController()
{
}
{ }
public AuthenticationController(IGlobalSettings globalSettings, UmbracoContext umbracoContext, ISqlContext sqlContext, ServiceContext services, CacheHelper applicationCache, ILogger logger, IProfilingLogger profilingLogger, IRuntimeState runtimeState)
: base(globalSettings, umbracoContext, sqlContext, services, applicationCache, logger, profilingLogger, runtimeState)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="AuthenticationController"/> class with all its dependencies.
/// </summary>
public AuthenticationController(IGlobalSettings globalSettings, IUmbracoContextAccessor umbracoContextAccessor, ISqlContext sqlContext, ServiceContext services, CacheHelper applicationCache, IProfilingLogger logger, IRuntimeState runtimeState)
: base(globalSettings, umbracoContextAccessor, sqlContext, services, applicationCache, logger, runtimeState)
{ }
protected BackOfficeUserManager<BackOfficeIdentityUser> UserManager => _userManager
?? (_userManager = TryGetOwinContext().Result.GetBackOfficeUserManager());
protected BackOfficeSignInManager SignInManager => _signInManager
?? (_signInManager = TryGetOwinContext().Result.GetBackOfficeSignInManager());
/// <summary>
/// Returns the configuration for the backoffice user membership provider - used to configure the change password dialog

View File

@@ -27,11 +27,17 @@ namespace Umbraco.Web.Editors
[WebApi.UmbracoAuthorize]
public class DashboardController : UmbracoApiController
{
/// <summary>
/// Initializes a new instance of the <see cref="DashboardController"/> with auto dependencies.
/// </summary>
public DashboardController()
{ }
public DashboardController(IGlobalSettings globalSettings, UmbracoContext umbracoContext, ISqlContext sqlContext, ServiceContext services, CacheHelper applicationCache, ILogger logger, IProfilingLogger profilingLogger, IRuntimeState runtimeState)
: base(globalSettings, umbracoContext, sqlContext, services, applicationCache, logger, profilingLogger, runtimeState)
/// <summary>
/// Initializes a new instance of the <see cref="DashboardController"/> with all its dependencies.
/// </summary>
public DashboardController(IGlobalSettings globalSettings, IUmbracoContextAccessor umbracoContextAccessor, ISqlContext sqlContext, ServiceContext services, CacheHelper applicationCache, IProfilingLogger logger, IRuntimeState runtimeState)
: base(globalSettings, umbracoContextAccessor, sqlContext, services, applicationCache, logger, runtimeState)
{ }
//we have just one instance of HttpClient shared for the entire application

View File

@@ -20,14 +20,18 @@ namespace Umbraco.Web.WebServices
// TODO: This controller should be moved to a more suitable place.
public class TagsController : UmbracoApiController
{
/// <summary>
/// Initializes a new instance of the <see cref="TagsController"/> with auto dependencies.
/// </summary>
public TagsController()
{
}
{ }
public TagsController(IGlobalSettings globalSettings, UmbracoContext umbracoContext, ISqlContext sqlContext, ServiceContext services, CacheHelper applicationCache, ILogger logger, IProfilingLogger profilingLogger, IRuntimeState runtimeState)
: base(globalSettings, umbracoContext, sqlContext, services, applicationCache, logger, profilingLogger, runtimeState)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="TagsController"/> with all its dependencies.
/// </summary>
public TagsController(IGlobalSettings globalSettings, IUmbracoContextAccessor umbracoContextAccessor, ISqlContext sqlContext, ServiceContext services, CacheHelper applicationCache, IProfilingLogger logger, IRuntimeState runtimeState)
: base(globalSettings, umbracoContextAccessor, sqlContext, services, applicationCache, logger, runtimeState)
{ }
/// <summary>
/// Get every tag stored in the database (with optional group)

View File

@@ -13,11 +13,18 @@ namespace Umbraco.Web.WebApi
/// </summary>
public abstract class UmbracoApiController : UmbracoApiControllerBase, IDiscoverable
{
/// <summary>
/// Initializes a new instance of the <see cref="UmbracoApiController"/> with auto dependencies.
/// </summary>
/// <remarks>Dependencies are obtained from the <see cref="Current"/> service locator.</remarks>
protected UmbracoApiController()
{ }
protected UmbracoApiController(IGlobalSettings globalSettings, UmbracoContext umbracoContext, ISqlContext sqlContext, ServiceContext services, CacheHelper applicationCache, ILogger logger, IProfilingLogger profilingLogger, IRuntimeState runtimeState)
: base(globalSettings, umbracoContext, sqlContext, services, applicationCache, logger, profilingLogger, runtimeState)
/// <summary>
/// Initialize a new instance of the <see cref="UmbracoApiController"/> with all its dependencies.
/// </summary>
protected UmbracoApiController(IGlobalSettings globalSettings, IUmbracoContextAccessor umbracoContextAccessor, ISqlContext sqlContext, ServiceContext services, CacheHelper applicationCache, IProfilingLogger logger, IRuntimeState runtimeState)
: base(globalSettings, umbracoContextAccessor, sqlContext, services, applicationCache, logger, runtimeState)
{ }
}
}

View File

@@ -21,48 +21,82 @@ namespace Umbraco.Web.WebApi
[FeatureAuthorize]
public abstract class UmbracoApiControllerBase : ApiController
{
private readonly IUmbracoContextAccessor _umbracoContextAccessor;
private UmbracoHelper _umbracoHelper;
// for debugging purposes
// 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(
Current.Factory.GetInstance<IGlobalSettings>(),
Current.Factory.GetInstance<IUmbracoContextAccessor>(),
Current.Factory.GetInstance<ISqlContext>(),
Current.Factory.GetInstance<ServiceContext>(),
Current.Factory.GetInstance<CacheHelper>(),
Current.Factory.GetInstance<IProfilingLogger>(),
Current.Factory.GetInstance<IRuntimeState>()
)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="UmbracoApiControllerBase"/> class with all its dependencies.
/// </summary>
protected UmbracoApiControllerBase(IGlobalSettings globalSettings, IUmbracoContextAccessor umbracoContextAccessor, ISqlContext sqlContext, ServiceContext services, CacheHelper applicationCache, IProfilingLogger logger, IRuntimeState runtimeState)
{
GlobalSettings = globalSettings;
_umbracoContextAccessor = umbracoContextAccessor;
SqlContext = sqlContext;
Services = services;
ApplicationCache = applicationCache;
Logger = logger;
RuntimeState = runtimeState;
}
/// <summary>
/// Gets a unique instance identifier.
/// </summary>
/// <remarks>For debugging purposes.</remarks>
internal Guid InstanceId { get; } = Guid.NewGuid();
/// <summary>
/// Gets or sets the Umbraco context.
/// Gets the Umbraco context.
/// </summary>
public virtual IGlobalSettings GlobalSettings { get; }
/// <summary>
/// Gets or sets the Umbraco context.
/// Gets the Umbraco context.
/// </summary>
public virtual UmbracoContext UmbracoContext { get; }
public virtual UmbracoContext UmbracoContext => _umbracoContextAccessor.UmbracoContext;
/// <summary>
/// Gets or sets the sql context.
/// Gets the sql context.
/// </summary>
public ISqlContext SqlContext { get; }
/// <summary>
/// Gets or sets the services context.
/// Gets the services context.
/// </summary>
public ServiceContext Services { get; }
/// <summary>
/// Gets or sets the application cache.
/// Gets the application cache.
/// </summary>
public CacheHelper ApplicationCache { get; }
/// <summary>
/// Gets or sets the logger.
/// Gets the logger.
/// </summary>
public ILogger Logger { get; }
public IProfilingLogger Logger { get; }
/// <summary>
/// Gets or sets the profiling logger.
/// </summary>
public IProfilingLogger ProfilingLogger { get; }
/// <summary>
/// Gets or sets the runtime state.
/// Gets the runtime state.
/// </summary>
internal IRuntimeState RuntimeState { get; }
@@ -87,49 +121,16 @@ namespace Umbraco.Web.WebApi
/// </summary>
public WebSecurity Security => UmbracoContext.Security;
protected UmbracoApiControllerBase()
: this(
Current.Factory.GetInstance<IGlobalSettings>(),
Current.Factory.GetInstance<IUmbracoContextAccessor>().UmbracoContext,
Current.Factory.GetInstance<ISqlContext>(),
Current.Factory.GetInstance<ServiceContext>(),
Current.Factory.GetInstance<CacheHelper>(),
Current.Factory.GetInstance<ILogger>(),
Current.Factory.GetInstance<IProfilingLogger>(),
Current.Factory.GetInstance<IRuntimeState>()
)
{
}
// fixme - Inject fewer things? (Aggregate more)
// fixme - inject the context accessor not the context itself?
// fixme - profiling logger is logger, merge!
protected UmbracoApiControllerBase(IGlobalSettings globalSettings, UmbracoContext umbracoContext, ISqlContext sqlContext, ServiceContext services, CacheHelper applicationCache, ILogger logger, IProfilingLogger profilingLogger, IRuntimeState runtimeState)
{
GlobalSettings = globalSettings;
UmbracoContext = umbracoContext;
SqlContext = sqlContext;
Services = services;
ApplicationCache = applicationCache;
Logger = logger;
ProfilingLogger = profilingLogger;
RuntimeState = runtimeState;
}
/// <summary>
/// Tries to get the current HttpContext.
/// </summary>
protected Attempt<HttpContextBase> TryGetHttpContext()
{
return Request.TryGetHttpContext();
}
=> Request.TryGetHttpContext();
/// <summary>
/// Tries to get the current OWIN context.
/// </summary>
protected Attempt<IOwinContext> TryGetOwinContext()
{
return Request.TryGetOwinContext();
}
=> Request.TryGetOwinContext();
}
}

View File

@@ -5,18 +5,18 @@ using Umbraco.Core.Logging;
using Umbraco.Web.WebApi.Filters;
using Umbraco.Core.Models.Identity;
using Umbraco.Core.Persistence;
using Umbraco.Core.Security;
using Umbraco.Core.Services;
using Umbraco.Web.Security;
namespace Umbraco.Web.WebApi
{
/// <summary>
/// Provides a base class for autorized auto-routed Umbraco API controllers.
/// Provides a base class for authorized auto-routed Umbraco API controllers.
/// </summary>
/// <remarks>
/// This controller will also append a custom header to the response if the user is logged in using forms authentication
/// which indicates the seconds remaining before their timeout expires.
/// This controller will also append a custom header to the response if the user
/// is logged in using forms authentication which indicates the seconds remaining
/// before their timeout expires.
/// </remarks>
[IsBackOffice]
[UmbracoUserTimeoutFilter]
@@ -30,14 +30,24 @@ namespace Umbraco.Web.WebApi
{
private BackOfficeUserManager<BackOfficeIdentityUser> _userManager;
protected BackOfficeUserManager<BackOfficeIdentityUser> UserManager
=> _userManager ?? (_userManager = TryGetOwinContext().Result.GetBackOfficeUserManager());
/// <summary>
/// Initializes a new instance of the <see cref="UmbracoAuthorizedApiController"/> with auto dependencies.
/// </summary>
/// <remarks>Dependencies are obtained from the <see cref="Current"/> service locator.</remarks>
protected UmbracoAuthorizedApiController()
{ }
protected UmbracoAuthorizedApiController(IGlobalSettings globalSettings, UmbracoContext umbracoContext, ISqlContext sqlContext, ServiceContext services, CacheHelper applicationCache, ILogger logger, IProfilingLogger profilingLogger, IRuntimeState runtimeState)
: base(globalSettings, umbracoContext, sqlContext, services, applicationCache, logger, profilingLogger, runtimeState)
/// <summary>
/// Initializes a new instance of the <see cref="UmbracoAuthorizedApiController"/> class with all its dependencies.
/// </summary>
protected UmbracoAuthorizedApiController(IGlobalSettings globalSettings, IUmbracoContextAccessor umbracoContextAccessor, ISqlContext sqlContext, ServiceContext services, CacheHelper applicationCache, IProfilingLogger logger, IRuntimeState runtimeState)
: base(globalSettings, umbracoContextAccessor, sqlContext, services, applicationCache, logger, runtimeState)
{ }
/// <summary>
/// Gets the user manager.
/// </summary>
protected BackOfficeUserManager<BackOfficeIdentityUser> UserManager
=> _userManager ?? (_userManager = TryGetOwinContext().Result.GetBackOfficeUserManager());
}
}