From 0cde51e01cdaf3239eca6a7afe97fb2a35cc5456 Mon Sep 17 00:00:00 2001 From: mcmuzzle Date: Mon, 9 Sep 2024 23:16:34 +0200 Subject: [PATCH] nettoyage des warning, ajout de localization --- .editorconfig | 1 + ...omponentsEndpointRouteBuilderExtensions.cs | 12 +- .../Account/IdentityNoOpEmailSender.cs | 8 +- .../Account/IdentityRedirectManager.cs | 4 + ...RevalidatingAuthenticationStateProvider.cs | 12 +- .../Account/IdentityUserAccessor.cs | 6 +- .../Components/Layout/NavMenu.razor | 15 +- .../Components/Pages/Auth.razor | 14 -- .../Components/Pages/Counter.razor | 18 --- .../Components/Pages/Games.razor | 28 ++++ .../Components/Pages/Home.razor | 18 ++- .../Components/Pages/Weather.razor | 60 -------- src/LudikZoneBlazor/Components/_Imports.razor | 1 + .../Data/ApplicationDbContext.cs | 9 ++ src/LudikZoneBlazor/Data/ApplicationUser.cs | 2 +- src/LudikZoneBlazor/Data/Model/Game.cs | 11 ++ src/LudikZoneBlazor/LudikZoneBlazor.csproj | 5 + .../Migrations/20240906111336_Init.cs | 3 +- src/LudikZoneBlazor/Program.cs | 15 ++ .../Resources/Components.Pages.Games.en.resx | 128 ++++++++++++++++++ .../Resources/Components.Pages.Games.fr.resx | 128 ++++++++++++++++++ 21 files changed, 376 insertions(+), 122 deletions(-) delete mode 100644 src/LudikZoneBlazor/Components/Pages/Auth.razor delete mode 100644 src/LudikZoneBlazor/Components/Pages/Counter.razor create mode 100644 src/LudikZoneBlazor/Components/Pages/Games.razor delete mode 100644 src/LudikZoneBlazor/Components/Pages/Weather.razor create mode 100644 src/LudikZoneBlazor/Data/Model/Game.cs create mode 100644 src/LudikZoneBlazor/Resources/Components.Pages.Games.en.resx create mode 100644 src/LudikZoneBlazor/Resources/Components.Pages.Games.fr.resx diff --git a/.editorconfig b/.editorconfig index 43ea4c3..effc0a7 100644 --- a/.editorconfig +++ b/.editorconfig @@ -6,6 +6,7 @@ root = true dotnet_diagnostic.IDE1006.severity = warning dotnet_diagnostic.IDE0005.severity = error dotnet_diagnostic.CA5394.severity = none +dotnet_diagnostic.CA1848.severity = none #optimisatio logger pas nécessaire # Exclude generated code [src/**/Migrations/*.cs] diff --git a/src/LudikZoneBlazor/Components/Account/IdentityComponentsEndpointRouteBuilderExtensions.cs b/src/LudikZoneBlazor/Components/Account/IdentityComponentsEndpointRouteBuilderExtensions.cs index 4246f4e..698fed3 100644 --- a/src/LudikZoneBlazor/Components/Account/IdentityComponentsEndpointRouteBuilderExtensions.cs +++ b/src/LudikZoneBlazor/Components/Account/IdentityComponentsEndpointRouteBuilderExtensions.cs @@ -45,7 +45,7 @@ internal static class IdentityComponentsEndpointRouteBuilderExtensions SignInManager signInManager, [FromForm] string returnUrl) => { - await signInManager.SignOutAsync(); + await signInManager.SignOutAsync().ConfigureAwait(true); return TypedResults.LocalRedirect($"~/{returnUrl}"); }); @@ -57,7 +57,7 @@ internal static class IdentityComponentsEndpointRouteBuilderExtensions [FromForm] string provider) => { // Clear the existing external cookie to ensure a clean login process - await context.SignOutAsync(IdentityConstants.ExternalScheme); + await context.SignOutAsync(IdentityConstants.ExternalScheme).ConfigureAwait(false); var redirectUrl = UriHelper.BuildRelative( context.Request.PathBase, @@ -76,13 +76,13 @@ internal static class IdentityComponentsEndpointRouteBuilderExtensions [FromServices] UserManager userManager, [FromServices] AuthenticationStateProvider authenticationStateProvider) => { - var user = await userManager.GetUserAsync(context.User); + var user = await userManager.GetUserAsync(context.User).ConfigureAwait(false); if (user is null) { return Results.NotFound($"Unable to load user with ID '{userManager.GetUserId(context.User)}'."); } - var userId = await userManager.GetUserIdAsync(user); + var userId = await userManager.GetUserIdAsync(user).ConfigureAwait(false); downloadLogger.LogInformation("User with ID '{UserId}' asked for their personal data.", userId); // Only include personal data for download @@ -94,13 +94,13 @@ internal static class IdentityComponentsEndpointRouteBuilderExtensions personalData.Add(p.Name, p.GetValue(user)?.ToString() ?? "null"); } - var logins = await userManager.GetLoginsAsync(user); + var logins = await userManager.GetLoginsAsync(user).ConfigureAwait(false); foreach (var l in logins) { personalData.Add($"{l.LoginProvider} external login provider key", l.ProviderKey); } - personalData.Add("Authenticator Key", (await userManager.GetAuthenticatorKeyAsync(user))!); + personalData.Add("Authenticator Key", (await userManager.GetAuthenticatorKeyAsync(user).ConfigureAwait(false))!); var fileBytes = JsonSerializer.SerializeToUtf8Bytes(personalData); context.Response.Headers.TryAdd("Content-Disposition", "attachment; filename=PersonalData.json"); diff --git a/src/LudikZoneBlazor/Components/Account/IdentityNoOpEmailSender.cs b/src/LudikZoneBlazor/Components/Account/IdentityNoOpEmailSender.cs index 781113b..497af1a 100644 --- a/src/LudikZoneBlazor/Components/Account/IdentityNoOpEmailSender.cs +++ b/src/LudikZoneBlazor/Components/Account/IdentityNoOpEmailSender.cs @@ -4,10 +4,12 @@ using LudikZoneBlazor.Data; namespace LudikZoneBlazor.Components.Account; -// Remove the "else if (EmailSender is IdentityNoOpEmailSender)" block from RegisterConfirmation.razor after updating with a real implementation. +#pragma warning disable CA1812 // Elle est instanciée en Injection de dependance + +/// Remove the "else if (EmailSender is IdentityNoOpEmailSender)" block from RegisterConfirmation.razor after updating with a real implementation. internal sealed class IdentityNoOpEmailSender : IEmailSender { - private readonly IEmailSender emailSender = new NoOpEmailSender(); + private readonly NoOpEmailSender emailSender = new NoOpEmailSender(); public Task SendConfirmationLinkAsync(ApplicationUser user, string email, string confirmationLink) => emailSender.SendEmailAsync(email, "Confirm your email", $"Please confirm your account by clicking here."); @@ -18,3 +20,5 @@ internal sealed class IdentityNoOpEmailSender : IEmailSender public Task SendPasswordResetCodeAsync(ApplicationUser user, string email, string resetCode) => emailSender.SendEmailAsync(email, "Reset your password", $"Please reset your password using the following code: {resetCode}"); } + +#pragma warning restore CA1812 \ No newline at end of file diff --git a/src/LudikZoneBlazor/Components/Account/IdentityRedirectManager.cs b/src/LudikZoneBlazor/Components/Account/IdentityRedirectManager.cs index 44f6083..a024a51 100644 --- a/src/LudikZoneBlazor/Components/Account/IdentityRedirectManager.cs +++ b/src/LudikZoneBlazor/Components/Account/IdentityRedirectManager.cs @@ -3,6 +3,8 @@ using Microsoft.AspNetCore.Components; namespace LudikZoneBlazor.Components.Account; +#pragma warning disable CA1812 // Elle est instanciée en Injection de dependance + internal sealed class IdentityRedirectManager(NavigationManager navigationManager) { public const string StatusCookieName = "Identity.StatusMessage"; @@ -56,3 +58,5 @@ internal sealed class IdentityRedirectManager(NavigationManager navigationManage public void RedirectToCurrentPageWithStatus(string message, HttpContext context) => RedirectToWithStatus(CurrentPath, message, context); } + +#pragma warning restore CA1812 \ No newline at end of file diff --git a/src/LudikZoneBlazor/Components/Account/IdentityRevalidatingAuthenticationStateProvider.cs b/src/LudikZoneBlazor/Components/Account/IdentityRevalidatingAuthenticationStateProvider.cs index 74b2a30..91d25a2 100644 --- a/src/LudikZoneBlazor/Components/Account/IdentityRevalidatingAuthenticationStateProvider.cs +++ b/src/LudikZoneBlazor/Components/Account/IdentityRevalidatingAuthenticationStateProvider.cs @@ -7,6 +7,8 @@ using LudikZoneBlazor.Data; namespace LudikZoneBlazor.Components.Account; +#pragma warning disable CA1812 // Elle est instanciée en Injection de dependance + // This is a server-side AuthenticationStateProvider that revalidates the security stamp for the connected user // every 30 minutes an interactive circuit is connected. internal sealed class IdentityRevalidatingAuthenticationStateProvider( @@ -21,14 +23,14 @@ internal sealed class IdentityRevalidatingAuthenticationStateProvider( AuthenticationState authenticationState, CancellationToken cancellationToken) { // Get the user manager from a new scope to ensure it fetches fresh data - await using var scope = scopeFactory.CreateAsyncScope(); + using AsyncServiceScope scope = scopeFactory.CreateAsyncScope(); var userManager = scope.ServiceProvider.GetRequiredService>(); - return await ValidateSecurityStampAsync(userManager, authenticationState.User); + return await ValidateSecurityStampAsync(userManager, authenticationState.User).ConfigureAwait(false); } private async Task ValidateSecurityStampAsync(UserManager userManager, ClaimsPrincipal principal) { - var user = await userManager.GetUserAsync(principal); + var user = await userManager.GetUserAsync(principal).ConfigureAwait(false); if (user is null) { return false; @@ -40,8 +42,10 @@ internal sealed class IdentityRevalidatingAuthenticationStateProvider( else { var principalStamp = principal.FindFirstValue(options.Value.ClaimsIdentity.SecurityStampClaimType); - var userStamp = await userManager.GetSecurityStampAsync(user); + var userStamp = await userManager.GetSecurityStampAsync(user).ConfigureAwait(false); return principalStamp == userStamp; } } } + +#pragma warning restore CA1812 \ No newline at end of file diff --git a/src/LudikZoneBlazor/Components/Account/IdentityUserAccessor.cs b/src/LudikZoneBlazor/Components/Account/IdentityUserAccessor.cs index c27faf9..33a799d 100644 --- a/src/LudikZoneBlazor/Components/Account/IdentityUserAccessor.cs +++ b/src/LudikZoneBlazor/Components/Account/IdentityUserAccessor.cs @@ -3,11 +3,13 @@ using LudikZoneBlazor.Data; namespace LudikZoneBlazor.Components.Account; +#pragma warning disable CA1812 // Elle est instanciée en Injection de dependance + internal sealed class IdentityUserAccessor(UserManager userManager, IdentityRedirectManager redirectManager) { public async Task GetRequiredUserAsync(HttpContext context) { - var user = await userManager.GetUserAsync(context.User); + var user = await userManager.GetUserAsync(context.User).ConfigureAwait(false); if (user is null) { @@ -17,3 +19,5 @@ internal sealed class IdentityUserAccessor(UserManager userMana return user; } } + +#pragma warning restore CA1812 \ No newline at end of file diff --git a/src/LudikZoneBlazor/Components/Layout/NavMenu.razor b/src/LudikZoneBlazor/Components/Layout/NavMenu.razor index 094949a..faec727 100644 --- a/src/LudikZoneBlazor/Components/Layout/NavMenu.razor +++ b/src/LudikZoneBlazor/Components/Layout/NavMenu.razor @@ -4,12 +4,11 @@ Home - Counter - Weather - Auth Required + Games - @context.User.Identity?.Name + + @context.User.Identity?.Name
@@ -19,8 +18,10 @@
- Register - Login + + Register + Login +
@@ -46,5 +47,3 @@ NavigationManager.LocationChanged -= OnLocationChanged; } } - - diff --git a/src/LudikZoneBlazor/Components/Pages/Auth.razor b/src/LudikZoneBlazor/Components/Pages/Auth.razor deleted file mode 100644 index 66b626e..0000000 --- a/src/LudikZoneBlazor/Components/Pages/Auth.razor +++ /dev/null @@ -1,14 +0,0 @@ -@page "/auth" - -@using Microsoft.AspNetCore.Authorization - -@attribute [Authorize] - -Auth - - -You are authenticated! - - - Hello @context.User.Identity?.Name! - diff --git a/src/LudikZoneBlazor/Components/Pages/Counter.razor b/src/LudikZoneBlazor/Components/Pages/Counter.razor deleted file mode 100644 index db95bf3..0000000 --- a/src/LudikZoneBlazor/Components/Pages/Counter.razor +++ /dev/null @@ -1,18 +0,0 @@ -@page "/counter" - -Counter - -Counter - -Current count: @currentCount - -Click me - -@code { - private int currentCount = 0; - - private void IncrementCount() - { - currentCount++; - } -} diff --git a/src/LudikZoneBlazor/Components/Pages/Games.razor b/src/LudikZoneBlazor/Components/Pages/Games.razor new file mode 100644 index 0000000..cb56a59 --- /dev/null +++ b/src/LudikZoneBlazor/Components/Pages/Games.razor @@ -0,0 +1,28 @@ +@page "/games" + +@using Microsoft.AspNetCore.Authorization + +@using LudikZoneBlazor.Data.Model + +@attribute [Authorize] + +@inject IStringLocalizer localizer + +@localizer["Games"] + +@localizer["Game list"] + + + @foreach (var g in GetGames()) + { + + } + + +@code { + + private List GetGames() + { + return new List(); + } +} diff --git a/src/LudikZoneBlazor/Components/Pages/Home.razor b/src/LudikZoneBlazor/Components/Pages/Home.razor index 2ba687d..f6f5279 100644 --- a/src/LudikZoneBlazor/Components/Pages/Home.razor +++ b/src/LudikZoneBlazor/Components/Pages/Home.razor @@ -39,11 +39,16 @@
Prerendering - If you're exploring the features of .NET 8 Blazor,
you might be pleasantly surprised to learn that each page is prerendered on the server,
regardless of the selected render mode.

