Skip to content

Instantly share code, notes, and snippets.

@bleroy
Created April 1, 2015 22:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bleroy/8c9fc54c87ff17585fa5 to your computer and use it in GitHub Desktop.
Save bleroy/8c9fc54c87ff17585fa5 to your computer and use it in GitHub Desktop.
CodePlex Issue #16399 Plain Text Attachments
# HG changeset patch
# User Andrew Ma
# Date 1271393612 25200
# Node ID 13780762c2fa9d7b86fe5b6550a9f88e5134d95f
# Parent 7331559a4c8fa3b4847c7aff1e7ad92952382627
Updating the User module use the Localizer class to localize strings.
Adding an extension method for ModelStateDictionary.AddModelError to accept LocalizedString objects.
diff -r 7331559a4c8f -r 13780762c2fa src/Orchard.Web/Modules/Orchard.Users/Controllers/AccountController.cs
--- a/src/Orchard.Web/Modules/Orchard.Users/Controllers/AccountController.cs Tue Apr 13 11:32:26 2010 -0700
+++ b/src/Orchard.Web/Modules/Orchard.Users/Controllers/AccountController.cs Thu Apr 15 21:53:32 2010 -0700
@@ -4,6 +4,7 @@
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 @@
_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 @@
return Redirect("~/");
}
else {
- ModelState.AddModelError("_FORM", ErrorCodeToString(/*createStatus*/MembershipCreateStatus.ProviderError));
+ ModelState.AddModelError("_FORM", T(ErrorCodeToString(/*createStatus*/MembershipCreateStatus.ProviderError)));
}
}
@@ -132,13 +135,12 @@
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 @@
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 @@
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 @@
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 -r 7331559a4c8f -r 13780762c2fa src/Orchard.Web/Modules/Orchard.Users/Controllers/AdminController.cs
--- a/src/Orchard.Web/Modules/Orchard.Users/Controllers/AdminController.cs Tue Apr 13 11:32:26 2010 -0700
+++ b/src/Orchard.Web/Modules/Orchard.Users/Controllers/AdminController.cs Thu Apr 15 21:53:32 2010 -0700
@@ -1,7 +1,8 @@
using System.Linq;
using System.Web.Mvc;
+using Orchard.ContentManagement;
using Orchard.Localization;
-using Orchard.ContentManagement;
+using Orchard.Mvc.Extensions;
using Orchard.Security;
using Orchard.UI.Notify;
using Orchard.Users.Drivers;
@@ -70,11 +71,11 @@
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 @@
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 @@
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());
- }
}
}
diff -r 7331559a4c8f -r 13780762c2fa src/Orchard/Mvc/Extensions/ModelStateDictionaryExtensions.cs
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/Orchard/Mvc/Extensions/ModelStateDictionaryExtensions.cs Thu Apr 15 21:53:32 2010 -0700
@@ -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 -r 7331559a4c8f -r 13780762c2fa src/Orchard/Orchard.Framework.csproj
--- a/src/Orchard/Orchard.Framework.csproj Tue Apr 13 11:32:26 2010 -0700
+++ b/src/Orchard/Orchard.Framework.csproj Thu Apr 15 21:53:32 2010 -0700
@@ -178,6 +178,7 @@
<Compile Include="Extensions\ShellTopology.cs" />
<Compile Include="Mvc\AntiForgery\ValidateAntiForgeryTokenOrchardAttribute.cs" />
<Compile Include="Mvc\Extensions\ControllerExtensions.cs" />
+ <Compile Include="Mvc\Extensions\ModelStateDictionaryExtensions.cs" />
<Compile Include="Mvc\Html\FileRegistrationContextExtensions.cs" />
<Compile Include="Mvc\Extensions\UrlHelperExtensions.cs" />
<Compile Include="Mvc\ViewModels\AdaptedViewModel.cs" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment