2022-01-26 11:32:16 -05:00
|
|
|
|
using System;
|
2019-01-03 15:39:22 -05:00
|
|
|
|
using System.Collections.Generic;
|
2022-01-26 11:32:16 -05:00
|
|
|
|
using System.Globalization;
|
2019-01-03 15:39:22 -05:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Security.Claims;
|
2019-08-04 21:38:55 -04:00
|
|
|
|
using System.Threading.Tasks;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
using Data.Models;
|
2022-01-26 11:32:16 -05:00
|
|
|
|
using Microsoft.AspNetCore.Authentication;
|
|
|
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.Filters;
|
2021-10-10 11:44:18 -04:00
|
|
|
|
using SharedLibraryCore.Configuration;
|
2022-01-26 11:32:16 -05:00
|
|
|
|
using SharedLibraryCore.Database.Models;
|
|
|
|
|
using SharedLibraryCore.Dtos;
|
|
|
|
|
using SharedLibraryCore.Interfaces;
|
|
|
|
|
using SharedLibraryCore.Localization;
|
2018-03-06 02:22:19 -05:00
|
|
|
|
|
2019-12-02 16:52:36 -05:00
|
|
|
|
namespace SharedLibraryCore
|
2018-03-06 02:22:19 -05:00
|
|
|
|
{
|
|
|
|
|
public class BaseController : Controller
|
|
|
|
|
{
|
2022-10-17 10:17:43 -04:00
|
|
|
|
protected readonly IInteractionRegistration InteractionRegistration;
|
2022-06-11 12:34:00 -04:00
|
|
|
|
protected readonly IAlertManager AlertManager;
|
|
|
|
|
|
2019-08-04 21:38:55 -04:00
|
|
|
|
/// <summary>
|
2022-01-26 11:32:16 -05:00
|
|
|
|
/// life span in months
|
2019-08-04 21:38:55 -04:00
|
|
|
|
/// </summary>
|
2022-06-15 20:37:34 -04:00
|
|
|
|
private const int CookieLifespan = 3;
|
2019-08-04 21:38:55 -04:00
|
|
|
|
|
2018-08-03 18:10:20 -04:00
|
|
|
|
private static readonly byte[] LocalHost = { 127, 0, 0, 1 };
|
2022-06-15 20:37:34 -04:00
|
|
|
|
private static string _socialLink;
|
|
|
|
|
private static string _socialTitle;
|
|
|
|
|
|
2019-01-03 15:39:22 -05:00
|
|
|
|
protected List<Page> Pages;
|
2022-04-04 23:16:40 -04:00
|
|
|
|
protected List<string> PermissionsSet;
|
2022-06-15 20:37:34 -04:00
|
|
|
|
protected bool Authorized { get; set; }
|
|
|
|
|
protected TranslationLookup Localization { get; }
|
|
|
|
|
protected EFClient Client { get; }
|
|
|
|
|
protected ApplicationConfiguration AppConfig { get; }
|
|
|
|
|
|
|
|
|
|
public IManager Manager { get; }
|
2018-03-06 02:22:19 -05:00
|
|
|
|
|
2019-12-02 16:52:36 -05:00
|
|
|
|
public BaseController(IManager manager)
|
2018-03-06 02:22:19 -05:00
|
|
|
|
{
|
2022-10-17 10:17:43 -04:00
|
|
|
|
InteractionRegistration = manager.InteractionRegistration;
|
2022-06-11 12:34:00 -04:00
|
|
|
|
AlertManager = manager.AlertManager;
|
2019-12-02 16:52:36 -05:00
|
|
|
|
Manager = manager;
|
2022-06-15 20:37:34 -04:00
|
|
|
|
Localization = Utilities.CurrentLocalization.LocalizationIndex;
|
2021-10-10 11:44:18 -04:00
|
|
|
|
AppConfig = Manager.GetApplicationSettings().Configuration();
|
2018-05-05 18:52:04 -04:00
|
|
|
|
|
2022-06-15 20:37:34 -04:00
|
|
|
|
if (AppConfig.EnableSocialLink && _socialLink == null)
|
2019-01-03 15:39:22 -05:00
|
|
|
|
{
|
2022-06-15 20:37:34 -04:00
|
|
|
|
_socialLink = AppConfig.SocialLinkAddress;
|
|
|
|
|
_socialTitle = AppConfig.SocialLinkTitle;
|
2018-04-26 02:13:04 -04:00
|
|
|
|
}
|
2018-09-16 16:34:16 -04:00
|
|
|
|
|
2019-01-03 15:39:22 -05:00
|
|
|
|
Pages = Manager.GetPageList().Pages
|
|
|
|
|
.Select(page => new Page
|
|
|
|
|
{
|
|
|
|
|
Name = page.Key,
|
|
|
|
|
Location = page.Value
|
|
|
|
|
}).ToList();
|
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
ViewBag.Version = Manager.Version;
|
2019-07-05 21:53:03 -04:00
|
|
|
|
ViewBag.IsFluid = false;
|
2021-10-10 11:44:18 -04:00
|
|
|
|
ViewBag.EnableColorCodes = AppConfig.EnableColorCodes;
|
2021-10-09 22:11:47 -04:00
|
|
|
|
ViewBag.Language = Utilities.CurrentLocalization.Culture.TwoLetterISOLanguageName;
|
2020-08-04 18:26:16 -04:00
|
|
|
|
|
2022-06-15 20:37:34 -04:00
|
|
|
|
Client = new EFClient
|
2020-08-04 18:26:16 -04:00
|
|
|
|
{
|
|
|
|
|
ClientId = -1,
|
2022-10-16 17:25:09 -04:00
|
|
|
|
Level = Data.Models.Client.EFClient.Permission.User,
|
2022-01-26 11:32:16 -05:00
|
|
|
|
CurrentAlias = new EFAlias { Name = "Webfront Guest" }
|
2020-08-04 18:26:16 -04:00
|
|
|
|
};
|
2018-04-26 02:13:04 -04:00
|
|
|
|
}
|
2022-10-17 10:17:43 -04:00
|
|
|
|
|
2019-08-04 21:38:55 -04:00
|
|
|
|
protected async Task SignInAsync(ClaimsPrincipal claimsPrinciple)
|
|
|
|
|
{
|
2022-01-26 11:32:16 -05:00
|
|
|
|
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrinciple,
|
|
|
|
|
new AuthenticationProperties
|
|
|
|
|
{
|
|
|
|
|
AllowRefresh = true,
|
2022-06-15 20:37:34 -04:00
|
|
|
|
ExpiresUtc = DateTime.UtcNow.AddMonths(CookieLifespan),
|
2022-01-26 11:32:16 -05:00
|
|
|
|
IsPersistent = true,
|
|
|
|
|
IssuedUtc = DateTime.UtcNow
|
|
|
|
|
});
|
2019-08-04 21:38:55 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-17 10:17:43 -04:00
|
|
|
|
public override async void OnActionExecuting(ActionExecutingContext context)
|
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
|
|
|
|
|
{
|
2022-01-26 11:32:16 -05:00
|
|
|
|
var clientId =
|
|
|
|
|
Convert.ToInt32(User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid)?.Value ?? "-1");
|
2019-04-07 21:14:59 -04:00
|
|
|
|
|
|
|
|
|
if (clientId > 0)
|
|
|
|
|
{
|
|
|
|
|
Client.ClientId = clientId;
|
2022-01-26 11:32:16 -05:00
|
|
|
|
Client.NetworkId = clientId == 1
|
|
|
|
|
? 0
|
2022-06-15 20:37:34 -04:00
|
|
|
|
: User.Claims.First(claim => claim.Type == ClaimTypes.PrimarySid).Value
|
2022-01-26 11:32:16 -05:00
|
|
|
|
.ConvertGuidToLong(NumberStyles.HexNumber);
|
|
|
|
|
Client.Level = (Data.Models.Client.EFClient.Permission)Enum.Parse(
|
|
|
|
|
typeof(Data.Models.Client.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 };
|
2019-04-07 21:14:59 -04:00
|
|
|
|
Authorized = Client.ClientId >= 0;
|
2022-06-15 20:37:34 -04:00
|
|
|
|
Client.GameName =
|
|
|
|
|
Enum.Parse<Reference.Game>(User.Claims
|
|
|
|
|
.First(claim => claim.Type == ClaimTypes.PrimaryGroupSid).Value);
|
2019-04-07 21:14:59 -04:00
|
|
|
|
}
|
2018-04-09 23:33:42 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (InvalidOperationException)
|
|
|
|
|
{
|
|
|
|
|
}
|
2018-04-28 17:39:45 -04:00
|
|
|
|
|
2019-03-24 22:34:20 -04:00
|
|
|
|
catch (KeyNotFoundException)
|
2018-04-28 17:39:45 -04:00
|
|
|
|
{
|
|
|
|
|
// 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-11-25 21:00:36 -05:00
|
|
|
|
// give the local host full access
|
2020-08-21 19:12:00 -04:00
|
|
|
|
else if (!HttpContext.Request.Headers.ContainsKey("X-Forwarded-For"))
|
2018-03-27 00:54:20 -04:00
|
|
|
|
{
|
2018-04-26 02:13:04 -04:00
|
|
|
|
Client.ClientId = 1;
|
2022-01-26 11:32:16 -05:00
|
|
|
|
Client.Level = Data.Models.Client.EFClient.Permission.Console;
|
|
|
|
|
Client.CurrentAlias = new EFAlias { Name = "IW4MAdmin" };
|
2018-11-25 21:00:36 -05:00
|
|
|
|
Authorized = true;
|
2019-08-04 21:38:55 -04:00
|
|
|
|
var claims = new[]
|
2019-07-29 13:08:25 -04:00
|
|
|
|
{
|
2019-08-04 21:38:55 -04:00
|
|
|
|
new Claim(ClaimTypes.NameIdentifier, Client.CurrentAlias.Name),
|
2022-01-26 11:32:16 -05:00
|
|
|
|
new Claim(ClaimTypes.Role, Client.Level.ToString()),
|
|
|
|
|
new Claim(ClaimTypes.Sid, Client.ClientId.ToString()),
|
2022-04-19 19:43:58 -04:00
|
|
|
|
new Claim(ClaimTypes.PrimarySid, Client.NetworkId.ToString("X")),
|
2022-06-15 20:37:34 -04:00
|
|
|
|
new Claim(ClaimTypes.PrimaryGroupSid, Client.GameName.ToString())
|
2019-08-04 21:38:55 -04:00
|
|
|
|
};
|
|
|
|
|
var claimsIdentity = new ClaimsIdentity(claims, "login");
|
|
|
|
|
SignInAsync(new ClaimsPrincipal(claimsIdentity)).Wait();
|
2018-03-27 00:54:20 -04:00
|
|
|
|
}
|
2022-04-04 23:16:40 -04:00
|
|
|
|
|
|
|
|
|
if (AppConfig.PermissionSets.ContainsKey(Client.Level.ToString()))
|
|
|
|
|
{
|
|
|
|
|
PermissionsSet = AppConfig.PermissionSets[Client.Level.ToString()];
|
|
|
|
|
}
|
2018-03-27 00:54:20 -04:00
|
|
|
|
|
2021-10-10 11:44:18 -04:00
|
|
|
|
var communityName = AppConfig.CommunityInformation?.Name;
|
2022-01-26 11:32:16 -05:00
|
|
|
|
var shouldUseCommunityName = !string.IsNullOrWhiteSpace(communityName)
|
|
|
|
|
&& !communityName.Contains("IW4MAdmin")
|
2021-10-10 11:44:18 -04:00
|
|
|
|
&& AppConfig.CommunityInformation.IsEnabled;
|
2022-01-26 11:32:16 -05:00
|
|
|
|
|
2022-10-17 10:17:43 -04:00
|
|
|
|
ViewBag.Interactions = await InteractionRegistration.GetInteractions("Webfront::Nav");
|
2018-03-09 03:01:12 -05:00
|
|
|
|
ViewBag.Authorized = Authorized;
|
2021-10-10 11:44:18 -04:00
|
|
|
|
ViewBag.Url = AppConfig.WebfrontUrl;
|
2018-04-26 02:13:04 -04:00
|
|
|
|
ViewBag.User = Client;
|
2021-08-30 21:30:06 -04:00
|
|
|
|
ViewBag.Version = Manager.Version;
|
2022-06-15 20:37:34 -04:00
|
|
|
|
ViewBag.SocialLink = _socialLink ?? "";
|
|
|
|
|
ViewBag.SocialTitle = _socialTitle;
|
2019-01-03 15:39:22 -05:00
|
|
|
|
ViewBag.Pages = Pages;
|
2019-05-17 10:02:09 -04:00
|
|
|
|
ViewBag.Localization = Utilities.CurrentLocalization.LocalizationIndex;
|
2021-10-09 22:11:47 -04:00
|
|
|
|
ViewBag.CustomBranding = shouldUseCommunityName
|
|
|
|
|
? communityName
|
2021-10-10 11:44:18 -04:00
|
|
|
|
: AppConfig.WebfrontCustomBranding ?? "IW4MAdmin";
|
|
|
|
|
ViewBag.EnableColorCodes = AppConfig.EnableColorCodes;
|
|
|
|
|
ViewBag.EnablePrivilegedUserPrivacy = AppConfig.EnablePrivilegedUserPrivacy;
|
|
|
|
|
ViewBag.Configuration = AppConfig;
|
2021-10-19 21:17:10 -04:00
|
|
|
|
ViewBag.ScriptInjection = AppConfig.Webfront?.ScriptInjection;
|
2022-04-19 19:43:58 -04:00
|
|
|
|
ViewBag.CommunityInformation = AppConfig.CommunityInformation;
|
|
|
|
|
ViewBag.ClientCount = Manager.GetServers().Sum(server => server.ClientNum);
|
|
|
|
|
ViewBag.AdminCount = Manager.GetServers().Sum(server =>
|
|
|
|
|
server.GetClientsAsList()
|
|
|
|
|
.Count(client => client.Level >= Data.Models.Client.EFClient.Permission.Trusted));
|
|
|
|
|
ViewBag.ReportCount = Manager.GetServers().Sum(server =>
|
|
|
|
|
server.Reports.Count(report => DateTime.UtcNow - report.ReportedOn <= TimeSpan.FromHours(24)));
|
|
|
|
|
ViewBag.PermissionsSet = PermissionsSet;
|
2023-05-14 22:44:28 -04:00
|
|
|
|
ViewBag.Alerts = AlertManager.RetrieveAlerts(Client);
|
2018-08-03 18:10:20 -04:00
|
|
|
|
|
2018-03-06 02:22:19 -05:00
|
|
|
|
base.OnActionExecuting(context);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-04 23:16:40 -04:00
|
|
|
|
}
|