introduce `Internal` namespace

implement ``ModerationController``
This commit is contained in:
Alan Moon 2025-02-27 14:50:46 -08:00
parent 3800fc49c8
commit df4c7e4017
2 changed files with 92 additions and 2 deletions

View File

@ -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;
}
}
}

View File

@ -52,8 +52,7 @@ public class ModerationService
if (userBans.Count == 0) return false; if (userBans.Count == 0) return false;
// delete all // delete all
foreach(var ban in userBans) foreach(var ban in userBans) { viking.UserBans.Remove(ban); }
viking.UserBans.Remove(ban);
ctx.SaveChanges(); ctx.SaveChanges();