From 05ca45db491e233820b32e971670dc2df99c1c1b Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Thu, 4 Jul 2024 23:39:12 +0300 Subject: [PATCH] perf: precompile regex --- Endpoint/Controllers/Configuration/SetupController.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Endpoint/Controllers/Configuration/SetupController.cs b/Endpoint/Controllers/Configuration/SetupController.cs index 7ccea5f..92ec9b6 100644 --- a/Endpoint/Controllers/Configuration/SetupController.cs +++ b/Endpoint/Controllers/Configuration/SetupController.cs @@ -33,7 +33,7 @@ namespace Mirea.Api.Endpoint.Controllers.Configuration; [ApiController] [MaintenanceModeIgnore] [ApiExplorerSettings(IgnoreApi = true)] -public class SetupController( +public partial class SetupController( ISetupToken setupToken, IMaintenanceModeNotConfigureService notConfigureService, IMemoryCache cache, @@ -208,7 +208,7 @@ public class SetupController( [BadRequestResponse] public ActionResult CreateAdmin([FromBody] CreateUserRequest user) { - if (user.Password.Length < 8 || !Regex.IsMatch(user.Password, "[A-Z]+") || !Regex.IsMatch(user.Password, "[!@#$%^&*]+")) + if (user.Password.Length < 8 || !PasswordExistUpperLetter().IsMatch(user.Password) || !PasswordExistSpecialSymbol().IsMatch(user.Password)) throw new ControllerArgumentException("The password must be at least 8 characters long and contain at least one uppercase letter and one special character."); if (!MailAddress.TryCreate(user.Email, out _)) @@ -348,4 +348,10 @@ public class SetupController( return true; } + + [GeneratedRegex("[A-Z]+")] + private static partial Regex PasswordExistUpperLetter(); + + [GeneratedRegex("[!@#$%^&*]+")] + private static partial Regex PasswordExistSpecialSymbol(); } \ No newline at end of file