Directing 401 results to an Access Denied url. Login fields are shown only when current user is anonymous. Account controller and views are relocated to the Orchard.Users package.

--HG--
extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4045026
This commit is contained in:
loudej
2010-01-06 04:31:38 +00:00
parent dcbda9458f
commit d5edf2b711
16 changed files with 162 additions and 35 deletions

View File

@@ -1,8 +1,9 @@
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<div id="logindisplay"><%
if (Request.IsAuthenticated) {
%>Welcome <strong><%=Html.Encode(Page.User.Identity.Name) %></strong>! [<%=Html.ActionLink("Log Off", "LogOff", "Account", new { area = "" }, new { }) %>]<%
} else {
%>[<%=Html.ActionLink("Log On", "LogOn", "Account", new{area=""}, new{}) %>]<%
}
%></div>
<div id="logindisplay">
<% if (Request.IsAuthenticated) { %>
Welcome <strong><%=Html.Encode(Page.User.Identity.Name) %></strong>!
[<%=Html.ActionLink("Log Off", "LogOff", new { Controller = "Account", Area = "Orchard.Users" })%>]
<% } else { %>
[<%=Html.ActionLink("Log On", "LogOn", new { Controller = "Account", Area = "Orchard.Users", ReturnUrl = Context.Request.RawUrl })%>]
<% } %>
</div>

View File

@@ -92,10 +92,6 @@
<Content Include="Default.aspx" />
<Content Include="Global.asax" />
<Content Include="Web.config" />
<Content Include="Views\Account\ChangePassword.ascx" />
<Content Include="Views\Account\ChangePasswordSuccess.ascx" />
<Content Include="Views\Account\LogOn.ascx" />
<Content Include="Views\Account\Register.ascx" />
<Content Include="Views\Home\About.ascx" />
<Content Include="Views\Home\Index.ascx" />
<Content Include="Views\Web.config" />

View File

@@ -1,23 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using Orchard.Data;
using Orchard.DevTools.ViewModels;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Records;
using Orchard.Mvc.ViewModels;
using Orchard.UI.Notify;
namespace Orchard.DevTools.Controllers
{
public class HomeController : Controller
{
private readonly INotifier _notifier;
public HomeController(INotifier notifier) {
_notifier = notifier;
}
public ActionResult Index()
{
return View(new BaseViewModel());
}
public ActionResult NotAuthorized() {
_notifier.Warning("Simulated error goes here.");
return new HttpUnauthorizedResult();
}
}
}

View File

@@ -2,3 +2,4 @@
<%@ Import Namespace="Orchard.Mvc.ViewModels"%>
<h1><%=Html.TitleForPage("Dev Tools") %></h1>
<p><%=Html.ActionLink("Contents", "Index", "Content") %></p>
<p><%=Html.ActionLink("Test Unauthorized Request", "NotAuthorized", "Home")%></p>

View File

