Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove ReCaptchaLoginFilter #15736

Merged
merged 23 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
9a6ad96
Convert FogotPassword views to shapes
MikeAlhayek Apr 11, 2024
1f1a9c0
Change the ResetPassword to shapes
MikeAlhayek Apr 11, 2024
204bba0
Change Register view to use shapes and remove the ReCaptchaLoginFilter
MikeAlhayek Apr 11, 2024
07666c1
cleanup
MikeAlhayek Apr 11, 2024
25a5f02
Update the Register Tests
MikeAlhayek Apr 12, 2024
a88088a
Merge branch 'main' into ma/remove-recapcha-filter
MikeAlhayek Apr 12, 2024
db71921
Merge branch 'main' into ma/remove-recapcha-filter
MikeAlhayek Apr 12, 2024
1cfe5ba
Use constants
MikeAlhayek Apr 12, 2024
681d3f2
Merge branch 'main' into ma/remove-recapcha-filter
MikeAlhayek Apr 13, 2024
0e5291e
Merge branch 'main' into ma/remove-recapcha-filter
MikeAlhayek Apr 13, 2024
dea7b89
cleanup
MikeAlhayek Apr 14, 2024
0602d08
cleanup
MikeAlhayek Apr 14, 2024
f28a1c9
cleanup
MikeAlhayek Apr 14, 2024
6e4dedd
Merge branch 'main' into ma/remove-recapcha-filter
MikeAlhayek Apr 15, 2024
488cf6d
Merge branch 'main' into ma/remove-recapcha-filter
MikeAlhayek Apr 16, 2024
9022ea3
No need to use .Edit in the views for forms
MikeAlhayek Apr 16, 2024
0ac2525
Merge branch 'ma/remove-recapcha-filter' of https://github.com/Orchar…
MikeAlhayek Apr 16, 2024
cfeb571
Merge branch 'main' into ma/remove-recapcha-filter
MikeAlhayek Apr 16, 2024
928203a
Add settings support for IdentityOptions
MikeAlhayek Apr 18, 2024
5ff7680
cleanup
MikeAlhayek Apr 18, 2024
d002ec4
Merge branch 'main' into ma/remove-recapcha-filter
MikeAlhayek Apr 18, 2024
71f2b99
seal classes
MikeAlhayek Apr 18, 2024
a280adb
Merge branch 'main' into ma/remove-recapcha-filter
MikeAlhayek Apr 18, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Threading.Tasks;
using OrchardCore.DisplayManagement.Handlers;
using OrchardCore.DisplayManagement.Views;
using OrchardCore.ReCaptcha.Configuration;
using OrchardCore.Settings;
using OrchardCore.Users.Models;

namespace OrchardCore.ReCaptcha.Drivers;

