From 13783e748ab017b9e9df5befdabd3e60ded5f556 Mon Sep 17 00:00:00 2001 From: Suha Can Date: Fri, 16 Apr 2010 13:14:45 -0700 Subject: [PATCH] - Patch for issue #16396 by Andrew Ma --- .../Controllers/AccountController.cs | 49 ++++++++++--------- .../Services/MembershipService.cs | 6 ++- .../Orchard.Users/Views/Account/LogOn.ascx | 6 +-- src/Orchard/Security/IMembershipService.cs | 2 +- 4 files changed, 35 insertions(+), 28 deletions(-) diff --git a/src/Orchard.Web/Modules/Orchard.Users/Controllers/AccountController.cs b/src/Orchard.Web/Modules/Orchard.Users/Controllers/AccountController.cs index 685f496a9..7eb3b4767 100644 --- a/src/Orchard.Web/Modules/Orchard.Users/Controllers/AccountController.cs +++ b/src/Orchard.Web/Modules/Orchard.Users/Controllers/AccountController.cs @@ -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; } diff --git a/src/Orchard.Web/Modules/Orchard.Users/Services/MembershipService.cs b/src/Orchard.Web/Modules/Orchard.Users/Services/MembershipService.cs index a7fb519fb..94eb85f91 100644 --- a/src/Orchard.Web/Modules/Orchard.Users/Services/MembershipService.cs +++ b/src/Orchard.Web/Modules/Orchard.Users/Services/MembershipService.cs @@ -51,8 +51,10 @@ namespace Orchard.Users.Services { return _contentManager.Get(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; diff --git a/src/Orchard.Web/Modules/Orchard.Users/Views/Account/LogOn.ascx b/src/Orchard.Web/Modules/Orchard.Users/Views/Account/LogOn.ascx index d910ccb98..de5f098cd 100644 --- a/src/Orchard.Web/Modules/Orchard.Users/Views/Account/LogOn.ascx +++ b/src/Orchard.Web/Modules/Orchard.Users/Views/Account/LogOn.ascx @@ -8,9 +8,9 @@ using (Html.BeginFormAntiForgeryPost(Url.Action("LogOn", new {ReturnUrl = Reques
<%=_Encoded("Account Information")%>
- - <%= Html.TextBox("username")%> - <%= Html.ValidationMessage("username")%> + + <%= Html.TextBox("userNameOrEmail")%> + <%= Html.ValidationMessage("userNameOrEmail")%>
diff --git a/src/Orchard/Security/IMembershipService.cs b/src/Orchard/Security/IMembershipService.cs index 44578cac1..0821fcdb1 100644 --- a/src/Orchard/Security/IMembershipService.cs +++ b/src/Orchard/Security/IMembershipService.cs @@ -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); } }