Updates the membership partial view templates to redirect to current page by default but allows this to be overridden. Creates a new RedirectToCurrentUmbracoUrl method which is slightly diff from RedirectToCurrentUmbracoPage since it doesn't care about the actual page item rendering, just the currently rendered URL which is useful if coming from a rewritten URL (which is what happens with public access rewrites)

This commit is contained in:
Shannon
2014-02-13 13:30:32 +11:00
parent 9827b40bea
commit f5ee02a3e9
11 changed files with 110 additions and 20 deletions

View File

@@ -1,4 +1,5 @@
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@using System.Web.Mvc.Html
@using ClientDependency.Core.Mvc
@using Umbraco.Web
@using Umbraco.Web.Models
@@ -12,6 +13,16 @@
Html.RequiresJs("/umbraco_client/ui/jquery.js");
Html.RequiresJs("/umbraco_client/Application/JQuery/jquery.validate.min.js");
Html.RequiresJs("/umbraco_client/Application/JQuery/jquery.validate.unobtrusive.min.js");
var logoutModel = new PostRedirectModel();
/*
* Here you can specify a redirect URL for after logging out, by default umbraco will simply
* redirect to the current page. Example to redirect to the home page:
*
* logoutModel.RedirectUrl = "/";
*
*/
}
@*NOTE: This RenderJsHere code should be put on your main template page where the rest of your script tags are placed*@
@@ -27,5 +38,7 @@
<legend>Logout</legend>
<button>Logout</button>
</fieldset>
@Html.HiddenFor(m => logoutModel.RedirectUrl)
}
}

View File

@@ -4,6 +4,7 @@ using System.Web.Security;
using umbraco.cms.businesslogic.member;
using Umbraco.Web.Models;
using Umbraco.Web.Mvc;
using Umbraco.Core;
namespace Umbraco.Web.Controllers
{
@@ -24,7 +25,16 @@ namespace Umbraco.Web.Controllers
return CurrentUmbracoPage();
}
return Redirect("/");
//if there is a specified path to redirect to then use it
if (model.RedirectUrl.IsNullOrWhiteSpace() == false)
{
return Redirect(model.RedirectUrl);
}
//redirect to current page by default
TempData.Add("LoginSuccess", true);
//return RedirectToCurrentUmbracoPage();
return RedirectToCurrentUmbracoUrl();
}
}
}

View File

@@ -4,13 +4,14 @@ using System.Web.Security;
using umbraco.cms.businesslogic.member;
using Umbraco.Web.Models;
using Umbraco.Web.Mvc;
using Umbraco.Core;
namespace Umbraco.Web.Controllers
{
public class UmbLoginStatusController : SurfaceController
{
[HttpPost]
public ActionResult HandleLogout([Bind(Prefix = "loginStatusModel")]LoginStatusModel model)
public ActionResult HandleLogout([Bind(Prefix = "logoutModel")]PostRedirectModel model)
{
if (ModelState.IsValid == false)
{
@@ -22,9 +23,15 @@ namespace Umbraco.Web.Controllers
FormsAuthentication.SignOut();
}
//TODO: Shouldn't we be redirecting to the current page or integrating this with the
// normal Umbraco protection stuff?
return Redirect("/");
//if there is a specified path to redirect to then use it
if (model.RedirectUrl.IsNullOrWhiteSpace() == false)
{
return Redirect(model.RedirectUrl);
}
//redirect to current page by default
TempData.Add("LogoutSuccess", true);
return RedirectToCurrentUmbracoPage();
}
}
}

View File

@@ -2,12 +2,13 @@
namespace Umbraco.Web.Models
{
public class LoginModel
public class LoginModel : PostRedirectModel
{
[Required]
public string Username { get; set; }
[Required]
public string Password { get; set; }
}
}

View File

@@ -1,4 +1,5 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.Web;
using Umbraco.Core;
using Umbraco.Web.Security;
@@ -49,6 +50,7 @@ namespace Umbraco.Web.Models
/// <summary>
/// The name of the member
/// </summary>
[Required]
public string Name { get; set; }
/// <summary>
@@ -59,6 +61,7 @@ namespace Umbraco.Web.Models
/// <summary>
/// The email of the member
/// </summary>
[Required]
public string Email { get; set; }
/// <summary>

View File

