Skip to content

Commit

Permalink
Autoroute container routing (#5665)
Browse files Browse the repository at this point in the history
  • Loading branch information
deanmarcussen authored Apr 11, 2020
1 parent ed9448a commit 95b3301
Show file tree
Hide file tree
Showing 75 changed files with 2,401 additions and 306 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
Expand Down Expand Up @@ -53,16 +54,28 @@ public override IDisplayResult Edit(AutoroutePart autoroutePart, BuildPartEditor
{
model.Path = autoroutePart.Path;
model.AutoroutePart = autoroutePart;
model.ContentItem = autoroutePart.ContentItem;
model.SetHomepage = false;

var siteSettings = await _siteService.GetSiteSettingsAsync();
var homeRoute = siteSettings.HomeRoute;

if (autoroutePart.ContentItem.ContentItemId == homeRoute?[_options.ContentItemIdKey]?.ToString())
if (homeRoute != null && homeRoute.TryGetValue(_options.ContainedContentItemIdKey, out var containedContentItemId))
{
if (string.Equals(autoroutePart.ContentItem.ContentItemId, containedContentItemId.ToString(), StringComparison.OrdinalIgnoreCase))
{
model.IsHomepage = true;
}
}
else if (string.Equals(autoroutePart.ContentItem.ContentItemId, homeRoute?[_options.ContentItemIdKey]?.ToString(), StringComparison.OrdinalIgnoreCase))
{
model.IsHomepage = true;
}

model.Disabled = autoroutePart.Disabled;
model.Absolute = autoroutePart.Absolute;
model.RouteContainedItems = autoroutePart.RouteContainedItems;

model.Settings = context.TypePartDefinition.GetSettings<AutoroutePartSettings>();
});
}
Expand All @@ -71,34 +84,42 @@ public override async Task<IDisplayResult> UpdateAsync(AutoroutePart model, IUpd
{
var viewModel = new AutoroutePartViewModel();

await updater.TryUpdateModelAsync(viewModel, Prefix, t => t.Path, t => t.UpdatePath);
await updater.TryUpdateModelAsync(viewModel, Prefix, t => t.Path, t => t.UpdatePath, t => t.RouteContainedItems, t => t.Absolute, t => t.Disabled);

var settings = context.TypePartDefinition.GetSettings<AutoroutePartSettings>();

if (settings.AllowCustomPath)
{
model.Path = viewModel.Path;
}
model.Disabled = viewModel.Disabled;
model.Absolute = viewModel.Absolute;
model.RouteContainedItems = viewModel.RouteContainedItems;

if (settings.AllowUpdatePath && viewModel.UpdatePath)
// When disabled these values are not updated.
if (!model.Disabled)
{
// Make it empty to force a regeneration
model.Path = "";
}
if (settings.AllowCustomPath)
{
model.Path = viewModel.Path;
}

if (settings.AllowUpdatePath && viewModel.UpdatePath)
{
// Make it empty to force a regeneration
model.Path = "";
}

var httpContext = _httpContextAccessor.HttpContext;
var httpContext = _httpContextAccessor.HttpContext;

if (httpContext != null && await _authorizationService.AuthorizeAsync(httpContext.User, Permissions.SetHomepage))
{
await updater.TryUpdateModelAsync(model, Prefix, t => t.SetHomepage);
}
if (httpContext != null && await _authorizationService.AuthorizeAsync(httpContext.User, Permissions.SetHomepage))
{
await updater.TryUpdateModelAsync(model, Prefix, t => t.SetHomepage);
}

await ValidateAsync(model, updater);
await ValidateAsync(model, updater, settings);
}

return Edit(model, context);
}

private async Task ValidateAsync(AutoroutePart autoroute, IUpdateModel updater)
private async Task ValidateAsync(AutoroutePart autoroute, IUpdateModel updater, AutoroutePartSettings settings)
{
if (autoroute.Path == "/")
{
Expand All @@ -116,9 +137,24 @@ private async Task ValidateAsync(AutoroutePart autoroute, IUpdateModel updater)
updater.ModelState.AddModelError(Prefix, nameof(autoroute.Path), S["Your permalink is too long. The permalink can only be up to {0} characters.", MaxPathLength]);
}

if (autoroute.Path != null && (await _session.QueryIndex<AutoroutePartIndex>(o => o.Path == autoroute.Path && o.ContentItemId != autoroute.ContentItem.ContentItemId).CountAsync()) > 0)
// This can only validate the path if the Autoroute is not managing content item routes or the path is absolute.
if (!String.IsNullOrEmpty(autoroute.Path) && (!settings.ManageContainedItemRoutes || (settings.ManageContainedItemRoutes && autoroute.Absolute)))
{
updater.ModelState.AddModelError(Prefix, nameof(autoroute.Path), S["Your permalink is already in use."]);
var possibleConflicts = await _session.QueryIndex<AutoroutePartIndex>(o => o.Path == autoroute.Path).ListAsync();
if (possibleConflicts.Any())
{
var hasConflict = false;
// This logic is different to the check in the handler as here we checking
if (possibleConflicts.Any(x => x.ContentItemId != autoroute.ContentItem.ContentItemId) ||
possibleConflicts.Any(x => !string.IsNullOrEmpty(x.ContainedContentItemId) && x.ContainedContentItemId != autoroute.ContentItem.ContentItemId))
{
hasConflict = true;
}
if (hasConflict)
{
updater.ModelState.AddModelError(Prefix, nameof(autoroute.Path), S["Your permalink is already in use."]);
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Routing;
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Handlers;
using OrchardCore.ContentManagement.Routing;

namespace OrchardCore.Autoroute.Handlers
{
public class AutorouteContentHandler : ContentHandlerBase
{
private readonly IAutorouteEntries _autorouteEntries;

public AutorouteContentHandler(IAutorouteEntries autorouteEntries)
{
_autorouteEntries = autorouteEntries;
}

public override Task GetContentItemAspectAsync(ContentItemAspectContext context)
{
return context.ForAsync<ContentItemMetadata>(metadata =>
{
// When a content item is contained we provide different route values when generating urls.
if (_autorouteEntries.TryGetEntryByContentItemId(context.ContentItem.ContentItemId, out var entry) &&
!string.IsNullOrEmpty(entry.ContainedContentItemId))
{
metadata.DisplayRouteValues = new RouteValueDictionary {
{ "Area", "OrchardCore.Contents" },
{ "Controller", "Item" },
{ "Action", "Display" },
{ "ContentItemId", entry.ContentItemId},
{ "ContainedContentItemId", entry.ContainedContentItemId }
};
}

return Task.CompletedTask;
});
}
}
}
Loading

0 comments on commit 95b3301

Please sign in to comment.