diff --git a/src/Orchard.Web/Modules/Orchard.Users/Controllers/AccountController.cs b/src/Orchard.Web/Modules/Orchard.Users/Controllers/AccountController.cs index bfab4c451..4c199a891 100644 --- a/src/Orchard.Web/Modules/Orchard.Users/Controllers/AccountController.cs +++ b/src/Orchard.Web/Modules/Orchard.Users/Controllers/AccountController.cs @@ -12,6 +12,7 @@ using Orchard.Users.Services; using Orchard.Users.ViewModels; using Orchard.ContentManagement; using Orchard.Users.Models; +using Orchard.UI.Notify; namespace Orchard.Users.Controllers { [HandleError, Themed] @@ -119,8 +120,8 @@ namespace Orchard.Users.Controllers { if (user != null) { if ( user.As().EmailStatus == UserStatus.Pending ) { - string challengeToken = _membershipService.GetEncryptedChallengeToken(user.As()); - _membershipService.SendChallengeEmail(user.As(), Url.AbsoluteAction(() => Url.Action("ChallengeEmail", "Account", new { Area = "Orchard.Users", token = challengeToken }))); + string challengeToken = _userService.GetNonce(user.As()); + _userService.SendChallengeEmail(user.As(), Url.AbsoluteAction(() => Url.Action("ChallengeEmail", "Account", new { Area = "Orchard.Users", token = challengeToken }))); return RedirectToAction("ChallengeEmailSent"); } @@ -141,6 +142,36 @@ namespace Orchard.Users.Controllers { return Register(); } + public ActionResult LostPassword() { + return View(); + } + + [HttpPost] + public ActionResult LostPassword(string username) { + + if(String.IsNullOrWhiteSpace(username)){ + _orchardServices.Notifier.Error(T("Invalid username or E-mail")); + return View(); + } + + _userService.SendLostPasswordEmail(username, nonce => Url.AbsoluteAction(() => Url.Action("ValidateLostPassword", "Account", new { Area = "Orchard.Users", nonce = nonce }))); + + _orchardServices.Notifier.Information(T("Check your e-mail for the confirmation link.")); + + return RedirectToAction("LogOn"); + } + + public ActionResult ValidateLostPassword(string nonce) { + IUser user; + if (null != (user = _userService.ValidateLostPassword(nonce))) { + _authenticationService.SignIn(user, false); + return RedirectToAction("ChangePassword"); + } + else { + return new RedirectResult("~/"); + } + } + [Authorize] public ActionResult ChangePassword() { ViewData["PasswordLength"] = MinPasswordLength; @@ -150,32 +181,23 @@ namespace Orchard.Users.Controllers { [Authorize] [HttpPost] - [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", - Justification = "Exceptions result in password not being changed.")] public ActionResult ChangePassword(string currentPassword, string newPassword, string confirmPassword) { ViewData["PasswordLength"] = MinPasswordLength; - if (!ValidateChangePassword(currentPassword, newPassword, confirmPassword)) { + 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.")); + } + + if (!ModelState.IsValid) { return View(); } - try { - var validated = _membershipService.ValidateUser(User.Identity.Name, currentPassword); - - if (validated != null) { - _membershipService.SetPassword(validated, newPassword); - return RedirectToAction("ChangePasswordSuccess"); - } - else { - ModelState.AddModelError("_FORM", - T("The current password is incorrect or the new password is invalid.")); - return ChangePassword(); - } - } - catch { - ModelState.AddModelError("_FORM", T("The current password is incorrect or the new password is invalid.")); - return ChangePassword(); - } + _membershipService.SetPassword(_orchardServices.WorkContext.CurrentUser, newPassword); + return RedirectToAction("ChangePasswordSuccess"); } public ActionResult RegistrationPending() { @@ -199,7 +221,7 @@ namespace Orchard.Users.Controllers { } public ActionResult ChallengeEmail(string token) { - var user = _membershipService.ValidateChallengeToken(token); + var user = _userService.ValidateChallenge(token); if ( user != null ) { _authenticationService.SignIn(user, false /* createPersistentCookie */); @@ -217,21 +239,6 @@ namespace Orchard.Users.Controllers { #region Validation Methods - 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; - } - private IUser ValidateLogOn(string userNameOrEmail, string password) { bool validate = true; diff --git a/src/Orchard.Web/Modules/Orchard.Users/Controllers/AdminController.cs b/src/Orchard.Web/Modules/Orchard.Users/Controllers/AdminController.cs index 324e41672..96f8d3c05 100644 --- a/src/Orchard.Web/Modules/Orchard.Users/Controllers/AdminController.cs +++ b/src/Orchard.Web/Modules/Orchard.Users/Controllers/AdminController.cs @@ -197,8 +197,8 @@ namespace Orchard.Users.Controllers { var user = Services.ContentManager.Get(id); if ( user != null ) { - string challengeToken = _membershipService.GetEncryptedChallengeToken(user.As()); - _membershipService.SendChallengeEmail(user.As(), Url.AbsoluteAction(() => Url.Action("ChallengeEmail", "Account", new {Area = "Orchard.Users", token = challengeToken}))); + string challengeToken = _userService.GetNonce(user.As()); + _userService.SendChallengeEmail(user.As(), Url.AbsoluteAction(() => Url.Action("ChallengeEmail", "Account", new {Area = "Orchard.Users", token = challengeToken}))); } Services.Notifier.Information(T("Challenge email sent")); diff --git a/src/Orchard.Web/Modules/Orchard.Users/Handlers/ModerationMessageAlteration.cs b/src/Orchard.Web/Modules/Orchard.Users/Handlers/ModerationMessageAlteration.cs index 596a80ca6..932e4eacf 100644 --- a/src/Orchard.Web/Modules/Orchard.Users/Handlers/ModerationMessageAlteration.cs +++ b/src/Orchard.Web/Modules/Orchard.Users/Handlers/ModerationMessageAlteration.cs @@ -29,11 +29,16 @@ namespace Orchard.Users.Handlers { context.MailMessage.Body = T("The following user account needs to be moderated: {0}", recipient.UserName).Text; } - if ( context.Type == MessageTypes.Validation ) { + if (context.Type == MessageTypes.Validation) { context.MailMessage.Subject = T("User account validation").Text; context.MailMessage.Body = T("Dear {0}, please click here to validate you email address.", recipient.UserName, context.Properties["ChallengeUrl"]).Text; } + if (context.Type == MessageTypes.LostPassword) { + context.MailMessage.Subject = T("Lost password").Text; + context.MailMessage.Body = T("Dear {0}, please click here to change your password.", recipient.UserName, context.Properties["LostPasswordUrl"]).Text; + } + } public void Sent(MessageContext context) { diff --git a/src/Orchard.Web/Modules/Orchard.Users/Models/MessageTypes.cs b/src/Orchard.Web/Modules/Orchard.Users/Models/MessageTypes.cs index e63e44887..ee78118ba 100644 --- a/src/Orchard.Web/Modules/Orchard.Users/Models/MessageTypes.cs +++ b/src/Orchard.Web/Modules/Orchard.Users/Models/MessageTypes.cs @@ -7,6 +7,7 @@ namespace Orchard.Users.Models { public static class MessageTypes { public const string Moderation = "ORCHARD_USERS_MODERATION"; public const string Validation = "ORCHARD_USERS_VALIDATION"; + public const string LostPassword = "ORCHARD_USERS_RESETPASSWORD"; } } \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Users/Orchard.Users.csproj b/src/Orchard.Web/Modules/Orchard.Users/Orchard.Users.csproj index 8099ad4df..73a4fa092 100644 --- a/src/Orchard.Web/Modules/Orchard.Users/Orchard.Users.csproj +++ b/src/Orchard.Web/Modules/Orchard.Users/Orchard.Users.csproj @@ -123,6 +123,9 @@ + + +