#region ========================================================================= USING ===================================================================================== using Lumina.Presentation.Web.Common.Authorization; using Lumina.Presentation.Web.Common.DTO.Themes; using Lumina.Presentation.Web.Common.Enums.Authorization; using Lumina.Presentation.Web.Common.Routes; using Lumina.Presentation.Web.Common.Services; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Localization; using System; using System.Collections.Generic; using System.Security.Claims; using System.Threading; using System.Threading.Tasks; #endregion namespace Lumina.Presentation.Web.Core.Themes; /// /// Builds the navigation menu model for the themed navigation template, resolving the authorization conditions and localized labels server side. /// public sealed class ThemeNavBuilder { // The resource base name deliberately excludes the "Core.Resources" segment: the localization factory re-roots it with the configured ResourcesPath. private const string VIEW_RESOURCE_BASE_NAME = "Lumina.Presentation.Web.Views.Shared._NavMenu"; private const string VIEW_RESOURCE_LOCATION = "Lumina.Presentation.Web"; private readonly IUrlService _urlService; private readonly IStringLocalizerFactory _stringLocalizerFactory; private readonly IAuthorizationService _authorizationService; private readonly IHttpContextAccessor _httpContextAccessor; private readonly IStringLocalizer _localizer; /// /// Initializes a new instance of the class. /// /// Injected service for generating URLs from action and controller names, with localization. /// Injected factory used to create the localizer of the navigation resources. /// Injected service used to resolve the roles and permissions of the current user. /// Injected accessor for the current HTTP context. public ThemeNavBuilder(IUrlService urlService, IStringLocalizerFactory stringLocalizerFactory, IAuthorizationService authorizationService, IHttpContextAccessor httpContextAccessor) { _urlService = urlService; _stringLocalizerFactory = stringLocalizerFactory; _authorizationService = authorizationService; _httpContextAccessor = httpContextAccessor; _localizer = stringLocalizerFactory.Create(VIEW_RESOURCE_BASE_NAME, VIEW_RESOURCE_LOCATION); } /// /// Builds the navigation menu model for the current user and request. /// /// Cancellation token that can be used to stop the execution. /// The navigation menu model, with the sections visible to the current user. public async Task BuildAsync(CancellationToken cancellationToken) { ClaimsPrincipal user = _httpContextAccessor.HttpContext?.User ?? new ClaimsPrincipal(); bool isAuthenticated = user.Identity?.IsAuthenticated == true; bool isAdmin = isAuthenticated && await _authorizationService.IsInRoleAsync("Admin", cancellationToken).ConfigureAwait(false); bool canCreateLibraries = isAuthenticated && (isAdmin || await _authorizationService.HasPermissionAsync(AuthorizationPermission.CanCreateLibraries, cancellationToken).ConfigureAwait(false)); string returnUrl = Uri.EscapeDataString(GetCurrentRequestUrl()); ThemeNavEntryDto[] languages = CreateLanguageEntries(returnUrl); // The mobile and desktop menus arrange the same sections differently, notably the language switcher, which is // its own mobile heading but a submenu under Tools on the desktop, so each menu gets its own section list. List mobileSections = []; mobileSections.Add(CreateSiteSection()); mobileSections.Add(CreateAccountSection(isAuthenticated)); if (isAdmin) mobileSections.Add(CreateAdministratorSection()); if (isAuthenticated) mobileSections.Add(CreateLibrarySection(canCreateLibraries)); mobileSections.Add(new ThemeNavSectionDto(Localize("Language"), languages)); if (isAuthenticated) mobileSections.Add(CreateToolsSection(isAuthenticated, languages: [])); mobileSections.Add(CreateHelpSection()); List menubarSections = []; menubarSections.Add(CreateSiteSection()); menubarSections.Add(CreateAccountSection(isAuthenticated)); if (isAdmin) menubarSections.Add(CreateAdministratorSection()); if (isAuthenticated) menubarSections.Add(CreateLibrarySection(canCreateLibraries)); menubarSections.Add(CreateToolsSection(isAuthenticated, languages)); menubarSections.Add(CreateHelpSection()); return new ThemeNavMenuDto("Lumina", mobileSections, menubarSections); } /// /// Creates the site section, which links to the home page. /// /// The site section of the navigation menu. private ThemeNavSectionDto CreateSiteSection() { return new ThemeNavSectionDto( "Lumina", [new ThemeNavEntryDto(Localize("Home"), _urlService.GetAbsoluteUrl(WebRoutes.Home.INDEX_CULTURED), "nav-link", [])]); } /// /// Creates the account section, whose entries depend on the authentication state of the user. /// /// Whether the current user is authenticated. /// The account section of the navigation menu. private ThemeNavSectionDto CreateAccountSection(bool isAuthenticated) { List items = []; if (isAuthenticated) { items.Add(new ThemeNavEntryDto(Localize("Logout"), _urlService.GetAbsoluteUrl(WebRoutes.Authentication.LOGOUT), CssClass: null, Children: [])); items.Add(new ThemeNavEntryDto(Localize("Register"), _urlService.GetAbsoluteUrl(WebRoutes.Authentication.REGISTER_VIEW), "nav-link", [])); items.Add(new ThemeNavEntryDto(Localize("ChangePassword"), _urlService.GetAbsoluteUrl(WebRoutes.Authentication.CHANGE_PASSWORD_VIEW), "nav-link", [])); items.Add(new ThemeNavEntryDto(Localize("Profile"), _urlService.GetAbsoluteUrl(WebRoutes.Authentication.PROFILE_VIEW), "nav-link", [])); } else { items.Add(new ThemeNavEntryDto(Localize("Login"), _urlService.GetAbsoluteUrl(WebRoutes.Authentication.LOGIN_VIEW), CssClass: null, Children: [])); items.Add(new ThemeNavEntryDto(Localize("RecoverPassword"), _urlService.GetAbsoluteUrl(WebRoutes.Authentication.RECOVER_PASSWORD_VIEW), CssClass: null, Children: [])); } return new ThemeNavSectionDto(Localize("Account"), items); } /// /// Creates the administrator section, shown only to administrators. /// /// The administrator section of the navigation menu. private ThemeNavSectionDto CreateAdministratorSection() { return new ThemeNavSectionDto( Localize("Administrator"), [ new ThemeNavEntryDto(Localize("ManageRoles"), _urlService.GetAbsoluteUrl(WebRoutes.Admin.MANAGE_ROLES), "nav-link", []), new ThemeNavEntryDto(Localize("ManagePermissions"), _urlService.GetAbsoluteUrl(WebRoutes.Admin.MANAGE_PERMISSIONS), "nav-link", []), new ThemeNavEntryDto(Localize("Plugins"), _urlService.GetAbsoluteUrl(WebRoutes.Plugins.INDEX), "nav-link", []), new ThemeNavEntryDto(Localize("Themes"), _urlService.GetAbsoluteUrl(WebRoutes.Admin.MANAGE_THEMES), "nav-link", []), new ThemeNavEntryDto(Localize("ScheduledJobs"), _urlService.GetAbsoluteUrl(WebRoutes.Scheduler.INDEX), "nav-link", []) ]); } /// /// Creates the library section, shown only to authenticated users. /// /// Whether the current user can create media libraries. /// The library section of the navigation menu. private ThemeNavSectionDto CreateLibrarySection(bool canCreateLibraries) { List items = [ new ThemeNavEntryDto(Localize("ManageLibraries"), _urlService.GetAbsoluteUrl(WebRoutes.LibraryManagement.INDEX), "nav-link", []) ]; if (canCreateLibraries) items.Add(new ThemeNavEntryDto(Localize("AddNewLibrary"), _urlService.GetAbsoluteUrl(WebRoutes.LibraryManagement.ADD_LIBRARY), "nav-link", [])); items.Add(new ThemeNavEntryDto(Localize("WrittenContent"), Url: null, CssClass: null, [ new ThemeNavEntryDto(Localize("Books"), "library/written-content-library/books-library/books", "nav-link", []), new ThemeNavEntryDto(Localize("Magazines"), "library/written-content/magazines", "nav-link", []) ])); items.Add(new ThemeNavEntryDto(Localize("Audio"), Url: null, CssClass: null, [ new ThemeNavEntryDto(Localize("Music"), "library/audio/music", "nav-link", []), new ThemeNavEntryDto(Localize("Interviews"), "library/audio/interviews", "nav-link", []) ])); items.Add(new ThemeNavEntryDto(Localize("Video"), Url: null, CssClass: null, [ new ThemeNavEntryDto(Localize("Movies"), "library/video/movies", "nav-link", []), new ThemeNavEntryDto(Localize("TvShows"), "library/video/tvshows", "nav-link", []) ])); return new ThemeNavSectionDto(Localize("Library"), items); } /// /// Creates the tools section, with the language submenu and the settings entry. /// /// Whether the current user is authenticated. /// The language entries of the submenu. /// The tools section of the navigation menu. private ThemeNavSectionDto CreateToolsSection(bool isAuthenticated, IReadOnlyList languages) { List items = []; if (languages.Count > 0) items.Add(new ThemeNavEntryDto(Localize("Language"), Url: null, CssClass: null, Children: languages)); if (isAuthenticated) items.Add(new ThemeNavEntryDto(Localize("Settings"), _urlService.GetAbsoluteUrl(WebRoutes.Settings.INDEX), "nav-link", [])); return new ThemeNavSectionDto(Localize("Tools"), items); } /// /// Creates the help section. /// /// The help section of the navigation menu. private ThemeNavSectionDto CreateHelpSection() { return new ThemeNavSectionDto( Localize("Help"), [ new ThemeNavEntryDto(Localize("Documentation"), "help/documentation", "nav-link", []), new ThemeNavEntryDto(Localize("About"), "help/about", "nav-link", []) ]); } /// /// Creates the language entries of the navigation menu, each keeping the current request URL as the return URL. /// /// The encoded current request URL used as the return URL. /// The language entries of the navigation menu. private ThemeNavEntryDto[] CreateLanguageEntries(string returnUrl) { string baseUrl = _urlService.GetAbsoluteUrl(WebRoutes.Language.SET_LANGUAGE) ?? string.Empty; (string Culture, string DisplayName)[] cultures = [ ("ar-sa", "العربية"), ("zh-cn", "简体中文"), ("de-de", "Deutsch"), ("en-us", "English"), ("es-es", "Español"), ("fr-fr", "Français"), ("it-it", "Italiano"), ("ja-jp", "日本語"), ("ro-ro", "Română"), ("uk-ua", "Українська") ]; ThemeNavEntryDto[] entries = new ThemeNavEntryDto[cultures.Length]; for (int index = 0; index < cultures.Length; index++) { (string culture, string displayName) = cultures[index]; entries[index] = new ThemeNavEntryDto(displayName, $"{baseUrl}?newCulture={culture}&returnUrl={returnUrl}", "lang-set", []); } return entries; } /// /// Gets the path and query string of the current request. /// /// The path and query string of the current request. private string GetCurrentRequestUrl() { HttpRequest request = _httpContextAccessor.HttpContext?.Request ?? throw new InvalidOperationException("The current HTTP context is unavailable."); return $"{request.Path}{request.QueryString}"; } /// /// Resolves a localized navigation label. /// /// The resource key of the label. /// The localized label. private string Localize(string key) { return _localizer[key].Value; } }