Skip to content

Commit

Permalink
more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeAlhayek committed Nov 26, 2024
1 parent 6c85e2c commit 96ea31a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Text.Json.Nodes;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.DataProtection;
Expand Down Expand Up @@ -593,29 +594,29 @@ private void UpdateAndValidateEmail(RegisterExternalLoginViewModel model, Extern
{
_logger.LogInformation("Attempting to do an external sign in.");

var externalClaims = info.Principal.GetSerializableClaims();
var externalClaims = info.Principal.GetSerializableClaims().ToArray();
var userRoles = await _userManager.GetRolesAsync(user);
var userInfo = user as User;

var context = new UpdateUserContext(user, info.LoginProvider, externalClaims, userInfo.Properties)
var context = new UpdateUserContext(user, info.LoginProvider, externalClaims, userInfo.Properties.DeepClone() as JsonObject)
{
UserClaims = userInfo.UserClaims,
UserRoles = userRoles,
};

foreach (var item in _externalLoginHandlers)
foreach (var externalLoginHandlers in _externalLoginHandlers)
{
try
{
await item.UpdateUserAsync(context);
await externalLoginHandlers.UpdateUserAsync(context);
}
catch (Exception ex)
{
_logger.LogError(ex, "{ExternalLoginHandler}.UpdateUserAsync threw an exception", item.GetType());
_logger.LogError(ex, "The method {ExternalLoginHandler}.UpdateUserAsync(context) threw an exception", externalLoginHandlers.GetType());
}
}

if (await UserManagerHelper.UpdateUserPropertiesAsync(_userManager, userInfo, context))
if (await _userManager.UpdateUserPropertiesAsync(userInfo, context))
{
await _userManager.UpdateAsync(user);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ public class UpdateUserContext : UserContextBase
/// <param name="loginProvider">The login provider.</param>
/// <param name="externalClaims">The user claims.</param>
/// <param name="userProperties">The user properties.</param>
public UpdateUserContext(IUser user, string loginProvider, IEnumerable<SerializableClaim> externalClaims, JsonObject userProperties) : base(user)
public UpdateUserContext(
IUser user,
string loginProvider,
IEnumerable<SerializableClaim> externalClaims,
JsonObject userProperties)
: base(user)
{
ExternalClaims = externalClaims.AsEnumerable();
ExternalClaims = externalClaims;
LoginProvider = loginProvider;
UserProperties = userProperties.DeepClone() as JsonObject;
UserProperties = userProperties;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

namespace OrchardCore.Users;

public static class UserManagerHelper
public static class UserManagerExtensions
{
private static readonly JsonMergeSettings _jsonMergeSettings = new()
{
MergeArrayHandling = MergeArrayHandling.Replace,
MergeNullValueHandling = MergeNullValueHandling.Merge,
};

public static async Task<bool> UpdateUserPropertiesAsync(UserManager<IUser> userManager, User user, UpdateUserContext context)
public static async Task<bool> UpdateUserPropertiesAsync(this UserManager<IUser> userManager, User user, UpdateUserContext context)
{
await userManager.AddToRolesAsync(user, context.RolesToAdd.Distinct());
await userManager.RemoveFromRolesAsync(user, context.RolesToRemove.Distinct());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ await context.UsingTenantScopeAsync(async scope =>
var externalClaims = new List<SerializableClaim>();
var userRoles = await userManager.GetRolesAsync(user);
var context = new UpdateUserContext(user, "TestLoginProvider", externalClaims, user.Properties)
var context = new UpdateUserContext(user, "TestLoginProvider", externalClaims, user.Properties.DeepClone() as JsonObject)
{
UserClaims = user.UserClaims,
UserRoles = userRoles
Expand Down Expand Up @@ -87,7 +87,7 @@ await context.UsingTenantScopeAsync(async scope =>
};
scriptExternalLoginEventHandler.UpdateUserInternal(context, loginSettings);
if (await UserManagerHelper.UpdateUserPropertiesAsync(userManager, user, context))
if (await UserManagerExtensions.UpdateUserPropertiesAsync(userManager, user, context))
{
await userManager.UpdateAsync(user);
}
Expand Down Expand Up @@ -115,7 +115,7 @@ await context.UsingTenantScopeAsync(async scope =>
var externalClaims = new List<SerializableClaim>();
var userRoles = await userManager.GetRolesAsync(user);
var updateContext = new UpdateUserContext(user, "TestLoginProvider", externalClaims, user.Properties)
var updateContext = new UpdateUserContext(user, "TestLoginProvider", externalClaims, user.Properties.DeepClone() as JsonObject)
{
UserClaims = user.UserClaims,
UserRoles = userRoles,
Expand Down Expand Up @@ -145,7 +145,7 @@ await context.UsingTenantScopeAsync(async scope =>
};
scriptExternalLoginEventHandler.UpdateUserInternal(updateContext, loginSettings);
if (await UserManagerHelper.UpdateUserPropertiesAsync(userManager, user, updateContext))
if (await UserManagerExtensions.UpdateUserPropertiesAsync(userManager, user, updateContext))
{
await userManager.UpdateAsync(user);
}
Expand Down

0 comments on commit 96ea31a

Please sign in to comment.