- Patch for issue #16396 by Andrew Ma

This commit is contained in:
Suha Can
2010-04-16 13:14:45 -07:00
parent 31c154ebc4
commit 13783e748a
4 changed files with 35 additions and 28 deletions

View File

@@ -1,9 +1,9 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Security.Principal;
using System.Web.Mvc;
using System.Web.Security;
using Orchard.Localization;
using Orchard.Logging;
using Orchard.Mvc.Extensions;
using Orchard.Mvc.ViewModels;
@@ -26,11 +26,9 @@ namespace Orchard.Users.Controllers {
_membershipService = membershipService;
_userService = userService;
Logger = NullLogger.Instance;
T = NullLocalizer.Instance;
}
public ILogger Logger { get; set; }
public Localizer T { get; set; }
public ActionResult AccessDenied() {
var returnUrl = Request.QueryString["ReturnUrl"];
@@ -57,8 +55,8 @@ namespace Orchard.Users.Controllers {
[HttpPost]
[SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings",
Justification = "Needs to take same parameter type as Controller.Redirect()")]
public ActionResult LogOn(string userName, string password, bool rememberMe) {
var user = ValidateLogOn(userName, password);
public ActionResult LogOn(string userNameOrEmail, string password, bool rememberMe) {
var user = ValidateLogOn(userNameOrEmail, password);
if (!ModelState.IsValid) {
return View("LogOn", new LogOnViewModel {Title = "Log On"});
}
@@ -100,7 +98,7 @@ namespace Orchard.Users.Controllers {
return Redirect("~/");
}
else {
ModelState.AddModelError("_FORM", T(ErrorCodeToString(/*createStatus*/MembershipCreateStatus.ProviderError)));
ModelState.AddModelError("_FORM", ErrorCodeToString(/*createStatus*/MembershipCreateStatus.ProviderError));
}
}
@@ -134,12 +132,13 @@ namespace Orchard.Users.Controllers {
return RedirectToAction("ChangePasswordSuccess");
}
else {
ModelState.AddModelError("_FORM", T("The current password is incorrect or the new password is invalid."));
ModelState.AddModelError("_FORM",
"The current password is incorrect or the new password is invalid.");
return ChangePassword();
}
}
catch {
ModelState.AddModelError("_FORM", T("The current password is incorrect or the new password is invalid."));
ModelState.AddModelError("_FORM", "The current password is incorrect or the new password is invalid.");
return ChangePassword();
}
}
@@ -158,29 +157,32 @@ namespace Orchard.Users.Controllers {
private bool ValidateChangePassword(string currentPassword, string newPassword, string confirmPassword) {
if (String.IsNullOrEmpty(currentPassword)) {
ModelState.AddModelError("currentPassword", T("You must specify a current password."));
ModelState.AddModelError("currentPassword", "You must specify a current password.");
}
if (newPassword == null || newPassword.Length < MinPasswordLength) {
ModelState.AddModelError("newPassword", T("You must specify a new password of {0} or more characters.", MinPasswordLength));
ModelState.AddModelError("newPassword",
String.Format(CultureInfo.CurrentCulture,
"You must specify a new password of {0} or more characters.",
MinPasswordLength));
}
if (!String.Equals(newPassword, confirmPassword, StringComparison.Ordinal)) {
ModelState.AddModelError("_FORM", T("The new password and confirmation password do not match."));
ModelState.AddModelError("_FORM", "The new password and confirmation password do not match.");
}
return ModelState.IsValid;
}
private IUser ValidateLogOn(string userName, string password) {
if (String.IsNullOrEmpty(userName)) {
ModelState.AddModelError("username", T("You must specify a username."));
private IUser ValidateLogOn(string userNameOrEmail, string password) {
if (String.IsNullOrEmpty(userNameOrEmail)) {
ModelState.AddModelError("userNameOrEmail", "You must specify a username or e-mail.");
}
if (String.IsNullOrEmpty(password)) {
ModelState.AddModelError("password", T("You must specify a password."));
ModelState.AddModelError("password", "You must specify a password.");
}
var user = _membershipService.ValidateUser(userName, password);
var user = _membershipService.ValidateUser(userNameOrEmail, password);
if (user == null) {
ModelState.AddModelError("_FORM", T("The username or password provided is incorrect."));
ModelState.AddModelError("_FORM", "The username or e-mail or password provided is incorrect.");
}
return user;
@@ -188,20 +190,23 @@ namespace Orchard.Users.Controllers {
private bool ValidateRegistration(string userName, string email, string password, string confirmPassword) {
if (String.IsNullOrEmpty(userName)) {
ModelState.AddModelError("username", T("You must specify a username."));
ModelState.AddModelError("username", "You must specify a username.");
}
if (String.IsNullOrEmpty(email)) {
ModelState.AddModelError("email", T("You must specify an email address."));
ModelState.AddModelError("email", "You must specify an email address.");
}
string userUnicityMessage = _userService.VerifyUserUnicity(userName, email);
if (userUnicityMessage != null) {
ModelState.AddModelError("userExists", T(userUnicityMessage));
ModelState.AddModelError("userExists", userUnicityMessage);
}
if (password == null || password.Length < MinPasswordLength) {
ModelState.AddModelError("password", T("You must specify a password of {0} or more characters.", MinPasswordLength));
ModelState.AddModelError("password",
String.Format(CultureInfo.CurrentCulture,
"You must specify a password of {0} or more characters.",
MinPasswordLength));
}
if (!String.Equals(password, confirmPassword, StringComparison.Ordinal)) {
ModelState.AddModelError("_FORM", T("The new password and confirmation password do not match."));
ModelState.AddModelError("_FORM", "The new password and confirmation password do not match.");
}
return ModelState.IsValid;
}

View File

@@ -51,8 +51,10 @@ namespace Orchard.Users.Services {
return _contentManager.Get<IUser>(userRecord.Id);
}
public IUser ValidateUser(string username, string password) {
var userRecord = _userRepository.Get(x => x.NormalizedUserName == username.ToLower());
public IUser ValidateUser(string userNameOrEmail, string password) {
var userRecord = _userRepository.Get(x => x.NormalizedUserName == userNameOrEmail.ToLower());
if(userRecord == null)
userRecord = _userRepository.Get(x => x.Email == userNameOrEmail.ToLower());
if (userRecord == null || ValidatePassword(userRecord, password) == false)
return null;

View File

@@ -8,9 +8,9 @@ using (Html.BeginFormAntiForgeryPost(Url.Action("LogOn", new {ReturnUrl = Reques
<fieldset>
<legend><%=_Encoded("Account Information")%></legend>
<div>
<label for="username"><%=_Encoded("Username:")%></label>
<%= Html.TextBox("username")%>
<%= Html.ValidationMessage("username")%>
<label for="username"><%=_Encoded("Username or Email:")%></label>
<%= Html.TextBox("userNameOrEmail")%>
<%= Html.ValidationMessage("userNameOrEmail")%>
</div>
<div>
<label for="password"><%=_Encoded("Password:")%></label>

View File

@@ -5,7 +5,7 @@
IUser CreateUser(CreateUserParams createUserParams);
IUser GetUser(string username);
IUser ValidateUser(string username, string password);
IUser ValidateUser(string userNameOrEmail, string password);
void SetPassword(IUser user, string password);
}
}