Posted on 21/12/2020 12:49:44
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