feat(realms): check for active subscription

This commit is contained in:
CyberL1 2024-05-25 18:33:52 +02:00
parent 83b2f0543d
commit ab237667a3
6 changed files with 59 additions and 0 deletions

View File

@ -0,0 +1,13 @@
using Minecraft_Realms_Emulator.Entities;
namespace Minecraft_Realms_Emulator.Attributes
{
[AttributeUsage(AttributeTargets.Method)]
public class CheckActiveSubscription : Attribute
{
public bool IsSubscriptionActive(DateTime subscriptionStartDate)
{
return ((DateTimeOffset)subscriptionStartDate.AddDays(30) - DateTime.Today).Days > 0;
}
}
}

View File

@ -0,0 +1,35 @@
using Minecraft_Realms_Emulator.Attributes;
using Minecraft_Realms_Emulator.Data;
using Minecraft_Realms_Emulator.Entities;
namespace Minecraft_Realms_Emulator.Middlewares
{
public class ActiveSubscriptionMiddleware(RequestDelegate next)
{
private readonly RequestDelegate _next = next;
public async Task Invoke(HttpContext httpContext, DataContext db)
{
var endpoint = httpContext.GetEndpoint();
var attribute = endpoint?.Metadata.GetMetadata<CheckActiveSubscription>();
if (attribute == null)
{
await _next(httpContext);
return;
}
Subscription subscription = db.Subscriptions.First(s => s.World.Id == int.Parse(httpContext.Request.RouteValues["wId"].ToString()));
Console.WriteLine(attribute.IsSubscriptionActive(subscription.StartDate));
if (!attribute.IsSubscriptionActive(subscription.StartDate))
{
httpContext.Response.StatusCode = 403;
await httpContext.Response.WriteAsync("You don't have an active subscription for this world");
return;
}
await _next(httpContext);
}
}
}

View File

@ -95,6 +95,7 @@ namespace Minecraft_Realms_Emulator.Modes.Realms
[HttpPost("{wId}")]
[CheckRealmOwner]
[CheckActiveSubscription]
public async Task<ActionResult<World>> InvitePlayer(int wId, PlayerRequest body)
{
string cookie = Request.Headers.Cookie;
@ -139,6 +140,7 @@ namespace Minecraft_Realms_Emulator.Modes.Realms
[HttpDelete("{wId}/invite/{uuid}")]
[CheckRealmOwner]
[CheckActiveSubscription]
public async Task<ActionResult<bool>> DeleteInvite(int wId, string uuid)
{
var world = await _context.Worlds.FirstOrDefaultAsync(w => w.Id == wId);

View File

@ -19,6 +19,7 @@ namespace Minecraft_Realms_Emulator.Modes.Realms
[HttpPost("{wId}/{uuid}")]
[CheckRealmOwner]
[CheckActiveSubscription]
public ActionResult<OpsResponse> OpPlayer(int wId, string uuid)
{
var ops = _context.Players.Where(p => p.World.Id == wId && p.Operator == true).ToList();
@ -48,6 +49,7 @@ namespace Minecraft_Realms_Emulator.Modes.Realms
[HttpDelete("{wId}/{uuid}")]
[CheckRealmOwner]
[CheckActiveSubscription]
public ActionResult<OpsResponse> DeopPlayer(int wId, string uuid)
{
var ops = _context.Players.Where(p => p.World.Id == wId && p.Operator == true).ToList();

View File

@ -298,6 +298,7 @@ namespace Minecraft_Realms_Emulator.Modes.Realms
[HttpPost("{wId}/reset")]
[CheckRealmOwner]
[CheckActiveSubscription]
public ActionResult<bool> Reset(int wId)
{
Console.WriteLine($"Resetting world {wId}");
@ -306,6 +307,7 @@ namespace Minecraft_Realms_Emulator.Modes.Realms
[HttpPut("{wId}/open")]
[CheckRealmOwner]
[CheckActiveSubscription]
public async Task<ActionResult<bool>> Open(int wId)
{
var worlds = await _context.Worlds.ToListAsync();
@ -333,6 +335,7 @@ namespace Minecraft_Realms_Emulator.Modes.Realms
[HttpPut("{wId}/close")]
[CheckRealmOwner]
[CheckActiveSubscription]
public async Task<ActionResult<bool>> Close(int wId)
{
var worlds = await _context.Worlds.ToListAsync();
@ -360,6 +363,7 @@ namespace Minecraft_Realms_Emulator.Modes.Realms
[HttpPost("{wId}")]
[CheckRealmOwner]
[CheckActiveSubscription]
public async Task<ActionResult<bool>> UpdateWorld(int wId, WorldCreateRequest body)
{
var worlds = await _context.Worlds.ToListAsync();
@ -378,6 +382,7 @@ namespace Minecraft_Realms_Emulator.Modes.Realms
[HttpPost("{wId}/slot/{sId}")]
[CheckRealmOwner]
[CheckActiveSubscription]
public async Task<ActionResult<bool>> UpdateSlotAsync(int wId, int sId, SlotOptionsRequest body)
{
var slots = await _context.Slots.Where(s => s.World.Id == wId).ToListAsync();
@ -401,6 +406,7 @@ namespace Minecraft_Realms_Emulator.Modes.Realms
[HttpPut("{wId}/slot/{sId}")]
[CheckRealmOwner]
[CheckActiveSubscription]
public ActionResult<bool> SwitchSlot(int wId, int sId)
{
var world = _context.Worlds.Find(wId);

View File

@ -95,6 +95,7 @@ app.UseRewriter(rewriteOptions);
app.UseMiddleware<MinecraftCookieMiddleware>();
app.UseMiddleware<CheckRealmOwnerMiddleware>();
app.UseMiddleware<ActiveSubscriptionMiddleware>();
Console.WriteLine($"Running in {mode.Value} mode");
app.Run();