mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-02-09 09:16:41 +08:00
#18298: Preventing infinite loops in url alternates
Work Item: 18298 --HG-- branch : 1.x
This commit is contained in:
@@ -10,17 +10,19 @@ namespace Orchard.DesignerTools.Services {
|
|||||||
[OrchardFeature("UrlAlternates")]
|
[OrchardFeature("UrlAlternates")]
|
||||||
public class UrlAlternatesFactory : ShapeDisplayEvents {
|
public class UrlAlternatesFactory : ShapeDisplayEvents {
|
||||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||||
private readonly List<string> _urlAlternates;
|
private readonly Lazy<List<string>> _urlAlternates;
|
||||||
|
|
||||||
public UrlAlternatesFactory(IHttpContextAccessor httpContextAccessor) {
|
public UrlAlternatesFactory(IHttpContextAccessor httpContextAccessor) {
|
||||||
_httpContextAccessor = httpContextAccessor;
|
_httpContextAccessor = httpContextAccessor;
|
||||||
var httpContext = _httpContextAccessor.Current();
|
|
||||||
|
|
||||||
if(httpContext == null) {
|
_urlAlternates = new Lazy<List<string>>(() => {
|
||||||
return;
|
var httpContext = _httpContextAccessor.Current();
|
||||||
}
|
|
||||||
|
|
||||||
var request = _httpContextAccessor.Current().Request;
|
if (httpContext == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
var request = _httpContextAccessor.Current().Request;
|
||||||
|
|
||||||
// extract each segment of the url
|
// extract each segment of the url
|
||||||
var urlSegments = VirtualPathUtility.ToAppRelative(request.Path.ToLower())
|
var urlSegments = VirtualPathUtility.ToAppRelative(request.Path.ToLower())
|
||||||
@@ -29,26 +31,34 @@ namespace Orchard.DesignerTools.Services {
|
|||||||
.Select(url => url.Replace("-", "__").Replace(".", "_")) // format the alternate
|
.Select(url => url.Replace("-", "__").Replace(".", "_")) // format the alternate
|
||||||
.ToArray();
|
.ToArray();
|
||||||
|
|
||||||
if ( String.IsNullOrWhiteSpace(urlSegments[0]) ) {
|
if (String.IsNullOrWhiteSpace(urlSegments[0])) {
|
||||||
urlSegments[0] = "homepage";
|
urlSegments[0] = "homepage";
|
||||||
}
|
}
|
||||||
|
|
||||||
_urlAlternates = Enumerable.Range(1, urlSegments.Count()).Select(range => String.Join("__", urlSegments.Take(range))).ToList();
|
return Enumerable.Range(1, urlSegments.Count()).Select(range => String.Join("__", urlSegments.Take(range))).ToList();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Displaying(ShapeDisplayingContext context) {
|
public override void Displaying(ShapeDisplayingContext context) {
|
||||||
if (_urlAlternates == null || !_urlAlternates.Any()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
context.ShapeMetadata.OnDisplaying(displayedContext => {
|
context.ShapeMetadata.OnDisplaying(displayedContext => {
|
||||||
|
|
||||||
|
if (_urlAlternates.Value == null || !_urlAlternates.Value.Any()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// prevent applying alternate again, c.f. http://orchard.codeplex.com/workitem/18298
|
||||||
|
if(displayedContext.ShapeMetadata.Alternates.Any(x => x.Contains("__url__"))) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// appends Url alternates to current ones
|
// appends Url alternates to current ones
|
||||||
displayedContext.ShapeMetadata.Alternates = displayedContext.ShapeMetadata.Alternates.SelectMany(
|
displayedContext.ShapeMetadata.Alternates = displayedContext.ShapeMetadata.Alternates.SelectMany(
|
||||||
alternate => new [] { alternate }.Union(_urlAlternates.Select(a => alternate + "__url__" + a))
|
alternate => new [] { alternate }.Union(_urlAlternates.Value.Select(a => alternate + "__url__" + a))
|
||||||
).ToList();
|
).ToList();
|
||||||
|
|
||||||
// appends [ShapeType]__url__[Url] alternates
|
// appends [ShapeType]__url__[Url] alternates
|
||||||
displayedContext.ShapeMetadata.Alternates = _urlAlternates.Select(url => displayedContext.ShapeMetadata.Type + "__url__" + url)
|
displayedContext.ShapeMetadata.Alternates = _urlAlternates.Value.Select(url => displayedContext.ShapeMetadata.Type + "__url__" + url)
|
||||||
.Union(displayedContext.ShapeMetadata.Alternates)
|
.Union(displayedContext.ShapeMetadata.Alternates)
|
||||||
.ToList();
|
.ToList();
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user