mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-09-23 04:43:35 +08:00
- 14887: User name and email uniqueness should be enforced.
--HG-- branch : dev
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Web.Mvc;
|
using System.Web.Mvc;
|
||||||
using Orchard.Localization;
|
using Orchard.Localization;
|
||||||
@@ -69,6 +71,11 @@ namespace Orchard.Users.Controllers {
|
|||||||
|
|
||||||
model.User = Services.ContentManager.UpdateEditorModel(user, this);
|
model.User = Services.ContentManager.UpdateEditorModel(user, this);
|
||||||
|
|
||||||
|
string userExistsMessage = VerifyUserUnicity(model.UserName, model.Email);
|
||||||
|
if (userExistsMessage != null) {
|
||||||
|
AddModelError("NotUniqueUserName", T(userExistsMessage));
|
||||||
|
}
|
||||||
|
|
||||||
if (model.Password != model.ConfirmPassword) {
|
if (model.Password != model.ConfirmPassword) {
|
||||||
AddModelError("ConfirmPassword", T("Password confirmation must match"));
|
AddModelError("ConfirmPassword", T("Password confirmation must match"));
|
||||||
}
|
}
|
||||||
@@ -102,6 +109,11 @@ namespace Orchard.Users.Controllers {
|
|||||||
// apply additional model properties that were posted on form
|
// apply additional model properties that were posted on form
|
||||||
UpdateModel(model);
|
UpdateModel(model);
|
||||||
|
|
||||||
|
string userExistsMessage = VerifyUserUnicity(id, model.UserName, model.Email);
|
||||||
|
if (userExistsMessage != null) {
|
||||||
|
AddModelError("NotUniqueUserName", T(userExistsMessage));
|
||||||
|
}
|
||||||
|
|
||||||
if (!ModelState.IsValid) {
|
if (!ModelState.IsValid) {
|
||||||
Services.TransactionManager.Cancel();
|
Services.TransactionManager.Cancel();
|
||||||
return View(model);
|
return View(model);
|
||||||
@@ -121,6 +133,40 @@ namespace Orchard.Users.Controllers {
|
|||||||
return RedirectToAction("Index");
|
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) {
|
bool IUpdateModel.TryUpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties) {
|
||||||
return TryUpdateModel(model, prefix, includeProperties, excludeProperties);
|
return TryUpdateModel(model, prefix, includeProperties, excludeProperties);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user