Go to the documentation of this file.00001 using System;
00002 using System.Collections.Generic;
00003 using System.Web;
00004 using System.Web.Mvc;
00005 using N2.Engine;
00006 using N2.Security;
00007 using N2.Engine.Globalization;
00008 using System.Threading;
00009 using System.Globalization;
00010
00011 namespace N2.Web.Mvc
00012 {
00017 public abstract class ContentController<T> : Controller
00018 where T : ContentItem
00019 {
00020 #region Accessors
00021 ContentItem currentPart;
00022 T currentItem;
00023 ContentItem currentPage;
00024 IEngine engine;
00025
00029 public virtual IEngine Engine
00030 {
00031 get
00032 {
00033 return engine
00034 ?? (engine = RouteData.GetEngine());
00035 }
00036 set { engine = value; }
00037 }
00038
00040 public virtual T CurrentItem
00041 {
00042 get
00043 {
00044 return currentItem
00045 ?? (currentItem = ControllerContext.RequestContext.CurrentItem<T>());
00046 }
00047 set { currentItem = value; }
00048 }
00049
00051 public ContentItem CurrentPage
00052 {
00053 get
00054 {
00055 return currentPage
00056 ?? (currentPage = RouteData.CurrentPage()
00057 ?? CurrentItem.ClosestPage()
00058 ?? Engine.UrlParser.CurrentPage);
00059 }
00060 set { currentPage = value; }
00061 }
00062
00064 public ContentItem CurrentPart
00065 {
00066 get
00067 {
00068 return currentPart
00069 ?? (currentPart = ControllerContext.RequestContext.CurrentPart<ContentItem>());
00070 }
00071 set { currentPart = value; }
00072 }
00073 #endregion
00074
00075
00076
00079 public virtual ActionResult Index()
00080 {
00081 return View(CurrentItem);
00082 }
00083
00084
00085
00086
00087 protected override void OnActionExecuting(ActionExecutingContext filterContext)
00088 {
00089 SetCulture(filterContext);
00090 ValidateAuthorization(filterContext);
00091
00092 MergeModelStateFromEarlierStep(filterContext.HttpContext);
00093
00094 base.OnActionExecuting(filterContext);
00095 }
00096
00097 protected virtual void SetCulture(ActionExecutingContext filterContext)
00098 {
00099 if (ControllerContext.IsChildAction || CurrentItem == null)
00100 return;
00101
00102 var gateway = Engine.Resolve<ILanguageGateway>();
00103 if (!gateway.Enabled)
00104 return;
00105
00106 ILanguage language = gateway.GetLanguage(CurrentItem);
00107 if (language == null || string.IsNullOrEmpty(language.LanguageCode))
00108 return;
00109
00110 var culture = CultureInfo.GetCultureInfoByIetfLanguageTag(language.LanguageCode);
00111 Thread.CurrentThread.CurrentCulture = culture;
00112 Thread.CurrentThread.CurrentUICulture = culture;
00113 }
00114
00117 protected virtual void ValidateAuthorization(ActionExecutingContext filterContext)
00118 {
00119 if (CurrentItem != null)
00120 {
00121 var securityManager = Engine.Resolve<ISecurityManager>();
00122
00123 if (!securityManager.IsAuthorized(CurrentItem, User))
00124 filterContext.Result = new HttpUnauthorizedResult();
00125 }
00126 }
00127
00128 protected override void OnResultExecuting(ResultExecutingContext filterContext)
00129 {
00130 ProcessOutputCache(filterContext);
00131
00132 base.OnResultExecuting(filterContext);
00133 }
00134
00137 protected virtual void ProcessOutputCache(ResultExecutingContext filterContext)
00138 {
00139 new ContentOutputCacheAttribute().OnResultExecuting(filterContext);
00140 }
00141
00142 private void MergeModelStateFromEarlierStep(HttpContextBase context)
00143 {
00144 const string modelStateForMergingKey = "N2_ModelStateForMerging";
00145
00146 ModelStateDictionary modelState = ModelState;
00147 if (context.Items.Contains(modelStateForMergingKey))
00148 {
00149 modelState = context.Items[modelStateForMergingKey] as ModelStateDictionary;
00150
00151 if (modelState != null)
00152 ModelState.Merge(modelState);
00153 }
00154
00155 context.Items[modelStateForMergingKey] = modelState;
00156 }
00157
00158 protected virtual ActionResult RedirectToParentPage()
00159 {
00160 return Redirect(CurrentPage.Url);
00161 }
00162
00167 protected internal virtual ViewPageResult ViewParentPage()
00168 {
00169 if (CurrentItem != null && CurrentItem.IsPage)
00170 {
00171 throw new InvalidOperationException(
00172 "The current page is already being rendered. ViewParentPage should only be used from content items to render their parent page.");
00173 }
00174
00175 return ViewPage(CurrentPage);
00176 }
00177
00182 protected internal virtual ViewPageResult ViewPage(ContentItem thePage)
00183 {
00184 if (thePage == null)
00185 throw new ArgumentNullException("thePage");
00186
00187 if (!thePage.IsPage)
00188 throw new InvalidOperationException("Item " + thePage.GetContentType().Name + " is not a page type and cannot be rendered on its own.");
00189
00190 if (thePage == CurrentItem)
00191 throw new InvalidOperationException("The page passed into ViewPage was the current page. This would cause an infinite loop.");
00192
00193 var mapper = Engine.Resolve<IControllerMapper>();
00194 var webContext = Engine.Resolve<IWebContext>();
00195
00196 return new ViewPageResult(thePage, mapper, webContext, ActionInvoker);
00197 }
00198
00204 protected new ViewResultBase View(string viewName)
00205 {
00206 return View(viewName, CurrentItem);
00207 }
00208 }
00209 }