using System;
using System.Collections.Concurrent;
using System.Web.Mvc;
using Umbraco.Core;
using Umbraco.Core.Logging;
using Umbraco.Core.Services;
using Umbraco.Web.Security;
using Umbraco.Web.WebApi;
namespace Umbraco.Web.Mvc
{
///
/// A base class for all plugin controllers to inherit from
///
public abstract class PluginController : Controller
{
///
/// stores the metadata about plugin controllers
///
private static readonly ConcurrentDictionary MetadataStorage = new ConcurrentDictionary();
private UmbracoHelper _umbracoHelper;
///
/// Default constructor
///
///
protected PluginController(UmbracoContext umbracoContext)
{
if (umbracoContext == null) throw new ArgumentNullException("umbracoContext");
UmbracoContext = umbracoContext;
InstanceId = Guid.NewGuid();
}
protected PluginController(UmbracoContext umbracoContext, UmbracoHelper umbracoHelper)
{
if (umbracoContext == null) throw new ArgumentNullException("umbracoContext");
if (umbracoHelper == null) throw new ArgumentNullException("umbracoHelper");
UmbracoContext = umbracoContext;
InstanceId = Guid.NewGuid();
_umbracoHelper = umbracoHelper;
}
///
/// Useful for debugging
///
internal Guid InstanceId { get; private set; }
///
/// Returns the MemberHelper instance
///
public MembershipHelper Members
{
get { return Umbraco.MembershipHelper; }
}
///
/// Returns an UmbracoHelper object
///
public virtual UmbracoHelper Umbraco
{
get { return _umbracoHelper ?? (_umbracoHelper = new UmbracoHelper(UmbracoContext)); }
}
///
/// Returns an ILogger
///
public ILogger Logger
{
get { return ProfilingLogger.Logger; }
}
///
/// Returns a ProfilingLogger
///
public virtual ProfilingLogger ProfilingLogger
{
get { return UmbracoContext.Application.ProfilingLogger; }
}
///
/// Returns the current UmbracoContext
///
public virtual UmbracoContext UmbracoContext { get; private set; }
///
/// Returns the current ApplicationContext
///
public virtual ApplicationContext ApplicationContext
{
get { return UmbracoContext.Application; }
}
///
/// Returns a ServiceContext
///
public ServiceContext Services
{
get { return ApplicationContext.Services; }
}
///
/// Returns a DatabaseContext
///
public DatabaseContext DatabaseContext
{
get { return ApplicationContext.DatabaseContext; }
}
///
/// Returns the metadata for this instance
///
internal PluginControllerMetadata Metadata
{
get { return GetMetadata(this.GetType()); }
}
///
/// Returns the metadata for a PluginController
///
///
///
internal static PluginControllerMetadata GetMetadata(Type type)
{
return MetadataStorage.GetOrAdd(type, type1 =>
{
var pluginAttribute = type.GetCustomAttribute(false);
//check if any inherited class of this type contains the IsBackOffice attribute
var backOfficeAttribute = type.GetCustomAttribute(true);
var meta = new PluginControllerMetadata()
{
AreaName = pluginAttribute == null ? null : pluginAttribute.AreaName,
ControllerName = ControllerExtensions.GetControllerName(type),
ControllerNamespace = type.Namespace,
ControllerType = type,
IsBackOffice = backOfficeAttribute != null
};
MetadataStorage.TryAdd(type, meta);
return meta;
});
}
}
}