diff --git a/src/Orchard.Web/Modules/Orchard.Users/Controllers/AccountController.cs b/src/Orchard.Web/Modules/Orchard.Users/Controllers/AccountController.cs index 018ffdbbe..b24ba80bd 100644 --- a/src/Orchard.Web/Modules/Orchard.Users/Controllers/AccountController.cs +++ b/src/Orchard.Web/Modules/Orchard.Users/Controllers/AccountController.cs @@ -14,6 +14,8 @@ using Orchard.Users.Services; using Orchard.ContentManagement; using Orchard.Users.Models; using Orchard.UI.Notify; +using Orchard.Users.Events; +using System.Collections.Generic; namespace Orchard.Users.Controllers { [HandleError, Themed] @@ -22,17 +24,19 @@ namespace Orchard.Users.Controllers { private readonly IMembershipService _membershipService; private readonly IUserService _userService; private readonly IOrchardServices _orchardServices; - + private readonly IEnumerable _userEventHandlers; public AccountController( IAuthenticationService authenticationService, IMembershipService membershipService, IUserService userService, - IOrchardServices orchardServices) { + IOrchardServices orchardServices, + IEnumerable userEventHandlers) { _authenticationService = authenticationService; _membershipService = membershipService; _userService = userService; _orchardServices = orchardServices; + _userEventHandlers = userEventHandlers; Logger = NullLogger.Instance; T = NullLocalizer.Instance; } @@ -51,8 +55,14 @@ namespace Orchard.Users.Controllers { } //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 + //Suggestion: Could instead use the new AccessDenined IUserEventHandler method and let modules decide if they want to log this event? Logger.Information("Access denied to user #{0} '{1}' on {2}", currentUser.Id, currentUser.UserName, returnUrl); + foreach (var userEventHandler in _userEventHandlers) + { + userEventHandler.AccessDenied(currentUser); + } + return View(); } @@ -75,13 +85,22 @@ namespace Orchard.Users.Controllers { } _authenticationService.SignIn(user, false); + foreach (var userEventHandler in _userEventHandlers) + { + userEventHandler.LoggedIn(user); + } return this.RedirectLocal(returnUrl); } public ActionResult LogOff(string returnUrl) { + IUser wasLoggedInUser = _authenticationService.GetAuthenticatedUser(); _authenticationService.SignOut(); - + if (wasLoggedInUser != null) + foreach (var userEventHandler in _userEventHandlers) + { + userEventHandler.LoggedOut(wasLoggedInUser); + } return this.RedirectLocal(returnUrl); } @@ -116,12 +135,17 @@ namespace Orchard.Users.Controllers { if (ValidateRegistration(userName, email, password, confirmPassword)) { // Attempt to register the user + // No need to report this to IUserEventHandler because _membershipService does that for us var user = _membershipService.CreateUser(new CreateUserParams(userName, password, email, null, null, false)); if (user != null) { if ( user.As().EmailStatus == UserStatus.Pending ) { _userService.SendChallengeEmail(user.As(), nonce => Url.AbsoluteAction(() => Url.Action("ChallengeEmail", "Account", new { Area = "Orchard.Users", nonce = nonce }))); + foreach (var userEventHandler in _userEventHandlers) + { + userEventHandler.SentChallengeEmail(user); + } return RedirectToAction("ChallengeEmailSent"); } @@ -194,6 +218,10 @@ namespace Orchard.Users.Controllers { if ( validated != null ) { _membershipService.SetPassword(validated, newPassword); + foreach (var userEventHandler in _userEventHandlers) + { + userEventHandler.ChangedPassword(validated); + } return RedirectToAction("ChangePasswordSuccess"); } @@ -265,6 +293,10 @@ namespace Orchard.Users.Controllers { var user = _userService.ValidateChallenge(nonce); if ( user != null ) { + foreach (var userEventHandler in _userEventHandlers) { + userEventHandler.ConfirmedEmail(user); + } + return RedirectToAction("ChallengeEmailSuccess"); } diff --git a/src/Orchard.Web/Modules/Orchard.Users/Events/IUserEventHandler.cs b/src/Orchard.Web/Modules/Orchard.Users/Events/IUserEventHandler.cs index 79b0962ed..f9f8017de 100644 --- a/src/Orchard.Web/Modules/Orchard.Users/Events/IUserEventHandler.cs +++ b/src/Orchard.Web/Modules/Orchard.Users/Events/IUserEventHandler.cs @@ -1,7 +1,10 @@ using Orchard.Events; +using Orchard.Security; -namespace Orchard.Users.Events { - public interface IUserEventHandler : IEventHandler { +namespace Orchard.Users.Events +{ + public interface IUserEventHandler : IEventHandler + { /// /// Called before a User is created /// @@ -11,6 +14,38 @@ namespace Orchard.Users.Events { /// Called once a user has been created /// void Created(UserContext context); + + // NEW BELOW HERE + + /// + /// Called once a user has logged in + /// + void LoggedIn(IUser user); + + /// + /// Called when a user explicitly logs out (as opposed to one whos session cookie simply expires) + /// + void LoggedOut(IUser user); + + /// + /// Called when access is denied to a user + /// + void AccessDenied(IUser user); + + /// + /// Called once a user has changed password + /// + void ChangedPassword(IUser user); + + /// + /// Called once a user has confirmed their email address + /// + void SentChallengeEmail(IUser user); + + /// + /// Called once a user has confirmed their email address + /// + void ConfirmedEmail(IUser user); } }