Developer forum

Forum » Ecommerce - Standard features » Impersonate
Anton Marinó Stefánsson
Reply

Greetings

 

We are having some trouble with the impersination feature. We never get any users to appear in the list

1. We set up a page and query publisher app. 

2. Setup it up with this query

But we are never able to get a list of users to impersonate. i also set up a feed page

wondering if im missing any steps


Replies

 
Nicolai Pedersen
Reply

Did you assign impersonation users and groups to any users?

Did you build the index after you did that...?

 
Anton Marinó Stefánsson
Reply

Hi Nicolai,

 

Tried assigning multiple users and redbuilt the index. but no luck

 

best regards,

Anton

 
Nicolai Pedersen
Reply

Well, I can make it work, so something else is probably wrong in your setup.

You have to go back and look into your setup and if you cannot find the cause, we need some more information if you need help.

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Anton,

 

If you are already impesonating a user AND you have impersonation set to "Full", you might not get it to work because the Macro is looking at users that can be impersonated by the "Prymary user", which is your impersonated account.

 

Can you confirm that to be the case?

 

A long time ago we've built a custom macro that returns the "real user", meaning, it would take the primary or secondary depending on the impersonation settings and it would always work. You could build one too if that helps . Here's how it looks https://www.screencast.com/t/anXnUhQC42

 

And here's the source code for it.

 

public static User GetRealUser()
{
    var user = User.GetCurrentUser(PagePermissionLevels.Frontend);

    if (user == null)
    {
        return null;
    }
    
    var impersonationSetting = SystemConfiguration.Instance.GetValue("/Globalsettings/Modules/Users/Impersonation");
    if (!"Full".Equals(impersonationSetting, StringComparison.CurrentCultureIgnoreCase))
    {
        return user;
    }
    
    var secondaryUser = user.CurrentSecondaryUser; 
    return secondaryUser ?? user;
}

public class GetRealUser : Macro
{
    private static readonly Dictionary<string, Func<object>> SupportedActionsInternal = new Dictionary<string, Func<object>>()
    {
        {
            "SecondaryUserIdOrPrimaryUserId",
            () => Extensions.Users.GetRealUser()?.ID ?? -1
        }
    };

    private static readonly object LockObject = new object();

    public override object Evaluate(string action)
    {
        lock (LockObject)
        {
            if (SupportedActionsInternal.ContainsKey(action))
            {
                try
                {
                    return SupportedActionsInternal[action]();
                }
                catch (Exception) { }
            }
        }

        return null;
    }

    public override string Name => "My Custom Macros";

    public override IEnumerable<string> SupportedActions
    {
        get
        {
            IEnumerable<string> result;
            lock (LockObject)
            {
                result = SupportedActionsInternal.Keys.ToList();
            }
            
            return result;
        }
    }
}

 

Best Regards,

Nuno Aguiar

 

You must be logged in to post in the forum