From 0fa617e7f09965e23b1becb1aa877e518083f9e6 Mon Sep 17 00:00:00 2001 From: Andrew Ma Date: Thu, 15 Apr 2010 21:53:32 -0700 Subject: [PATCH] Updating the User module use the Localizer class to localize strings. Adding an extension method for ModelStateDictionary.AddModelError to accept LocalizedString objects. --- .../Controllers/AccountController.cs | 38 +++++++++---------- .../Controllers/AdminController.cs | 12 +++--- .../ModelStateDictionaryExtensions.cs | 11 ++++++ src/Orchard/Orchard.Framework.csproj | 1 + 4 files changed, 34 insertions(+), 28 deletions(-) create mode 100644 src/Orchard/Mvc/Extensions/ModelStateDictionaryExtensions.cs diff --git a/src/Orchard.Web/Modules/Orchard.Users/Controllers/AccountController.cs b/src/Orchard.Web/Modules/Orchard.Users/Controllers/AccountController.cs index 5975abe20..6de70a07f 100644 --- a/src/Orchard.Web/Modules/Orchard.Users/Controllers/AccountController.cs +++ b/src/Orchard.Web/Modules/Orchard.Users/Controllers/AccountController.cs @@ -4,6 +4,7 @@ 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,9 +27,11 @@ 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"]; @@ -98,7 +101,7 @@ namespace Orchard.Users.Controllers { return Redirect("~/"); } else { - ModelState.AddModelError("_FORM", ErrorCodeToString(/*createStatus*/MembershipCreateStatus.ProviderError)); + ModelState.AddModelError("_FORM", T(ErrorCodeToString(/*createStatus*/MembershipCreateStatus.ProviderError))); } } @@ -132,13 +135,12 @@ namespace Orchard.Users.Controllers { return RedirectToAction("ChangePasswordSuccess"); } else { - ModelState.AddModelError("_FORM", - "The current password is incorrect or the new password is invalid."); + ModelState.AddModelError("_FORM", T("The current password is incorrect or the new password is invalid.")); return ChangePassword(); } } catch { - ModelState.AddModelError("_FORM", "The current password is incorrect or the new password is invalid."); + ModelState.AddModelError("_FORM", T("The current password is incorrect or the new password is invalid.")); return ChangePassword(); } } @@ -157,17 +159,14 @@ namespace Orchard.Users.Controllers { private bool ValidateChangePassword(string currentPassword, string newPassword, string confirmPassword) { if (String.IsNullOrEmpty(currentPassword)) { - ModelState.AddModelError("currentPassword", "You must specify a current password."); + ModelState.AddModelError("currentPassword", T("You must specify a current password.")); } if (newPassword == null || newPassword.Length < MinPasswordLength) { - ModelState.AddModelError("newPassword", - String.Format(CultureInfo.CurrentCulture, - "You must specify a new password of {0} or more characters.", - MinPasswordLength)); + ModelState.AddModelError("newPassword", T("You must specify a new password of {0} or more characters.", MinPasswordLength)); } if (!String.Equals(newPassword, confirmPassword, StringComparison.Ordinal)) { - ModelState.AddModelError("_FORM", "The new password and confirmation password do not match."); + ModelState.AddModelError("_FORM", T("The new password and confirmation password do not match.")); } return ModelState.IsValid; @@ -175,14 +174,14 @@ namespace Orchard.Users.Controllers { private IUser ValidateLogOn(string userName, string password) { if (String.IsNullOrEmpty(userName)) { - ModelState.AddModelError("username", "You must specify a username."); + ModelState.AddModelError("username", T("You must specify a username.")); } if (String.IsNullOrEmpty(password)) { - ModelState.AddModelError("password", "You must specify a password."); + ModelState.AddModelError("password", T("You must specify a password.")); } var user = _membershipService.ValidateUser(userName, password); if (user == null) { - ModelState.AddModelError("_FORM", "The username or password provided is incorrect."); + ModelState.AddModelError("_FORM", T("The username or password provided is incorrect.")); } return user; @@ -190,23 +189,20 @@ namespace Orchard.Users.Controllers { private bool ValidateRegistration(string userName, string email, string password, string confirmPassword) { if (String.IsNullOrEmpty(userName)) { - ModelState.AddModelError("username", "You must specify a username."); + ModelState.AddModelError("username", T("You must specify a username.")); } if (String.IsNullOrEmpty(email)) { - ModelState.AddModelError("email", "You must specify an email address."); + ModelState.AddModelError("email", T("You must specify an email address.")); } string userUnicityMessage = _userService.VerifyUserUnicity(userName, email); if (userUnicityMessage != null) { - ModelState.AddModelError("userExists", userUnicityMessage); + ModelState.AddModelError("userExists", T(userUnicityMessage)); } if (password == null || password.Length < MinPasswordLength) { - ModelState.AddModelError("password", - String.Format(CultureInfo.CurrentCulture, - "You must specify a password of {0} or more characters.", - MinPasswordLength)); + ModelState.AddModelError("password", T("You must specify a password of {0} or more characters.", MinPasswordLength)); } if (!String.Equals(password, confirmPassword, StringComparison.Ordinal)) { - ModelState.AddModelError("_FORM", "The new password and confirmation password do not match."); + ModelState.AddModelError("_FORM", T("The new password and confirmation password do not match.")); } return ModelState.IsValid; } diff --git a/src/Orchard.Web/Modules/Orchard.Users/Controllers/AdminController.cs b/src/Orchard.Web/Modules/Orchard.Users/Controllers/AdminController.cs index d6f95b0ca..3502f0449 100644 --- a/src/Orchard.Web/Modules/Orchard.Users/Controllers/AdminController.cs +++ b/src/Orchard.Web/Modules/Orchard.Users/Controllers/AdminController.cs @@ -1,7 +1,8 @@ using System.Linq; using System.Web.Mvc; -using Orchard.Localization; using Orchard.ContentManagement; +using Orchard.Localization; +using Orchard.Mvc.Extensions; using Orchard.Security; using Orchard.UI.Notify; using Orchard.Users.Drivers; @@ -70,11 +71,11 @@ namespace Orchard.Users.Controllers { string userExistsMessage = _userService.VerifyUserUnicity(model.UserName, model.Email); if (userExistsMessage != null) { - AddModelError("NotUniqueUserName", T(userExistsMessage)); + ModelState.AddModelError("NotUniqueUserName", T(userExistsMessage)); } if (model.Password != model.ConfirmPassword) { - AddModelError("ConfirmPassword", T("Password confirmation must match")); + ModelState.AddModelError("ConfirmPassword", T("Password confirmation must match")); } user = _membershipService.CreateUser(new CreateUserParams( @@ -122,7 +123,7 @@ namespace Orchard.Users.Controllers { string userExistsMessage = _userService.VerifyUserUnicity(id, model.UserName, model.Email); if (userExistsMessage != null) { - AddModelError("NotUniqueUserName", T(userExistsMessage)); + ModelState.AddModelError("NotUniqueUserName", T(userExistsMessage)); } if (!ModelState.IsValid) { @@ -147,9 +148,6 @@ namespace Orchard.Users.Controllers { bool IUpdateModel.TryUpdateModel(TModel model, string prefix, string[] includeProperties, string[] excludeProperties) { return TryUpdateModel(model, prefix, includeProperties, excludeProperties); } - public void AddModelError(string key, LocalizedString errorMessage) { - ModelState.AddModelError(key, errorMessage.ToString()); - } } } diff --git a/src/Orchard/Mvc/Extensions/ModelStateDictionaryExtensions.cs b/src/Orchard/Mvc/Extensions/ModelStateDictionaryExtensions.cs new file mode 100644 index 000000000..e68990229 --- /dev/null +++ b/src/Orchard/Mvc/Extensions/ModelStateDictionaryExtensions.cs @@ -0,0 +1,11 @@ +using System; +using System.Web.Mvc; +using Orchard.Localization; + +namespace Orchard.Mvc.Extensions { + public static class ModelStateDictionaryExtensions { + public static void AddModelError(this ModelStateDictionary modelStateDictionary, string key, LocalizedString errorMessage) { + modelStateDictionary.AddModelError(key, errorMessage.ToString()); + } + } +} diff --git a/src/Orchard/Orchard.Framework.csproj b/src/Orchard/Orchard.Framework.csproj index ef1ac119c..742c0a02d 100644 --- a/src/Orchard/Orchard.Framework.csproj +++ b/src/Orchard/Orchard.Framework.csproj @@ -178,6 +178,7 @@ +