@@ -6,8 +6,9 @@ using System.Web.Mvc;
using System.Web.Security;
using Orchard.Mvc.ViewModels;
using Orchard.Security;
using Orchard.Users.ViewModels;
namespace Orchard.Controllers {
namespace Orchard.Users.Controllers {
[HandleError]
public class AccountController : Controller {
private readonly IAuthenticationService _authenticationService;
@@ -19,17 +20,24 @@ namespace Orchard.Controllers {
_membershipService = membershipService;
}
public ActionResult LogOn() {
public ActionResult AccessDenied(string returnUrl) {
if (_authenticationService.GetAuthenticatedUser() == null)
return View("LogOn", new LogOnViewModel { Title = "Access Denied", ReturnUrl = returnUrl });
return View(new BaseViewModel());
}
public ActionResult LogOn(string returnUrl) {
return View("LogOn", new LogOnViewModel { Title = "Log On", ReturnUrl = returnUrl });
}
[AcceptVerbs(HttpVerbs.Post)]
[SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings",
Justification = "Needs to take same parameter type as Controller.Redirect()")]
public ActionResult LogOn(string userName, string password, bool rememberMe, string returnUrl) {
var user = ValidateLogOn(userName, password);
if (!ModelState.IsValid) {
return LogOn();
return View("LogOn", new LogOnViewModel { Title = "Log On", ReturnUrl = returnUrl });
}
_authenticationService.SignIn(user, rememberMe);
@@ -38,14 +46,14 @@ namespace Orchard.Controllers {
return Redirect(returnUrl);
}
else {
return RedirectToAction("Index", "Home");
return Redirect("~/");
}
}
public ActionResult LogOff() {
_authenticationService.SignOut();
return RedirectToAction("Index", "Home");
return Redirect("~/");
}
int MinPasswordLength {
@@ -67,11 +75,11 @@ namespace Orchard.Controllers {
if (ValidateRegistration(userName, email, password, confirmPassword)) {
// Attempt to register the user
var user = _membershipService.CreateUser(new CreateUserParams(userName, password, email, null, null, true));
if (user != null) {
_authenticationService.SignIn(user, false /* createPersistentCookie */);
return RedirectToAction("Index", "Home");
return Redirect("~/");
}
else {
ModelState.AddModelError("_FORM", ErrorCodeToString(/*createStatus*/MembershipCreateStatus.ProviderError));
@@ -102,7 +110,7 @@ namespace Orchard.Controllers {
try {
var validated = _membershipService.ValidateUser(User.Identity.Name, currentPassword);
if (validated != null) {
_membershipService.SetPassword(validated, newPassword);
return RedirectToAction("ChangePasswordSuccess");
@@ -225,7 +233,6 @@ namespace Orchard.Controllers {
#endregion
}
public interface IMembershipServiceShim {
int MinPasswordLength { get; }

View File

@@ -1,10 +1,7 @@
using System;
using System.Linq;
using System.Web.Mvc;
using Orchard.Data;
using Orchard.Localization;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Handlers;
using Orchard.Security;
using Orchard.UI.Notify;
using Orchard.Users.Models;

View File

@@ -61,6 +61,7 @@
<Reference Include="System.Web.Mobile" />
</ItemGroup>
<ItemGroup>
<Compile Include="Controllers\AccountController.cs" />
<Compile Include="Controllers\AdminController.cs" />
<Compile Include="Controllers\UserDriver.cs" />
<Compile Include="Models\User.cs" />
@@ -69,12 +70,18 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Services\MembershipService.cs" />
<Compile Include="AdminMenu.cs" />
<Compile Include="ViewModels\LogOnViewModel.cs" />
<Compile Include="ViewModels\UserCreateViewModel.cs" />
<Compile Include="ViewModels\UserEditViewModel.cs" />
<Compile Include="ViewModels\UsersIndexViewModel.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="Package.txt" />
<Content Include="Views\Account\ChangePassword.ascx" />
<Content Include="Views\Account\ChangePasswordSuccess.ascx" />
<Content Include="Views\Account\AccessDenied.ascx" />
<Content Include="Views\Account\LogOn.ascx" />
<Content Include="Views\Account\Register.ascx" />
<Content Include="Views\Admin\Edit.aspx" />
<Content Include="Views\Admin\Create.aspx" />
<Content Include="Views\Admin\EditorTemplates\inputPasswordLarge.ascx" />

View File

@@ -0,0 +1,10 @@
using System;
using Orchard.Mvc.ViewModels;
namespace Orchard.Users.ViewModels {
public class LogOnViewModel : BaseViewModel {
public string Title { get; set; }
public string ReturnUrl { get; set; }
}
}

View File

@@ -0,0 +1,5 @@
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<BaseViewModel>" %>
<%@ Import Namespace="Orchard.Mvc.ViewModels"%>
<h2><%=Html.TitleForPage("Access Denied") %></h2>
<p>You do not have permission to complete your request.</p>

View File

@@ -0,0 +1,31 @@
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<h2><%=Html.TitleForPage("Change Password") %></h2>
<p>Use the form below to change your password. </p>
<p>New passwords are required to be a minimum of <%=Html.Encode(ViewData["PasswordLength"])%> characters in length.</p>
<%= Html.ValidationSummary("Password change was unsuccessful. Please correct the errors and try again.")%>
<% using (Html.BeginFormAntiForgeryPost()) { %>
<div>
<fieldset>
<legend>Account Information</legend>
<p>
<label for="currentPassword">Current password:</label>
<%= Html.Password("currentPassword") %>
<%= Html.ValidationMessage("currentPassword") %>
</p>
<p>
<label for="newPassword">New password:</label>
<%= Html.Password("newPassword") %>
<%= Html.ValidationMessage("newPassword") %>
</p>
<p>
<label for="confirmPassword">Confirm new password:</label>
<%= Html.Password("confirmPassword") %>
<%= Html.ValidationMessage("confirmPassword") %>
</p>
<p>
<input type="submit" value="Change Password" />
</p>
</fieldset>
</div>
<% } %>

View File

@@ -0,0 +1,3 @@
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<h2><%=Html.TitleForPage("Change Password") %></h2>
<p>Your password has been changed successfully.</p>

View File

@@ -0,0 +1,32 @@
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<LogOnViewModel>" %>
<%@ Import Namespace="Orchard.Users.ViewModels"%>
<%@ Import Namespace="Orchard.Mvc.ViewModels"%>
<h2><%=Html.TitleForPage(Model.Title) %></h2>
<p>Please enter your username and password. <%= Html.ActionLink("Register", "Register") %> if you don't have an account.</p>
<%= Html.ValidationSummary("Login was unsuccessful. Please correct the errors and try again.") %>
<% using (Html.BeginForm(new {Action="LogOn"})) { %>
<div>
<fieldset>
<legend>Account Information</legend>
<p>
<label for="username">Username:</label>
<%= Html.TextBox("username") %>
<%= Html.ValidationMessage("username") %>
</p>
<p>
<label for="password">Password:</label>
<%= Html.Password("password") %>
<%= Html.ValidationMessage("password") %>
</p>
<p>
<%= Html.CheckBox("rememberMe") %> <label class="inline" for="rememberMe">Remember me?</label>
</p>
<p>
<%=Html.HiddenFor(m=>m.ReturnUrl) %>
<%=Html.AntiForgeryTokenOrchard() %>
<input type="submit" value="Log On" />
</p>
</fieldset>
</div>
<% } %>

View File

@@ -0,0 +1,35 @@
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<h2><%=Html.TitleForPage("Create a New Account") %></h2>
<p>Use the form below to create a new account. </p>
<p>Passwords are required to be a minimum of <%=Html.Encode(ViewData["PasswordLength"])%> characters in length.</p>
<%= Html.ValidationSummary("Account creation was unsuccessful. Please correct the errors and try again.") %>
<% using (Html.BeginFormAntiForgeryPost()) { %>
<div>
<fieldset>
<legend>Account Information</legend>
<p>
<label for="username">Username:</label>
<%= Html.TextBox("username") %>
<%= Html.ValidationMessage("username") %>
</p>
<p>
<label for="email">Email:</label>
<%= Html.TextBox("email") %>
<%= Html.ValidationMessage("email") %>
</p>
<p>
<label for="password">Password:</label>
<%= Html.Password("password") %>
<%= Html.ValidationMessage("password") %>
</p>
<p>
<label for="confirmPassword">Confirm password:</label>
<%= Html.Password("confirmPassword") %>
<%= Html.ValidationMessage("confirmPassword") %>
</p>
<p>
<input type="submit" value="Register" />
</p>
</fieldset>
</div>
<% } %>

View File

@@ -1,5 +1,5 @@
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<AdminViewModel>" %>
<%@ Import Namespace="Orchard.Mvc.ViewModels"%>
<% if (Model.CurrentUser != null) { //todo: (heskew) localize the string format "User: <username> | a:logoff"
%><div id="login"><%="User"%>: <%=Html.Encode(Model.CurrentUser.UserName)%> | <%=Html.ActionLink("Logout", "LogOff", new { Area = "", Controller = "Account" }) %></div><%
%><div id="login"><%="User"%>: <%=Html.Encode(Model.CurrentUser.UserName)%> | <%=Html.ActionLink("Logout", "LogOff", new { Area = "Orchard.Users", Controller = "Account" }) %></div><%
} %>

View File

@@ -58,7 +58,7 @@
ASP.NET to identify an incoming user.
-->
<authentication mode="Forms">
<forms loginUrl="~/Account/LogOn" timeout="2880" />
<forms loginUrl="~/Users/Account/AccessDenied" timeout="2880" />
</authentication>
<membership defaultProvider="OrchardMembershipProvider">

View File

@@ -179,7 +179,6 @@
<Compile Include="ContentManagement\Records\Utility.cs" />
<Compile Include="Mvc\ViewModels\ContentItemViewModel.cs" />
<Compile Include="ContentManagement\ViewModels\TemplateViewModel.cs" />
<Compile Include="Controllers\AccountController.cs" />
<Compile Include="Controllers\HomeController.cs" />
<Compile Include="Data\Conventions\AttributeCollectionConvention.cs" />
<Compile Include="Data\Conventions\CascadeAllDeleteOrphanAttribute.cs" />