Updating the User module use the Localizer class to localize strings.

Adding an extension method for ModelStateDictionary.AddModelError to accept LocalizedString objects.
This commit is contained in:
Andrew Ma
2010-04-15 21:53:32 -07:00
parent aaf1ecc762
commit 0fa617e7f0
4 changed files with 34 additions and 28 deletions

View File

@@ -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;
}

View File

@@ -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>(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());
}
}
}