2019-01-03 15:39:22 -05:00
|
|
|
|
using Microsoft.AspNetCore.Authentication;
|
2019-08-04 21:38:55 -04:00
|
|
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
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.Models;
|
2019-12-02 16:52:36 -05:00
|
|
|
|
using SharedLibraryCore.Dtos;
|
2018-04-08 14:48:40 -04:00
|
|
|
|
using SharedLibraryCore.Interfaces;
|
2019-01-03 15:39:22 -05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
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.Context;
|
|
|
|
|
using Data.Models;
|
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
|
|
|
|
|
{
|
2019-08-04 21:38:55 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// life span in months
|
|
|
|
|
/// </summary>
|
|
|
|
|
private const int COOKIE_LIFESPAN = 3;
|
|
|
|
|
|
2019-12-02 16:52:36 -05:00
|
|
|
|
public IManager Manager { get; private set; }
|
2018-04-08 02:44:42 -04:00
|
|
|
|
protected readonly DatabaseContext Context;
|
2018-06-26 21:17:24 -04:00
|
|
|
|
protected bool Authorized { get; set; }
|
2021-03-22 12:09:25 -04:00
|
|
|
|
protected Localization.TranslationLookup Localization { get; private set; }
|
2018-04-26 02:13:04 -04:00
|
|
|
|
protected EFClient Client { get; private set; }
|
2018-08-03 18:10:20 -04:00
|
|
|
|
private static readonly byte[] LocalHost = { 127, 0, 0, 1 };
|
2018-05-05 18:52:04 -04:00
|
|
|
|
private static string SocialLink;
|
|
|
|
|
private static string SocialTitle;
|
2019-01-03 15:39:22 -05:00
|
|
|
|
protected List<Page> Pages;
|
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
|
|
|
|
{
|
2019-12-02 16:52:36 -05:00
|
|
|
|
Manager = manager;
|
2018-05-05 18:52:04 -04:00
|
|
|
|
|
|
|
|
|
if (Localization == null)
|
2019-01-03 15:39:22 -05:00
|
|
|
|
{
|
|
|
|
|
Localization = Utilities.CurrentLocalization.LocalizationIndex;
|
|
|
|
|
}
|
2018-05-05 18:52:04 -04:00
|
|
|
|
|
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-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;
|
2019-08-02 19:04:34 -04:00
|
|
|
|
ViewBag.EnableColorCodes = Manager.GetApplicationSettings().Configuration().EnableColorCodes;
|
2020-08-04 18:26:16 -04:00
|
|
|
|
|
|
|
|
|
Client ??= new EFClient()
|
|
|
|
|
{
|
|
|
|
|
ClientId = -1,
|
2021-06-30 22:13:25 -04:00
|
|
|
|
Level = EFClient.Permission.Banned,
|
2020-08-04 18:26:16 -04:00
|
|
|
|
CurrentAlias = new EFAlias() { Name = "Webfront Guest" }
|
|
|
|
|
};
|
2018-04-26 02:13:04 -04:00
|
|
|
|
}
|
2018-03-27 00:54:20 -04:00
|
|
|
|
|
2019-08-04 21:38:55 -04:00
|
|
|
|
protected async Task SignInAsync(ClaimsPrincipal claimsPrinciple)
|
|
|
|
|
{
|
|
|
|
|
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrinciple, new AuthenticationProperties()
|
|
|
|
|
{
|
|
|
|
|
AllowRefresh = true,
|
|
|
|
|
ExpiresUtc = DateTime.UtcNow.AddMonths(COOKIE_LIFESPAN),
|
|
|
|
|
IsPersistent = true,
|
|
|
|
|
IssuedUtc = DateTime.UtcNow
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-26 02:13:04 -04:00
|
|
|
|
public override void OnActionExecuting(ActionExecutingContext context)
|
|
|
|
|
{
|
|
|
|
|
if (!HttpContext.Connection.RemoteIpAddress.GetAddressBytes().SequenceEqual(LocalHost))
|
2018-03-27 00:54:20 -04:00
|
|
|
|
{
|
2018-04-09 23:33:42 -04:00
|
|
|
|
try
|
|
|
|
|
{
|
2019-04-07 21:14:59 -04:00
|
|
|
|
int clientId = Convert.ToInt32(User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid)?.Value ?? "-1");
|
|
|
|
|
|
|
|
|
|
if (clientId > 0)
|
|
|
|
|
{
|
|
|
|
|
Client.ClientId = clientId;
|
2020-01-15 19:43:52 -05:00
|
|
|
|
Client.NetworkId = clientId == 1 ? 0 : User.Claims.First(_claim => _claim.Type == ClaimTypes.PrimarySid).Value.ConvertGuidToLong(System.Globalization.NumberStyles.HexNumber);
|
2019-04-07 21:14:59 -04:00
|
|
|
|
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 };
|
|
|
|
|
Authorized = Client.ClientId >= 0;
|
|
|
|
|
}
|
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;
|
2018-11-05 22:01:29 -05:00
|
|
|
|
Client.Level = EFClient.Permission.Console;
|
2018-04-26 02:13:04 -04:00
|
|
|
|
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),
|
|
|
|
|
new Claim(ClaimTypes.Role, Client.Level.ToString()),
|
|
|
|
|
new Claim(ClaimTypes.Sid, Client.ClientId.ToString()),
|
|
|
|
|
new Claim(ClaimTypes.PrimarySid, Client.NetworkId.ToString("X"))
|
|
|
|
|
};
|
|
|
|
|
var claimsIdentity = new ClaimsIdentity(claims, "login");
|
|
|
|
|
SignInAsync(new ClaimsPrincipal(claimsIdentity)).Wait();
|
2018-03-27 00:54:20 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-09 03:01:12 -05:00
|
|
|
|
ViewBag.Authorized = Authorized;
|
2019-01-03 15:39:22 -05:00
|
|
|
|
ViewBag.Url = Manager.GetApplicationSettings().Configuration().WebfrontUrl;
|
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;
|
2019-01-03 15:39:22 -05:00
|
|
|
|
ViewBag.Pages = Pages;
|
2019-05-17 10:02:09 -04:00
|
|
|
|
ViewBag.Localization = Utilities.CurrentLocalization.LocalizationIndex;
|
2019-07-27 16:23:45 -04:00
|
|
|
|
ViewBag.CustomBranding = Manager.GetApplicationSettings().Configuration().WebfrontCustomBranding ?? "IW4MAdmin";
|
2019-08-02 19:04:34 -04:00
|
|
|
|
ViewBag.EnableColorCodes = Manager.GetApplicationSettings().Configuration().EnableColorCodes;
|
2021-01-09 13:37:20 -05:00
|
|
|
|
ViewBag.EnablePrivilegedUserPrivacy = Manager.GetApplicationSettings().Configuration().EnablePrivilegedUserPrivacy;
|
2018-08-03 18:10:20 -04:00
|
|
|
|
|
2018-03-06 02:22:19 -05:00
|
|
|
|
base.OnActionExecuting(context);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|