2009-11-07 22:49:58 +00:00
using System ;
2011-01-17 16:14:05 -07:00
using System.Text.RegularExpressions ;
2009-11-07 22:49:58 +00:00
using System.Diagnostics.CodeAnalysis ;
2010-04-16 13:33:45 -07:00
using Orchard.Localization ;
2009-11-07 22:49:58 +00:00
using System.Web.Mvc ;
using System.Web.Security ;
2010-01-06 04:49:55 +00:00
using Orchard.Logging ;
2011-01-06 15:29:28 -08:00
using Orchard.Mvc ;
2010-04-05 22:36:30 -07:00
using Orchard.Mvc.Extensions ;
2009-11-12 03:46:14 +00:00
using Orchard.Security ;
2010-09-30 16:25:25 -07:00
using Orchard.Themes ;
2010-03-02 17:23:45 -08:00
using Orchard.Users.Services ;
2010-08-31 12:57:15 -07:00
using Orchard.ContentManagement ;
using Orchard.Users.Models ;
2010-11-26 17:02:22 -08:00
using Orchard.UI.Notify ;
2011-03-19 10:02:56 -07:00
using Orchard.Users.Events ;
2012-01-13 16:38:39 -08:00
using Orchard.Utility.Extensions ;
2009-11-07 22:49:58 +00:00
2010-01-06 04:31:38 +00:00
namespace Orchard.Users.Controllers {
2010-09-30 16:37:32 -07:00
[HandleError, Themed]
2009-11-07 22:49:58 +00:00
public class AccountController : Controller {
2009-11-12 03:46:14 +00:00
private readonly IAuthenticationService _authenticationService ;
private readonly IMembershipService _membershipService ;
2010-03-02 17:23:45 -08:00
private readonly IUserService _userService ;
2010-11-04 20:24:48 -07:00
private readonly IOrchardServices _orchardServices ;
2013-05-26 12:15:50 +02:00
private readonly IUserEventHandler _userEventHandler ;
2009-11-07 22:49:58 +00:00
2010-01-06 04:49:55 +00:00
public AccountController (
IAuthenticationService authenticationService ,
2010-03-02 17:23:45 -08:00
IMembershipService membershipService ,
2010-11-04 20:24:48 -07:00
IUserService userService ,
2011-03-19 10:02:56 -07:00
IOrchardServices orchardServices ,
2013-05-26 12:15:50 +02:00
IUserEventHandler userEventHandler ) {
2009-11-12 03:46:14 +00:00
_authenticationService = authenticationService ;
_membershipService = membershipService ;
2010-03-02 17:23:45 -08:00
_userService = userService ;
2010-11-04 20:24:48 -07:00
_orchardServices = orchardServices ;
2013-05-26 12:15:50 +02:00
_userEventHandler = userEventHandler ;
2010-01-06 04:49:55 +00:00
Logger = NullLogger . Instance ;
2010-04-16 13:33:45 -07:00
T = NullLocalizer . Instance ;
2009-11-12 03:46:14 +00:00
}
2009-11-07 22:49:58 +00:00
2010-01-06 04:49:55 +00:00
public ILogger Logger { get ; set ; }
2010-04-16 13:33:45 -07:00
public Localizer T { get ; set ; }
2010-01-06 04:49:55 +00:00
2012-02-24 17:51:25 -08:00
[AlwaysAccessible]
2010-03-09 00:41:17 -08:00
public ActionResult AccessDenied ( ) {
var returnUrl = Request . QueryString [ "ReturnUrl" ] ;
2010-01-06 04:49:55 +00:00
var currentUser = _authenticationService . GetAuthenticatedUser ( ) ;
if ( currentUser = = null ) {
Logger . Information ( "Access denied to anonymous request on {0}" , returnUrl ) ;
2011-01-06 15:29:28 -08:00
var shape = _orchardServices . New . LogOn ( ) . Title ( T ( "Access Denied" ) . Text ) ;
2011-01-10 15:49:56 -08:00
return new ShapeResult ( this , shape ) ;
2010-01-06 04:49:55 +00:00
}
2010-01-06 04:31:38 +00:00
2010-03-09 00:41:17 -08:00
//TODO: (erikpo) Add a setting for whether or not to log access denieds since these can fill up a database pretty fast from bots on a high traffic site
2011-03-19 10:02:56 -07:00
//Suggestion: Could instead use the new AccessDenined IUserEventHandler method and let modules decide if they want to log this event?
2010-01-06 04:49:55 +00:00
Logger . Information ( "Access denied to user #{0} '{1}' on {2}" , currentUser . Id , currentUser . UserName , returnUrl ) ;
2010-03-09 00:41:17 -08:00
2013-05-26 12:15:50 +02:00
_userEventHandler . AccessDenied ( currentUser ) ;
2011-03-19 10:02:56 -07:00
2010-09-01 15:51:41 -07:00
return View ( ) ;
2009-11-07 22:49:58 +00:00
}
2012-02-24 17:51:25 -08:00
[AlwaysAccessible]
2014-10-17 13:43:40 +02:00
public ActionResult LogOn ( string returnUrl ) {
2010-03-09 00:41:17 -08:00
if ( _authenticationService . GetAuthenticatedUser ( ) ! = null )
2014-10-17 13:43:40 +02:00
return this . RedirectLocal ( returnUrl ) ;
2010-03-09 00:41:17 -08:00
2011-01-06 15:29:28 -08:00
var shape = _orchardServices . New . LogOn ( ) . Title ( T ( "Log On" ) . Text ) ;
2011-01-10 15:49:56 -08:00
return new ShapeResult ( this , shape ) ;
2010-01-06 04:31:38 +00:00
}
2010-04-05 22:36:30 -07:00
[HttpPost]
2012-02-24 17:51:25 -08:00
[AlwaysAccessible]
2013-10-23 15:49:32 -07:00
[ValidateInput(false)]
2009-11-07 22:49:58 +00:00
[ SuppressMessage ( "Microsoft.Design" , "CA1054:UriParametersShouldNotBeStrings" ,
Justification = "Needs to take same parameter type as Controller.Redirect()" ) ]
2012-07-05 13:39:17 -07:00
public ActionResult LogOn ( string userNameOrEmail , string password , string returnUrl , bool rememberMe = false ) {
2014-05-17 09:46:12 +01:00
_userEventHandler . LoggingIn ( userNameOrEmail , password ) ;
2010-04-16 13:14:45 -07:00
var user = ValidateLogOn ( userNameOrEmail , password ) ;
2009-11-12 03:46:14 +00:00
if ( ! ModelState . IsValid ) {
2011-01-06 15:29:28 -08:00
var shape = _orchardServices . New . LogOn ( ) . Title ( T ( "Log On" ) . Text ) ;
2011-01-10 15:49:56 -08:00
return new ShapeResult ( this , shape ) ;
2009-11-07 22:49:58 +00:00
}
2012-07-03 09:27:11 -07:00
_authenticationService . SignIn ( user , rememberMe ) ;
2013-05-26 12:15:50 +02:00
_userEventHandler . LoggedIn ( user ) ;
2009-11-12 03:46:14 +00:00
2010-12-10 14:20:08 -08:00
return this . RedirectLocal ( returnUrl ) ;
2009-11-07 22:49:58 +00:00
}
2010-05-14 22:30:16 -07:00
public ActionResult LogOff ( string returnUrl ) {
2011-03-21 15:37:20 -07:00
var wasLoggedInUser = _authenticationService . GetAuthenticatedUser ( ) ;
2009-11-12 03:46:14 +00:00
_authenticationService . SignOut ( ) ;
2011-03-19 10:02:56 -07:00
if ( wasLoggedInUser ! = null )
2013-05-26 12:15:50 +02:00
_userEventHandler . LoggedOut ( wasLoggedInUser ) ;
2010-12-10 14:20:08 -08:00
return this . RedirectLocal ( returnUrl ) ;
2009-11-07 22:49:58 +00:00
}
2009-11-12 03:46:14 +00:00
int MinPasswordLength {
get {
2009-11-14 06:24:54 +00:00
return _membershipService . GetSettings ( ) . MinRequiredPasswordLength ;
2009-11-12 03:46:14 +00:00
}
}
2012-02-24 17:51:25 -08:00
[AlwaysAccessible]
2009-11-07 22:49:58 +00:00
public ActionResult Register ( ) {
2010-08-31 12:57:15 -07:00
// ensure users can register
2010-11-04 20:24:48 -07:00
var registrationSettings = _orchardServices . WorkContext . CurrentSite . As < RegistrationSettingsPart > ( ) ;
2010-08-31 12:57:15 -07:00
if ( ! registrationSettings . UsersCanRegister ) {
2010-10-14 11:30:58 -07:00
return HttpNotFound ( ) ;
2010-08-31 12:57:15 -07:00
}
2009-11-12 03:46:14 +00:00
ViewData [ "PasswordLength" ] = MinPasswordLength ;
2009-11-07 22:49:58 +00:00
2011-01-31 17:54:12 -08:00
var shape = _orchardServices . New . Register ( ) ;
return new ShapeResult ( this , shape ) ;
2009-11-07 22:49:58 +00:00
}
2010-01-21 21:13:35 +00:00
[HttpPost]
2012-02-24 17:51:25 -08:00
[AlwaysAccessible]
2013-10-23 15:49:32 -07:00
[ValidateInput(false)]
2013-11-19 10:06:34 +01:00
public ActionResult Register ( string userName , string email , string password , string confirmPassword , string returnUrl = null ) {
2010-08-31 12:57:15 -07:00
// ensure users can register
2010-11-04 20:24:48 -07:00
var registrationSettings = _orchardServices . WorkContext . CurrentSite . As < RegistrationSettingsPart > ( ) ;
2010-08-31 12:57:15 -07:00
if ( ! registrationSettings . UsersCanRegister ) {
2010-10-14 11:30:58 -07:00
return HttpNotFound ( ) ;
2010-08-31 12:57:15 -07:00
}
2009-11-12 03:46:14 +00:00
ViewData [ "PasswordLength" ] = MinPasswordLength ;
2009-11-07 22:49:58 +00:00
if ( ValidateRegistration ( userName , email , password , confirmPassword ) ) {
// Attempt to register the user
2011-03-19 10:02:56 -07:00
// No need to report this to IUserEventHandler because _membershipService does that for us
2010-09-01 14:39:28 -07:00
var user = _membershipService . CreateUser ( new CreateUserParams ( userName , password , email , null , null , false ) ) ;
2009-11-07 22:49:58 +00:00
2009-11-12 03:46:14 +00:00
if ( user ! = null ) {
2010-09-01 14:39:28 -07:00
if ( user . As < UserPart > ( ) . EmailStatus = = UserStatus . Pending ) {
2013-10-28 17:43:32 -07:00
var siteUrl = _orchardServices . WorkContext . CurrentSite . BaseUrl ;
2012-01-13 16:38:39 -08:00
if ( String . IsNullOrWhiteSpace ( siteUrl ) ) {
siteUrl = HttpContext . Request . ToRootUrlString ( ) ;
}
2012-02-28 15:54:10 -08:00
_userService . SendChallengeEmail ( user . As < UserPart > ( ) , nonce = > Url . MakeAbsolute ( Url . Action ( "ChallengeEmail" , "Account" , new { Area = "Orchard.Users" , nonce = nonce } ) , siteUrl ) ) ;
2010-09-01 14:39:28 -07:00
2013-05-26 12:15:50 +02:00
_userEventHandler . SentChallengeEmail ( user ) ;
2010-09-01 14:39:28 -07:00
return RedirectToAction ( "ChallengeEmailSent" ) ;
}
2010-11-05 15:18:26 -07:00
if ( user . As < UserPart > ( ) . RegistrationStatus = = UserStatus . Pending ) {
return RedirectToAction ( "RegistrationPending" ) ;
}
2009-11-12 03:46:14 +00:00
_authenticationService . SignIn ( user , false /* createPersistentCookie */ ) ;
2013-11-19 10:06:34 +01:00
return this . RedirectLocal ( returnUrl ) ;
2009-11-07 22:49:58 +00:00
}
2010-12-07 17:11:55 -08:00
ModelState . AddModelError ( "_FORM" , T ( ErrorCodeToString ( /*createStatus*/ MembershipCreateStatus . ProviderError ) ) ) ;
2009-11-07 22:49:58 +00:00
}
// If we got this far, something failed, redisplay form
2011-01-31 17:54:12 -08:00
var shape = _orchardServices . New . Register ( ) ;
return new ShapeResult ( this , shape ) ;
2009-11-07 22:49:58 +00:00
}
2012-02-24 17:51:25 -08:00
[AlwaysAccessible]
2010-11-30 17:19:13 -08:00
public ActionResult RequestLostPassword ( ) {
// ensure users can request lost password
var registrationSettings = _orchardServices . WorkContext . CurrentSite . As < RegistrationSettingsPart > ( ) ;
if ( ! registrationSettings . EnableLostPassword ) {
return HttpNotFound ( ) ;
}
2010-11-26 17:02:22 -08:00
return View ( ) ;
}
[HttpPost]
2012-02-24 17:51:25 -08:00
[AlwaysAccessible]
2010-11-30 17:19:13 -08:00
public ActionResult RequestLostPassword ( string username ) {
// ensure users can request lost password
var registrationSettings = _orchardServices . WorkContext . CurrentSite . As < RegistrationSettingsPart > ( ) ;
if ( ! registrationSettings . EnableLostPassword ) {
return HttpNotFound ( ) ;
}
2010-11-26 17:02:22 -08:00
if ( String . IsNullOrWhiteSpace ( username ) ) {
2013-06-07 19:44:09 +01:00
ModelState . AddModelError ( "userNameOrEmail" , T ( "Invalid username or E-mail." ) ) ;
2010-11-26 17:02:22 -08:00
return View ( ) ;
}
2013-10-28 17:43:32 -07:00
var siteUrl = _orchardServices . WorkContext . CurrentSite . BaseUrl ;
2012-02-28 16:48:26 -08:00
if ( String . IsNullOrWhiteSpace ( siteUrl ) ) {
siteUrl = HttpContext . Request . ToRootUrlString ( ) ;
}
_userService . SendLostPasswordEmail ( username , nonce = > Url . MakeAbsolute ( Url . Action ( "LostPassword" , "Account" , new { Area = "Orchard.Users" , nonce = nonce } ) , siteUrl ) ) ;
2010-11-26 17:02:22 -08:00
_orchardServices . Notifier . Information ( T ( "Check your e-mail for the confirmation link." ) ) ;
2013-11-19 10:06:34 +01:00
2010-11-26 17:02:22 -08:00
return RedirectToAction ( "LogOn" ) ;
}
2009-11-07 22:49:58 +00:00
[Authorize]
2012-02-24 17:51:25 -08:00
[AlwaysAccessible]
2009-11-07 22:49:58 +00:00
public ActionResult ChangePassword ( ) {
2009-11-12 03:46:14 +00:00
ViewData [ "PasswordLength" ] = MinPasswordLength ;
2009-11-07 22:49:58 +00:00
2010-09-01 15:51:41 -07:00
return View ( ) ;
2009-11-07 22:49:58 +00:00
}
[Authorize]
2010-01-21 21:13:35 +00:00
[HttpPost]
2012-02-24 17:51:25 -08:00
[AlwaysAccessible]
2014-06-23 22:34:58 +01:00
[ValidateInput(false)]
2010-11-30 17:19:13 -08:00
[ SuppressMessage ( "Microsoft.Design" , "CA1031:DoNotCatchGeneralExceptionTypes" ,
Justification = "Exceptions result in password not being changed." ) ]
2009-11-07 22:49:58 +00:00
public ActionResult ChangePassword ( string currentPassword , string newPassword , string confirmPassword ) {
2009-11-12 03:46:14 +00:00
ViewData [ "PasswordLength" ] = MinPasswordLength ;
2009-11-07 22:49:58 +00:00
2010-11-30 17:19:13 -08:00
if ( ! ValidateChangePassword ( currentPassword , newPassword , confirmPassword ) ) {
return View ( ) ;
}
try {
var validated = _membershipService . ValidateUser ( User . Identity . Name , currentPassword ) ;
if ( validated ! = null ) {
_membershipService . SetPassword ( validated , newPassword ) ;
2013-05-26 12:15:50 +02:00
_userEventHandler . ChangedPassword ( validated ) ;
2010-11-30 17:19:13 -08:00
return RedirectToAction ( "ChangePasswordSuccess" ) ;
}
2010-12-07 17:11:55 -08:00
ModelState . AddModelError ( "_FORM" ,
T ( "The current password is incorrect or the new password is invalid." ) ) ;
return ChangePassword ( ) ;
2011-02-08 11:22:47 -08:00
} catch {
2010-11-30 17:19:13 -08:00
ModelState . AddModelError ( "_FORM" , T ( "The current password is incorrect or the new password is invalid." ) ) ;
return ChangePassword ( ) ;
}
}
2015-06-25 12:57:09 -07:00
[AlwaysAccessible]
2010-11-30 17:19:13 -08:00
public ActionResult LostPassword ( string nonce ) {
if ( _userService . ValidateLostPassword ( nonce ) = = null ) {
return RedirectToAction ( "LogOn" ) ;
}
ViewData [ "PasswordLength" ] = MinPasswordLength ;
return View ( ) ;
}
[HttpPost]
2015-06-25 12:57:09 -07:00
[AlwaysAccessible]
2013-10-23 15:49:32 -07:00
[ValidateInput(false)]
2010-11-30 17:19:13 -08:00
public ActionResult LostPassword ( string nonce , string newPassword , string confirmPassword ) {
IUser user ;
if ( ( user = _userService . ValidateLostPassword ( nonce ) ) = = null ) {
return Redirect ( "~/" ) ;
}
ViewData [ "PasswordLength" ] = MinPasswordLength ;
2010-11-26 17:02:22 -08:00
if ( newPassword = = null | | newPassword . Length < MinPasswordLength ) {
ModelState . AddModelError ( "newPassword" , T ( "You must specify a new password of {0} or more characters." , MinPasswordLength ) ) ;
2009-11-07 22:49:58 +00:00
}
2010-11-26 17:02:22 -08:00
if ( ! String . Equals ( newPassword , confirmPassword , StringComparison . Ordinal ) ) {
ModelState . AddModelError ( "_FORM" , T ( "The new password and confirmation password do not match." ) ) ;
2009-11-07 22:49:58 +00:00
}
2010-11-26 17:02:22 -08:00
if ( ! ModelState . IsValid ) {
return View ( ) ;
2009-11-07 22:49:58 +00:00
}
2010-11-26 17:02:22 -08:00
2010-11-30 17:19:13 -08:00
_membershipService . SetPassword ( user , newPassword ) ;
2012-04-24 14:38:31 -07:00
2013-05-26 12:15:50 +02:00
_userEventHandler . ChangedPassword ( user ) ;
2012-04-24 14:38:31 -07:00
2010-11-26 17:02:22 -08:00
return RedirectToAction ( "ChangePasswordSuccess" ) ;
2009-11-07 22:49:58 +00:00
}
2015-06-25 12:57:09 -07:00
[AlwaysAccessible]
2010-11-30 17:19:13 -08:00
public ActionResult ChangePasswordSuccess ( ) {
2010-11-05 15:18:26 -07:00
return View ( ) ;
}
2010-11-30 17:19:13 -08:00
public ActionResult RegistrationPending ( ) {
2010-09-01 15:51:41 -07:00
return View ( ) ;
2009-11-07 22:49:58 +00:00
}
2010-09-01 14:39:28 -07:00
public ActionResult ChallengeEmailSent ( ) {
2010-09-02 21:29:10 -07:00
return View ( ) ;
2010-09-01 14:39:28 -07:00
}
2010-09-01 18:18:53 -07:00
public ActionResult ChallengeEmailSuccess ( ) {
2010-09-02 21:29:10 -07:00
return View ( ) ;
2010-09-01 18:18:53 -07:00
}
public ActionResult ChallengeEmailFail ( ) {
2010-09-02 21:29:10 -07:00
return View ( ) ;
2010-09-01 18:18:53 -07:00
}
2010-12-02 15:46:33 -08:00
public ActionResult ChallengeEmail ( string nonce ) {
var user = _userService . ValidateChallenge ( nonce ) ;
2010-09-01 14:39:28 -07:00
if ( user ! = null ) {
2013-05-26 12:15:50 +02:00
_userEventHandler . ConfirmedEmail ( user ) ;
2011-03-19 10:02:56 -07:00
2010-09-01 18:18:53 -07:00
return RedirectToAction ( "ChallengeEmailSuccess" ) ;
2010-09-01 14:39:28 -07:00
}
2010-09-01 18:18:53 -07:00
return RedirectToAction ( "ChallengeEmailFail" ) ;
2010-09-01 14:39:28 -07:00
}
2009-11-07 22:49:58 +00:00
#region Validation Methods
2010-11-30 17:19:13 -08:00
private bool ValidateChangePassword ( string currentPassword , string newPassword , string confirmPassword ) {
if ( String . IsNullOrEmpty ( currentPassword ) ) {
ModelState . AddModelError ( "currentPassword" , T ( "You must specify a current password." ) ) ;
}
if ( newPassword = = null | | newPassword . Length < 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" , T ( "The new password and confirmation password do not match." ) ) ;
}
return ModelState . IsValid ;
}
2009-11-07 22:49:58 +00:00
2010-04-16 13:14:45 -07:00
private IUser ValidateLogOn ( string userNameOrEmail , string password ) {
2010-05-13 16:31:40 -07:00
bool validate = true ;
2010-04-16 13:14:45 -07:00
if ( String . IsNullOrEmpty ( userNameOrEmail ) ) {
2010-04-16 13:33:45 -07:00
ModelState . AddModelError ( "userNameOrEmail" , T ( "You must specify a username or e-mail." ) ) ;
2010-05-13 16:31:40 -07:00
validate = false ;
2009-11-07 22:49:58 +00:00
}
if ( String . IsNullOrEmpty ( password ) ) {
2010-04-16 13:33:45 -07:00
ModelState . AddModelError ( "password" , T ( "You must specify a password." ) ) ;
2010-05-13 16:31:40 -07:00
validate = false ;
2009-11-07 22:49:58 +00:00
}
2010-05-13 16:31:40 -07:00
if ( ! validate )
return null ;
2010-04-16 13:14:45 -07:00
var user = _membershipService . ValidateUser ( userNameOrEmail , password ) ;
2009-11-12 03:46:14 +00:00
if ( user = = null ) {
2014-06-02 22:29:34 +02:00
_userEventHandler . LogInFailed ( userNameOrEmail , password ) ;
2010-04-16 13:33:45 -07:00
ModelState . AddModelError ( "_FORM" , T ( "The username or e-mail or password provided is incorrect." ) ) ;
2009-11-07 22:49:58 +00:00
}
2009-11-12 03:46:14 +00:00
return user ;
2009-11-07 22:49:58 +00:00
}
private bool ValidateRegistration ( string userName , string email , string password , string confirmPassword ) {
2010-05-13 16:31:40 -07:00
bool validate = true ;
2009-11-07 22:49:58 +00:00
if ( String . IsNullOrEmpty ( userName ) ) {
2010-04-16 13:33:45 -07:00
ModelState . AddModelError ( "username" , T ( "You must specify a username." ) ) ;
2010-05-13 16:31:40 -07:00
validate = false ;
2009-11-07 22:49:58 +00:00
}
2014-03-20 15:28:05 -07:00
else {
if ( userName . Length > = 255 ) {
ModelState . AddModelError ( "username" , T ( "The username you provided is too long." ) ) ;
validate = false ;
}
}
2011-02-07 17:26:00 -08:00
2009-11-07 22:49:58 +00:00
if ( String . IsNullOrEmpty ( email ) ) {
2010-04-16 13:33:45 -07:00
ModelState . AddModelError ( "email" , T ( "You must specify an email address." ) ) ;
2010-05-13 16:31:40 -07:00
validate = false ;
2009-11-07 22:49:58 +00:00
}
2014-03-20 15:28:05 -07:00
else if ( email . Length > = 255 ) {
ModelState . AddModelError ( "email" , T ( "The email address you provided is too long." ) ) ;
validate = false ;
}
2011-02-07 17:26:00 -08:00
else if ( ! Regex . IsMatch ( email , UserPart . EmailPattern , RegexOptions . IgnoreCase ) ) {
// http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx
2011-01-17 16:14:05 -07:00
ModelState . AddModelError ( "email" , T ( "You must specify a valid email address." ) ) ;
validate = false ;
}
2010-05-13 16:31:40 -07:00
if ( ! validate )
return false ;
2010-12-08 18:18:52 -08:00
if ( ! _userService . VerifyUserUnicity ( userName , email ) ) {
ModelState . AddModelError ( "userExists" , T ( "User with that username and/or email already exists." ) ) ;
2010-03-02 17:23:45 -08:00
}
2009-11-12 03:46:14 +00:00
if ( password = = null | | password . Length < MinPasswordLength ) {
2010-04-16 13:33:45 -07:00
ModelState . AddModelError ( "password" , T ( "You must specify a password of {0} or more characters." , MinPasswordLength ) ) ;
2009-11-07 22:49:58 +00:00
}
if ( ! String . Equals ( password , confirmPassword , StringComparison . Ordinal ) ) {
2010-04-16 13:33:45 -07:00
ModelState . AddModelError ( "_FORM" , T ( "The new password and confirmation password do not match." ) ) ;
2009-11-07 22:49:58 +00:00
}
return ModelState . IsValid ;
}
private static string ErrorCodeToString ( MembershipCreateStatus createStatus ) {
// See http://msdn.microsoft.com/en-us/library/system.web.security.membershipcreatestatus.aspx for
// a full list of status codes.
switch ( createStatus ) {
case MembershipCreateStatus . DuplicateUserName :
return "Username already exists. Please enter a different user name." ;
case MembershipCreateStatus . DuplicateEmail :
return "A username for that e-mail address already exists. Please enter a different e-mail address." ;
case MembershipCreateStatus . InvalidPassword :
return "The password provided is invalid. Please enter a valid password value." ;
case MembershipCreateStatus . InvalidEmail :
return "The e-mail address provided is invalid. Please check the value and try again." ;
case MembershipCreateStatus . InvalidAnswer :
return "The password retrieval answer provided is invalid. Please check the value and try again." ;
case MembershipCreateStatus . InvalidQuestion :
return "The password retrieval question provided is invalid. Please check the value and try again." ;
case MembershipCreateStatus . InvalidUserName :
return "The user name provided is invalid. Please check the value and try again." ;
case MembershipCreateStatus . ProviderError :
return
"The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator." ;
case MembershipCreateStatus . UserRejected :
return
"The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator." ;
default :
return
"An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator." ;
}
}
#endregion
}
}