Skip to content

Commit

Permalink
fix: removed IExternalLoginMapper
Browse files Browse the repository at this point in the history
fix: removed DefaultExternalLoginMapper
fix: restored UsersServiceCollectionExtensions
fix: implemente check of RequireUniqueEmail in AccountController
  • Loading branch information
PiemP committed Jun 15, 2024
1 parent 63e26ca commit 5c378b8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using OrchardCore.ContentManagement;
using OrchardCore.DisplayManagement;
Expand Down Expand Up @@ -51,8 +52,7 @@ public class AccountController : AccountBaseController
private readonly IClock _clock;
private readonly IDistributedCache _distributedCache;
private readonly IEnumerable<IExternalLoginEventHandler> _externalLoginHandlers;
private readonly IEnumerable<IExternalLoginMapper> _externalLoginMappers;

private readonly IdentityOptions _identityOptions;
private static readonly JsonMergeSettings _jsonMergeSettings = new()
{
MergeArrayHandling = MergeArrayHandling.Replace,
Expand All @@ -79,7 +79,7 @@ public AccountController(
IDisplayManager<LoginForm> loginFormDisplayManager,
IUpdateModelAccessor updateModelAccessor,
IEnumerable<IExternalLoginEventHandler> externalLoginHandlers,
IEnumerable<IExternalLoginMapper> externalLoginMappers)
IOptions<IdentityOptions> identityOptions)
{
_signInManager = signInManager;
_userManager = userManager;
Expand All @@ -95,8 +95,7 @@ public AccountController(
_loginFormDisplayManager = loginFormDisplayManager;
_updateModelAccessor = updateModelAccessor;
_externalLoginHandlers = externalLoginHandlers;
// Reverse the order of services to prioritize third-party implementations first, placing them before the default one.
_externalLoginMappers = externalLoginMappers.Reverse();
_identityOptions = identityOptions.Value;

H = htmlLocalizer;
S = stringLocalizer;
Expand Down Expand Up @@ -404,32 +403,31 @@ public async Task<IActionResult> ExternalLoginCallback(string returnUrl = null,
}
else
{
var extLoginMapper = _externalLoginMappers.FirstOrDefault(x => x.CanHandle(info));
if (extLoginMapper != null)
if (_identityOptions.User.RequireUniqueEmail)
{
iUser = await extLoginMapper.FindByLoginAsync(info);
}
iUser = await _userManager.FindByEmailAsync(info.GetEmail());

ViewData["ReturnUrl"] = returnUrl;
ViewData["LoginProvider"] = info.LoginProvider;
ViewData["ReturnUrl"] = returnUrl;
ViewData["LoginProvider"] = info.LoginProvider;

if (iUser != null)
{
if (iUser is User userToLink && registrationSettings.UsersMustValidateEmail && !userToLink.EmailConfirmed)
if (iUser != null)
{
return RedirectToAction(nameof(EmailConfirmationController.ConfirmEmailSent),
new
{
Area = UserConstants.Features.Users,
Controller = typeof(EmailConfirmationController).ControllerName(),
ReturnUrl = returnUrl,
});
}
if (iUser is User userToLink && registrationSettings.UsersMustValidateEmail && !userToLink.EmailConfirmed)
{
return RedirectToAction(nameof(EmailConfirmationController.ConfirmEmailSent),
new
{
Area = UserConstants.Features.Users,
Controller = typeof(EmailConfirmationController).ControllerName(),
ReturnUrl = returnUrl,
});
}

// Link external login to an existing user
ViewData["UserName"] = iUser.UserName;
// Link external login to an existing user
ViewData["UserName"] = iUser.UserName;

return View(nameof(LinkExternalLogin));
return View(nameof(LinkExternalLogin));
}
}

// No user could be matched, check if a new user can register.
Expand Down Expand Up @@ -649,8 +647,7 @@ public async Task<IActionResult> LinkExternalLogin(LinkExternalLoginViewModel mo
return NotFound();
}

var extLoginMapper = _externalLoginMappers.FirstOrDefault(x => x.CanHandle(info));
var user = await extLoginMapper?.FindByLoginAsync(info);
var user = await _userManager.FindByEmailAsync(info.GetEmail());

if (user == null)
{
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ public static IServiceCollection AddUsers(this IServiceCollection services)
services.TryAddScoped<IUserAuthenticationTokenStore<IUser>>(sp => sp.GetRequiredService<UserStore>());
services.TryAddScoped<IUserPhoneNumberStore<IUser>>(sp => sp.GetRequiredService<UserStore>());

services.AddScoped<IExternalLoginMapper, DefaultExternalLoginMapper>();

services.AddScoped<NullRoleStore>();
services.TryAddScoped<IRoleClaimStore<IRole>>(sp => sp.GetRequiredService<NullRoleStore>());
services.TryAddScoped<IRoleStore<IRole>>(sp => sp.GetRequiredService<NullRoleStore>());
Expand Down

This file was deleted.

0 comments on commit 5c378b8

Please sign in to comment.