0

I would like to log when a role is added or removed for a user.
But none of these methods are being triggered on such use cases.

patch.config:

<sitecore>
  <events>
    <event name="roles:usersAdded">
        <handler type="MyCode.AccountEventHandler, MyCode" method="OnRoleAddedForUser"/>
    </event>
    <event name="roles:usersRemoved">
        <handler type="MyCode.AccountEventHandler, MyCode" method="OnRoleRemovedForUser"/>
    </event>
    <event name="user:runtimerolesupdated">
      <handler type="MyCode.AccountEventHandler, MyCode" method="OnUserRuntimeRolesUpdated"/>
    </event>
    <event name="user:updated">
      <handler type="MyCode.AccountEventHandler, MyCode" method="OnUserUpdated"/>
    </event>
  </events>
</sitecore>

AccountEventHandler.cs

protected void OnRoleAddedForUser(object sender, EventArgs args)
        {
            Log.Info("Role added for user");
        }

        protected void OnRoleRemovedForUser(object sender, EventArgs args)
        {
            Log.Info("Role removed for user");
        }
        
        protected void OnUserRuntimeRolesUpdated(object sender, EventArgs args)
        {
            Log.Info("Role updated for user");
        }

        protected void OnUserUpdated(object sender, EventArgs args)
        {
            Log.Info("user updated");
        }

I have extended other events like user:created & user:deleted. They work fine.

1 Answer 1

2

If you see the code for the Edit User Dialogue from the User Manager, here is the location of the file in the Sitecore webroot.

inetpub\wwwroot\<your application root>\sitecore\shell\Applications\Security\EditUser\EditUser.xaml.xml

This file refers to the DLL i.e. Sitecore.Client and the event is written in this class Sitecore.Shell.Applications.Security.EditUser.EditUserPage. And here is the code snippet to add Edit User or Role.

protected override void OK_Click()
{
  if (!this.Validate() || !this.ValidateTicket())
    return;
  User user = EditUserPage.GetUser();
  Assert.IsNotNull((object) user, typeof (User), "User not found");
  UserProfile profile = user.Profile;
  Assert.IsNotNull((object) profile, typeof (UserProfile));
  try
  {
    IEnumerable<Role> roles = this.Roles.Items.Cast<System.Web.UI.WebControls.ListItem>().Where<System.Web.UI.WebControls.ListItem>((Func<System.Web.UI.WebControls.ListItem, bool>) (item => System.Web.Security.Roles.RoleExists(item.Value))).Select<System.Web.UI.WebControls.ListItem, Role>((Func<System.Web.UI.WebControls.ListItem, Role>) (item => Role.FromName(item.Value)));
    HttpContext current = HttpContext.Current;
    Assert.IsNotNull((object) current, typeof (HttpContext));
    string controlValue = string.Empty;
    foreach (string key in current.Request.Form.Keys)
    {
      if (key != null && key.EndsWith("StartUrlSelector", StringComparison.InvariantCulture))
      {
        controlValue = current.Request.Form[key];
        break;
      }
    }
    if (controlValue == "Default")
      controlValue = string.Empty;
    if (controlValue == "Custom")
      controlValue = this.StartUrl.Text;
    user.Roles.Replace(roles);
    ....
    ....
}

So you can override this class and replace it in the XML file to use your logic.

You can also try this with an Update event like this.

<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>

See this thread for more details.

How to send email, when users roles change - User Manager customization

I hope this will help.

4
  • I have extended other events like user:created & user:deleted. Those are triggered fine.
    – sukesh
    Jul 12, 2023 at 11:59
  • So on OnUserUpdated event are you not able to write code for role? Jul 12, 2023 at 12:22
  • OnUserUpdated is not being triggered when I add/remove a role.
    – sukesh
    Jul 12, 2023 at 12:30
  • I added some more information to my answer. Please check the link I sent in answer. Jul 12, 2023 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.