2021-08-11 12:17:35 +02:00
|
|
|
using System;
|
2020-04-20 12:20:47 +02:00
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2021-02-09 10:22:42 +01:00
|
|
|
using Umbraco.Cms.Core.Cache;
|
|
|
|
|
using Umbraco.Cms.Core.Composing;
|
|
|
|
|
using Umbraco.Cms.Core.Logging;
|
|
|
|
|
using Umbraco.Cms.Core.Services;
|
|
|
|
|
using Umbraco.Cms.Core.Web;
|
|
|
|
|
using Umbraco.Cms.Core.Web.Mvc;
|
2021-02-12 13:36:50 +01:00
|
|
|
using Umbraco.Cms.Infrastructure.Persistence;
|
2021-02-10 11:42:04 +01:00
|
|
|
using Umbraco.Cms.Web.Common.Attributes;
|
2020-05-07 10:08:23 +02:00
|
|
|
using Umbraco.Extensions;
|
2020-04-20 12:20:47 +02:00
|
|
|
|
2021-02-10 11:42:04 +01:00
|
|
|
namespace Umbraco.Cms.Web.Common.Controllers
|
2020-04-20 12:20:47 +02:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Provides a base class for plugin controllers.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public abstract class PluginController : Controller, IDiscoverable
|
|
|
|
|
{
|
|
|
|
|
private static readonly ConcurrentDictionary<Type, PluginControllerMetadata> MetadataStorage
|
|
|
|
|
= new ConcurrentDictionary<Type, PluginControllerMetadata>();
|
|
|
|
|
|
|
|
|
|
// for debugging purposes
|
|
|
|
|
internal Guid InstanceId { get; } = Guid.NewGuid();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the Umbraco context.
|
|
|
|
|
/// </summary>
|
2021-08-11 12:17:35 +02:00
|
|
|
public virtual IUmbracoContext UmbracoContext
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2021-08-17 11:41:28 +02:00
|
|
|
var umbracoContext = UmbracoContextAccessor.GetRequiredUmbracoContext();
|
2021-08-11 12:17:35 +02:00
|
|
|
return umbracoContext;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-20 12:20:47 +02:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the database context accessor.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public virtual IUmbracoContextAccessor UmbracoContextAccessor { get; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the database context.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IUmbracoDatabaseFactory DatabaseFactory { get; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the services context.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public ServiceContext Services { get; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the application cache.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public AppCaches AppCaches { get; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the profiling logger.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IProfilingLogger ProfilingLogger { get; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets metadata for this instance.
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal PluginControllerMetadata Metadata => GetMetadata(GetType());
|
|
|
|
|
|
2020-09-21 11:46:29 +02:00
|
|
|
protected PluginController(IUmbracoContextAccessor umbracoContextAccessor, IUmbracoDatabaseFactory databaseFactory, ServiceContext services, AppCaches appCaches, IProfilingLogger profilingLogger)
|
2020-04-20 12:20:47 +02:00
|
|
|
{
|
|
|
|
|
UmbracoContextAccessor = umbracoContextAccessor;
|
|
|
|
|
DatabaseFactory = databaseFactory;
|
|
|
|
|
Services = services;
|
|
|
|
|
AppCaches = appCaches;
|
|
|
|
|
ProfilingLogger = profilingLogger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets metadata for a controller type.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="controllerType">The controller type.</param>
|
|
|
|
|
/// <returns>Metadata for the controller type.</returns>
|
2020-05-14 17:04:16 +10:00
|
|
|
public static PluginControllerMetadata GetMetadata(Type controllerType)
|
2020-04-20 12:20:47 +02:00
|
|
|
{
|
|
|
|
|
return MetadataStorage.GetOrAdd(controllerType, type =>
|
|
|
|
|
{
|
|
|
|
|
// plugin controller? back-office controller?
|
|
|
|
|
var pluginAttribute = controllerType.GetCustomAttribute<PluginControllerAttribute>(false);
|
|
|
|
|
var backOfficeAttribute = controllerType.GetCustomAttribute<IsBackOfficeAttribute>(true);
|
|
|
|
|
|
|
|
|
|
return new PluginControllerMetadata
|
|
|
|
|
{
|
|
|
|
|
AreaName = pluginAttribute?.AreaName,
|
|
|
|
|
ControllerName = ControllerExtensions.GetControllerName(controllerType),
|
|
|
|
|
ControllerNamespace = controllerType.Namespace,
|
|
|
|
|
ControllerType = controllerType,
|
|
|
|
|
IsBackOffice = backOfficeAttribute != null
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|