- This means that you'll need to inject all necessary services on the server,
even when opting for the wasm (WebAssembly) render mode.

- This prerendering functionality is crucial to ensuring that WebAssembly mode feels fast and responsive,
especially when it comes to initial page load times.

- For more information on how to detect prerendering and leverage the RenderContext, you can refer to the following link: - + If you're exploring the features of .NET 8 Blazor,
you might be pleasantly surprised to learn that each page + is prerendered on the server,
regardless of the selected render mode.

+ This means that you'll need to inject all necessary services on the server,
even when opting for the wasm + (WebAssembly) render mode.

+ This prerendering functionality is crucial to ensuring that WebAssembly mode feels fast and responsive,
+ especially when it comes to initial page load times.

+ For more information on how to detect prerendering and leverage the RenderContext, you can refer to the following + link: + More details
@@ -52,7 +57,8 @@ InteractiveAuto A discussion on how to achieve this can be found here: - + More details \ No newline at end of file diff --git a/src/LudikZoneBlazor/Components/Pages/Weather.razor b/src/LudikZoneBlazor/Components/Pages/Weather.razor deleted file mode 100644 index 3ffd7d8..0000000 --- a/src/LudikZoneBlazor/Components/Pages/Weather.razor +++ /dev/null @@ -1,60 +0,0 @@ -@page "/weather" - - - -Weather - -Weather forecast -This component demonstrates fetching data from the server. - -@if (forecasts == null) -{ - -} -else -{ - - - Date - Temp. (C) - Temp. (F) - Summary - - - @context.Date - @context.TemperatureC - @context.TemperatureF - @context.Summary - - - - - -} - -@code { - private WeatherForecast[]? forecasts; - - protected override async Task OnInitializedAsync() - { - // Simulate asynchronous loading to demonstrate a loading indicator - await Task.Delay(500); - - var startDate = DateOnly.FromDateTime(DateTime.Now); - var summaries = new[] { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" }; - forecasts = Enumerable.Range(1, 5).Select(index => new WeatherForecast - { - Date = startDate.AddDays(index), - TemperatureC = Random.Shared.Next(-20, 55), - Summary = summaries[Random.Shared.Next(summaries.Length)] - }).ToArray(); - } - - private class WeatherForecast - { - public DateOnly Date { get; set; } - public int TemperatureC { get; set; } - public string? Summary { get; set; } - public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); - } -} diff --git a/src/LudikZoneBlazor/Components/_Imports.razor b/src/LudikZoneBlazor/Components/_Imports.razor index 0f22753..bc93005 100644 --- a/src/LudikZoneBlazor/Components/_Imports.razor +++ b/src/LudikZoneBlazor/Components/_Imports.razor @@ -12,3 +12,4 @@ @using MudBlazor.Services @using LudikZoneBlazor @using LudikZoneBlazor.Components +@using Microsoft.Extensions.Localization \ No newline at end of file diff --git a/src/LudikZoneBlazor/Data/ApplicationDbContext.cs b/src/LudikZoneBlazor/Data/ApplicationDbContext.cs index 6ad52a7..60a9042 100644 --- a/src/LudikZoneBlazor/Data/ApplicationDbContext.cs +++ b/src/LudikZoneBlazor/Data/ApplicationDbContext.cs @@ -1,8 +1,17 @@ +using LudikZoneBlazor.Data.Model; using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; namespace LudikZoneBlazor.Data; +/// +/// Le context BDD de l'application +/// +/// + public class ApplicationDbContext(DbContextOptions options) : IdentityDbContext(options) { + + /// Tout les jeux existant sur la plateforme + public virtual DbSet Games => Set(); } diff --git a/src/LudikZoneBlazor/Data/ApplicationUser.cs b/src/LudikZoneBlazor/Data/ApplicationUser.cs index 24f0d2f..18a9a3c 100644 --- a/src/LudikZoneBlazor/Data/ApplicationUser.cs +++ b/src/LudikZoneBlazor/Data/ApplicationUser.cs @@ -2,7 +2,7 @@ using Microsoft.AspNetCore.Identity; namespace LudikZoneBlazor.Data; -// Add profile data for application users by adding properties to the ApplicationUser class +/// Add profile data for application users by adding properties to the ApplicationUser class public class ApplicationUser : IdentityUser { } diff --git a/src/LudikZoneBlazor/Data/Model/Game.cs b/src/LudikZoneBlazor/Data/Model/Game.cs new file mode 100644 index 0000000..15479bf --- /dev/null +++ b/src/LudikZoneBlazor/Data/Model/Game.cs @@ -0,0 +1,11 @@ +namespace LudikZoneBlazor.Data.Model; + +/// +/// Informations relative a un jeu précis +/// +public class Game +{ + + /// Identifiant unique d'un jeu + public int Id { get; init; } +} \ No newline at end of file diff --git a/src/LudikZoneBlazor/LudikZoneBlazor.csproj b/src/LudikZoneBlazor/LudikZoneBlazor.csproj index 7e31e71..898045d 100644 --- a/src/LudikZoneBlazor/LudikZoneBlazor.csproj +++ b/src/LudikZoneBlazor/LudikZoneBlazor.csproj @@ -4,6 +4,11 @@ net8.0 enable enable + true + true + true + All + aspnet-LudikZoneBlazor-b18f2a2e-d082-4232-8521-6cacaa2a4ba2 diff --git a/src/LudikZoneBlazor/Migrations/20240906111336_Init.cs b/src/LudikZoneBlazor/Migrations/20240906111336_Init.cs index e02f275..b03b11f 100644 --- a/src/LudikZoneBlazor/Migrations/20240906111336_Init.cs +++ b/src/LudikZoneBlazor/Migrations/20240906111336_Init.cs @@ -1,5 +1,4 @@ -using System; -using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Migrations; using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; #nullable disable diff --git a/src/LudikZoneBlazor/Program.cs b/src/LudikZoneBlazor/Program.cs index bb9b649..ecbc262 100644 --- a/src/LudikZoneBlazor/Program.cs +++ b/src/LudikZoneBlazor/Program.cs @@ -49,6 +49,10 @@ builder.Services.AddIdentityCore(options => options.SignIn.Requ builder.Services.AddSingleton, IdentityNoOpEmailSender>(); +// Begin I18N configuration +builder.Services.AddLocalization(opts => { opts.ResourcesPath = "Resources"; }); +// End I18N configuration + var app = builder.Build(); // Configure the HTTP request pipeline. @@ -74,6 +78,17 @@ app.MapRazorComponents() // Add additional endpoints required by the Identity /Account Razor components. app.MapAdditionalIdentityEndpoints(); +// Begin I18N configuration +var supportedCultures = new[] { "fr", "en" }; + +var localizationOptions = new RequestLocalizationOptions() + .SetDefaultCulture(supportedCultures[0]) + .AddSupportedCultures(supportedCultures) + .AddSupportedUICultures(supportedCultures); + +app.UseRequestLocalization(localizationOptions); +// End I18N configuration + // Appliquer les migrations automatiquement lors du démarrage using (var scope = app.Services.CreateScope()) { diff --git a/src/LudikZoneBlazor/Resources/Components.Pages.Games.en.resx b/src/LudikZoneBlazor/Resources/Components.Pages.Games.en.resx new file mode 100644 index 0000000..dec1410 --- /dev/null +++ b/src/LudikZoneBlazor/Resources/Components.Pages.Games.en.resx @@ -0,0 +1,128 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Games anglais 4 + comment game + + + Game list + title game list + + \ No newline at end of file diff --git a/src/LudikZoneBlazor/Resources/Components.Pages.Games.fr.resx b/src/LudikZoneBlazor/Resources/Components.Pages.Games.fr.resx new file mode 100644 index 0000000..1ac1cff --- /dev/null +++ b/src/LudikZoneBlazor/Resources/Components.Pages.Games.fr.resx @@ -0,0 +1,128 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Games francais 4 + comment game + + + Liste des jeux + Titre de la liste des jeux + + \ No newline at end of file -- 2.49.1