arthur_menu #16

Merged
arthur.valin merged 3 commits from arthur_menu into master 2 years ago

@ -1,4 +1,5 @@
<CascadingBlazoredModal>
@using CraftSharp.Models;
<CascadingBlazoredModal>
<Router AppAssembly="@typeof(App).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(CraftLayout)" />
@ -6,10 +7,12 @@
</Found>
<NotFound>
<CascadingAuthenticationState>
<LayoutView Layout="@typeof(CraftLayout)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
<LayoutView Layout="@typeof(CraftLayout)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
</CascadingAuthenticationState>
</NotFound>
</Router>
</CascadingBlazoredModal>
</CascadingBlazoredModal>

@ -7,7 +7,7 @@
public string UserName { get; set; }
public int NumberOfKeys { get; set; } = 0;
public List<Item> Inventory { get; set; } = new List<Item>();
public List<UserRoles> Roles { get; set; } = new List<UserRoles>() { UserRoles.User };
}
}

@ -10,7 +10,7 @@
<DataAnnotationsValidator />
<label for="inputUsername" class="sr-only">Pseudonyme</label>
<InputText id="inputUsername" class="form-control" @bind-Value="loginRequest.UserName" autofocus placeholder="Entrez votre pseudonyme" />
<InputText id="inputUsername" class="form-control" @bind-Value="loginRequest.UserName" autofocus placeholder="Entrez votre pseudonyme" />
<ValidationMessage For="@(() => loginRequest.UserName)" />
<br />

@ -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();

@ -2,6 +2,8 @@
using Microsoft.Extensions.Localization;
using CraftSharp.Models;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using CraftSharp.Services;
namespace CraftSharp.Pages
{

@ -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<AuthenticationState> 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()

@ -91,7 +91,7 @@
align-items:center;
position:relative;
height: 500px;
margin-top: 10%;
}
.chest {

@ -4,6 +4,5 @@
@{
Layout = "_Layout";
}
<component type="typeof(App)" render-mode="ServerPrerendered" />
<component type="typeof(App)" render-mode="ServerPrerendered"/>

@ -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<RequestLocalizationOptions>(options =>
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{

@ -12,10 +12,10 @@ namespace CraftSharp.Services
{
CurrentUser = new List<AppUser>
{
new AppUser { UserName = "Admin", Password = "123456", Roles = new List<UserRoles> { UserRoles.Admin }, numberOfKeys=5 }
new AppUser { UserName = "Admin", Password = "123456", Roles = new List<UserRoles> { 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)
};
}

@ -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());
}
private CurrentUser GetCurrentUser()
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();
}
}

@ -4,8 +4,6 @@ namespace CraftSharp.Services
{
public interface IAuthService
{
AppUser GetCurrentUser(string userName);
CurrentUser GetUser(string userName);
void Login(ConnexionModel loginRequest);

@ -13,6 +13,7 @@
<div class="px-4 culture">
<CultureSelector/>
</div>
</div>
</div>

@ -11,34 +11,4 @@
}
</select>
</label>
</p>
@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);
}
}
}
</p>

@ -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);
}
}
}
}

@ -15,18 +15,17 @@
</NavLink>
</li>
<li class="nav-item">
<NavLink class="nav-link" href="list">
<NavLink class="nav-link" href="shop">
<span class="topbar_item">
<img src="/Images/shop_icon.png" class="topbar_itemimg"/> @Localizer["Shop"]
</span>
</NavLink>
</li>
<span class="img_wrap">
<a class="img_wrap" href="/index">
<img class="nav-image" src="Images/CraftSharp-Logo.png" alt="Logo Application"/>
<img class="nav-image" src="/Images/@(Localizer["subtext-img"]).png" width="70%" />
</span>
</a>
<li class="nav-item">
<NavLink class="nav-link" href="opening">
@ -35,9 +34,26 @@
</span>
</NavLink>
</li>
<button type="button" class="btn btn-link ml-md-auto" @onclick="@LogoutClick">Logout</button>
@if (isUserAdmin)
{
<li class="nav-item">
<NavLink class="nav-link" href="list">
<span class="topbar_item">
<img src="/Images/admin_icon.png" class="topbar_itemimg" /> Admin
</span>
</NavLink>
</li>
}
else
{
<li class="nav-item">
<span class="topbar_item" style="visibility: hidden">
</span>
</li>
}
</ul>
</div>
<button type="button" class="btn btn-link ml-md-auto" @onclick="@LogoutClick">Logout</button>
</div>
</nav>
</div>

@ -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;
@ -20,6 +21,13 @@ namespace CraftSharp.Shared
[CascadingParameter]
private Task<AuthenticationState> AuthenticationState { get; set; }
private bool isUserAdmin = false;
protected override async Task OnInitializedAsync()
{
isAdmin();
}
void goInscription()
{
NavigationManager.NavigateTo("inscription");
@ -30,14 +38,11 @@ namespace CraftSharp.Shared
NavigationManager.NavigateTo("connexion");
}
/* protected override async Task OnParametersSetAsync()
async public void isAdmin()
{
if (!(await AuthenticationState).User.Identity.IsAuthenticated)
{
NavigationManager.NavigateTo("/inscription");
}
}*/
var roles = AuthStateProvider.GetCurrentUser().Roles;
isUserAdmin = roles.Contains(UserRoles.Admin);
}
private async Task LogoutClick()
{
await AuthStateProvider.Logout();

@ -45,8 +45,6 @@
position: absolute;
left: 75%;
transform: translate(-50%, 0);
user-select: none;
pointer-events: none;
}
.nav-image {

Loading…
Cancel
Save