Skip to content

Commit

Permalink
Remove ReCaptchaLoginFilter (#15736)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeAlhayek authored Apr 18, 2024
1 parent 9688beb commit cd9f9f4
Show file tree
Hide file tree
Showing 55 changed files with 1,165 additions and 512 deletions.
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

0 comments on commit cd9f9f4

Please sign in to comment.