public sealed class ReCaptchaForgotPasswordFormDisplayDriver : DisplayDriver<ForgotPasswordForm>
{
private readonly ISiteService _siteService;

public ReCaptchaForgotPasswordFormDisplayDriver(ISiteService siteService)
{
_siteService = siteService;
}

public override async Task<IDisplayResult> EditAsync(ForgotPasswordForm model, BuildEditorContext context)
{
var _reCaptchaSettings = (await _siteService.GetSiteSettingsAsync()).As<ReCaptchaSettings>();

if (!_reCaptchaSettings.IsValid())
{
return null;
}

return View("FormReCaptcha", model).Location("Content:after");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace OrchardCore.ReCaptcha.Drivers;

public class ReCaptchaLoginFormDisplayDriver : DisplayDriver<LoginForm>
public sealed class ReCaptchaLoginFormDisplayDriver : DisplayDriver<LoginForm>
{
private readonly ISiteService _siteService;
private readonly ReCaptchaService _reCaptchaService;
Expand All @@ -30,6 +30,6 @@ public override async Task<IDisplayResult> EditAsync(LoginForm model, BuildEdito
return null;
}

return View("LoginFormReCaptcha_Edit", model).Location("Content:after");
return View("FormReCaptcha", model).Location("Content:after");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Threading.Tasks;
using OrchardCore.DisplayManagement.Handlers;
using OrchardCore.DisplayManagement.Views;
using OrchardCore.ReCaptcha.Configuration;
using OrchardCore.Settings;
using OrchardCore.Users.Models;

namespace OrchardCore.ReCaptcha.Drivers;

public sealed class ReCaptchaResetPasswordFormDisplayDriver : DisplayDriver<ResetPasswordForm>
{
private readonly ISiteService _siteService;

public ReCaptchaResetPasswordFormDisplayDriver(ISiteService siteService)
{
_siteService = siteService;
}

public override async Task<IDisplayResult> EditAsync(ResetPasswordForm model, BuildEditorContext context)
{
var _reCaptchaSettings = (await _siteService.GetSiteSettingsAsync()).As<ReCaptchaSettings>();

if (!_reCaptchaSettings.IsValid())
{
return null;
}

return View("FormReCaptcha", model).Location("Content:after");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Threading.Tasks;
using OrchardCore.DisplayManagement.Handlers;
using OrchardCore.DisplayManagement.Views;
using OrchardCore.ReCaptcha.Configuration;
using OrchardCore.Settings;
using OrchardCore.Users.Models;

namespace OrchardCore.ReCaptcha.Drivers;

public sealed class RegisterUserFormDisplayDriver : DisplayDriver<RegisterUserForm>
{
private readonly ISiteService _siteService;

public RegisterUserFormDisplayDriver(ISiteService siteService)
{
_siteService = siteService;
}

public override async Task<IDisplayResult> EditAsync(RegisterUserForm model, BuildEditorContext context)
{
var _reCaptchaSettings = (await _siteService.GetSiteSettingsAsync()).As<ReCaptchaSettings>();

if (!_reCaptchaSettings.IsValid())
{
return null;
}

return View("FormReCaptcha", model).Location("Content:after");
}
}

This file was deleted.

33 changes: 25 additions & 8 deletions src/OrchardCore.Modules/OrchardCore.ReCaptcha/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using OrchardCore.DisplayManagement.Handlers;
using OrchardCore.Modules;
Expand All @@ -10,13 +9,14 @@
using OrchardCore.Security.Permissions;
using OrchardCore.Settings;
using OrchardCore.Settings.Deployment;
using OrchardCore.Users;
using OrchardCore.Users.Events;
using OrchardCore.Users.Models;

namespace OrchardCore.ReCaptcha
{
[Feature("OrchardCore.ReCaptcha")]
public class Startup : StartupBase
public sealed class Startup : StartupBase
{
public override void ConfigureServices(IServiceCollection services)
{
Expand All @@ -30,7 +30,7 @@ public override void ConfigureServices(IServiceCollection services)

[Feature("OrchardCore.ReCaptcha")]
[RequireFeatures("OrchardCore.Deployment")]
public class DeploymentStartup : StartupBase
public sealed class DeploymentStartup : StartupBase
{
public override void ConfigureServices(IServiceCollection services)
{
Expand All @@ -39,18 +39,35 @@ public override void ConfigureServices(IServiceCollection services)
}

[Feature("OrchardCore.ReCaptcha.Users")]
public class StartupUsers : StartupBase
public sealed class UsersStartup : StartupBase
{
public override void ConfigureServices(IServiceCollection services)
{
services.AddScoped<IRegistrationFormEvents, RegistrationFormEventHandler>();
services.AddScoped<ILoginFormEvent, LoginFormEventEventHandler>();
services.AddScoped<IPasswordRecoveryFormEvents, PasswordRecoveryFormEventEventHandler>();
services.AddScoped<IDisplayDriver<LoginForm>, ReCaptchaLoginFormDisplayDriver>();
services.Configure<MvcOptions>((options) =>
{
options.Filters.Add<ReCaptchaLoginFilter>();
});
}
}

[Feature("OrchardCore.ReCaptcha.Users")]
[RequireFeatures(UserConstants.Features.ResetPassword)]
public sealed class UsersResetPasswordStartup : StartupBase
{
public override void ConfigureServices(IServiceCollection services)
{
services.AddScoped<IDisplayDriver<ForgotPasswordForm>, ReCaptchaForgotPasswordFormDisplayDriver>();
services.AddScoped<IDisplayDriver<ResetPasswordForm>, ReCaptchaResetPasswordFormDisplayDriver>();
}
}

[Feature("OrchardCore.ReCaptcha.Users")]
[RequireFeatures(UserConstants.Features.UserRegistration)]
public sealed class UsersRegistrationStartup : StartupBase
{
public override void ConfigureServices(IServiceCollection services)
{
services.AddScoped<IDisplayDriver<RegisterUserForm>, RegisterUserFormDisplayDriver>();
}
}
}
2 changes: 1 addition & 1 deletion src/OrchardCore.Modules/OrchardCore.Users/AdminMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public Task BuildNavigationAsync(string name, NavigationBuilder builder)
}
}

[Feature("OrchardCore.Users.ResetPassword")]
[Feature(UserConstants.Features.ResetPassword)]
public class ResetPasswordAdminMenu : INavigationProvider
{
private static readonly RouteValueDictionary _routeValues = new()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace OrchardCore.Users.AuditTrail.ResetPassword
{
[RequireFeatures("OrchardCore.Users.AuditTrail", "OrchardCore.Users.ResetPassword")]
[RequireFeatures("OrchardCore.Users.AuditTrail", UserConstants.Features.ResetPassword)]
public class Startup : StartupBase
{
public override void ConfigureServices(IServiceCollection services)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,12 +442,11 @@ public async Task<IActionResult> ExternalLoginCallback(string returnUrl = null,

if (noInformationRequired)
{
iUser = await this.RegisterUser(new RegisterViewModel()
iUser = await this.RegisterUser(new RegisterUserForm()
{
UserName = externalLoginViewModel.UserName,
Email = externalLoginViewModel.Email,
Password = null,
ConfirmPassword = null
}, S["Confirm your account"], _logger);

// If the registration was successful we can link the external provider and redirect the user.
Expand Down Expand Up @@ -559,12 +558,11 @@ public async Task<IActionResult> RegisterExternalLogin(RegisterExternalLoginView
if (TryValidateModel(model) && ModelState.IsValid)
{
var iUser = await this.RegisterUser(
new RegisterViewModel()
new RegisterUserForm()
{
UserName = model.UserName,
Email = model.Email,
Password = model.Password,
ConfirmPassword = model.ConfirmPassword
}, S["Confirm your account"], _logger);

if (iUser is null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,15 +499,15 @@ public async Task<IActionResult> EditPassword(string id)
return Forbid();
}

var model = new ResetPasswordViewModel { Email = user.Email };
var model = new ResetPasswordViewModel { Identifier = user.UserName };

return View(model);
}

[HttpPost]
public async Task<IActionResult> EditPassword(ResetPasswordViewModel model)
{
if (await _userManager.FindByEmailAsync(model.Email) is not User user)
if (await _userService.GetUserAsync(model.Identifier) is not User user)
{
return NotFound();
}
Expand All @@ -521,7 +521,7 @@ public async Task<IActionResult> EditPassword(ResetPasswordViewModel model)
{
var token = await _userManager.GeneratePasswordResetTokenAsync(user);

if (await _userService.ResetPasswordAsync(model.Email, token, model.NewPassword, ModelState.AddModelError))
if (await _userService.ResetPasswordAsync(model.Identifier, token, model.NewPassword, ModelState.AddModelError))
{
await _notifier.SuccessAsync(H["Password updated correctly."]);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
Expand All @@ -9,6 +9,7 @@
using Microsoft.Extensions.Logging;
using OrchardCore.DisplayManagement;
using OrchardCore.Email;
using OrchardCore.Environment.Shell;
using OrchardCore.Modules;
using OrchardCore.Settings;
using OrchardCore.Users.Events;
Expand Down Expand Up @@ -55,20 +56,37 @@ internal static async Task<bool> SendEmailAsync(this Controller controller, stri
/// <param name="confirmationEmailSubject"></param>
/// <param name="logger"></param>
/// <returns></returns>
internal static async Task<IUser> RegisterUser(this Controller controller, RegisterViewModel model, string confirmationEmailSubject, ILogger logger)
internal static async Task<IUser> RegisterUser(this Controller controller, RegisterUserForm model, string confirmationEmailSubject, ILogger logger)
{
var registrationEvents = controller.ControllerContext.HttpContext.RequestServices.GetRequiredService<IEnumerable<IRegistrationFormEvents>>();
var userService = controller.ControllerContext.HttpContext.RequestServices.GetRequiredService<IUserService>();
var shellFeaturesManager = controller.ControllerContext.HttpContext.RequestServices.GetRequiredService<IShellFeaturesManager>();

var registrationFeatureIsAvailable = (await shellFeaturesManager.GetAvailableFeaturesAsync())
.Any(feature => feature.Id == UserConstants.Features.UserRegistration);

if (!registrationFeatureIsAvailable)
{
return null;
}

var settings = (await controller.ControllerContext.HttpContext.RequestServices.GetRequiredService<ISiteService>().GetSiteSettingsAsync()).As<RegistrationSettings>();
var signInManager = controller.ControllerContext.HttpContext.RequestServices.GetRequiredService<SignInManager<IUser>>();

if (settings.UsersCanRegister != UserRegistrationType.NoRegistration)
{
var registrationEvents = controller.ControllerContext.HttpContext.RequestServices.GetServices<IRegistrationFormEvents>();

await registrationEvents.InvokeAsync((e, modelState) => e.RegistrationValidationAsync((key, message) => modelState.AddModelError(key, message)), controller.ModelState, logger);

if (controller.ModelState.IsValid)
{
var user = await userService.CreateUserAsync(new User { UserName = model.UserName, Email = model.Email, EmailConfirmed = !settings.UsersMustValidateEmail, IsEnabled = !settings.UsersAreModerated }, model.Password, (key, message) => controller.ModelState.AddModelError(key, message)) as User;
var userService = controller.ControllerContext.HttpContext.RequestServices.GetRequiredService<IUserService>();

var user = await userService.CreateUserAsync(new User
{
UserName = model.UserName,
Email = model.Email,
EmailConfirmed = !settings.UsersMustValidateEmail,
IsEnabled = !settings.UsersAreModerated
}, model.Password, controller.ModelState.AddModelError) as User;

if (user != null && controller.ModelState.IsValid)
{
Expand All @@ -80,6 +98,8 @@ internal static async Task<IUser> RegisterUser(this Controller controller, Regis
}
else if (!(settings.UsersAreModerated && !user.IsEnabled))
{
var signInManager = controller.ControllerContext.HttpContext.RequestServices.GetRequiredService<SignInManager<IUser>>();

await signInManager.SignInAsync(user, isPersistent: false);
}
logger.LogInformation(3, "User created a new account with password.");
Expand All @@ -89,6 +109,7 @@ internal static async Task<IUser> RegisterUser(this Controller controller, Regis
}
}
}

return null;
}

Expand Down
Loading