@using System.Security.Claims @using Microsoft.AspNetCore.Authorization @using Microsoft.AspNetCore.Components.Authorization @using Moonlight.Client.Interfaces @using Moonlight.Client.Models @inherits MoonCore.Blazor.FlyonUi.Drawers.DrawerBase @inject NavigationManager Navigation @inject IEnumerable SidebarItemProviders @inject IAuthorizationService AuthorizationService @implements IDisposable @{ var url = new Uri(Navigation.Uri); }
Logo

Moonlight v2.1

@code { [CascadingParameter] public Task AuthState { get; set; } private Dictionary Items = new(); private string Username; private string Email; private ClaimsPrincipal Identity; protected override async Task OnInitializedAsync() { var authState = await AuthState; Identity = authState.User; Username = Identity.FindFirst(ClaimTypes.Name)!.Value; Email = Identity.FindFirst(ClaimTypes.Email)!.Value; var sidebarItems = new List(); foreach (var provider in SidebarItemProviders) provider.ModifySidebar(sidebarItems); var itemsToRemove = new List(); foreach (var sidebarItem in sidebarItems) { if(string.IsNullOrEmpty(sidebarItem.Policy)) continue; var authResult = await AuthorizationService.AuthorizeAsync(Identity, sidebarItem.Policy); if(authResult.Succeeded) continue; itemsToRemove.Add(sidebarItem); } foreach (var sidebarItem in itemsToRemove) sidebarItems.Remove(sidebarItem); Items = sidebarItems .GroupBy(x => x.Group ?? "") .OrderByDescending(x => string.IsNullOrEmpty(x.Key)) .ToDictionary(x => x.Key, x => x.OrderBy(y => y.Priority).ToArray()); Navigation.LocationChanged += OnNavigated; } private async void OnNavigated(object? sender, LocationChangedEventArgs e) { // No async void without try catch to prevent hard app crashes when async task fails try { await InvokeAsync(StateHasChanged); } catch (Exception) { // ignored } } private Task LogoutAsync() { Navigation.NavigateTo("/api/auth/logout", true); return Task.CompletedTask; } public void Dispose() { Navigation.LocationChanged -= OnNavigated; } }