Expanding IUserEventHandler to track many more user events such as logon, logoff, confirmed password, ...

--HG--
branch : contributions
This commit is contained in:
Ian Mercer
2011-03-19 10:02:56 -07:00
parent 04955509ca
commit 9e40349aa8
2 changed files with 72 additions and 5 deletions

View File

@@ -14,6 +14,8 @@ using Orchard.Users.Services;
using Orchard.ContentManagement; using Orchard.ContentManagement;
using Orchard.Users.Models; using Orchard.Users.Models;
using Orchard.UI.Notify; using Orchard.UI.Notify;
using Orchard.Users.Events;
using System.Collections.Generic;
namespace Orchard.Users.Controllers { namespace Orchard.Users.Controllers {
[HandleError, Themed] [HandleError, Themed]
@@ -22,17 +24,19 @@ namespace Orchard.Users.Controllers {
private readonly IMembershipService _membershipService; private readonly IMembershipService _membershipService;
private readonly IUserService _userService; private readonly IUserService _userService;
private readonly IOrchardServices _orchardServices; private readonly IOrchardServices _orchardServices;
private readonly IEnumerable<IUserEventHandler> _userEventHandlers;
public AccountController( public AccountController(
IAuthenticationService authenticationService, IAuthenticationService authenticationService,
IMembershipService membershipService, IMembershipService membershipService,
IUserService userService, IUserService userService,
IOrchardServices orchardServices) { IOrchardServices orchardServices,
IEnumerable<IUserEventHandler> userEventHandlers) {
_authenticationService = authenticationService; _authenticationService = authenticationService;
_membershipService = membershipService; _membershipService = membershipService;
_userService = userService; _userService = userService;
_orchardServices = orchardServices; _orchardServices = orchardServices;
_userEventHandlers = userEventHandlers;
Logger = NullLogger.Instance; Logger = NullLogger.Instance;
T = NullLocalizer.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 //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); 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(); return View();
} }
@@ -75,13 +85,22 @@ namespace Orchard.Users.Controllers {
} }
_authenticationService.SignIn(user, false); _authenticationService.SignIn(user, false);
foreach (var userEventHandler in _userEventHandlers)
{
userEventHandler.LoggedIn(user);
}
return this.RedirectLocal(returnUrl); return this.RedirectLocal(returnUrl);
} }
public ActionResult LogOff(string returnUrl) { public ActionResult LogOff(string returnUrl) {
IUser wasLoggedInUser = _authenticationService.GetAuthenticatedUser();
_authenticationService.SignOut(); _authenticationService.SignOut();
if (wasLoggedInUser != null)
foreach (var userEventHandler in _userEventHandlers)
{
userEventHandler.LoggedOut(wasLoggedInUser);
}
return this.RedirectLocal(returnUrl); return this.RedirectLocal(returnUrl);
} }
@@ -116,12 +135,17 @@ namespace Orchard.Users.Controllers {
if (ValidateRegistration(userName, email, password, confirmPassword)) { if (ValidateRegistration(userName, email, password, confirmPassword)) {
// Attempt to register the user // 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)); var user = _membershipService.CreateUser(new CreateUserParams(userName, password, email, null, null, false));
if (user != null) { if (user != null) {
if ( user.As<UserPart>().EmailStatus == UserStatus.Pending ) { if ( user.As<UserPart>().EmailStatus == UserStatus.Pending ) {
_userService.SendChallengeEmail(user.As<UserPart>(), nonce => Url.AbsoluteAction(() => Url.Action("ChallengeEmail", "Account", new { Area = "Orchard.Users", nonce = nonce }))); _userService.SendChallengeEmail(user.As<UserPart>(), nonce => Url.AbsoluteAction(() => Url.Action("ChallengeEmail", "Account", new { Area = "Orchard.Users", nonce = nonce })));
foreach (var userEventHandler in _userEventHandlers)
{
userEventHandler.SentChallengeEmail(user);
}
return RedirectToAction("ChallengeEmailSent"); return RedirectToAction("ChallengeEmailSent");
} }
@@ -194,6 +218,10 @@ namespace Orchard.Users.Controllers {
if ( validated != null ) { if ( validated != null ) {
_membershipService.SetPassword(validated, newPassword); _membershipService.SetPassword(validated, newPassword);
foreach (var userEventHandler in _userEventHandlers)
{
userEventHandler.ChangedPassword(validated);
}
return RedirectToAction("ChangePasswordSuccess"); return RedirectToAction("ChangePasswordSuccess");
} }
@@ -265,6 +293,10 @@ namespace Orchard.Users.Controllers {
var user = _userService.ValidateChallenge(nonce); var user = _userService.ValidateChallenge(nonce);
if ( user != null ) { if ( user != null ) {
foreach (var userEventHandler in _userEventHandlers) {
userEventHandler.ConfirmedEmail(user);
}
return RedirectToAction("ChallengeEmailSuccess"); return RedirectToAction("ChallengeEmailSuccess");
} }

View File

@@ -1,7 +1,10 @@
using Orchard.Events; using Orchard.Events;
using Orchard.Security;
namespace Orchard.Users.Events { namespace Orchard.Users.Events
public interface IUserEventHandler : IEventHandler { {
public interface IUserEventHandler : IEventHandler
{
/// <summary> /// <summary>
/// Called before a User is created /// Called before a User is created
/// </summary> /// </summary>
@@ -11,6 +14,38 @@ namespace Orchard.Users.Events {
/// Called once a user has been created /// Called once a user has been created
/// </summary> /// </summary>
void Created(UserContext context); void Created(UserContext context);
// NEW BELOW HERE
/// <summary>
/// Called once a user has logged in
/// </summary>
void LoggedIn(IUser user);
/// <summary>
/// Called when a user explicitly logs out (as opposed to one whos session cookie simply expires)
/// </summary>
void LoggedOut(IUser user);
/// <summary>
/// Called when access is denied to a user
/// </summary>
void AccessDenied(IUser user);
/// <summary>
/// Called once a user has changed password
/// </summary>
void ChangedPassword(IUser user);
/// <summary>
/// Called once a user has confirmed their email address
/// </summary>
void SentChallengeEmail(IUser user);
/// <summary>
/// Called once a user has confirmed their email address
/// </summary>
void ConfirmedEmail(IUser user);
} }
} }