Skip to content

Commit 7e4892d

Browse files
committed
feat(liquid filter): add returnurlfilter
1 parent c852c35 commit 7e4892d

File tree

8 files changed

+51
-11
lines changed

8 files changed

+51
-11
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using Fluid;
2+
using Fluid.Values;
3+
using Microsoft.AspNetCore.Http;
4+
using Microsoft.AspNetCore.Mvc;
5+
using OrchardCore.Liquid;
6+
using System;
7+
using System.Threading.Tasks;
8+
9+
namespace StatCan.OrchardCore.DisplayHelpers.Filters
10+
{
11+
public class ReturnUrlFilter : ILiquidFilter
12+
{
13+
private readonly HttpContext _httpContext;
14+
15+
public ReturnUrlFilter(IHttpContextAccessor httpContextAccessor)
16+
{
17+
_httpContext = httpContextAccessor.HttpContext;
18+
}
19+
20+
public ValueTask<FluidValue> ProcessAsync(FluidValue input, FilterArguments arguments, TemplateContext context)
21+
{
22+
if (!context.AmbientValues.TryGetValue("UrlHelper", out var urlHelper))
23+
{
24+
throw new ArgumentException("UrlHelper missing while invoking 'return_url'");
25+
}
26+
27+
var returnPath = (_httpContext.Request.PathBase + _httpContext.Request.Path).ToString();
28+
if (arguments["type"].Or(arguments.At(0)).ToStringValue() != "")
29+
{
30+
returnPath = arguments["type"].Or(arguments.At(0)).ToStringValue();
31+
}
32+
returnPath = returnPath.TrimEnd('/').Trim();
33+
34+
if (input.ToStringValue() == "")
35+
{
36+
return new ValueTask<FluidValue>(new StringValue(((IUrlHelper)urlHelper).Content(returnPath)));
37+
}
38+
39+
var targetPath = input.ToStringValue().TrimEnd('/').Trim();
40+
return new ValueTask<FluidValue>(new StringValue(((IUrlHelper)urlHelper).Content(string.Concat(targetPath, "?returnUrl=", returnPath))));
41+
}
42+
}
43+
}

src/Modules/StatCan.OrchardCore.DisplayHelpers/Startup.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public override void ConfigureServices(IServiceCollection services)
2828
services.AddLiquidFilter<UserEmailFilter>("user_email");
2929
services.AddLiquidFilter<IsCurrentUrlFilter>("is_current_url");
3030
services.AddLiquidFilter<GetClaimLiquidFilter>("get_claim");
31+
services.AddLiquidFilter<ReturnUrlFilter>("return_url");
3132
services.AddTagHelpers<SetHttpContextItemsTagHelper>();
3233
services.AddScoped<ILiquidTemplateEventHandler, HttpContextItemsEventHandler>();
3334
}
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
{% assign page_url = Model.ContentItem | display_url %}
21
{% for item in Model.ContentItem.Content.FlowPart.Widgets %}
32
{{ item | shape_build_display | shape_render }}
43
{% endfor %}
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
{% comment %}page_url must be set on the Page content{% endcomment %}
2-
{% assign return_url = '~/login?returnUrl=' | append: page_url | href %}
31
{% if User.Identity.IsAuthenticated %}
42
<a href="#" class="dropdown-item d-lg-none d-flex" data-toggle="modal" data-target="#profile-modal" >{{ "Account" | t }}</a>
53
<a href="#" class="btn btn-primary is-circle d-lg-flex d-none" data-toggle="modal" data-target="#profile-modal" aria-label="{{ 'Account' | t }}"><i class="fas fa-user"></i></a>
64
{% else %}
7-
<a href="{{ return_url }}" class="dropdown-item d-lg-none d-flex">{{ "Log in" | t }}</a>
8-
<a href="{{ return_url }}" class="btn btn-primary is-circle d-lg-flex d-none" aria-label="{{ 'Log in' | t }}"><i class="fas fa-sign-in-alt"></i></a>
5+
<a href="{{ '~/login' | return_url }}" class="dropdown-item d-lg-none d-flex">{{ "Log in" | t }}</a>
6+
<a href="{{ '~/login' | return_url }}" class="btn btn-primary is-circle d-lg-flex d-none" aria-label="{{ 'Log in' | t }}"><i class="fas fa-sign-in-alt"></i></a>
97
{% endif %}

src/Themes/PortalTheme/Views/ModalBonusAction.liquid

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<div class="modal-footer di-modal">
3333
<input name="User" type="hidden" value="{{ User.Identity.Name }}" />
3434
<input name="Email" type="hidden" value="{{ User | get_claim: 'email' }}" />
35+
<input name="ReturnUrl" type="hidden" value="{{ '' | return_url }}" />
3536
{% antiforgerytoken %}
3637
<button type="submit" class="btn btn-lg btn-primary is-rounded">{{ "Send request" | t }}</button>
3738
</div>

src/Themes/PortalTheme/Views/ModalFeedback.liquid

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
<div class="modal-footer di-modal">
8989
<input name="User" type="hidden" value="{{ User.Identity.Name }}" />
9090
<input name="Email" type="hidden" value="{{ User | get_claim: 'email' }}" />
91+
<input name="ReturnUrl" type="hidden" value="{{ '' | return_url }}" />
9192
{% antiforgerytoken %}
9293
<button type="submit" class="btn btn-lg btn-primary is-rounded">{{ "Send feedback" | t }}</button>
9394
</div>

src/Themes/PortalTheme/Views/ModalProfile.liquid

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{% zone "Modal" %}
2-
{% comment %}page_url must be set on the Page content{% endcomment %}
3-
{% assign return_url = '~/login?returnUrl=' | append: page_url | href %}
2+
{% assign return_url = '' | return_url %}
43
<div id="profile-modal" class="modal fade" tabindex="-1" role="dialog" aria-hidden="true">
54
<div class="modal-dialog modal-dialog-centered di-modal" role="document">
65
<div class="modal-content section-panel di-modal">
@@ -29,7 +28,7 @@
2928
</div>
3029
</div>
3130
<div class="modal-footer di-modal">
32-
{% form asp_route_area:"OrchardCore.Users", asp_controller:"Account", asp_action:"LogOff", asp_returnUrl:return_url, method: "post" %}
31+
{% form asp_route_area:"OrchardCore.Users", asp_controller:"Account", asp_action:"LogOff", method: "post" %}
3332
<button type="submit" class="btn btn-lg btn-primary is-rounded">{{ "Log out" | t }}</button>
3433
{% endform %}
3534
</div>

src/Themes/PortalTheme/Views/Widget-Hero.liquid

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
{% comment %}page_url must be set on the Page content{% endcomment %}
2-
{% assign return_url = '~/login?returnUrl=' | append: page_url | href %}
31
<section class="hero is-fullheight portal-hud">
42
<div class="container">
53
<header class="hero-header">
@@ -15,7 +13,7 @@
1513
{% else %}
1614
{% unless User.Identity.IsAuthenticated %}
1715
<ul class="navbar-nav">
18-
<a href="{{ return_url }}" class="btn btn-lg btn-primary is-rounded nav-link mt-5">{{ "Log in" | t }}</a>
16+
<a href="{{ '~/login' | return_url }}" class="btn btn-lg btn-primary is-rounded nav-link mt-5">{{ "Log in" | t }}</a>
1917
</ul>
2018
{% endunless %}
2119
{% endif %}

0 commit comments

Comments
 (0)