Refactoring HttpNotFound results

--HG--
branch : 1.x
This commit is contained in:
Sebastien Ros
2011-09-14 15:12:32 -07:00
parent 57554e112f
commit 13577fba22
2 changed files with 35 additions and 31 deletions

View File

@@ -24,6 +24,10 @@ namespace Orchard.Core.Contents.Controllers {
// /Contents/Item/Display/72 // /Contents/Item/Display/72
public ActionResult Display(int id) { public ActionResult Display(int id) {
var contentItem = _contentManager.Get(id, VersionOptions.Published); var contentItem = _contentManager.Get(id, VersionOptions.Published);
if (contentItem == null)
return HttpNotFound();
dynamic model = _contentManager.BuildDisplay(contentItem); dynamic model = _contentManager.BuildDisplay(contentItem);
return new ShapeResult(this, model); return new ShapeResult(this, model);
} }
@@ -32,12 +36,16 @@ namespace Orchard.Core.Contents.Controllers {
// /Contents/Item/Preview/72?version=5 // /Contents/Item/Preview/72?version=5
public ActionResult Preview(int id, int? version) { public ActionResult Preview(int id, int? version) {
var versionOptions = VersionOptions.Latest; var versionOptions = VersionOptions.Latest;
if (version != null) if (version != null)
versionOptions = VersionOptions.Number((int)version); versionOptions = VersionOptions.Number((int)version);
var contentItem = _contentManager.Get(id, versionOptions); var contentItem = _contentManager.Get(id, versionOptions);
if (!Services.Authorizer.Authorize(Permissions.EditContent, contentItem, T("Cannot edit content"))) if (contentItem == null)
return HttpNotFound();
if (!Services.Authorizer.Authorize(Permissions.EditContent, contentItem, T("Cannot preview content")))
return new HttpUnauthorizedResult(); return new HttpUnauthorizedResult();
dynamic model = _contentManager.BuildDisplay(contentItem); dynamic model = _contentManager.BuildDisplay(contentItem);

View File

@@ -1,65 +1,61 @@
using System; using System.Linq;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc; using System.Web.Mvc;
using Orchard.ContentManagement; using Orchard.ContentManagement;
using Orchard.ContentManagement.Aspects; using Orchard.ContentManagement.Aspects;
using Orchard.Core.Contents;
using Orchard.Core.Routable.Models; using Orchard.Core.Routable.Models;
using Orchard.Core.Routable.Services; using Orchard.Core.Routable.Services;
using Orchard.Data; using Orchard.Data;
using Orchard.DisplayManagement;
using Orchard.Localization; using Orchard.Localization;
using Orchard.Mvc; using Orchard.Mvc;
using Orchard.Services;
using Orchard.Themes; using Orchard.Themes;
namespace Orchard.Core.Routable.Controllers { namespace Orchard.Core.Routable.Controllers {
[ValidateInput(false)] [ValidateInput(false)]
public class ItemController : Controller, IUpdateModel { public class ItemController : Controller, IUpdateModel {
private readonly IContentManager _contentManager;
private readonly ITransactionManager _transactionManager; private readonly ITransactionManager _transactionManager;
private readonly IRoutablePathConstraint _routablePathConstraint; private readonly IRoutablePathConstraint _routablePathConstraint;
private readonly IWorkContextAccessor _workContextAccessor;
private readonly IHomePageProvider _routableHomePageProvider;
public ItemController( public ItemController(
IContentManager contentManager,
ITransactionManager transactionManager, ITransactionManager transactionManager,
IRoutablePathConstraint routablePathConstraint, IRoutablePathConstraint routablePathConstraint,
IShapeFactory shapeFactory, IOrchardServices services
IWorkContextAccessor workContextAccessor, ) {
IEnumerable<IHomePageProvider> homePageProviders) {
_contentManager = contentManager;
_transactionManager = transactionManager; _transactionManager = transactionManager;
_routablePathConstraint = routablePathConstraint; _routablePathConstraint = routablePathConstraint;
_workContextAccessor = workContextAccessor; Services = services;
_routableHomePageProvider = homePageProviders.SingleOrDefault(p => p.GetProviderName() == RoutableHomePageProvider.Name);
Shape = shapeFactory;
T = NullLocalizer.Instance; T = NullLocalizer.Instance;
} }
public Localizer T { get; set; } public Localizer T { get; set; }
dynamic Shape { get; set; } public IOrchardServices Services { get; private set; }
[Themed] [Themed]
public ActionResult Display(string path) { public ActionResult Display(string path) {
var matchedPath = _routablePathConstraint.FindPath(path); var matchedPath = _routablePathConstraint.FindPath(path);
if (matchedPath == null) { if (matchedPath == null) {
throw new ApplicationException(T("404 - should not have passed path constraint").Text); return HttpNotFound(T("Should not have passed path constraint").Text);
} }
var hits = _contentManager var hits = Services.ContentManager
.Query<RoutePart, RoutePartRecord>(VersionOptions.Published) .Query<RoutePart, RoutePartRecord>(VersionOptions.Published)
.Where(r => r.Path == matchedPath) .Where(r => r.Path == matchedPath)
.Slice(0, 2); .Slice(0, 2)
.ToList();
if (hits.Count() == 0) { if (hits.Count() == 0) {
throw new ApplicationException(T("404 - should not have passed path constraint").Text); return HttpNotFound(T("Should not have passed path constraint").Text);
}
if (hits.Count() != 1) {
throw new ApplicationException(T("Ambiguous content").Text);
} }
dynamic model = _contentManager.BuildDisplay(hits.Single()); if (hits.Count() != 1) {
return HttpNotFound(T("Ambiguous content").Text);
}
if (!Services.Authorizer.Authorize(Permissions.EditContent, hits.Single(), T("Cannot preview content")))
return new HttpUnauthorizedResult();
dynamic model = Services.ContentManager.BuildDisplay(hits.Single());
return new ShapeResult(this, model); return new ShapeResult(this, model);
} }
@@ -71,19 +67,19 @@ namespace Orchard.Core.Routable.Controllers {
return Json(slug); return Json(slug);
if (id != null) if (id != null)
contentItem = _contentManager.Get((int)id, VersionOptions.Latest); contentItem = Services.ContentManager.Get((int)id, VersionOptions.Latest);
if (contentItem == null) { if (contentItem == null) {
contentItem = _contentManager.Create(contentType, VersionOptions.Draft); contentItem = Services.ContentManager.Create(contentType, VersionOptions.Draft);
if (containerId != null) { if (containerId != null) {
var containerItem = _contentManager.Get((int)containerId); var containerItem = Services.ContentManager.Get((int)containerId);
contentItem.As<ICommonPart>().Container = containerItem; contentItem.As<ICommonPart>().Container = containerItem;
} }
} }
_contentManager.UpdateEditor(contentItem, this); Services.ContentManager.UpdateEditor(contentItem, this);
_contentManager.Publish(contentItem); Services.ContentManager.Publish(contentItem);
_transactionManager.Cancel(); _transactionManager.Cancel();
return Json(contentItem.As<IRoutableAspect>().GetEffectiveSlug() ?? slug); return Json(contentItem.As<IRoutableAspect>().GetEffectiveSlug() ?? slug);