Skip to content

Commit

Permalink
Use the correct values for CURRENT_USER while impersonating
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkMpn committed Oct 29, 2024
1 parent 9dd470a commit 4c173ea
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion MarkMpn.Sql4Cds.Engine/ExecutionPlan/ExpressionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
using Microsoft.SqlServer.TransactSql.ScriptDom;
using Microsoft.Xrm.Sdk;
using Wmhelp.XPath2;
#if NETCOREAPP
using Microsoft.PowerPlatform.Dataverse.Client;
#else
using Microsoft.Xrm.Tooling.Connector;
#endif

namespace MarkMpn.Sql4Cds.Engine.ExecutionPlan
{
Expand Down Expand Up @@ -2384,7 +2389,24 @@ private static SqlDateTime GetCurrentTimestamp(ExpressionExecutionContext contex

private static SqlEntityReference GetCurrentUser(ExpressionExecutionContext context)
{
return new SqlEntityReference(context.Options.PrimaryDataSource, "systemuser", context.Options.UserId);
var callerId = Guid.Empty;

#if NETCOREAPP
if (context.PrimaryDataSource.Connection is ServiceClient svc)
callerId = svc.CallerId;
#else
if (context.PrimaryDataSource.Connection is Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy svcProxy)
callerId = svcProxy.CallerId;
else if (context.PrimaryDataSource.Connection is Microsoft.Xrm.Sdk.WebServiceClient.OrganizationWebProxyClient webProxy)
callerId = webProxy.CallerId;
else if (context.PrimaryDataSource.Connection is CrmServiceClient svc)
callerId = svc.CallerId;
#endif

if (callerId == Guid.Empty)
callerId = context.Options.UserId;

return new SqlEntityReference(context.Options.PrimaryDataSource, "systemuser", callerId);
}

/// <summary>
Expand Down

0 comments on commit 4c173ea

Please sign in to comment.