Posted on 21/11/2020 12:53:25
Hi Aki,
Here's a Macro we developed a while back
using System;
using System.Collections.Generic;
using System.Linq;
using Dynamicweb.Extensibility.Macros;
namespace Dna.RepositoryExtensions
{
/// <summary>
/// Represents user macro
/// </summary>
/// <seealso cref="Macro" />
/// <autogeneratedoc />
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();
/// <summary>
/// Evaluates the specified action.
/// </summary>
/// <param name="action">The action.</param>
/// <returns>The value of the action that was evaluated; <c>null</c> if the action did not evaluate to a specific value.</returns>
public override object Evaluate(string action)
{
lock (LockObject)
{
if (SupportedActionsInternal.ContainsKey(action))
{
try
{
return SupportedActionsInternal[action]();
}
catch (Exception)
{
// ignored
}
}
}
return null;
}
/// <summary>
/// Gets the name.
/// </summary>
/// <value>The name.</value>
public override string Name => "Rizzo Macros";
/// <summary>
/// Gets the supported actions.
/// </summary>
/// <value>The supported actions.</value>
public override IEnumerable<string> SupportedActions
{
get
{
IEnumerable<string> result;
lock (LockObject)
{
result = SupportedActionsInternal.Keys.ToList();
}
return result;
}
}
}
}
It uses this method that lives elsewhere
using System;
using Dynamicweb.Configuration;
using Dynamicweb.Security.UserManagement;
namespace Dna.Extensions
{
public static class Users
{
/// <summary>Gets the user of the user who logged in, not the impersonated user.
/// </summary>
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;
}
}
}
And it looks like this in the backend
https://www.screencast.com/t/DstepFBxifUp
Happy coding,
Nuno