IW4M-Admin/WebfrontCore/Controllers/BaseController.cs
RaidMax ed83c4c011 started work on getting the restart functionality in the gamelogserver
fix bug with unbanned players still showing as banned via lock icon
move player based stuff into client class
finally renamed Player to EFClient via partial class
don't try to run this build because it's in between stages
2018-11-05 21:01:29 -06:00

107 lines
3.8 KiB
C#

using System;
using System.Linq;
using System.Security.Claims;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using SharedLibraryCore.Database;
using SharedLibraryCore.Database.Models;
using SharedLibraryCore.Interfaces;
using SharedLibraryCore.Objects;
using WebfrontCore.ViewModels;
namespace WebfrontCore.Controllers
{
public class BaseController : Controller
{
protected IManager Manager;
protected readonly DatabaseContext Context;
protected bool Authorized { get; set; }
protected SharedLibraryCore.Localization.Index Localization { get; private set; }
protected EFClient Client { get; private set; }
private static readonly byte[] LocalHost = { 127, 0, 0, 1 };
private static string SocialLink;
private static string SocialTitle;
public BaseController()
{
if (Manager == null)
Manager = Program.Manager;
if (Localization == null)
Localization = SharedLibraryCore.Utilities.CurrentLocalization.LocalizationIndex;
if (Manager.GetApplicationSettings().Configuration().EnableSocialLink && SocialLink == null)
{
SocialLink = Manager.GetApplicationSettings().Configuration().SocialLinkAddress;
SocialTitle = Manager.GetApplicationSettings().Configuration().SocialLinkTitle;
}
ViewBag.Version = Manager.Version;
}
public override void OnActionExecuting(ActionExecutingContext context)
{
Client = Client ?? new EFClient()
{
ClientId = -1,
Level = EFClient.Permission.User,
CurrentAlias = new EFAlias() { Name = "Web Console Guest" }
};
if (!HttpContext.Connection.RemoteIpAddress.GetAddressBytes().SequenceEqual(LocalHost))
{
try
{
Client.ClientId = Convert.ToInt32(base.User.Claims.First(c => c.Type == ClaimTypes.Sid).Value);
Client.Level = (EFClient.Permission)Enum.Parse(typeof(EFClient.Permission), User.Claims.First(c => c.Type == ClaimTypes.Role).Value);
Client.CurrentAlias = new EFAlias() { Name = User.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value };
var stillExists = Manager.GetPrivilegedClients()[Client.ClientId];
// this happens if their level has been updated
if (stillExists.Level != Client.Level)
{
Client.Level = stillExists.Level;
}
}
catch (InvalidOperationException)
{
}
catch (System.Collections.Generic.KeyNotFoundException)
{
// force the "banned" client to be signed out
HttpContext.SignOutAsync().Wait(5000);
}
}
else
{
Client.ClientId = 1;
Client.Level = EFClient.Permission.Console;
Client.CurrentAlias = new EFAlias() { Name = "IW4MAdmin" };
}
Authorized = Client.ClientId >= 0;
ViewBag.Authorized = Authorized;
ViewBag.Url = Manager.GetApplicationSettings().Configuration().WebfrontBindUrl;
ViewBag.User = Client;
ViewBag.SocialLink = SocialLink ?? "";
ViewBag.SocialTitle = SocialTitle;
// grab the pages from plugins
ViewBag.Pages = Manager.GetPageList().Pages
.Select(page => new Page
{
Name = page.Key,
Location = page.Value
}).ToList();
base.OnActionExecuting(context);
}
}
}