Add settings endpoints and registration status check: implement new API endpoints for settings management, including registration status retrieval, and update the registration command handler to enforce registration rules based on site settings.
This commit is contained in:
@@ -9,6 +9,7 @@ using TeleWave.Application.Auth.Logout;
|
||||
using TeleWave.Application.Auth.Me;
|
||||
using TeleWave.Application.Auth.Refresh;
|
||||
using TeleWave.Application.Auth.Register;
|
||||
using TeleWave.Application.Settings.GetSiteSettings;
|
||||
|
||||
namespace TeleWave.Api.Endpoints;
|
||||
|
||||
@@ -22,6 +23,7 @@ public static class AuthEndpoints
|
||||
.WithTags("Auth")
|
||||
.RequireRateLimiting(RateLimiting.AuthPolicy);
|
||||
|
||||
group.MapGet("/registration", RegistrationStatus).Produces<RegistrationStatusDto>();
|
||||
group.MapPost("/register", Register).Produces<AuthResponseDto>();
|
||||
group.MapPost("/login", Login).Produces<AuthResponseDto>();
|
||||
group.MapPost("/refresh", Refresh).Produces<AuthResponseDto>();
|
||||
@@ -46,6 +48,15 @@ public static class AuthEndpoints
|
||||
return app;
|
||||
}
|
||||
|
||||
private static async Task<IResult> RegistrationStatus(
|
||||
ISender sender,
|
||||
CancellationToken cancellationToken
|
||||
)
|
||||
{
|
||||
var settings = await sender.Send(new GetSiteSettingsQuery(), cancellationToken);
|
||||
return Results.Ok(new RegistrationStatusDto(settings.RegistrationEnabled));
|
||||
}
|
||||
|
||||
private static async Task<IResult> Register(
|
||||
RegisterCommand command,
|
||||
ISender sender,
|
||||
@@ -218,3 +229,6 @@ public sealed record AuthResponseDto(
|
||||
DateTimeOffset ExpiresAt,
|
||||
CurrentUserDto User
|
||||
);
|
||||
|
||||
/// <summary>Публичный ответ: включена ли открытая регистрация (для страниц входа/регистрации).</summary>
|
||||
public sealed record RegistrationStatusDto(bool Enabled);
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
using LiteCqrs;
|
||||
using TeleWave.Api.Common;
|
||||
using TeleWave.Application.Settings;
|
||||
using TeleWave.Application.Settings.GetSiteSettings;
|
||||
using TeleWave.Application.Settings.UpdateSiteSettings;
|
||||
using TeleWave.Infrastructure.Identity;
|
||||
|
||||
namespace TeleWave.Api.Endpoints;
|
||||
|
||||
public static class SettingsEndpoints
|
||||
{
|
||||
public static IEndpointRouteBuilder MapSettingsEndpoints(this IEndpointRouteBuilder app)
|
||||
{
|
||||
var admin = app.MapGroup("/api/admin/settings")
|
||||
.WithTags("Admin.Settings")
|
||||
.RequireAuthorization(policy => policy.RequireRole(RoleNames.Admin));
|
||||
|
||||
admin.MapGet("", GetSettings).Produces<SiteSettingsDto>();
|
||||
admin.MapPut("", UpdateSettings).Produces(StatusCodes.Status204NoContent);
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
private static async Task<IResult> GetSettings(ISender sender, CancellationToken cancellationToken)
|
||||
{
|
||||
var settings = await sender.Send(new GetSiteSettingsQuery(), cancellationToken);
|
||||
return Results.Ok(settings);
|
||||
}
|
||||
|
||||
private static async Task<IResult> UpdateSettings(
|
||||
UpdateSiteSettingsBody body,
|
||||
ISender sender,
|
||||
CancellationToken cancellationToken
|
||||
)
|
||||
{
|
||||
var result = await sender.Send(
|
||||
new UpdateSiteSettingsCommand(body.RegistrationEnabled),
|
||||
cancellationToken
|
||||
);
|
||||
return result.ToHttpResult();
|
||||
}
|
||||
}
|
||||
|
||||
public sealed record UpdateSiteSettingsBody(bool RegistrationEnabled);
|
||||
@@ -117,6 +117,7 @@ app.MapShowEndpoints();
|
||||
app.MapChannelEndpoints();
|
||||
app.MapStreamingEndpoints();
|
||||
app.MapMaintenanceEndpoints();
|
||||
app.MapSettingsEndpoints();
|
||||
|
||||
// Раздача статики SPA из wwwroot + fallback на index.html для клиентских маршрутов.
|
||||
app.UseDefaultFiles();
|
||||
|
||||
Reference in New Issue
Block a user