Intercept the action of a user being updated and get the 'before update' and 'after update' value of

Andrew Malone asked on May 21, 2018 17:23

Using a custom global event handler, I am trying to intercept the action of a user being updated specifically the enabled("UserEnabled") value of a user being updated when they are edited in the CMS. I need to compare the before and after update value of the field and perform custom logic if the user was not enabled and is then changed to enabled.

I have been scouring through all the documentation and devnet posts that are somewhat related to this issue but cannot find the resolution I require. Can anyone help? Code attached:

    using System;
    using CMS;
    using CMS.DataEngine;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using CMSApp.CustomEventHandlers;
    using CMS.Membership;
    using CMS.EventLog;
    using System.Diagnostics;
    using CMS.ContactManagement;
    using CMSApp.Code;
    using System.Text;
    using System.Net.Mail;
    using System.Web.Configuration;

    // Registers the custom module into the system
    [assembly: RegisterModule(typeof(UserEventHandler))]

    public class UserEventHandler : Module
    {
        // Module class constructor, the system registers the module under the name "CustomUserEventHandler"
        public UserEventHandler() : base("CustomUserEventHandler")
        {

        }

        // Contains initialization code that is executed when the application starts
        protected override void OnInit()
        {
            base.OnInit();

            UserInfo.TYPEINFO.Events.Update.Before += UserGroup_UpdateAfterEventHandler;
        }

        private void UserGroup_UpdateAfterEventHandler(object sender, ObjectEventArgs user)
        {
            //Check if user is enabled
            //Check enabled field "before value"
            bool userEnabled = (bool)user.Object.GetValue("UserEnabled");
            var userEmail = user.Object.GetValue("Email");
            var userName = user.Object.GetValue("UserName");
            bool userTermsAndConditions = user.Object.GetBooleanValue("TermsAndConditions", false);

            try
            {
                //check if user is enabled and has agreed to T&C on register
                if (userEnabled == true && userTermsAndConditions == true)
                {


                   //custom code

                }
                else if(userEnabled == true && userTermsAndConditions == false)
                {
                    return;
                }
            }
            catch (Exception ex)
            {
                EventLogProvider.LogEvent(EventType.ERROR, "Global Event Handler: User update", "GEVUpdatedUser", "Error Message: " + ex);
                return;
            }
        }
    }

Correct Answer

Peter Mogilnitski answered on May 22, 2018 05:38

Here is a snippet for you, use GetOriginalValue - contains the original value of a column before the event:

protected override void OnInit()
{
  base.OnInit();

  UserInfo.TYPEINFO.Events.Update.Before += UserInfo_UpdateBeforeHandler;
}

private void UserInfo_UpdateBeforeHandler(object sender, ObjectEventArgs e)
{
    var ui = (UserInfo)e.Object;
    bool userEnabledOriginal = (bool)ui.GetOriginalValue("UserEnabled");
    bool userEnabledNew = (bool)ui.GetValue("UserEnabled");
    ...
}
1 votesVote for this answer Unmark Correct answer

Recent Answers


Trevor Fayas answered on May 21, 2018 20:03

Hook on the before as you have, the user.object will have the user info that will be saved momentarily. You can then do a lookup using the UserInfoProvider to get the current user state and compare. If you need the custom logic to occur after the update, then you will be to save in the session the user Id and past enabled state during the before and then you can pull that up in the after hook.

0 votesVote for this answer Mark as a Correct answer

Andrew Malone answered on May 22, 2018 00:21

Could you perhaps give me an example using my code snippet so I can understand your advice a little better? I find that when my method UserGroup_UpdateAfterEventHandler is triggered, the user.Object "Enabled" value is not the 'before' value, but is instead the updated value.

0 votesVote for this answer Mark as a Correct answer

Andrew Malone answered on May 22, 2018 10:21

Thank you Peter, That was the resolution I was looking for.

0 votesVote for this answer Mark as a Correct answer

Dev Comwerks answered on March 12, 2019 09:46

Hi, I'm doing something similar. I'm running a function before the user object gets updated. I'm comparing the UserInfo GetOriginalValue() and GetValue() values and they are identical. They are both showing the "before" values. I need to get the value that was entered on the form on the webpage so that I can do a comparison. My code is similar to the codes shared by Peter Mogilnitski, just that the column I'm checking is the user's "LastName". How do I get the new value that was entered by the user?

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.