From df4c7e4017a649f0ebedcb6f8bec911be8c8169d Mon Sep 17 00:00:00 2001 From: AlanMoonbase Date: Thu, 27 Feb 2025 14:50:46 -0800 Subject: [PATCH] introduce ``Internal`` namespace implement ``ModerationController`` --- .../Internal/ModerationController.cs | 91 +++++++++++++++++++ src/Services/ModerationService.cs | 3 +- 2 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 src/Controllers/Internal/ModerationController.cs diff --git a/src/Controllers/Internal/ModerationController.cs b/src/Controllers/Internal/ModerationController.cs new file mode 100644 index 0000000..640cc3f --- /dev/null +++ b/src/Controllers/Internal/ModerationController.cs @@ -0,0 +1,91 @@ +using System.Linq.Expressions; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using sodoff.Model; +using sodoff.Schema; +using sodoff.Services; + +namespace sodoff.Controllers.Internal +{ + [ApiController] + public class ModerationController : Controller + { + public readonly DBContext ctx; + public readonly ModerationService moderationService; + public ModerationController(DBContext ctx, ModerationService moderationService) + { + this.ctx = ctx; + this.moderationService = moderationService; + } + + [HttpPost] + [Route("Moderation/AddBanToVikingByGuid")] + public IActionResult AddBanToVikingByGuid([FromForm] Guid token, [FromForm] Guid userId, [FromForm] int banType, [FromForm] int days = 0) + { + var validationResult = ValidateSession(token); + + if(validationResult) + { + // get the viking + Viking? viking = ctx.Vikings.FirstOrDefault(e => e.Uid == userId); + + // get execution timestamp + DateTime timestamp = DateTime.UtcNow; + + DateTime expiration = new DateTime(); + + if (days == 0) expiration = new DateTime(9999, 99, 99); // a days value of 0 should mean indefinite ban (might change later) + else expiration = timestamp.AddDays(days); + + if (viking != null) return Ok(moderationService.AddBanToViking(viking, (UserBanType)banType, expiration)); + else return NotFound(); + } + + return Unauthorized("You Do Not Have Sufficient Permissions To Moderate Users"); + } + + [HttpDelete] + [Route("Moderation/RemoveBansFromVikingByGuidAndType")] + public IActionResult RemoveBanFromVikingByGuid([FromForm] Guid token, [FromForm] Guid userId, [FromForm] int banType) + { + var validationResult = ValidateSession(token); + + if (validationResult) + { + // get the viking + Viking? viking = ctx.Vikings.FirstOrDefault(e => e.Uid == userId); + + // remove all bans of type + if (viking != null) return Ok(moderationService.RemoveBansFromVikingByType(viking, (UserBanType)banType)); + else return NotFound(); + } + + return Unauthorized("You Do Not Have Sufficient Permissions To Moderate Users"); + } + + [HttpGet] + [Route("Moderation/CheckForVikingBan")] + public IActionResult CheckForVikingBan([FromForm] Guid token) + { + // get viking session + var session = ctx.Sessions.FirstOrDefault(e => e.ApiToken == token); + + if (session != null && session.Viking != null) return Ok(moderationService.IsVikingBanned(session.Viking)); + else return Ok(UserBanType.NotBanned); // invalid session, for now just return not banned + } + + private bool ValidateSession(Guid token) + { + // get active session + var session = ctx.Sessions.FirstOrDefault(e => e.ApiToken == token); + + if (session != null) + { + // most endpoints here should only be activated by a 'Moderator' or above + Role? vikingRole = session.Viking?.MMORoles.FirstOrDefault()?.Role; + if (vikingRole != null && (vikingRole == Role.Moderator || vikingRole == Role.Admin)) return true; + else return false; + } else return false; + } + } +} diff --git a/src/Services/ModerationService.cs b/src/Services/ModerationService.cs index e1bb61b..344cf97 100644 --- a/src/Services/ModerationService.cs +++ b/src/Services/ModerationService.cs @@ -52,8 +52,7 @@ public class ModerationService if (userBans.Count == 0) return false; // delete all - foreach(var ban in userBans) - viking.UserBans.Remove(ban); + foreach(var ban in userBans) { viking.UserBans.Remove(ban); } ctx.SaveChanges();