From 905db681619d66c621eb4c277d165244b76e3d44 Mon Sep 17 00:00:00 2001 From: "arthur.valin" Date: Wed, 28 Dec 2022 21:02:35 +0100 Subject: [PATCH 1/3] Adding admin option to the menu --- src/CraftSharp/Services/AuthService.cs | 2 +- src/CraftSharp/Shared/CraftLayout.razor | 1 + src/CraftSharp/Shared/HeaderLayout.razor | 22 ++++++++++++++++++--- src/CraftSharp/Shared/HeaderLayout.razor.cs | 20 ++++++++++++++++++- 4 files changed, 40 insertions(+), 5 deletions(-) diff --git a/src/CraftSharp/Services/AuthService.cs b/src/CraftSharp/Services/AuthService.cs index 5b2c225..c975fc2 100644 --- a/src/CraftSharp/Services/AuthService.cs +++ b/src/CraftSharp/Services/AuthService.cs @@ -12,7 +12,7 @@ namespace CraftSharp.Services { CurrentUser = new List { - new AppUser { UserName = "Admin", Password = "123456", Roles = new List { UserRoles.Admin }, numberOfKeys=5 } + new AppUser { UserName = "Admin", Password = "123456", Roles = new List { UserRoles.Admin }, numberOfKeys=999 } }; } public AppUser GetCurrentUser(string userName) diff --git a/src/CraftSharp/Shared/CraftLayout.razor b/src/CraftSharp/Shared/CraftLayout.razor index e85193d..b8613a5 100644 --- a/src/CraftSharp/Shared/CraftLayout.razor +++ b/src/CraftSharp/Shared/CraftLayout.razor @@ -13,6 +13,7 @@
+ diff --git a/src/CraftSharp/Shared/HeaderLayout.razor b/src/CraftSharp/Shared/HeaderLayout.razor index 24fcf4e..a145f58 100644 --- a/src/CraftSharp/Shared/HeaderLayout.razor +++ b/src/CraftSharp/Shared/HeaderLayout.razor @@ -15,9 +15,8 @@ - - + @if (isUserAdmin) + { + + } + else + { + + } + diff --git a/src/CraftSharp/Shared/HeaderLayout.razor.cs b/src/CraftSharp/Shared/HeaderLayout.razor.cs index 7ba3cc3..c4ed708 100644 --- a/src/CraftSharp/Shared/HeaderLayout.razor.cs +++ b/src/CraftSharp/Shared/HeaderLayout.razor.cs @@ -1,4 +1,5 @@ -using CraftSharp.Services; +using CraftSharp.Models; +using CraftSharp.Services; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Authorization; using Microsoft.Extensions.Localization; @@ -11,6 +12,9 @@ namespace CraftSharp.Shared [Inject] public IStringLocalizer Localizer { get; set; } + [Inject] + public IAuthService AuthService { get; set; } + [Inject] public CustomStateProvider AuthStateProvider { get; set; } @@ -20,6 +24,13 @@ namespace CraftSharp.Shared [CascadingParameter] private Task AuthenticationState { get; set; } + private bool isUserAdmin = false; + + protected override async Task OnInitializedAsync() + { + isAdmin(); + } + void goInscription() { NavigationManager.NavigateTo("inscription"); @@ -30,6 +41,13 @@ namespace CraftSharp.Shared NavigationManager.NavigateTo("connexion"); } + async public void isAdmin() + { + var authState = await AuthenticationState; + var roles = AuthService.GetCurrentUser(authState.User.Identity.Name).Roles; + isUserAdmin = roles.Contains(UserRoles.Admin); + } + /* protected override async Task OnParametersSetAsync() { if (!(await AuthenticationState).User.Identity.IsAuthenticated) From 7883166abedda838b5e091c477a679551212d76e Mon Sep 17 00:00:00 2001 From: "arthur.valin" Date: Wed, 28 Dec 2022 21:18:52 +0100 Subject: [PATCH 2/3] Correcting bug with GetCurrentUser function --- src/CraftSharp/Models/CurrentUser.cs | 2 +- src/CraftSharp/Pages/Opening.razor.cs | 4 ++-- src/CraftSharp/Services/AuthService.cs | 5 +++-- src/CraftSharp/Services/CustomStateProvider.cs | 2 +- src/CraftSharp/Services/IAuthService.cs | 2 -- src/CraftSharp/Shared/HeaderLayout.razor.cs | 14 +------------- 6 files changed, 8 insertions(+), 21 deletions(-) diff --git a/src/CraftSharp/Models/CurrentUser.cs b/src/CraftSharp/Models/CurrentUser.cs index 1e0c0a8..94c2718 100644 --- a/src/CraftSharp/Models/CurrentUser.cs +++ b/src/CraftSharp/Models/CurrentUser.cs @@ -7,7 +7,7 @@ public string UserName { get; set; } public int NumberOfKeys { get; set; } = 0; public List Inventory { get; set; } = new List(); - + public List Roles { get; set; } = new List() { UserRoles.User }; } } diff --git a/src/CraftSharp/Pages/Opening.razor.cs b/src/CraftSharp/Pages/Opening.razor.cs index 540b8d0..a9c132b 100644 --- a/src/CraftSharp/Pages/Opening.razor.cs +++ b/src/CraftSharp/Pages/Opening.razor.cs @@ -25,7 +25,7 @@ namespace CraftSharp.Pages public IDataService DataService { get; set; } [Inject] - public IAuthService AuthService { get; set; } + public CustomStateProvider AuthService { get; set; } [CascadingParameter] public Task Context { get; set; } @@ -46,7 +46,7 @@ namespace CraftSharp.Pages items = await DataService.List(0, totalItem); var authState = await Context; - NumberOfKeys = AuthService.GetCurrentUser(authState.User.Identity.Name).numberOfKeys; + NumberOfKeys = AuthService.GetCurrentUser().NumberOfKeys; } bool canOpen() diff --git a/src/CraftSharp/Services/AuthService.cs b/src/CraftSharp/Services/AuthService.cs index c975fc2..50c2c55 100644 --- a/src/CraftSharp/Services/AuthService.cs +++ b/src/CraftSharp/Services/AuthService.cs @@ -15,7 +15,7 @@ namespace CraftSharp.Services new AppUser { UserName = "Admin", Password = "123456", Roles = new List { UserRoles.Admin }, numberOfKeys=999 } }; } - public AppUser GetCurrentUser(string userName) +/* public AppUser GetCurrentUser(string userName) { var user = CurrentUser.FirstOrDefault(w => w.UserName == userName); @@ -25,7 +25,7 @@ namespace CraftSharp.Services } return user; - } + }*/ public CurrentUser GetUser(string userName) @@ -46,6 +46,7 @@ namespace CraftSharp.Services UserName = user.UserName, NumberOfKeys = user.numberOfKeys, Inventory = user.inventory, + Roles = user.Roles, Claims = claims.ToDictionary(c => c.Type, c => c.Value) }; } diff --git a/src/CraftSharp/Services/CustomStateProvider.cs b/src/CraftSharp/Services/CustomStateProvider.cs index 7270a67..59abbd2 100644 --- a/src/CraftSharp/Services/CustomStateProvider.cs +++ b/src/CraftSharp/Services/CustomStateProvider.cs @@ -62,7 +62,7 @@ namespace CraftSharp.Services NotifyAuthenticationStateChanged(GetAuthenticationStateAsync()); } - private CurrentUser GetCurrentUser() + public CurrentUser GetCurrentUser() { if (_currentUser != null && _currentUser.IsAuthenticated) { diff --git a/src/CraftSharp/Services/IAuthService.cs b/src/CraftSharp/Services/IAuthService.cs index 99a9140..255eb81 100644 --- a/src/CraftSharp/Services/IAuthService.cs +++ b/src/CraftSharp/Services/IAuthService.cs @@ -4,8 +4,6 @@ namespace CraftSharp.Services { public interface IAuthService { - AppUser GetCurrentUser(string userName); - CurrentUser GetUser(string userName); void Login(ConnexionModel loginRequest); diff --git a/src/CraftSharp/Shared/HeaderLayout.razor.cs b/src/CraftSharp/Shared/HeaderLayout.razor.cs index c4ed708..468525a 100644 --- a/src/CraftSharp/Shared/HeaderLayout.razor.cs +++ b/src/CraftSharp/Shared/HeaderLayout.razor.cs @@ -12,9 +12,6 @@ namespace CraftSharp.Shared [Inject] public IStringLocalizer Localizer { get; set; } - [Inject] - public IAuthService AuthService { get; set; } - [Inject] public CustomStateProvider AuthStateProvider { get; set; } @@ -44,18 +41,9 @@ namespace CraftSharp.Shared async public void isAdmin() { var authState = await AuthenticationState; - var roles = AuthService.GetCurrentUser(authState.User.Identity.Name).Roles; + var roles = AuthStateProvider.GetCurrentUser().Roles; isUserAdmin = roles.Contains(UserRoles.Admin); } - - /* protected override async Task OnParametersSetAsync() - { - if (!(await AuthenticationState).User.Identity.IsAuthenticated) - { - NavigationManager.NavigateTo("/inscription"); - } - }*/ - private async Task LogoutClick() { await AuthStateProvider.Logout(); From 3b96b4498921d7a421ed3f3fcce75bbc1c83b459 Mon Sep 17 00:00:00 2001 From: "arthur.valin" Date: Thu, 29 Dec 2022 21:59:14 +0100 Subject: [PATCH 3/3] Misc --- src/CraftSharp/App.razor | 13 ++++--- src/CraftSharp/Pages/Connexion.razor | 2 +- src/CraftSharp/Pages/Connexion.razor.cs | 3 ++ src/CraftSharp/Pages/Index.razor.cs | 2 ++ src/CraftSharp/Pages/Opening.razor.css | 2 +- src/CraftSharp/Pages/_Host.cshtml | 3 +- src/CraftSharp/Program.cs | 4 ++- .../Services/CustomStateProvider.cs | 34 +++++++++++------- src/CraftSharp/Shared/CultureSelector.razor | 32 +---------------- .../Shared/CultureSelector.razor.cs | 36 +++++++++++++++++++ src/CraftSharp/Shared/HeaderLayout.razor | 4 +-- src/CraftSharp/Shared/HeaderLayout.razor.cs | 1 - src/CraftSharp/Shared/HeaderLayout.razor.css | 2 -- 13 files changed, 80 insertions(+), 58 deletions(-) create mode 100644 src/CraftSharp/Shared/CultureSelector.razor.cs diff --git a/src/CraftSharp/App.razor b/src/CraftSharp/App.razor index 33c8548..79d057a 100644 --- a/src/CraftSharp/App.razor +++ b/src/CraftSharp/App.razor @@ -1,4 +1,5 @@ - +@using CraftSharp.Models; + @@ -6,10 +7,12 @@ - -

Sorry, there's nothing at this address.

-
+ +

Sorry, there's nothing at this address.

+
-
\ No newline at end of file +
+ + diff --git a/src/CraftSharp/Pages/Connexion.razor b/src/CraftSharp/Pages/Connexion.razor index 04e5809..43d4812 100644 --- a/src/CraftSharp/Pages/Connexion.razor +++ b/src/CraftSharp/Pages/Connexion.razor @@ -10,7 +10,7 @@ - +
diff --git a/src/CraftSharp/Pages/Connexion.razor.cs b/src/CraftSharp/Pages/Connexion.razor.cs index f7bd173..0d6c7e1 100644 --- a/src/CraftSharp/Pages/Connexion.razor.cs +++ b/src/CraftSharp/Pages/Connexion.razor.cs @@ -5,6 +5,8 @@ using Microsoft.Extensions.Logging; using Microsoft.AspNetCore.Components; using CraftSharp.Models; using CraftSharp.Services; +using Blazorise; +using Newtonsoft.Json; namespace CraftSharp.Pages { @@ -15,6 +17,7 @@ namespace CraftSharp.Pages [Inject] public NavigationManager NavigationManager { get; set; } + private string error { get; set; } private ConnexionModel loginRequest { get; set; } = new ConnexionModel(); diff --git a/src/CraftSharp/Pages/Index.razor.cs b/src/CraftSharp/Pages/Index.razor.cs index 46a0474..8c2e8fa 100644 --- a/src/CraftSharp/Pages/Index.razor.cs +++ b/src/CraftSharp/Pages/Index.razor.cs @@ -2,6 +2,8 @@ using Microsoft.Extensions.Localization; using CraftSharp.Models; using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using CraftSharp.Services; namespace CraftSharp.Pages { diff --git a/src/CraftSharp/Pages/Opening.razor.css b/src/CraftSharp/Pages/Opening.razor.css index 5a4f471..9dd2a89 100644 --- a/src/CraftSharp/Pages/Opening.razor.css +++ b/src/CraftSharp/Pages/Opening.razor.css @@ -91,7 +91,7 @@ align-items:center; position:relative; height: 500px; - + margin-top: 10%; } .chest { diff --git a/src/CraftSharp/Pages/_Host.cshtml b/src/CraftSharp/Pages/_Host.cshtml index 99f3fba..30a855b 100644 --- a/src/CraftSharp/Pages/_Host.cshtml +++ b/src/CraftSharp/Pages/_Host.cshtml @@ -4,6 +4,5 @@ @{ Layout = "_Layout"; } - - + diff --git a/src/CraftSharp/Program.cs b/src/CraftSharp/Program.cs index 47a1b7d..31efd19 100644 --- a/src/CraftSharp/Program.cs +++ b/src/CraftSharp/Program.cs @@ -15,6 +15,9 @@ using GraphQL.Client.Abstractions; using GraphQL.Client.Http; using GraphQL.Client.Serializer.Newtonsoft; using CraftSharp; +using Microsoft.AspNetCore.Mvc.Infrastructure; +using System; +using Microsoft.JSInterop; var builder = WebApplication.CreateBuilder(args); @@ -60,7 +63,6 @@ builder.Services.Configure(options => }); var app = builder.Build(); - // Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) { diff --git a/src/CraftSharp/Services/CustomStateProvider.cs b/src/CraftSharp/Services/CustomStateProvider.cs index 59abbd2..bedf535 100644 --- a/src/CraftSharp/Services/CustomStateProvider.cs +++ b/src/CraftSharp/Services/CustomStateProvider.cs @@ -1,13 +1,21 @@ using CraftSharp.Models; using Microsoft.AspNetCore.Components.Authorization; using System.Security.Claims; +using Microsoft.AspNetCore.Http; +using Newtonsoft.Json; +using Microsoft.AspNetCore.Components; +using Blazorise; +using Microsoft.JSInterop; +using Microsoft.Extensions.Caching.Memory; namespace CraftSharp.Services { public class CustomStateProvider : AuthenticationStateProvider { private readonly IAuthService _authService; - private CurrentUser _currentUser; + + [CascadingParameter] + public CurrentUser UserObject { get; set; } public CustomStateProvider(IAuthService authService) { @@ -22,7 +30,7 @@ namespace CraftSharp.Services var userInfo = GetCurrentUser(); if (userInfo.IsAuthenticated) { - var claims = new[] { new Claim(ClaimTypes.Name, _currentUser.UserName) }.Concat(_currentUser.Claims.Select(c => new Claim(c.Key, c.Value))); + var claims = new[] { new Claim(ClaimTypes.Name, UserObject.UserName) }.Concat(UserObject.Claims.Select(c => new Claim(c.Key, c.Value))); identity = new ClaimsIdentity(claims, "Server authentication"); } } @@ -37,17 +45,18 @@ namespace CraftSharp.Services public async Task Login(ConnexionModel loginParameters) { _authService.Login(loginParameters); - // No error - Login the user - var user = _authService.GetUser(loginParameters.UserName); - _currentUser = user; - + CurrentUser user; + user = _authService.GetUser(loginParameters.UserName); + UserObject = user; + Console.WriteLine("\t\tLOGIN: " + UserObject.UserName); NotifyAuthenticationStateChanged(GetAuthenticationStateAsync()); } public async Task Logout() { - _currentUser = null; + + UserObject = new CurrentUser(); NotifyAuthenticationStateChanged(GetAuthenticationStateAsync()); } @@ -57,18 +66,19 @@ namespace CraftSharp.Services // No error - Login the user var user = _authService.GetUser(registerParameters.UserName); - _currentUser = user; - + UserObject = user; NotifyAuthenticationStateChanged(GetAuthenticationStateAsync()); } public CurrentUser GetCurrentUser() { - if (_currentUser != null && _currentUser.IsAuthenticated) + CurrentUser cacheUser; + + if (UserObject != null && UserObject.IsAuthenticated) { - return _currentUser; + Console.WriteLine("Return user"); + return UserObject; } - return new CurrentUser(); } } diff --git a/src/CraftSharp/Shared/CultureSelector.razor b/src/CraftSharp/Shared/CultureSelector.razor index ddca085..73d4b6e 100644 --- a/src/CraftSharp/Shared/CultureSelector.razor +++ b/src/CraftSharp/Shared/CultureSelector.razor @@ -11,34 +11,4 @@ } -

- -@code -{ - private CultureInfo[] supportedCultures = new[] - { - new CultureInfo("en-US"), - new CultureInfo("fr-FR"), - new CultureInfo("tr-TR") - }; - - private CultureInfo Culture - { - get => CultureInfo.CurrentCulture; - set - { - if (CultureInfo.CurrentUICulture == value) - { - return; - } - - var culture = value.Name.ToLower(CultureInfo.InvariantCulture); - - var uri = new Uri(this.NavigationManager.Uri).GetComponents(UriComponents.PathAndQuery, UriFormat.Unescaped); - var query = $"?culture={Uri.EscapeDataString(culture)}&" + $"redirectUri={Uri.EscapeDataString(uri)}"; - - // Redirect the user to the culture controller to set the cookie - this.NavigationManager.NavigateTo("/Culture/SetCulture" + query, forceLoad: true); - } - } -} \ No newline at end of file +

\ No newline at end of file diff --git a/src/CraftSharp/Shared/CultureSelector.razor.cs b/src/CraftSharp/Shared/CultureSelector.razor.cs new file mode 100644 index 0000000..b343a6c --- /dev/null +++ b/src/CraftSharp/Shared/CultureSelector.razor.cs @@ -0,0 +1,36 @@ +using CraftSharp.Services; +using Microsoft.AspNetCore.Components; +using System.Globalization; + +namespace CraftSharp.Shared +{ + public partial class CultureSelector + { + private CultureInfo[] supportedCultures = new[] +{ + new CultureInfo("en-US"), + new CultureInfo("fr-FR"), + new CultureInfo("tr-TR") + }; + + private CultureInfo Culture + { + get => CultureInfo.CurrentCulture; + set + { + if (CultureInfo.CurrentUICulture == value) + { + return; + } + + var culture = value.Name.ToLower(CultureInfo.InvariantCulture); + + var uri = new Uri(this.NavigationManager.Uri).GetComponents(UriComponents.PathAndQuery, UriFormat.Unescaped); + var query = $"?culture={Uri.EscapeDataString(culture)}&" + $"redirectUri={Uri.EscapeDataString(uri)}"; + + // Redirect the user to the culture controller to set the cookie + this.NavigationManager.NavigateTo("/Culture/SetCulture" + query, forceLoad: true); + } + } + } +} diff --git a/src/CraftSharp/Shared/HeaderLayout.razor b/src/CraftSharp/Shared/HeaderLayout.razor index a145f58..b6bd972 100644 --- a/src/CraftSharp/Shared/HeaderLayout.razor +++ b/src/CraftSharp/Shared/HeaderLayout.razor @@ -22,10 +22,10 @@ - + Logo Application - +