@@ -0,0 +1,15 @@
namespace Umbraco.Web.Models
{
/// <summary>
/// A base model containing a value to indicate to Umbraco where to redirect to after Posting if
/// a developer doesn't want the controller to redirect to the current Umbraco page - which is the default.
/// </summary>
public class PostRedirectModel
{
/// <summary>
/// The path to redirect to when update is successful, if not specified then the user will be
/// redirected to the current Umbraco page
/// </summary>
public string RedirectUrl { get; set; }
}
}

View File

@@ -10,7 +10,7 @@ using Umbraco.Web.Security;
namespace Umbraco.Web.Models
{
public class ProfileModel
public class ProfileModel : PostRedirectModel
{
public static ProfileModel CreateModel()
{
@@ -60,10 +60,5 @@ namespace Umbraco.Web.Models
/// </remarks>
public List<UmbracoProperty> MemberProperties { get; set; }
/// <summary>
/// The path to redirect to when update is successful, if not specified then the user will be
/// redirected to the current Umbraco page
/// </summary>
public string RedirectUrl { get; set; }
}
}

View File

@@ -10,7 +10,7 @@ using Umbraco.Web.Security;
namespace Umbraco.Web.Models
{
public class RegisterModel
public class RegisterModel : PostRedirectModel
{
/// <summary>
/// Creates a new empty RegisterModel
@@ -74,13 +74,7 @@ namespace Umbraco.Web.Models
[ReadOnly(true)]
[Obsolete("This is no longer used and will be removed from the codebase in future versions")]
public bool RedirectOnSucces { get; set; }
/// <summary>
/// The path to redirect to when registration is successful, if not specified then the user will be
/// redirected to the current Umbraco page
/// </summary>
public string RedirectUrl { get; set; }
/// <summary>
/// The username of the model, if UsernameIsEmail is true then this is ignored.
/// </summary>

View File

@@ -7,6 +7,43 @@ using Umbraco.Web.Routing;
namespace Umbraco.Web.Mvc
{
/// <summary>
/// Redirects to the current URL rendering an Umbraco page
/// </summary>
/// <remarks>
/// this is useful if you need to redirect
/// to the current page but the current page is actually a rewritten URL normally done with something like
/// Server.Transfer.
/// </remarks>
public class RedirectToUmbracoUrlResult : ActionResult
{
private readonly UmbracoContext _umbracoContext;
/// <summary>
/// Creates a new RedirectToUmbracoResult
/// </summary>
/// <param name="umbracoContext"></param>
public RedirectToUmbracoUrlResult(UmbracoContext umbracoContext)
{
_umbracoContext = umbracoContext;
}
public override void ExecuteResult(ControllerContext context)
{
if (context == null) throw new ArgumentNullException("context");
if (context.IsChildAction)
{
throw new InvalidOperationException("Cannot redirect from a Child Action");
}
var destinationUrl = _umbracoContext.OriginalRequestUrl.PathAndQuery;
context.Controller.TempData.Keep();
context.HttpContext.Response.Redirect(destinationUrl, endResponse: false);
}
}
/// <summary>
/// Redirects to an Umbraco page by Id or Entity
/// </summary>

View File

@@ -75,6 +75,20 @@ namespace Umbraco.Web.Mvc
return new RedirectToUmbracoPageResult(CurrentPage, UmbracoContext);
}
/// <summary>
/// Redirects to the currently rendered Umbraco URL
/// </summary>
/// <returns></returns>
/// <remarks>
/// this is useful if you need to redirect
/// to the current page but the current page is actually a rewritten URL normally done with something like
/// Server.Transfer.
/// </remarks>
protected RedirectToUmbracoUrlResult RedirectToCurrentUmbracoUrl()
{
return new RedirectToUmbracoUrlResult(UmbracoContext);
}
/// <summary>
/// Returns the currently rendered Umbraco page
/// </summary>

View File

@@ -297,6 +297,7 @@
<Compile Include="Editors\MediaController.cs" />
<Compile Include="Models\ChangingPasswordModel.cs" />
<Compile Include="Models\IRenderModel.cs" />
<Compile Include="Models\PostRedirectModel.cs" />
<Compile Include="Models\RenderModelOfTContent.cs" />
<Compile Include="Mvc\EnsurePublishedContentRequestAttribute.cs" />
<Compile Include="Mvc\UmbracoTemplatePageOfTContent.cs" />