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}")] [HttpPost("{wId}")]
[CheckRealmOwner] [CheckRealmOwner]
[CheckActiveSubscription]
public async Task<ActionResult<World>> InvitePlayer(int wId, PlayerRequest body) public async Task<ActionResult<World>> InvitePlayer(int wId, PlayerRequest body)
{ {
string cookie = Request.Headers.Cookie; string cookie = Request.Headers.Cookie;
@ -139,6 +140,7 @@ namespace Minecraft_Realms_Emulator.Modes.Realms
[HttpDelete("{wId}/invite/{uuid}")] [HttpDelete("{wId}/invite/{uuid}")]
[CheckRealmOwner] [CheckRealmOwner]
[CheckActiveSubscription]
public async Task<ActionResult<bool>> DeleteInvite(int wId, string uuid) public async Task<ActionResult<bool>> DeleteInvite(int wId, string uuid)
{ {
var world = await _context.Worlds.FirstOrDefaultAsync(w => w.Id == wId); 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}")] [HttpPost("{wId}/{uuid}")]
[CheckRealmOwner] [CheckRealmOwner]
[CheckActiveSubscription]
public ActionResult<OpsResponse> OpPlayer(int wId, string uuid) public ActionResult<OpsResponse> OpPlayer(int wId, string uuid)
{ {
var ops = _context.Players.Where(p => p.World.Id == wId && p.Operator == true).ToList(); 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}")] [HttpDelete("{wId}/{uuid}")]
[CheckRealmOwner] [CheckRealmOwner]
[CheckActiveSubscription]
public ActionResult<OpsResponse> DeopPlayer(int wId, string uuid) public ActionResult<OpsResponse> DeopPlayer(int wId, string uuid)
{ {
var ops = _context.Players.Where(p => p.World.Id == wId && p.Operator == true).ToList(); 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")] [HttpPost("{wId}/reset")]
[CheckRealmOwner] [CheckRealmOwner]
[CheckActiveSubscription]
public ActionResult<bool> Reset(int wId) public ActionResult<bool> Reset(int wId)
{ {
Console.WriteLine($"Resetting world {wId}"); Console.WriteLine($"Resetting world {wId}");
@ -306,6 +307,7 @@ namespace Minecraft_Realms_Emulator.Modes.Realms
[HttpPut("{wId}/open")] [HttpPut("{wId}/open")]
[CheckRealmOwner] [CheckRealmOwner]
[CheckActiveSubscription]
public async Task<ActionResult<bool>> Open(int wId) public async Task<ActionResult<bool>> Open(int wId)
{ {
var worlds = await _context.Worlds.ToListAsync(); var worlds = await _context.Worlds.ToListAsync();
@ -333,6 +335,7 @@ namespace Minecraft_Realms_Emulator.Modes.Realms
[HttpPut("{wId}/close")] [HttpPut("{wId}/close")]
[CheckRealmOwner] [CheckRealmOwner]
[CheckActiveSubscription]
public async Task<ActionResult<bool>> Close(int wId) public async Task<ActionResult<bool>> Close(int wId)
{ {
var worlds = await _context.Worlds.ToListAsync(); var worlds = await _context.Worlds.ToListAsync();
@ -360,6 +363,7 @@ namespace Minecraft_Realms_Emulator.Modes.Realms
[HttpPost("{wId}")] [HttpPost("{wId}")]
[CheckRealmOwner] [CheckRealmOwner]
[CheckActiveSubscription]
public async Task<ActionResult<bool>> UpdateWorld(int wId, WorldCreateRequest body) public async Task<ActionResult<bool>> UpdateWorld(int wId, WorldCreateRequest body)
{ {
var worlds = await _context.Worlds.ToListAsync(); var worlds = await _context.Worlds.ToListAsync();
@ -378,6 +382,7 @@ namespace Minecraft_Realms_Emulator.Modes.Realms
[HttpPost("{wId}/slot/{sId}")] [HttpPost("{wId}/slot/{sId}")]
[CheckRealmOwner] [CheckRealmOwner]
[CheckActiveSubscription]
public async Task<ActionResult<bool>> UpdateSlotAsync(int wId, int sId, SlotOptionsRequest body) public async Task<ActionResult<bool>> UpdateSlotAsync(int wId, int sId, SlotOptionsRequest body)
{ {
var slots = await _context.Slots.Where(s => s.World.Id == wId).ToListAsync(); 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}")] [HttpPut("{wId}/slot/{sId}")]
[CheckRealmOwner] [CheckRealmOwner]
[CheckActiveSubscription]
public ActionResult<bool> SwitchSlot(int wId, int sId) public ActionResult<bool> SwitchSlot(int wId, int sId)
{ {
var world = _context.Worlds.Find(wId); var world = _context.Worlds.Find(wId);

View File

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