mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
#16849: Replacing strings and console.writelines by exception throwing.
--HG-- branch : dev
This commit is contained in:
@@ -30,19 +30,20 @@ namespace Orchard.Users.Commands {
|
||||
[CommandHelp("user create /UserName:<username> /Password:<password> /Email:<email>\r\n\t" + "Creates a new User")]
|
||||
[OrchardSwitches("UserName,Password,Email")]
|
||||
public string Create() {
|
||||
string userUnicityMessage = _userService.VerifyUserUnicity(UserName, Email);
|
||||
if (userUnicityMessage != null) {
|
||||
return userUnicityMessage;
|
||||
if (!_userService.VerifyUserUnicity(UserName, Email)) {
|
||||
throw new OrchardException(T("User with that username and/or email already exists."));
|
||||
}
|
||||
|
||||
if (Password == null || Password.Length < MinPasswordLength) {
|
||||
return T("You must specify a password of {0} or more characters.", MinPasswordLength).ToString();
|
||||
throw new OrchardException(T("You must specify a password of {0} or more characters.", MinPasswordLength));
|
||||
}
|
||||
|
||||
var user = _membershipService.CreateUser(new CreateUserParams(UserName, Password, Email, null, null, true));
|
||||
if (user != null)
|
||||
return T("User created successfully").ToString();
|
||||
|
||||
return T("The authentication provider returned an error").ToString();
|
||||
if (user == null) {
|
||||
throw new OrchardException(T("The authentication provider returned an error"));
|
||||
}
|
||||
|
||||
return T("User created successfully").ToString();
|
||||
}
|
||||
|
||||
int MinPasswordLength {
|
||||
|
@@ -333,9 +333,8 @@ namespace Orchard.Users.Controllers {
|
||||
if (!validate)
|
||||
return false;
|
||||
|
||||
string userUnicityMessage = _userService.VerifyUserUnicity(userName, email);
|
||||
if (userUnicityMessage != null) {
|
||||
ModelState.AddModelError("userExists", T(userUnicityMessage));
|
||||
if (!_userService.VerifyUserUnicity(userName, email)) {
|
||||
ModelState.AddModelError("userExists", T("User with that username and/or email already exists."));
|
||||
}
|
||||
if (password == null || password.Length < MinPasswordLength) {
|
||||
ModelState.AddModelError("password", T("You must specify a password of {0} or more characters.", MinPasswordLength));
|
||||
|
@@ -77,9 +77,8 @@ namespace Orchard.Users.Controllers {
|
||||
return new HttpUnauthorizedResult();
|
||||
|
||||
if (!string.IsNullOrEmpty(createModel.UserName)) {
|
||||
string userExistsMessage = _userService.VerifyUserUnicity(createModel.UserName, createModel.Email);
|
||||
if (userExistsMessage != null) {
|
||||
AddModelError("NotUniqueUserName", T(userExistsMessage));
|
||||
if (!_userService.VerifyUserUnicity(createModel.UserName, createModel.Email)) {
|
||||
AddModelError("NotUniqueUserName", T("User with that username and/or email already exists."));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -139,9 +138,8 @@ namespace Orchard.Users.Controllers {
|
||||
|
||||
var editModel = new UserEditViewModel {User = user};
|
||||
if (TryUpdateModel(editModel)) {
|
||||
string userExistsMessage = _userService.VerifyUserUnicity(id, editModel.UserName, editModel.Email);
|
||||
if (userExistsMessage != null) {
|
||||
AddModelError("NotUniqueUserName", T(userExistsMessage));
|
||||
if (!_userService.VerifyUserUnicity(id, editModel.UserName, editModel.Email)) {
|
||||
AddModelError("NotUniqueUserName", T("User with that username and/or email already exists."));
|
||||
}
|
||||
else {
|
||||
// also update the Super user if this is the renamed account
|
||||
|
@@ -2,8 +2,8 @@ using Orchard.Security;
|
||||
using System;
|
||||
namespace Orchard.Users.Services {
|
||||
public interface IUserService : IDependency {
|
||||
string VerifyUserUnicity(string userName, string email);
|
||||
string VerifyUserUnicity(int id, string userName, string email);
|
||||
bool VerifyUserUnicity(string userName, string email);
|
||||
bool VerifyUserUnicity(int id, string userName, string email);
|
||||
|
||||
void SendChallengeEmail(IUser user, Func<string, string> createUrl);
|
||||
IUser ValidateChallenge(string challengeToken);
|
||||
|
@@ -36,7 +36,7 @@ namespace Orchard.Users.Services {
|
||||
|
||||
public ILogger Logger { get; set; }
|
||||
|
||||
public string VerifyUserUnicity(string userName, string email) {
|
||||
public bool VerifyUserUnicity(string userName, string email) {
|
||||
string normalizedUserName = userName.ToLower();
|
||||
|
||||
if (_contentManager.Query<UserPart, UserPartRecord>()
|
||||
@@ -44,13 +44,13 @@ namespace Orchard.Users.Services {
|
||||
user.NormalizedUserName == normalizedUserName ||
|
||||
user.Email == email)
|
||||
.List().Any()) {
|
||||
return "User with that username and/or email already exists.";
|
||||
return false;
|
||||
}
|
||||
|
||||
return null;
|
||||
return true;
|
||||
}
|
||||
|
||||
public string VerifyUserUnicity(int id, string userName, string email) {
|
||||
public bool VerifyUserUnicity(int id, string userName, string email) {
|
||||
string normalizedUserName = userName.ToLower();
|
||||
|
||||
if (_contentManager.Query<UserPart, UserPartRecord>()
|
||||
@@ -58,10 +58,10 @@ namespace Orchard.Users.Services {
|
||||
user.NormalizedUserName == normalizedUserName ||
|
||||
user.Email == email)
|
||||
.List().Any(user => user.Id != id)) {
|
||||
return "User with that username and/or email already exists.";
|
||||
return false;
|
||||
}
|
||||
|
||||
return null;
|
||||
return true;
|
||||
}
|
||||
|
||||
public string CreateNonce(IUser user, TimeSpan delay) {
|
||||
|
Reference in New Issue
Block a user