Developer forum

Forum » Ecommerce - Standard features » Event for detecting

Event for detecting

Stephen Anthony Jackson
Reply

We are subscribing to OnExtranetLogin which allows us to set the Discounts for a customer within a specific customer group.

However, when using impersonation, the OnExtranetLogin does not appear to be fired, or I am not capturing something correctly, so the correct discount will not be set for the impersonated user.

Is there an event for "OnImpersonatedLogin" or something?

Cheers

//Steve


Replies

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Stephen,

 

The user you impersonate is always (or should be - depends on your settings) the primary user, so the discount would be applied to it (the impersonated user).

 

If you need to run any logic when impersonating a user, you can look for SecondaryUserId being null. If it's not null, then it's someone impersonating.

 

Here's something we use that you might find useful

        public static User GetRealUser()
        {
            return GetUserByUserType(Impersonation.UserType.Real);
        }
        
        public static User GetImpersonatedUser()
        {
            return GetUserByUserType(Impersonation.UserType.Impersonated);
        }
 
        private static User GetUserByUserType(Impersonation.UserType userType)
        {
            var user = User.GetCurrentFrontendUser();
 
            if (user == null || userType == Impersonation.UserType.Impersonated && user.CurrentSecondaryUser == null)
            {
                return null;
            }
 
            return IsRequestedUserPrimaryUser(userType) ? user : user.CurrentSecondaryUser ?? user;
        }
        
        private static bool IsRequestedUserPrimaryUser(Impersonation.UserType userType)
        {
            var impersonationSetting = SystemConfiguration.Instance.GetValue("/Globalsettings/Modules/Users/Impersonation");
            var isImpersonationFull = impersonationSetting.Equals("Full", StringComparison.CurrentCultureIgnoreCase);
 
            return userType == Impersonation.UserType.Impersonated && isImpersonationFull || userType == Impersonation.UserType.Real && !isImpersonationFull;
        }

 

The Impersonation.UserType is a simple enum

    internal static class Impersonation
    {
        internal enum UserType
        {
            Real,
            Impersonated
        }
    }
 

Does this help?

 

Best Regards,

Nuno Aguiar 

 
Stephen Anthony Jackson
Reply

Hi Nuno.

The problem here is that we are using a custom priceprovider / tablestucture for our discounts, so we are not using the regular built-in mechanisms.

When user logs in, a query is performed to find the appropriate discounts, and these are set in a session variable which is in turn used by the price provider.

But I dont seem to be getting the correct customernumber when I impersonate. I dont think the ExtranetLogin event is fired when switching to an impersonated user. If there is an event for this, then great, cos I can run the same code which sets the discounts for the impersonated user instead.

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Stephen,

 

From our experience the OnExtranetLogin notification is always triggered when impersonation. We do it like this

 

namespace Dna.AutoImpersonate.NotificationSubscribers
{
    [Subscribe(Standard.User.OnExtranetLogin)]
    public class OnExtranetLogin : NotificationSubscriber
    {
        public override void OnNotify(string notification, NotificationArgs args)
        {
            if (!(args is Standard.User.OnExtranetLoginArgs userArgs)
                || Dynamicweb.Context.Current.Request.Params.AllKeys.Contains("DwExtranetRemoveSecondaryUser"))
            {
                return;
            }
 
            if (IsImpersonating())
            {
            //Your code here
            }
        }
 
        internal static bool IsImpersonating()
        {
            var realUser = Extensions.Users.GetRealUser();
            var primaryUser = User.GetCurrentFrontendUser();
 
            return realUser != null && primaryUser != null && primaryUser.ID != realUser.ID;
        }
    }
}

 

You must be logged in to post in the forum