mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-02 22:11:10 +00:00
Adds localization to Lumina.Presentation.Web project
This commit is contained in:
1 parent
771fa74991
commit
2c31fd8714
65 files changed
+1111
-319
No files matched your search
+5
-4
@@ -15,7 +15,7 @@ namespace Lumina.Presentation.Web.Controllers.FileSystemManagement;
|
||||
/// Controller for managing file system directories.
|
||||
/// </summary>
|
||||
[Authorize]
|
||||
[Route("[controller]")]
|
||||
[Route("directories")]
|
||||
public class DirectoriesController : Controller
|
||||
{
|
||||
private readonly IApiHttpClient _apiHttpClient;
|
||||
@@ -39,7 +39,7 @@ public class DirectoriesController : Controller
|
||||
public async Task<IActionResult> GetDirectories([FromQuery] string path, [FromQuery] bool includeHiddenElements, CancellationToken cancellationToken)
|
||||
{
|
||||
IEnumerable<DirectoryModel> response = await _apiHttpClient.GetAsync<DirectoryModel[]>(
|
||||
$"directories/get-directories?path={Uri.EscapeDataString(path)}&includeHiddenElements={includeHiddenElements}", cancellationToken);
|
||||
$"directories/get-directories?path={Uri.EscapeDataString(path)}&includeHiddenElements={includeHiddenElements}", cancellationToken).ConfigureAwait(false);
|
||||
return Json(new { success = true, data = response });
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ public class DirectoriesController : Controller
|
||||
public async Task<IActionResult> GetTreeDirectories([FromQuery] string path, [FromQuery] bool includeHiddenElements, CancellationToken cancellationToken)
|
||||
{
|
||||
IEnumerable<FileSystemTreeNodeModel> response = await _apiHttpClient.GetAsync<FileSystemTreeNodeModel[]>(
|
||||
$"directories/get-tree-directories?path={Uri.EscapeDataString(path)}&includeHiddenElements={includeHiddenElements}", cancellationToken);
|
||||
$"directories/get-tree-directories?path={Uri.EscapeDataString(path)}&includeHiddenElements={includeHiddenElements}", cancellationToken).ConfigureAwait(false);
|
||||
return Json(new { success = true, data = response });
|
||||
}
|
||||
|
||||
@@ -66,7 +66,8 @@ public class DirectoriesController : Controller
|
||||
[HttpGet("get-directory-tree")]
|
||||
public async Task<IActionResult> GetDirectoryTree([FromQuery] string path, [FromQuery] bool includeFiles, [FromQuery] bool includeHiddenElements, CancellationToken cancellationToken)
|
||||
{
|
||||
FileSystemTreeNodeModel[] response = await _apiHttpClient.GetAsync<FileSystemTreeNodeModel[]>($"directories/get-directory-tree?path={Uri.EscapeDataString(path)}&includeFiles={includeFiles}&includeHiddenElements={includeHiddenElements}", cancellationToken);
|
||||
FileSystemTreeNodeModel[] response = await _apiHttpClient.GetAsync<FileSystemTreeNodeModel[]>(
|
||||
$"directories/get-directory-tree?path={Uri.EscapeDataString(path)}&includeFiles={includeFiles}&includeHiddenElements={includeHiddenElements}", cancellationToken).ConfigureAwait(false);
|
||||
return Json(new { success = true, data = response });
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,7 @@ namespace Lumina.Presentation.Web.Controllers.FileSystemManagement;
|
||||
/// Controller for managing file system drives.
|
||||
/// </summary>
|
||||
[Authorize]
|
||||
[Route("[controller]")]
|
||||
[Route("drives")]
|
||||
public class DrivesController : Controller
|
||||
{
|
||||
private readonly IApiHttpClient _apiHttpClient;
|
||||
@@ -34,7 +34,7 @@ public class DrivesController : Controller
|
||||
[HttpGet("get-drives")]
|
||||
public async Task<IActionResult> GetDrives(CancellationToken cancellationToken)
|
||||
{
|
||||
FileSystemTreeNodeModel[] response = await _apiHttpClient.GetAsync<FileSystemTreeNodeModel[]>($"drives/get-drives", cancellationToken);
|
||||
FileSystemTreeNodeModel[] response = await _apiHttpClient.GetAsync<FileSystemTreeNodeModel[]>($"drives/get-drives", cancellationToken).ConfigureAwait(false);
|
||||
return Json(new { success = true, data = new { drives = response } });
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -13,7 +13,7 @@ namespace Lumina.Presentation.Web.Controllers.FileSystemManagement;
|
||||
/// Controller for managing file systems.
|
||||
/// </summary>
|
||||
[Authorize]
|
||||
[Route("/file-system")]
|
||||
[Route("file-system")]
|
||||
public class FileSystemController : Controller
|
||||
{
|
||||
private readonly IApiHttpClient _apiHttpClient;
|
||||
@@ -34,7 +34,7 @@ public class FileSystemController : Controller
|
||||
[HttpGet("get-type")]
|
||||
public async Task<IActionResult> GetType(CancellationToken cancellationToken)
|
||||
{
|
||||
FileSystemTypeModel response = await _apiHttpClient.GetAsync<FileSystemTypeModel>($"file-system/get-type", cancellationToken);
|
||||
FileSystemTypeModel response = await _apiHttpClient.GetAsync<FileSystemTypeModel>($"file-system/get-type", cancellationToken).ConfigureAwait(false);
|
||||
return Json(new { success = true, data = new { platformType = response.PlatformType.ToString() } });
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,7 @@ namespace Lumina.Presentation.Web.Controllers.FileSystemManagement;
|
||||
/// Controller for managing file system files.
|
||||
/// </summary>
|
||||
[Authorize]
|
||||
[Route("[controller]")]
|
||||
[Route("files")]
|
||||
public class FilesController : Controller
|
||||
{
|
||||
private readonly IApiHttpClient _apiHttpClient;
|
||||
@@ -39,7 +39,7 @@ public class FilesController : Controller
|
||||
public async Task<IActionResult> GetTreeFiles([FromQuery] string path, [FromQuery] bool includeHiddenElements, CancellationToken cancellationToken)
|
||||
{
|
||||
IEnumerable<FileSystemTreeNodeModel> response = await _apiHttpClient.GetAsync<FileSystemTreeNodeModel[]>(
|
||||
$"files/get-tree-files?path={Uri.EscapeDataString(path)}&includeHiddenElements={includeHiddenElements}", cancellationToken);
|
||||
$"files/get-tree-files?path={Uri.EscapeDataString(path)}&includeHiddenElements={includeHiddenElements}", cancellationToken).ConfigureAwait(false);
|
||||
return Json(new { success = true, data = response });
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ public class FilesController : Controller
|
||||
public async Task<IActionResult> GetFiles([FromQuery] string path, [FromQuery] bool includeHiddenElements, CancellationToken cancellationToken)
|
||||
{
|
||||
IEnumerable<FileModel> response = await _apiHttpClient.GetAsync<FileModel[]>(
|
||||
$"files/get-files?path={Uri.EscapeDataString(path)}&includeHiddenElements={includeHiddenElements}", cancellationToken);
|
||||
$"files/get-files?path={Uri.EscapeDataString(path)}&includeHiddenElements={includeHiddenElements}", cancellationToken).ConfigureAwait(false);
|
||||
return Json(new { success = true, data = response });
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@ namespace Lumina.Presentation.Web.Controllers.FileSystemManagement;
|
||||
/// Controller for managing file system paths.
|
||||
/// </summary>
|
||||
[Authorize]
|
||||
[Route("[controller]")]
|
||||
[Route("path")]
|
||||
public class PathController : Controller
|
||||
{
|
||||
private readonly IApiHttpClient _apiHttpClient;
|
||||
@@ -36,7 +36,7 @@ public class PathController : Controller
|
||||
[HttpGet("get-path-root")]
|
||||
public async Task<IActionResult> GetPathRoot([FromQuery] string path, CancellationToken cancellationToken)
|
||||
{
|
||||
PathSegmentModel response = await _apiHttpClient.GetAsync<PathSegmentModel>($"path/get-path-root?path={Uri.EscapeDataString(path)}", cancellationToken);
|
||||
PathSegmentModel response = await _apiHttpClient.GetAsync<PathSegmentModel>($"path/get-path-root?path={Uri.EscapeDataString(path)}", cancellationToken).ConfigureAwait(false);
|
||||
return Json(new { success = true, data = new { root = response } });
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ public class PathController : Controller
|
||||
[HttpGet("get-path-separator")]
|
||||
public async Task<IActionResult> GetPathSeparator(CancellationToken cancellationToken)
|
||||
{
|
||||
PathSeparatorModel response = await _apiHttpClient.GetAsync<PathSeparatorModel>($"path/get-path-separator", cancellationToken);
|
||||
PathSeparatorModel response = await _apiHttpClient.GetAsync<PathSeparatorModel>($"path/get-path-separator", cancellationToken).ConfigureAwait(false);
|
||||
return Json(new { success = true, data = new { pathSeparator = response.Separator } });
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ public class PathController : Controller
|
||||
[HttpGet("get-path-parent")]
|
||||
public async Task<IActionResult> GetPathParent([FromQuery] string path, CancellationToken cancellationToken)
|
||||
{
|
||||
PathSegmentModel[] response = await _apiHttpClient.GetAsync<PathSegmentModel[]>($"path/get-path-parent?path={Uri.EscapeDataString(path)}", cancellationToken);
|
||||
PathSegmentModel[] response = await _apiHttpClient.GetAsync<PathSegmentModel[]>($"path/get-path-parent?path={Uri.EscapeDataString(path)}", cancellationToken).ConfigureAwait(false);
|
||||
return Json(new { success = true, data = new { pathSegments = response } });
|
||||
}
|
||||
|
||||
@@ -71,7 +71,7 @@ public class PathController : Controller
|
||||
[HttpGet("split")]
|
||||
public async Task<IActionResult> SplitPath([FromQuery] string path, CancellationToken cancellationToken)
|
||||
{
|
||||
PathSegmentModel[] response = await _apiHttpClient.GetAsync<PathSegmentModel[]>($"path/split?path={Uri.EscapeDataString(path)}", cancellationToken);
|
||||
PathSegmentModel[] response = await _apiHttpClient.GetAsync<PathSegmentModel[]>($"path/split?path={Uri.EscapeDataString(path)}", cancellationToken).ConfigureAwait(false);
|
||||
return Json(new { success = true, data = new { pathSegments = response } });
|
||||
}
|
||||
|
||||
@@ -83,7 +83,7 @@ public class PathController : Controller
|
||||
[HttpGet("validate")]
|
||||
public async Task<IActionResult> ValidatePath([FromQuery] string path, CancellationToken cancellationToken)
|
||||
{
|
||||
PathValidModel response = await _apiHttpClient.GetAsync<PathValidModel>($"path/validate?path={Uri.EscapeDataString(path)}", cancellationToken);
|
||||
PathValidModel response = await _apiHttpClient.GetAsync<PathValidModel>($"path/validate?path={Uri.EscapeDataString(path)}", cancellationToken).ConfigureAwait(false);
|
||||
return Json(new { success = true, data = new { isValid = response.IsValid } });
|
||||
}
|
||||
|
||||
@@ -96,7 +96,8 @@ public class PathController : Controller
|
||||
[HttpGet("check-path-exists")]
|
||||
public async Task<IActionResult> CheckPathExists([FromQuery] string path, [FromQuery] bool includeHiddenElements, CancellationToken cancellationToken)
|
||||
{
|
||||
PathExistsModel response = await _apiHttpClient.GetAsync<PathExistsModel>($"path/check-path-exists?path={Uri.EscapeDataString(path)}&includeHiddenElements={includeHiddenElements}", cancellationToken);
|
||||
PathExistsModel response = await _apiHttpClient.GetAsync<PathExistsModel>(
|
||||
$"path/check-path-exists?path={Uri.EscapeDataString(path)}&includeHiddenElements={includeHiddenElements}", cancellationToken).ConfigureAwait(false);
|
||||
return Json(new { success = true, data = new { exists = response.Exists } });
|
||||
}
|
||||
}
|
||||
@@ -1,32 +1,55 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Presentation.Web.Models;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Serilog;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Diagnostics;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.Controllers;
|
||||
|
||||
/// <summary>
|
||||
/// Controller for home page.
|
||||
/// </summary>
|
||||
[Authorize]
|
||||
[Route("")] // randle root path - this is needed because app.UseCultureRedirect will redirect to the correct route
|
||||
[Route("{culture}")] // culture-specific routes
|
||||
public class HomeController : Controller
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly ILogger<HomeController> _logger;
|
||||
|
||||
public HomeController(ILogger logger)
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="HomeController"/> class.
|
||||
/// </summary>
|
||||
/// <param name="logger">Injected service for logging.</param>
|
||||
public HomeController(ILogger<HomeController> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Displays the view for the home page.
|
||||
/// </summary>
|
||||
[HttpGet("")]
|
||||
public IActionResult Index()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Displays the view for privacy.
|
||||
/// </summary>
|
||||
[HttpGet("privacy")]
|
||||
public IActionResult Privacy()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Displays the view for errors.
|
||||
/// </summary>
|
||||
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
||||
[HttpGet("error")]
|
||||
public IActionResult Error()
|
||||
{
|
||||
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
|
||||
|
||||
+16
-7
@@ -1,12 +1,9 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Presentation.Web.Common.Api;
|
||||
using Lumina.Presentation.Web.Common.Enums.Library;
|
||||
using Lumina.Presentation.Web.Common.Models.Libraries;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Formatters;
|
||||
using System;
|
||||
using System.Security.Claims;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
@@ -14,28 +11,40 @@ using System.Threading.Tasks;
|
||||
namespace Lumina.Presentation.Web.Controllers.Library.Management;
|
||||
|
||||
[Authorize]
|
||||
[Route("/library/manage/")]
|
||||
[Route("{culture}/library/manage")]
|
||||
public class LibraryManagementController : Controller
|
||||
{
|
||||
private readonly IApiHttpClient _apiHttpClient;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="LibraryManagementController"/> class.
|
||||
/// </summary>
|
||||
/// <param name="apiHttpClient">Injected service for interactions with the API.</param>
|
||||
public LibraryManagementController(IApiHttpClient apiHttpClient)
|
||||
{
|
||||
_apiHttpClient = apiHttpClient;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Displays the view for displaying the list of media libraries.
|
||||
/// </summary>
|
||||
[HttpGet("")]
|
||||
public IActionResult Index()
|
||||
{
|
||||
return View("/Views/Library/Management/Index.cshtml");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Displays the view for adding or editing a media library.
|
||||
/// </summary>
|
||||
/// <param name="id">The optional id of the media library to edit.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
[HttpGet("item/{id?}")]
|
||||
public async Task<IActionResult> EditLibrary(Guid? id = null, CancellationToken cancellationToken = default)
|
||||
{
|
||||
LibraryModel libraryModel = new();
|
||||
if (id is not null)
|
||||
libraryModel = await _apiHttpClient.GetAsync<LibraryModel>($"libraries/{id}", cancellationToken);
|
||||
libraryModel = await _apiHttpClient.GetAsync<LibraryModel>($"libraries/{id}", cancellationToken).ConfigureAwait(false);
|
||||
return View("/Views/Library/Management/Item.cshtml", libraryModel);
|
||||
}
|
||||
|
||||
@@ -50,8 +59,8 @@ public class LibraryManagementController : Controller
|
||||
{
|
||||
// call different API endpoints based on whether this is a new library or an existing one
|
||||
LibraryModel response = data.Id.HasValue
|
||||
? await _apiHttpClient.PutAsync<LibraryModel, LibraryModel>($"libraries/{data.Id}", data, cancellationToken)
|
||||
: await _apiHttpClient.PostAsync<LibraryModel, LibraryModel>("libraries", data, cancellationToken);
|
||||
? await _apiHttpClient.PutAsync<LibraryModel, LibraryModel>($"libraries/{data.Id}", data, cancellationToken).ConfigureAwait(false)
|
||||
: await _apiHttpClient.PostAsync<LibraryModel, LibraryModel>("libraries", data, cancellationToken).ConfigureAwait(false);
|
||||
return Json(new { success = true, data = response });
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -5,7 +5,7 @@ using System;
|
||||
namespace Lumina.Presentation.Web.Controllers.Library.WrittenContentLibrary.BookLibrary;
|
||||
|
||||
[Authorize]
|
||||
[Route("/library/written-content-library/books-library/books/")]
|
||||
[Route("{culture}library/written-content-library/books-library/books")]
|
||||
public class BooksController : Controller
|
||||
{
|
||||
[HttpGet("")]
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Presentation.Web.Common.Services;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Localization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.Controllers.Tools;
|
||||
|
||||
/// <summary>
|
||||
/// Controller for language related operations.
|
||||
/// </summary>
|
||||
[Route("{culture}/tools/language")]
|
||||
public class LanguageController : Controller
|
||||
{
|
||||
private readonly IUrlService _urlService;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="LanguageController"/> class.
|
||||
/// </summary>
|
||||
/// <param name="urlService">Injected service for generating URLs from action and controller names, with localization.</param>
|
||||
public LanguageController(IUrlService urlService)
|
||||
{
|
||||
_urlService = urlService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the culture used in the application.
|
||||
/// </summary>
|
||||
/// <param name="newCulture">The new culture to set.</param>
|
||||
/// <param name="returnUrl">The URL to return to after setting the new culture.</param>
|
||||
[HttpGet("set-language")]
|
||||
public IActionResult SetLanguage(string newCulture, string returnUrl)
|
||||
{
|
||||
// store culture preference in cookie
|
||||
Response.Cookies.Append(
|
||||
CookieRequestCultureProvider.DefaultCookieName,
|
||||
CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(newCulture)),
|
||||
new CookieOptions
|
||||
{
|
||||
Expires = DateTimeOffset.UtcNow.AddYears(1),
|
||||
IsEssential = true,
|
||||
SameSite = SameSiteMode.Lax,
|
||||
Path = "/" // ensure cookie works across all paths
|
||||
}
|
||||
);
|
||||
// ensure returnUrl includes the correct culture
|
||||
if (string.IsNullOrEmpty(returnUrl))
|
||||
returnUrl = _urlService.GetAbsoluteUrl("Index", "Home")!; // default fallback URL
|
||||
// handle culture replacement in return URL
|
||||
string? currentCulture = HttpContext.Request.RouteValues["culture"]?.ToString() ?? "en-us";
|
||||
if (currentCulture != null)
|
||||
{
|
||||
// ensure we handle both with and without trailing slashes
|
||||
string culturePath = $"/{currentCulture.ToLower()}/";
|
||||
string newCulturePath = $"/{newCulture.ToLower()}/";
|
||||
|
||||
// Handle base path if present
|
||||
if (!string.IsNullOrEmpty(Request.PathBase))
|
||||
returnUrl = returnUrl.Replace(Request.PathBase.Value!, "", StringComparison.OrdinalIgnoreCase);
|
||||
// replace culture in URL
|
||||
returnUrl = returnUrl.Replace(culturePath, newCulturePath, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
// re-apply base path if present
|
||||
if (!string.IsNullOrEmpty(Request.PathBase))
|
||||
returnUrl = Request.PathBase.Value + returnUrl;
|
||||
}
|
||||
// redirect to the original page
|
||||
return LocalRedirect(returnUrl);
|
||||
}
|
||||
}
|
||||
@@ -5,11 +5,13 @@ using Lumina.Presentation.Web.Common.Filters;
|
||||
using Lumina.Presentation.Web.Common.Http;
|
||||
using Lumina.Presentation.Web.Common.Models.UsersManagement;
|
||||
using Lumina.Presentation.Web.Common.Security;
|
||||
using Lumina.Presentation.Web.Common.Services;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Security.Claims;
|
||||
@@ -24,21 +26,24 @@ namespace Lumina.Presentation.Web.Controllers.UsersManagement;
|
||||
/// Controller for authentication related operations.
|
||||
/// </summary>
|
||||
[Authorize]
|
||||
[Route("/auth/")]
|
||||
[Route("{culture}/auth")]
|
||||
public class AuthController : Controller
|
||||
{
|
||||
private readonly IApiHttpClient _apiHttpClient;
|
||||
private readonly ICryptographyService _cryptographyService;
|
||||
private readonly IUrlService _urlService;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="AuthController"/> class.
|
||||
/// </summary>
|
||||
/// <param name="apiHttpClient">Injected service for interactions with the API.</param>
|
||||
/// <param name="cryptographyService">Injected service for cryptographic functionality.</param>
|
||||
public AuthController(IApiHttpClient apiHttpClient, ICryptographyService cryptographyService)
|
||||
/// <param name="urlService">Injected service for generating URLs from action and controller names, with localization.</param>
|
||||
public AuthController(IApiHttpClient apiHttpClient, ICryptographyService cryptographyService, IUrlService urlService)
|
||||
{
|
||||
_apiHttpClient = apiHttpClient;
|
||||
_cryptographyService = cryptographyService;
|
||||
_urlService = urlService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -67,14 +72,14 @@ public class AuthController : Controller
|
||||
ViewData["ReturnUrl"] = returnUrl;
|
||||
// if the user is already logged in, redirect them to the home page
|
||||
if (User?.Identity?.IsAuthenticated == true)
|
||||
return RedirectToAction("Index", "Home");
|
||||
return Redirect(_urlService.GetAbsoluteUrl("Index", "Home")!);
|
||||
// check if the application is initialized (does contain at least the super user account); if it's not, redirect to registration view
|
||||
string? isPendingSuperAdminSetup = HttpContext.Session.GetString(HttpContextItemKeys.PENDING_SUPER_ADMIN_SETUP);
|
||||
if (isPendingSuperAdminSetup == "true")
|
||||
// TODO: should be:
|
||||
// return View("~/Views/Auth/Register", new RegisterRequestDto());
|
||||
// but doesn't work because of an ASP.NET bug: https://github.com/dotnet/AspNetCore.Docs/issues/25157
|
||||
return RedirectToAction("Register", "Auth");
|
||||
return Redirect(_urlService.GetAbsoluteUrl("Register", "Auth")!);
|
||||
return View(new LoginRequestModel());
|
||||
}
|
||||
|
||||
@@ -88,7 +93,7 @@ public class AuthController : Controller
|
||||
{
|
||||
// if the user is already logged in, redirect them to the home page
|
||||
if (User?.Identity?.IsAuthenticated == true)
|
||||
return RedirectToAction("Index", "Home");
|
||||
return Redirect(_urlService.GetAbsoluteUrl("Index", "Home")!);
|
||||
return View();
|
||||
}
|
||||
|
||||
@@ -123,7 +128,7 @@ public class AuthController : Controller
|
||||
// call different endpoints based on the view hidden field - registration for normal users, or initial application admin account setup
|
||||
string endpoint = data.RegistrationType == "Admin" ? "initialization" : "auth/register";
|
||||
// attempt API registration
|
||||
RegisterResponseModel response = await _apiHttpClient.PostAsync<RegisterResponseModel, RegisterRequestModel>(endpoint, data, cancellationToken);
|
||||
RegisterResponseModel response = await _apiHttpClient.PostAsync<RegisterResponseModel, RegisterRequestModel>(endpoint, data, cancellationToken).ConfigureAwait(false);
|
||||
return Json(new { success = true, data = response });
|
||||
}
|
||||
|
||||
@@ -140,7 +145,7 @@ public class AuthController : Controller
|
||||
try
|
||||
{
|
||||
// attempt API registration
|
||||
LoginResponseModel response = await _apiHttpClient.PostAsync<LoginResponseModel, LoginRequestModel>("auth/login", data, cancellationToken);
|
||||
LoginResponseModel response = await _apiHttpClient.PostAsync<LoginResponseModel, LoginRequestModel>("auth/login", data, cancellationToken).ConfigureAwait(false);
|
||||
// store the received token in a secure cookie
|
||||
Response.Cookies.Delete("Token");
|
||||
Response.Cookies.Append("Token", _cryptographyService.Encrypt(response.Token!), new CookieOptions
|
||||
@@ -148,7 +153,9 @@ public class AuthController : Controller
|
||||
Expires = DateTimeOffset.UtcNow.AddMonths(1),
|
||||
Path = "/",
|
||||
HttpOnly = true,
|
||||
Secure = true
|
||||
Secure = true,
|
||||
IsEssential = true,
|
||||
SameSite = SameSiteMode.Strict
|
||||
});
|
||||
// tell asp.net we are logged in
|
||||
List<Claim> claims =
|
||||
@@ -158,9 +165,29 @@ public class AuthController : Controller
|
||||
new Claim("Token", response.Token!),
|
||||
];
|
||||
ClaimsIdentity claimsIdentity = new(claims, CookieAuthenticationDefaults.AuthenticationScheme);
|
||||
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity));
|
||||
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity)).ConfigureAwait(false);
|
||||
// get current culture from route data or use default if not set
|
||||
string currentCulture = HttpContext.Request.RouteValues["culture"]?.ToString() ?? "en-US";
|
||||
// handle ReturnUrl and ensure it includes the correct culture
|
||||
string redirectUrl;
|
||||
if (!string.IsNullOrEmpty(data.ReturnUrl) && Url.IsLocalUrl(data.ReturnUrl)) // ensure that return URL contains the correct culture
|
||||
redirectUrl = data.ReturnUrl.StartsWith($"/{currentCulture}/") ? data.ReturnUrl : $"/{currentCulture}{data.ReturnUrl}";
|
||||
else
|
||||
{
|
||||
// if no ReturnUrl is provided or it's invalid, use a default URL with respect to base paths
|
||||
redirectUrl = Url.Content("~/");
|
||||
|
||||
// handle reverse proxy scenarios by respecting any base path or forwarded headers
|
||||
if (HttpContext.Request.Headers.TryGetValue("X-Forwarded-Prefix", out StringValues pathBase))
|
||||
redirectUrl = pathBase + redirectUrl;
|
||||
|
||||
// ensure that default redirect contains the correct culture
|
||||
if (!redirectUrl.StartsWith($"/{currentCulture}/"))
|
||||
redirectUrl = $"/{currentCulture}{redirectUrl}";
|
||||
}
|
||||
|
||||
// return success status and redirect URL
|
||||
return Json(new { success = true, data = string.IsNullOrEmpty(data.ReturnUrl) || !Url.IsLocalUrl(data.ReturnUrl) ? Url.Content("~/") : Url.Content(data.ReturnUrl) });
|
||||
return Json(new { success = true, data = redirectUrl });
|
||||
}
|
||||
catch (ApiException ex)
|
||||
{
|
||||
@@ -186,10 +213,10 @@ public class AuthController : Controller
|
||||
{
|
||||
// if the user is not logged in, redirect them to the home page
|
||||
if (User?.Identity?.IsAuthenticated == false)
|
||||
return RedirectToAction("Index", "Home");
|
||||
return Redirect(_urlService.GetAbsoluteUrl("Index", "Home")!);
|
||||
Response.Cookies.Delete("Token");
|
||||
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
|
||||
return RedirectToAction("Login", "Auth");
|
||||
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme).ConfigureAwait(false);
|
||||
return Redirect(_urlService.GetAbsoluteUrl("Login", "Auth")!);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -202,7 +229,7 @@ public class AuthController : Controller
|
||||
[HttpPost("recover-password")]
|
||||
public async Task<IActionResult> RecoverPassword([FromBody] RecoverPasswordRequestModel data, CancellationToken cancellationToken)
|
||||
{
|
||||
RecoverPasswordResponseModel response = await _apiHttpClient.PostAsync<RecoverPasswordResponseModel, RecoverPasswordRequestModel>("auth/recover-password", data, cancellationToken);
|
||||
RecoverPasswordResponseModel response = await _apiHttpClient.PostAsync<RecoverPasswordResponseModel, RecoverPasswordRequestModel>("auth/recover-password", data, cancellationToken).ConfigureAwait(false);
|
||||
return Json(new { success = true, data = response });
|
||||
}
|
||||
|
||||
@@ -216,7 +243,7 @@ public class AuthController : Controller
|
||||
public async Task<IActionResult> ChangePassword([FromBody] ChangePasswordRequestModel data, CancellationToken cancellationToken)
|
||||
{
|
||||
data = data with { Username = User?.Identity?.Name }; // assign the currently logged in user as the user for which to change the password
|
||||
ChangePasswordResponseModel response = await _apiHttpClient.PostAsync<ChangePasswordResponseModel, ChangePasswordRequestModel>("auth/change-password", data, cancellationToken);
|
||||
ChangePasswordResponseModel response = await _apiHttpClient.PostAsync<ChangePasswordResponseModel, ChangePasswordRequestModel>("auth/change-password", data, cancellationToken).ConfigureAwait(false);
|
||||
return Json(new { success = true, data = response });
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user