- Fixing 15873: Username is case sensitive.

--HG--
branch : dev
This commit is contained in:
Suha Can
2010-03-02 17:23:45 -08:00
parent 6cc1b9d939
commit afb9f1800e
8 changed files with 82 additions and 42 deletions

View File

@@ -33,6 +33,7 @@ namespace Orchard.Tests.Modules.Users.Controllers {
builder.Register<DefaultContentManager>().As<IContentManager>();
builder.Register<DefaultContentQuery>().As<IContentQuery>().FactoryScoped();
builder.Register<MembershipService>().As<IMembershipService>();
builder.Register<UserService>().As<IUserService>();
builder.Register<UserHandler>().As<IContentHandler>();
builder.Register<OrchardServices>().As<IOrchardServices>();
builder.Register<TransactionManager>().As<ITransactionManager>();

View File

@@ -7,6 +7,7 @@ using System.Web.Security;
using Orchard.Logging;
using Orchard.Mvc.ViewModels;
using Orchard.Security;
using Orchard.Users.Services;
using Orchard.Users.ViewModels;
namespace Orchard.Users.Controllers {
@@ -14,13 +15,16 @@ namespace Orchard.Users.Controllers {
public class AccountController : Controller {
private readonly IAuthenticationService _authenticationService;
private readonly IMembershipService _membershipService;
private readonly IUserService _userService;
public AccountController(
IAuthenticationService authenticationService,
IMembershipService membershipService) {
IMembershipService membershipService,
IUserService userService) {
_authenticationService = authenticationService;
_membershipService = membershipService;
_userService = userService;
Logger = NullLogger.Instance;
}
@@ -189,6 +193,10 @@ namespace Orchard.Users.Controllers {
if (String.IsNullOrEmpty(email)) {
ModelState.AddModelError("email", "You must specify an email address.");
}
string userUnicityMessage = _userService.VerifyUserUnicity(userName, email);
if (userUnicityMessage != null) {
ModelState.AddModelError("userExists", userUnicityMessage);
}
if (password == null || password.Length < MinPasswordLength) {
ModelState.AddModelError("password",
String.Format(CultureInfo.CurrentCulture,

View File

@@ -1,5 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Orchard.Localization;
@@ -7,18 +5,22 @@ using Orchard.ContentManagement;
using Orchard.Security;
using Orchard.UI.Notify;
using Orchard.Users.Models;
using Orchard.Users.Services;
using Orchard.Users.ViewModels;
namespace Orchard.Users.Controllers {
public class AdminController : Controller, IUpdateModel {
private readonly IMembershipService _membershipService;
private readonly IUserService _userService;
public AdminController(
IOrchardServices services,
IMembershipService membershipService) {
IMembershipService membershipService,
IUserService userService) {
Services = services;
_membershipService = membershipService;
_userService = userService;
T = NullLocalizer.Instance;
}
@@ -63,7 +65,7 @@ namespace Orchard.Users.Controllers {
var model = new UserCreateViewModel();
UpdateModel(model);
string userExistsMessage = VerifyUserUnicity(model.UserName, model.Email);
string userExistsMessage = _userService.VerifyUserUnicity(model.UserName, model.Email);
if (userExistsMessage != null) {
AddModelError("NotUniqueUserName", T(userExistsMessage));
}
@@ -109,7 +111,9 @@ namespace Orchard.Users.Controllers {
// apply additional model properties that were posted on form
UpdateModel(model);
string userExistsMessage = VerifyUserUnicity(id, model.UserName, model.Email);
model.User.Item.NormalizedUserName = model.UserName.ToLower();
string userExistsMessage = _userService.VerifyUserUnicity(id, model.UserName, model.Email);
if (userExistsMessage != null) {
AddModelError("NotUniqueUserName", T(userExistsMessage));
}
@@ -133,40 +137,6 @@ namespace Orchard.Users.Controllers {
return RedirectToAction("Index");
}
#region private
private string VerifyUserUnicity(string userName, string email) {
IEnumerable<User> allUsers = Services.ContentManager.Query<User, UserRecord>().List();
foreach (var user in allUsers) {
if (String.Equals(userName, user.UserName, StringComparison.OrdinalIgnoreCase)) {
return "A user with that name already exists";
}
if (String.Equals(email, user.Email, StringComparison.OrdinalIgnoreCase)) {
return "A user with that email already exists";
}
}
return null;
}
private string VerifyUserUnicity(int id, string userName, string email) {
IEnumerable<User> allUsers = Services.ContentManager.Query<User, UserRecord>().List();
foreach (var user in allUsers) {
if (user.Id == id)
continue;
if (String.Equals(userName, user.UserName, StringComparison.OrdinalIgnoreCase)) {
return "A user with that name already exists";
}
if (String.Equals(email, user.Email, StringComparison.OrdinalIgnoreCase)) {
return "A user with that email already exists";
}
}
return null;
}
#endregion
bool IUpdateModel.TryUpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties) {
return TryUpdateModel(model, prefix, includeProperties, excludeProperties);
}

View File

@@ -16,5 +16,10 @@ namespace Orchard.Users.Models {
get { return Record.Email; }
set { Record.Email = value; }
}
public string NormalizedUserName {
get { return Record.NormalizedUserName; }
set { Record.NormalizedUserName = value; }
}
}
}

View File

@@ -5,6 +5,7 @@ namespace Orchard.Users.Models {
public class UserRecord : ContentPartRecord {
public virtual string UserName { get; set; }
public virtual string Email { get; set; }
public virtual string NormalizedUserName { get; set; }
public virtual string Password { get; set; }
public virtual MembershipPasswordFormat PasswordFormat { get; set; }

View File

@@ -71,6 +71,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Services\MembershipService.cs" />
<Compile Include="AdminMenu.cs" />
<Compile Include="Services\UserService.cs" />
<Compile Include="ViewModels\LogOnViewModel.cs" />
<Compile Include="ViewModels\UserCreateViewModel.cs" />
<Compile Include="ViewModels\UserEditViewModel.cs" />

View File

@@ -36,12 +36,13 @@ namespace Orchard.Users.Services {
{
init.Record.UserName = createUserParams.Username;
init.Record.Email = createUserParams.Email;
init.Record.NormalizedUserName = createUserParams.Username.ToLower();
SetPassword(init.Record, createUserParams.Password);
});
}
public IUser GetUser(string username) {
var userRecord = _userRepository.Get(x => x.UserName == username);
var userRecord = _userRepository.Get(x => x.NormalizedUserName == username.ToLower());
if (userRecord == null) {
return null;
}
@@ -49,7 +50,7 @@ namespace Orchard.Users.Services {
}
public IUser ValidateUser(string username, string password) {
var userRecord = _userRepository.Get(x => x.UserName == username);
var userRecord = _userRepository.Get(x => x.NormalizedUserName == username.ToLower());
if (userRecord == null || ValidatePassword(userRecord, password) == false)
return null;

View File

@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using Orchard.Logging;
using Orchard.ContentManagement;
using Orchard.Users.Models;
namespace Orchard.Users.Services {
public class UserService : IUserService {
private readonly IContentManager _contentManager;
public UserService(IContentManager contentManager) {
_contentManager = contentManager;
Logger = NullLogger.Instance;
}
public ILogger Logger { get; set; }
public string VerifyUserUnicity(string userName, string email) {
IEnumerable<User> allUsers = _contentManager.Query<User, UserRecord>().List();
foreach (var user in allUsers) {
if (String.Equals(userName.ToLower(), user.NormalizedUserName, StringComparison.OrdinalIgnoreCase)) {
return "A user with that name already exists";
}
if (String.Equals(email, user.Email, StringComparison.OrdinalIgnoreCase)) {
return "A user with that email already exists";
}
}
return null;
}
public string VerifyUserUnicity(int id, string userName, string email) {
IEnumerable<User> allUsers = _contentManager.Query<User, UserRecord>().List();
foreach (var user in allUsers) {
if (user.Id == id)
continue;
if (String.Equals(userName.ToLower(), user.NormalizedUserName, StringComparison.OrdinalIgnoreCase)) {
return "A user with that name already exists";
}
if (String.Equals(email, user.Email, StringComparison.OrdinalIgnoreCase)) {
return "A user with that email already exists";
}
}
return null;
}
}
public interface IUserService : IDependency {
string VerifyUserUnicity(string userName, string email);
string VerifyUserUnicity(int id, string userName, string email);
}
}