Skip to content

Commit ae18763

Browse files
committed
VCST-2048: Use ClaimsPrincipalExtensions GetUserId
feat: Replace custom code with VirtoCommerce.Platform.Core.Security.ClaimsPrincipalExtensions that resolves user id and name from IdentityOptions settings.
1 parent b5cb31e commit ae18763

File tree

6 files changed

+13
-5
lines changed

6 files changed

+13
-5
lines changed
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
using System;
12
using System.Security.Claims;
3+
using VirtoCommerce.Platform.Core.Security;
24
using static VirtoCommerce.Xapi.Core.ModuleConstants;
35

46
namespace VirtoCommerce.Xapi.Core.Extensions;
57

68
public static class ClaimsPrincipalExtensions
79
{
10+
[Obsolete("Use VirtoCommerce.Platform.Core.Security.GetUserId extension", DiagnosticId = "VC0009", UrlFormat = "https://docs.virtocommerce.org/platform/user-guide/versions/virto3-products-versions/")]
811
public static string GetCurrentUserId(this ClaimsPrincipal claimsPrincipal)
912
{
10-
return claimsPrincipal?.FindFirstValue(ClaimTypes.NameIdentifier) ?? claimsPrincipal?.FindFirstValue("name") ?? AnonymousUser.UserName;
13+
return claimsPrincipal?.GetUserId() ?? AnonymousUser.UserName;
1114
}
1215
}

src/VirtoCommerce.Xapi.Core/Extensions/ResolveEventStreamContextExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Security.Claims;
22
using GraphQL.Server.Transports.Subscriptions.Abstractions;
33
using GraphQL.Subscription;
4+
using VirtoCommerce.Platform.Core.Security;
45

56
namespace VirtoCommerce.Xapi.Core.Extensions
67
{
@@ -9,7 +10,7 @@ public static class ResolveEventStreamContextExtensions
910
public static string GetCurrentUserId(this IResolveEventStreamContext resolveContext)
1011
{
1112
var claimsPrincipal = GetCurrentPrincipal(resolveContext);
12-
return claimsPrincipal?.FindFirstValue(ClaimTypes.NameIdentifier) ?? claimsPrincipal?.FindFirstValue("name") ?? ModuleConstants.AnonymousUser.UserName;
13+
return claimsPrincipal?.GetUserId() ?? ModuleConstants.AnonymousUser.UserName;
1314
}
1415

1516
public static ClaimsPrincipal GetCurrentPrincipal(this IResolveEventStreamContext resolveContext)

src/VirtoCommerce.Xapi.Core/Extensions/ResolveFieldContextExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
using VirtoCommerce.CoreModule.Core.Currency;
88
using VirtoCommerce.CustomerModule.Core.Extensions;
99
using VirtoCommerce.Platform.Core.Common;
10+
using VirtoCommerce.Platform.Core.Security;
1011
using VirtoCommerce.Xapi.Core.Helpers;
1112
using VirtoCommerce.Xapi.Core.Infrastructure;
1213
using VirtoCommerce.Xapi.Core.Queries;
14+
using static VirtoCommerce.Xapi.Core.ModuleConstants;
1315

1416
namespace VirtoCommerce.Xapi.Core.Extensions
1517
{
@@ -65,7 +67,7 @@ public static bool IsAuthenticated(this IResolveFieldContext resolveContext)
6567

6668
public static string GetCurrentUserId(this IResolveFieldContext resolveContext)
6769
{
68-
return resolveContext.GetCurrentPrincipal()?.GetCurrentUserId();
70+
return resolveContext.GetCurrentPrincipal()?.GetUserId() ?? AnonymousUser.UserName;
6971
}
7072

7173
public static string GetCurrentOrganizationId(this IResolveFieldContext resolveContext)

src/VirtoCommerce.Xapi.Core/VirtoCommerce.Xapi.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<PackageReference Include="GraphQL.Server.Transports.AspNetCore.NewtonsoftJson" Version="5.0.2" />
3030
<PackageReference Include="PipelineNet" Version="0.9.0" />
3131
<PackageReference Include="RedLock.net" Version="2.3.2" />
32+
<PackageReference Include="VirtoCommerce.Platform.Core" Version="3.861.0-alpha.12998-vcst-2048" />
3233
<PackageReference Include="VirtoCommerce.CoreModule.Core" Version="3.808.0" />
3334
<PackageReference Include="VirtoCommerce.CustomerModule.Core" Version="3.817.0" />
3435
<PackageReference Include="VirtoCommerce.SearchModule.Core" Version="3.804.0" />

src/VirtoCommerce.Xapi.Data/Services/ExternalSignInUserBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public virtual async Task BuildNewUser(ApplicationUser user, ExternalLoginInfo e
4040

4141
protected virtual string ResolveContactName(ApplicationUser user, ExternalLoginInfo externalLoginInfo)
4242
{
43-
return externalLoginInfo.Principal.FindFirstValue("name");
43+
return externalLoginInfo.Principal.GetUserId();
4444
}
4545

4646
protected virtual string ResolveContactFirstName(ApplicationUser user, ExternalLoginInfo externalLoginInfo)

src/VirtoCommerce.Xapi.Data/Services/UserManagerCore.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using VirtoCommerce.Xapi.Core.Extensions;
88
using VirtoCommerce.Xapi.Core.Security.Authorization;
99
using VirtoCommerce.Xapi.Core.Services;
10+
using static VirtoCommerce.Xapi.Core.ModuleConstants;
1011

1112
namespace VirtoCommerce.Xapi.Data.Services
1213
{
@@ -37,7 +38,7 @@ public Task CheckUserState(string userId, bool allowAnonymous)
3738
public Task CheckCurrentUserState(IResolveFieldContext context, bool allowAnonymous)
3839
{
3940
var principal = context.GetCurrentPrincipal();
40-
var userId = principal.GetCurrentUserId();
41+
var userId = principal?.GetUserId() ?? AnonymousUser.UserName;
4142
var isExternalSignIn = principal.IsExternalSignIn();
4243

4344
return CheckUserState(userId, allowAnonymous, isExternalSignIn);

0 commit comments

Comments
 (0)