One option is to hook into Sitecore's user:updated
event (and optionally user:updated:remote
). This event is raised when a user has been updated, the remote event handler is raised when a user was updated on a remote Sitecore instance.
EDIT: As pointed out in the comments by @SzymonKuzniak, you can also hook into the roles:usersAdded[:remote]
event to specifically handle added roles. Likewise, there is also the roles:rolesRemoved[:remote]
event.
Create a new configuration file and put it in App_Config\Include:
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<events>
<event name="user:updated">
<handler type="Custom.UserUpdatedEventHandler, Custom" method="OnUserUpdated"/>
</event>
<event name="user:updated:remote">
<handler type="Custom.UserUpdatedEventHandler, Custom" method="OnUserUpdatedRemote"/>
</event>
</events>
</sitecore>
</configuration>
Create a class UserUpdatedEventHandler
in the Custom
namespace:
public class UserUpdatedEventHandler
{
public void OnUserUpdated(object o, EventArgs args)
{
var sitecoreArgs = (SitecoreEventArgs)args;
var membershipUser = (MembershipUser)sitecoreArgs.Parameters[0];
// Check user and roles
// Send e-mail
}
public void OnUserUpdatedRemote(object o, EventArgs args)
{
var sitecoreArgs = (SitecoreEventArgs)args;
var userRemoteEventArgs = (UserUpdatedRemoteEventArgs)sitecoreArgs.Parameters[0];
var userName = userRemoteEventArgs.UserName;
// Check user and roles
// Send e-mail
}
}