2018-04-08 14:48:40 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Security.Claims;
|
2018-04-28 17:39:45 -04:00
|
|
|
|
using Microsoft.AspNetCore.Authentication;
|
2018-03-06 02:22:19 -05:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.Filters;
|
2018-04-08 14:48:40 -04:00
|
|
|
|
|
2018-04-08 02:44:42 -04:00
|
|
|
|
using SharedLibraryCore.Database;
|
|
|
|
|
using SharedLibraryCore.Database.Models;
|
2018-04-08 14:48:40 -04:00
|
|
|
|
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
|
|
|
|
|
{
|
2018-04-08 14:48:40 -04:00
|
|
|
|
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; }
|
2018-05-05 16:36:26 -04:00
|
|
|
|
protected SharedLibraryCore.Localization.Index Localization { get; private set; }
|
2018-04-26 02:13:04 -04:00
|
|
|
|
protected EFClient Client { get; private set; }
|
|
|
|
|
private static byte[] LocalHost = { 127, 0, 0, 1 };
|
2018-05-05 18:52:04 -04:00
|
|
|
|
private static string SocialLink;
|
|
|
|
|
private static string SocialTitle;
|
2018-03-06 02:22:19 -05:00
|
|
|
|
|
2018-04-26 02:13:04 -04:00
|
|
|
|
public BaseController()
|
2018-03-06 02:22:19 -05:00
|
|
|
|
{
|
2018-05-05 18:52:04 -04:00
|
|
|
|
if (Manager == null)
|
|
|
|
|
Manager = Program.Manager;
|
|
|
|
|
|
|
|
|
|
if (Localization == null)
|
|
|
|
|
Localization = SharedLibraryCore.Utilities.CurrentLocalization.LocalizationIndex;
|
|
|
|
|
|
2018-05-08 00:58:46 -04:00
|
|
|
|
if (Manager.GetApplicationSettings().Configuration().EnableSocialLink && SocialLink == null)
|
2018-04-26 02:13:04 -04:00
|
|
|
|
{
|
2018-05-08 00:58:46 -04:00
|
|
|
|
SocialLink = Manager.GetApplicationSettings().Configuration().SocialLinkAddress;
|
2018-05-05 18:52:04 -04:00
|
|
|
|
SocialTitle = Manager.GetApplicationSettings().Configuration().SocialLinkTitle;
|
2018-04-26 02:13:04 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-03-27 00:54:20 -04:00
|
|
|
|
|
2018-04-26 02:13:04 -04:00
|
|
|
|
public override void OnActionExecuting(ActionExecutingContext context)
|
|
|
|
|
{
|
|
|
|
|
Client = Client ?? new EFClient()
|
2018-03-27 00:54:20 -04:00
|
|
|
|
{
|
2018-04-28 21:11:13 -04:00
|
|
|
|
ClientId = -1,
|
|
|
|
|
Level = Player.Permission.User,
|
|
|
|
|
CurrentAlias = new EFAlias() { Name = "Web Console Guest" }
|
2018-03-27 00:54:20 -04:00
|
|
|
|
};
|
|
|
|
|
|
2018-04-26 02:13:04 -04:00
|
|
|
|
if (!HttpContext.Connection.RemoteIpAddress.GetAddressBytes().SequenceEqual(LocalHost))
|
2018-03-27 00:54:20 -04:00
|
|
|
|
{
|
2018-04-09 23:33:42 -04:00
|
|
|
|
try
|
|
|
|
|
{
|
2018-04-26 02:13:04 -04:00
|
|
|
|
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 };
|
2018-04-28 17:39:45 -04:00
|
|
|
|
var stillExists = Manager.GetPrivilegedClients()[Client.ClientId];
|
2018-04-28 21:11:13 -04:00
|
|
|
|
|
2018-04-28 17:39:45 -04:00
|
|
|
|
// this happens if their level has been updated
|
|
|
|
|
if (stillExists.Level != Client.Level)
|
|
|
|
|
{
|
|
|
|
|
Client.Level = stillExists.Level;
|
|
|
|
|
}
|
2018-04-09 23:33:42 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (InvalidOperationException)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
2018-04-28 17:39:45 -04:00
|
|
|
|
|
|
|
|
|
catch (System.Collections.Generic.KeyNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
// force the "banned" client to be signed out
|
2018-05-14 13:55:10 -04:00
|
|
|
|
HttpContext.SignOutAsync().Wait(5000);
|
2018-04-28 17:39:45 -04:00
|
|
|
|
}
|
2018-03-27 00:54:20 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-09 23:33:42 -04:00
|
|
|
|
else
|
2018-03-27 00:54:20 -04:00
|
|
|
|
{
|
2018-04-26 02:13:04 -04:00
|
|
|
|
Client.ClientId = 1;
|
|
|
|
|
Client.Level = Player.Permission.Console;
|
|
|
|
|
Client.CurrentAlias = new EFAlias() { Name = "IW4MAdmin" };
|
2018-03-27 00:54:20 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-26 02:13:04 -04:00
|
|
|
|
Authorized = Client.ClientId >= 0;
|
2018-03-09 03:01:12 -05:00
|
|
|
|
ViewBag.Authorized = Authorized;
|
2018-04-28 21:11:13 -04:00
|
|
|
|
ViewBag.Url = Manager.GetApplicationSettings().Configuration().WebfrontBindUrl;
|
2018-04-26 02:13:04 -04:00
|
|
|
|
ViewBag.User = Client;
|
2018-05-05 18:52:04 -04:00
|
|
|
|
ViewBag.SocialLink = SocialLink ?? "";
|
|
|
|
|
ViewBag.SocialTitle = SocialTitle;
|
2018-04-02 01:25:06 -04:00
|
|
|
|
|
2018-03-06 02:22:19 -05:00
|
|
|
|
base.OnActionExecuting(context);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|