IW4M-Admin/WebfrontCore/Controllers/BaseController.cs

75 lines
2.5 KiB
C#
Raw Normal View History

using System;
using System.Linq;
using System.Security.Claims;
2018-03-06 02:22:19 -05:00
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
2018-04-08 02:44:42 -04:00
using SharedLibraryCore.Database;
using SharedLibraryCore.Database.Models;
using SharedLibraryCore.Interfaces;
2018-04-08 02:44:42 -04:00
using SharedLibraryCore.Objects;
2018-03-06 02:22:19 -05:00
namespace WebfrontCore.Controllers
{
public class BaseController : Controller
{
protected IManager Manager;
2018-04-08 02:44:42 -04:00
protected readonly DatabaseContext Context;
2018-03-06 02:22:19 -05:00
protected bool Authorized { get; private set; }
protected EFClient Client { get; private set; }
private static byte[] LocalHost = { 127, 0, 0, 1 };
private static string DiscordLink;
2018-03-06 02:22:19 -05:00
public BaseController()
2018-03-06 02:22:19 -05:00
{
var Manager = Program.Manager;
if (Manager.GetApplicationSettings().Configuration().EnableDiscordLink)
{
string inviteLink = Manager.GetApplicationSettings().Configuration().DiscordInviteCode;
if (inviteLink != null)
DiscordLink = inviteLink.Contains("https") ? inviteLink : $"https://discordapp.com/invite/{inviteLink}";
else
DiscordLink = "";
}
}
public override void OnActionExecuting(ActionExecutingContext context)
{
Client = Client ?? new EFClient()
{
ClientId = -1
};
if (!HttpContext.Connection.RemoteIpAddress.GetAddressBytes().SequenceEqual(LocalHost))
{
try
{
Client.ClientId = Convert.ToInt32(base.User.Claims.First(c => c.Type == ClaimTypes.Sid).Value);
Client.Level = (Player.Permission)Enum.Parse(typeof(Player.Permission), User.Claims.First(c => c.Type == ClaimTypes.Role).Value);
Client.CurrentAlias = new EFAlias() { Name = User.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value };
}
catch (InvalidOperationException)
{
}
}
else
{
Client.ClientId = 1;
Client.Level = Player.Permission.Console;
Client.CurrentAlias = new EFAlias() { Name = "IW4MAdmin" };
}
Authorized = Client.ClientId >= 0;
2018-03-09 03:01:12 -05:00
ViewBag.Authorized = Authorized;
ViewBag.Url = Startup.Configuration["Web:Address"];
ViewBag.User = Client;
ViewBag.DiscordLink = DiscordLink;
2018-03-06 02:22:19 -05:00
base.OnActionExecuting(context);
}
}
}