2021-01-07 22:05:23 +11:00
using System ;
2019-02-14 12:51:46 +01:00
using System.IO ;
using System.Text ;
using Umbraco.Core.Configuration ;
2020-09-08 13:03:43 +02:00
using Umbraco.Core.Configuration.Models ;
2020-04-03 11:03:06 +11:00
using Umbraco.Core.Hosting ;
2019-02-14 12:51:46 +01:00
using Umbraco.Core.Models.PublishedContent ;
using Umbraco.Core.Services ;
using Umbraco.Web.PublishedCache ;
using Umbraco.Web.Security ;
namespace Umbraco.Web
{
2021-01-07 22:05:23 +11:00
// NOTE: This has been migrated to netcore
2019-02-14 12:51:46 +01:00
/// <summary>
2020-02-09 18:53:37 +01:00
/// Creates and manages <see cref="IUmbracoContext"/> instances.
2019-02-14 12:51:46 +01:00
/// </summary>
public class UmbracoContextFactory : IUmbracoContextFactory
{
private readonly IUmbracoContextAccessor _umbracoContextAccessor ;
private readonly IPublishedSnapshotService _publishedSnapshotService ;
private readonly IVariationContextAccessor _variationContextAccessor ;
private readonly IDefaultCultureAccessor _defaultCultureAccessor ;
2020-09-08 13:03:43 +02:00
private readonly GlobalSettings _globalSettings ;
2019-02-14 12:51:46 +01:00
private readonly IUserService _userService ;
2020-04-03 11:03:06 +11:00
private readonly IHostingEnvironment _hostingEnvironment ;
2020-02-13 12:29:08 +01:00
private readonly IHttpContextAccessor _httpContextAccessor ;
2020-02-19 09:26:51 +01:00
private readonly ICookieManager _cookieManager ;
2020-02-17 12:07:51 +01:00
private readonly UriUtility _uriUtility ;
2019-02-14 12:51:46 +01:00
/// <summary>
/// Initializes a new instance of the <see cref="UmbracoContextFactory"/> class.
/// </summary>
2020-02-17 12:07:51 +01:00
public UmbracoContextFactory (
IUmbracoContextAccessor umbracoContextAccessor ,
IPublishedSnapshotService publishedSnapshotService ,
IVariationContextAccessor variationContextAccessor ,
IDefaultCultureAccessor defaultCultureAccessor ,
2020-09-08 13:03:43 +02:00
GlobalSettings globalSettings ,
2020-02-17 12:07:51 +01:00
IUserService userService ,
2020-04-03 11:03:06 +11:00
IHostingEnvironment hostingEnvironment ,
2020-02-17 12:45:37 +01:00
UriUtility uriUtility ,
2020-02-19 09:26:51 +01:00
IHttpContextAccessor httpContextAccessor ,
ICookieManager cookieManager )
2019-02-14 12:51:46 +01:00
{
_umbracoContextAccessor = umbracoContextAccessor ? ? throw new ArgumentNullException ( nameof ( umbracoContextAccessor ) ) ;
_publishedSnapshotService = publishedSnapshotService ? ? throw new ArgumentNullException ( nameof ( publishedSnapshotService ) ) ;
_variationContextAccessor = variationContextAccessor ? ? throw new ArgumentNullException ( nameof ( variationContextAccessor ) ) ;
_defaultCultureAccessor = defaultCultureAccessor ? ? throw new ArgumentNullException ( nameof ( defaultCultureAccessor ) ) ;
2020-08-23 13:55:36 +02:00
_globalSettings = globalSettings ? ? throw new ArgumentNullException ( nameof ( globalSettings ) ) ;
2019-02-14 12:51:46 +01:00
_userService = userService ? ? throw new ArgumentNullException ( nameof ( userService ) ) ;
2020-04-03 11:03:06 +11:00
_hostingEnvironment = hostingEnvironment ;
2020-02-17 12:07:51 +01:00
_uriUtility = uriUtility ;
2020-02-13 12:29:08 +01:00
_httpContextAccessor = httpContextAccessor ;
2020-02-19 09:26:51 +01:00
_cookieManager = cookieManager ;
2019-02-14 12:51:46 +01:00
}
2020-02-13 13:57:39 +01:00
private IUmbracoContext CreateUmbracoContext ( )
2019-02-14 12:51:46 +01:00
{
// make sure we have a variation context
if ( _variationContextAccessor . VariationContext = = null )
2019-07-12 13:15:41 +10:00
{
// TODO: By using _defaultCultureAccessor.DefaultCulture this means that the VariationContext will always return a variant culture, it will never
// return an empty string signifying that the culture is invariant. But does this matter? Are we actually expecting this to return an empty string
// for invariant routes? From what i can tell throughout the codebase is that whenever we are checking against the VariationContext.Culture we are
// also checking if the content type varies by culture or not. This is fine, however the code in the ctor of VariationContext is then misleading
// since it's assuming that the Culture can be empty (invariant) when in reality of a website this will never be empty since a real culture is always set here.
2019-02-14 12:51:46 +01:00
_variationContextAccessor . VariationContext = new VariationContext ( _defaultCultureAccessor . DefaultCulture ) ;
2019-07-12 13:15:41 +10:00
}
2019-11-20 15:21:09 +01:00
2020-10-21 16:51:00 +11:00
return new UmbracoContext ( _httpContextAccessor , _publishedSnapshotService , new BackOfficeSecurity ( ) , _globalSettings , _hostingEnvironment , _variationContextAccessor , _uriUtility , _cookieManager ) ;
2019-02-14 12:51:46 +01:00
}
/// <inheritdoc />
2020-02-13 13:57:39 +01:00
public UmbracoContextReference EnsureUmbracoContext ( )
2019-02-14 12:51:46 +01:00
{
var currentUmbracoContext = _umbracoContextAccessor . UmbracoContext ;
2019-06-20 17:58:40 +10:00
if ( currentUmbracoContext ! = null )
2020-02-19 15:46:54 +11:00
return new UmbracoContextReference ( currentUmbracoContext , false , _umbracoContextAccessor ) ;
2019-02-14 12:51:46 +01:00
2019-03-11 15:36:39 +01:00
2020-02-13 13:57:39 +01:00
var umbracoContext = CreateUmbracoContext ( ) ;
2019-02-14 12:51:46 +01:00
_umbracoContextAccessor . UmbracoContext = umbracoContext ;
2020-02-19 15:46:54 +11:00
return new UmbracoContextReference ( umbracoContext , true , _umbracoContextAccessor ) ;
2019-02-14 12:51:46 +01:00
}
}
2019-02-15 10:42:02 +11:00
}