From 9789c8452e50787da381e883aec9946184db0a51 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Tue, 24 Sep 2024 02:36:29 +0200 Subject: [PATCH] [backend/razor] Show login button when displaying public preview disabled error page --- .../Controllers/Razor/RedirectController.cs | 3 +-- Iceshrimp.Backend/Core/Extensions/ServiceExtensions.cs | 2 +- .../Core/Middleware/ErrorHandlerMiddleware.cs | 8 ++++++-- Iceshrimp.Backend/Pages/Note.cshtml.cs | 3 +-- Iceshrimp.Backend/Pages/Shared/ErrorPage.cshtml | 7 +++++++ Iceshrimp.Backend/Pages/Shared/_Layout.cshtml | 10 +++++----- Iceshrimp.Backend/Pages/User.cshtml.cs | 3 +-- Iceshrimp.Shared/Schemas/Web/ErrorResponse.cs | 4 +++- 8 files changed, 25 insertions(+), 15 deletions(-) diff --git a/Iceshrimp.Backend/Controllers/Razor/RedirectController.cs b/Iceshrimp.Backend/Controllers/Razor/RedirectController.cs index 1e534e4e..6941b2dd 100644 --- a/Iceshrimp.Backend/Controllers/Razor/RedirectController.cs +++ b/Iceshrimp.Backend/Controllers/Razor/RedirectController.cs @@ -30,8 +30,7 @@ public class RedirectController(IOptionsSnapshot config, { var localUser = HttpContext.GetUser(); if (config.Value.PublicPreview == Enums.PublicPreview.Lockdown && localUser == null) - throw GracefulException.Forbidden("Public preview is disabled on this instance.", - "The instance administrator has intentionally disabled this feature for privacy reasons."); + throw new PublicPreviewDisabledException(); var user = await db.Users.IncludeCommonProperties().FirstOrDefaultAsync(p => p.Id == id) ?? throw GracefulException.NotFound("User not found"); diff --git a/Iceshrimp.Backend/Core/Extensions/ServiceExtensions.cs b/Iceshrimp.Backend/Core/Extensions/ServiceExtensions.cs index eeaf7b6f..8d0e0fa1 100644 --- a/Iceshrimp.Backend/Core/Extensions/ServiceExtensions.cs +++ b/Iceshrimp.Backend/Core/Extensions/ServiceExtensions.cs @@ -290,7 +290,7 @@ public static class ServiceExtensions { context.HttpContext.Response.StatusCode = 429; context.HttpContext.Response.ContentType = "application/json"; - var res = new ErrorResponse + var res = new ErrorResponse(new Exception()) { Error = "Too Many Requests", StatusCode = 429, diff --git a/Iceshrimp.Backend/Core/Middleware/ErrorHandlerMiddleware.cs b/Iceshrimp.Backend/Core/Middleware/ErrorHandlerMiddleware.cs index 08eb6f44..d7b32ef0 100644 --- a/Iceshrimp.Backend/Core/Middleware/ErrorHandlerMiddleware.cs +++ b/Iceshrimp.Backend/Core/Middleware/ErrorHandlerMiddleware.cs @@ -92,7 +92,7 @@ public class ErrorHandlerMiddleware( } else { - var error = new ErrorResponse + var error = new ErrorResponse(e) { StatusCode = ctx.Response.StatusCode, Error = verbosity >= ExceptionVerbosity.Basic ? ce.Error : ce.StatusCode.ToString(), @@ -121,7 +121,7 @@ public class ErrorHandlerMiddleware( ctx.Response.StatusCode = 500; ctx.Response.Headers.RequestId = ctx.TraceIdentifier; - var error = new ErrorResponse + var error = new ErrorResponse(e) { StatusCode = 500, Error = "Internal Server Error", @@ -277,6 +277,10 @@ public class InstanceBlockedException(string uri, string? host = null) public string Uri => uri; } +public class PublicPreviewDisabledException() + : GracefulException(HttpStatusCode.Forbidden, "Public preview is disabled on this instance.", + "The instance administrator has intentionally disabled this feature for privacy reasons."); + public class ValidationException( HttpStatusCode statusCode, string error, diff --git a/Iceshrimp.Backend/Pages/Note.cshtml.cs b/Iceshrimp.Backend/Pages/Note.cshtml.cs index 2f1dd0df..4b3d12c2 100644 --- a/Iceshrimp.Backend/Pages/Note.cshtml.cs +++ b/Iceshrimp.Backend/Pages/Note.cshtml.cs @@ -41,8 +41,7 @@ public class NoteModel( return Partial("Shared/FrontendSPA"); if (security.Value.PublicPreview == Enums.PublicPreview.Lockdown) - throw GracefulException.Forbidden("Public preview is disabled on this instance.", - "The instance administrator has intentionally disabled this feature for privacy reasons."); + throw new PublicPreviewDisabledException(); InstanceName = await meta.Get(MetaEntity.InstanceName) ?? InstanceName; diff --git a/Iceshrimp.Backend/Pages/Shared/ErrorPage.cshtml b/Iceshrimp.Backend/Pages/Shared/ErrorPage.cshtml index dcf2b463..c44c8914 100644 --- a/Iceshrimp.Backend/Pages/Shared/ErrorPage.cshtml +++ b/Iceshrimp.Backend/Pages/Shared/ErrorPage.cshtml @@ -1,5 +1,6 @@ @page @using Iceshrimp.Backend.Core.Configuration +@using Iceshrimp.Backend.Core.Middleware @using Microsoft.Extensions.Options @model ErrorPageModel @inject IOptions Instance @@ -60,6 +61,12 @@

Iceshrimp.NET v@(Instance.Value.Version) + @if (Model.Error.Exception is PublicPreviewDisabledException) + { + + Login + + }
\ No newline at end of file diff --git a/Iceshrimp.Backend/Pages/Shared/_Layout.cshtml b/Iceshrimp.Backend/Pages/Shared/_Layout.cshtml index 00b33cfd..fa5df94f 100644 --- a/Iceshrimp.Backend/Pages/Shared/_Layout.cshtml +++ b/Iceshrimp.Backend/Pages/Shared/_Layout.cshtml @@ -20,12 +20,12 @@ @await RenderSectionAsync("scripts", false) \ No newline at end of file diff --git a/Iceshrimp.Backend/Pages/User.cshtml.cs b/Iceshrimp.Backend/Pages/User.cshtml.cs index 90911e8d..a1e0ce39 100644 --- a/Iceshrimp.Backend/Pages/User.cshtml.cs +++ b/Iceshrimp.Backend/Pages/User.cshtml.cs @@ -39,8 +39,7 @@ public class UserModel( return Partial("Shared/FrontendSPA"); if (security.Value.PublicPreview == Enums.PublicPreview.Lockdown) - throw GracefulException.Forbidden("Public preview is disabled on this instance.", - "The instance administrator has intentionally disabled this feature for privacy reasons."); + throw new PublicPreviewDisabledException(); InstanceName = await meta.Get(MetaEntity.InstanceName) ?? InstanceName; diff --git a/Iceshrimp.Shared/Schemas/Web/ErrorResponse.cs b/Iceshrimp.Shared/Schemas/Web/ErrorResponse.cs index 47481a13..5e6248bf 100644 --- a/Iceshrimp.Shared/Schemas/Web/ErrorResponse.cs +++ b/Iceshrimp.Shared/Schemas/Web/ErrorResponse.cs @@ -5,7 +5,7 @@ using JI = System.Text.Json.Serialization.JsonIgnoreAttribute; namespace Iceshrimp.Shared.Schemas.Web; [XmlRoot("Error")] -public class ErrorResponse +public class ErrorResponse(Exception exception) { [XmlElement("Status")] public required int StatusCode { get; set; } [XmlElement("Error")] public required string Error { get; set; } @@ -37,6 +37,8 @@ public class ErrorResponse public string? Source { get; set; } [XmlElement("RequestId")] public required string RequestId { get; set; } + + [JI] [XmlIgnore] public Exception Exception => exception; } public class XmlValidationError