Skip to content

Commit

Permalink
style: name changes, docs, indent, new line
Browse files Browse the repository at this point in the history
style: formatting comment and code
style: added last extra line
style: change var name to usrLocator
docs: reviewd comment
  • Loading branch information
PiemP committed Jun 10, 2024
1 parent 0ce3a96 commit bd01473
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class AccountController : AccountBaseController
private readonly IClock _clock;
private readonly IDistributedCache _distributedCache;
private readonly IEnumerable<IExternalLoginEventHandler> _externalLoginHandlers;
private readonly IEnumerable<IExternalLoginUserToRelateFinder> _externalLoginUsrFinder;
private readonly IEnumerable<IExternalLoginUserToRelateFinder> _externalLoginUserLocator;

private static readonly JsonMergeSettings _jsonMergeSettings = new()
{
Expand Down Expand Up @@ -81,7 +81,7 @@ public AccountController(
IDisplayManager<LoginForm> loginFormDisplayManager,
IUpdateModelAccessor updateModelAccessor,
IEnumerable<IExternalLoginEventHandler> externalLoginHandlers,
IEnumerable<IExternalLoginUserToRelateFinder> externalLoginUsrFinder)
IEnumerable<IExternalLoginUserToRelateFinder> externalLoginUserLocator)
{
_signInManager = signInManager;
_userManager = userManager;
Expand All @@ -97,7 +97,9 @@ public AccountController(
_loginFormDisplayManager = loginFormDisplayManager;
_updateModelAccessor = updateModelAccessor;
_externalLoginHandlers = externalLoginHandlers;
_externalLoginUsrFinder = externalLoginUsrFinder.Reverse();
// reverse services loaded from DI context to select before last services registered
// really important the order of the services registration in the dependency injection context
_externalLoginUserLocator = externalLoginUserLocator.Reverse();

H = htmlLocalizer;
S = stringLocalizer;
Expand Down Expand Up @@ -320,7 +322,7 @@ private async Task<SignInResult> ExternalLoginSignInAsync(IUser user, ExternalLo
var userInfo = user as User;

var context = new UpdateUserContext(user, info.LoginProvider, externalClaims, userInfo.Properties)
{
{
UserClaims = userInfo.UserClaims,
UserRoles = userRoles,
};
Expand Down Expand Up @@ -406,9 +408,8 @@ public async Task<IActionResult> ExternalLoginCallback(string returnUrl = null,
}
else
{
//really important the order of the services registration in the dependency injection context
var usrFinder = _externalLoginUsrFinder.Where(x => x.CanManageThis(info.LoginProvider)).FirstOrDefault();
iUser = usrFinder == null ? null : await usrFinder.FindUserToRelateAsync(info);
var userLocator = _externalLoginUserLocator.Where(x => x.CanHandle(info)).FirstOrDefault();
iUser = userLocator == null ? null : await userLocator.GetUserAsync(info);

ViewData["ReturnUrl"] = returnUrl;
ViewData["LoginProvider"] = info.LoginProvider;
Expand All @@ -428,7 +429,7 @@ public async Task<IActionResult> ExternalLoginCallback(string returnUrl = null,

// Link external login to an existing user
ViewData["UserName"] = iUser.UserName;
ViewData["LinkParameterValue"] = usrFinder?.GetValueThatLinkAccount(info);
ViewData["LinkParameterValue"] = userLocator?.GetValueThatLinkAccount(info);

return View(nameof(LinkExternalLogin));
}
Expand Down Expand Up @@ -651,9 +652,8 @@ public async Task<IActionResult> LinkExternalLogin(LinkExternalLoginViewModel mo

return NotFound();
}
//really important the order of the services registration in the dependency injection context
var usrFinder = _externalLoginUsrFinder.Where(x => x.CanManageThis(info.LoginProvider)).FirstOrDefault();
var user = await usrFinder?.FindUserToRelateAsync(info) ?? null;
var userLocator = _externalLoginUserLocator.Where(x => x.CanHandle(info)).FirstOrDefault();
var user = await userLocator?.GetUserAsync(info) ?? null;

if (user == null)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@

using System.Runtime.CompilerServices;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
Expand All @@ -9,34 +7,34 @@ namespace OrchardCore.Users.Services;

public class DefaultExternalLoginUserToRelateFinder : IExternalLoginUserToRelateFinder
{
private readonly UserManager<IUser> _userManager;

public DefaultExternalLoginUserToRelateFinder(UserManager<IUser> userManager)
{
_userManager = userManager;
}

public bool CanManageThis(string extLoginKind)
{
return true;
}

public async Task<IUser> FindUserToRelateAsync(ExternalLoginInfo info)
{
//the default behavior previously used in OrchardCore
var email = info.Principal.FindFirstValue(ClaimTypes.Email) ?? info.Principal.FindFirstValue("email");

IUser iUser = null;
if (!string.IsNullOrWhiteSpace(email))
private readonly UserManager<IUser> _userManager;

public DefaultExternalLoginUserToRelateFinder(UserManager<IUser> userManager)
{
_userManager = userManager;
}

public bool CanHandle(ExternalLoginInfo info)
{
iUser = await _userManager.FindByEmailAsync(email);
return true;
}

return iUser;
}
public async Task<IUser> GetUserAsync(ExternalLoginInfo info)
{
// the default behavior previously used in OrchardCore
var email = info.Principal.FindFirstValue(ClaimTypes.Email) ?? info.Principal.FindFirstValue("email");

public string GetValueThatLinkAccount(ExternalLoginInfo info)
{
return info.Principal.FindFirstValue(ClaimTypes.Email) ?? info.Principal.FindFirstValue("email");
}
}
IUser iUser = null;
if (!string.IsNullOrWhiteSpace(email))
{
iUser = await _userManager.FindByEmailAsync(email);
}

return iUser;
}

public string GetValueThatLinkAccount(ExternalLoginInfo info)
{
return info.Principal.FindFirstValue(ClaimTypes.Email) ?? info.Principal.FindFirstValue("email");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<form asp-controller="Account" asp-action="LinkExternalLogin" asp-route-returnurl="@ViewData["ReturnUrl"]" method="post" class="form-horizontal no-multisubmit">
<h4>@T["Link your account."]</h4>
<p class="text-info">
@T["You've successfully authenticated with <strong>{0}</strong>. Seems already exist an account identificable by this information: <strong>{1}</strong>. Enter your local account password and click the Register button to link the accounts and finish logging in.", ViewData["LoginProvider"], ViewData["LinkParameterValue"]]
@T["You've successfully authenticated with <strong>{0}</strong>. Seems already exist an account related to: <strong>{1}</strong>. Enter your local account password and click the Register button to link the accounts and finish logging in.", ViewData["LoginProvider"], ViewData["LinkParameterValue"]]
</p>
<hr />
<div asp-validation-summary="ModelOnly"></div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@

using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;

namespace OrchardCore.Users.Abstractions;

public interface IExternalLoginUserToRelateFinder
{
bool CanManageThis(string extLoginKind);
bool CanHandle(ExternalLoginInfo info);

Task<IUser> FindUserToRelateAsync(ExternalLoginInfo info);
Task<IUser> GetUserAsync(ExternalLoginInfo info);

string GetValueThatLinkAccount(ExternalLoginInfo info);
}
string GetValueThatLinkAccount(ExternalLoginInfo info);
}

0 comments on commit bd01473

Please sign in to comment.