diff --git a/OutOfSchool/OutOfSchool.IdentityServer.Tests/Controllers/AuthControllerTests.cs b/OutOfSchool/OutOfSchool.IdentityServer.Tests/Controllers/AuthControllerTests.cs index 1a961135a5..c988b97161 100644 --- a/OutOfSchool/OutOfSchool.IdentityServer.Tests/Controllers/AuthControllerTests.cs +++ b/OutOfSchool/OutOfSchool.IdentityServer.Tests/Controllers/AuthControllerTests.cs @@ -21,6 +21,7 @@ using OutOfSchool.Services.Models; using OutOfSchool.Services.Repository; using SignInResult = Microsoft.AspNetCore.Identity.SignInResult; +using Microsoft.Extensions.Localization; namespace OutOfSchool.IdentityServer.Tests.Controllers { @@ -33,6 +34,7 @@ public class AuthControllerTests private readonly Mock> fakeLogger; private readonly Mock fakeparentRepository; private AuthController authController; + private readonly Mock> fakeLocalizer; public AuthControllerTests() { @@ -41,17 +43,22 @@ public AuthControllerTests() fakeSignInManager = new Mock(); fakeLogger = new Mock>(); fakeparentRepository = new Mock(); + fakeLocalizer = new Mock>(); } [SetUp] public void Setup() { + fakeLocalizer + .Setup(localizer => localizer[It.IsAny()]) + .Returns(new LocalizedString("mock", "error")); authController = new AuthController( fakeUserManager.Object, fakeSignInManager.Object, fakeInteractionService.Object, fakeLogger.Object, - fakeparentRepository.Object + fakeparentRepository.Object, + fakeLocalizer.Object ); } diff --git a/OutOfSchool/OutOfSchool.IdentityServer/Controllers/AuthController.cs b/OutOfSchool/OutOfSchool.IdentityServer/Controllers/AuthController.cs index f8807f66f1..690fd49bed 100644 --- a/OutOfSchool/OutOfSchool.IdentityServer/Controllers/AuthController.cs +++ b/OutOfSchool/OutOfSchool.IdentityServer/Controllers/AuthController.cs @@ -3,6 +3,7 @@ using IdentityServer4.Services; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Localization; using Microsoft.Extensions.Logging; using OutOfSchool.IdentityServer.ViewModels; using OutOfSchool.Services.Enums; @@ -22,6 +23,7 @@ public class AuthController : Controller private readonly IIdentityServerInteractionService interactionService; private readonly ILogger logger; private readonly IParentRepository parentRepository; + private readonly IStringLocalizer localizer; /// /// Initializes a new instance of the class. @@ -31,18 +33,21 @@ public class AuthController : Controller /// Identity Server 4 interaction service. /// Repository for Parent model. /// ILogger class. + /// Localizer. public AuthController( UserManager userManager, SignInManager signInManager, IIdentityServerInteractionService interactionService, ILogger logger, - IParentRepository parentRepository) + IParentRepository parentRepository, + IStringLocalizer localizer) { this.logger = logger; this.parentRepository = parentRepository; this.signInManager = signInManager; this.userManager = userManager; this.interactionService = interactionService; + this.localizer = localizer; } /// @@ -110,7 +115,7 @@ public async Task Login(LoginViewModel model) return BadRequest(); } - ModelState.AddModelError(string.Empty, "Login or password is wrong"); + ModelState.AddModelError(string.Empty, localizer["Login or password is wrong"]); return View(new LoginViewModel { ExternalProviders = await signInManager.GetExternalAuthenticationSchemesAsync(), @@ -159,15 +164,15 @@ public async Task Register(RegisterViewModel model) var user = new User() { - UserName = model.Email, - FirstName = model.FirstName, - LastName = model.LastName, - MiddleName = model.MiddleName, - Email = model.Email, - PhoneNumber = model.PhoneNumber, - CreatingTime = DateTimeOffset.UtcNow, - Role = model.Role, - IsRegistered = false, + UserName = model.Email, + FirstName = model.FirstName, + LastName = model.LastName, + MiddleName = model.MiddleName, + Email = model.Email, + PhoneNumber = model.PhoneNumber, + CreatingTime = DateTimeOffset.UtcNow, + Role = model.Role, + IsRegistered = false, }; try @@ -216,7 +221,7 @@ public async Task Register(RegisterViewModel model) { if (error.Code == "DuplicateUserName") { - error.Description = $"Email {error.Description.Substring(10).Split('\'')[0]} is alredy taken"; + error.Description = localizer["Email {0} is already taken", error.Description.Substring(10).Split('\'')[0]]; } ModelState.AddModelError(string.Empty, error.Description); @@ -230,7 +235,7 @@ public async Task Register(RegisterViewModel model) await userManager.RemoveFromRoleAsync(user, user.Role); await userManager.DeleteAsync(user); - ModelState.AddModelError(string.Empty, "Error! Something happened on the server!"); + ModelState.AddModelError(string.Empty, localizer["Error! Something happened on the server!"]); logger.Log(LogLevel.Error, "Error happened while creating Parent entity! " + ex.Message); @@ -243,4 +248,4 @@ public Task ExternalLogin(string provider, string returnUrl) throw new NotImplementedException(); } } -} \ No newline at end of file +} diff --git a/OutOfSchool/OutOfSchool.IdentityServer/Resources/SharedResource.en.resx b/OutOfSchool/OutOfSchool.IdentityServer/Resources/SharedResource.en.resx index f44ec0d083..40d04a9934 100644 --- a/OutOfSchool/OutOfSchool.IdentityServer/Resources/SharedResource.en.resx +++ b/OutOfSchool/OutOfSchool.IdentityServer/Resources/SharedResource.en.resx @@ -258,4 +258,43 @@ Підтвердження емейлу + + Error! Something happened on the server! + + + is already taken + + + Login or password is wrong + + + Password is required + + + Email is required + + + FirstName is required + + + LastName is required + + + Password confirmation is required + + + Password must contain at least one capital, number and symbol(@$!%*?&). + + + Passwords doesn't match + + + Phone number format is incorrect. Example: +38XXX-XXX-XX-XX + + + Phone number is required + + + Email {0} is already taken + \ No newline at end of file diff --git a/OutOfSchool/OutOfSchool.IdentityServer/Resources/SharedResource.uk.resx b/OutOfSchool/OutOfSchool.IdentityServer/Resources/SharedResource.uk.resx index 53e23218eb..a7c42e1c8c 100644 --- a/OutOfSchool/OutOfSchool.IdentityServer/Resources/SharedResource.uk.resx +++ b/OutOfSchool/OutOfSchool.IdentityServer/Resources/SharedResource.uk.resx @@ -256,4 +256,43 @@ Підтвердження емейлу + + Помилка! Щось сталося на сервері! + + + вже зайнятий + + + Логін або пароль неправильні + + + Необхідний пароль + + + Потрібна електронна адреса + + + Обов'язкове ім'я + + + Потрібно прізвище + + + Потрібно підтвердження пароля + + + Пароль повинен містити принаймні одну велику літеру, число та символ (@$!%*? &). + + + Паролі не збігаються + + + Формат номера телефону неправильний. Приклад: +38XXX-XXX-XX-XX + + + Номер телефону обов’язковий + + + Емейл {0} вже зайнятий + \ No newline at end of file diff --git a/OutOfSchool/OutOfSchool.IdentityServer/Startup.cs b/OutOfSchool/OutOfSchool.IdentityServer/Startup.cs index dcba1145cc..dabe5374b1 100644 --- a/OutOfSchool/OutOfSchool.IdentityServer/Startup.cs +++ b/OutOfSchool/OutOfSchool.IdentityServer/Startup.cs @@ -98,7 +98,12 @@ public void ConfigureServices(IServiceCollection services) builder => builder.Bind(config.GetSection(SmtpOptions.SectionName))); services.AddControllersWithViews() - .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix); + .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix) + .AddDataAnnotationsLocalization(options => + { + options.DataAnnotationLocalizerProvider = (type, factory) => + factory.Create(typeof(SharedResource)); + }); services.AddTransient(); } @@ -152,7 +157,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) app.UseRouting(); - app.UseCookiePolicy(new CookiePolicyOptions {MinimumSameSitePolicy = SameSiteMode.Lax}); + app.UseCookiePolicy(new CookiePolicyOptions { MinimumSameSitePolicy = SameSiteMode.Lax }); app.UseStaticFiles(); diff --git a/OutOfSchool/OutOfSchool.IdentityServer/ViewModels/LoginViewModel.cs b/OutOfSchool/OutOfSchool.IdentityServer/ViewModels/LoginViewModel.cs index e102bbc298..542402b847 100644 --- a/OutOfSchool/OutOfSchool.IdentityServer/ViewModels/LoginViewModel.cs +++ b/OutOfSchool/OutOfSchool.IdentityServer/ViewModels/LoginViewModel.cs @@ -6,7 +6,7 @@ namespace OutOfSchool.IdentityServer.ViewModels { public class LoginViewModel { - [Required] + [Required(ErrorMessage = "Email is required")] public string Username { get; set; } [Required(ErrorMessage = "Password is required")]