From 905db681619d66c621eb4c277d165244b76e3d44 Mon Sep 17 00:00:00 2001 From: "arthur.valin" Date: Wed, 28 Dec 2022 21:02:35 +0100 Subject: [PATCH] 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)