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;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2018-03-06 02:22:19 -05:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.Filters;
|
2019-01-03 15:39:22 -05:00
|
|
|
|
using SharedLibraryCore;
|
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;
|
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;
|
2018-08-03 18:10:20 -04:00
|
|
|
|
using WebfrontCore.ViewModels;
|
2018-03-06 02:22:19 -05:00
|
|
|
|
|
|
|
|
|
namespace WebfrontCore.Controllers
|
|
|
|
|
{
|
|
|
|
|
public class BaseController : Controller
|
|
|
|
|
{
|
2019-08-04 21:38:55 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// life span in months
|
|
|
|
|
/// </summary>
|
|
|
|
|
private const int COOKIE_LIFESPAN = 3;
|
|
|
|
|
|
2018-04-08 14:48:40 -04:00
|
|
|
|
protected IManager Manager;
|
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; }
|
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; }
|
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
|
|
|
|
|
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)
|
2019-01-03 15:39:22 -05:00
|
|
|
|
{
|
2018-05-05 18:52:04 -04:00
|
|
|
|
Manager = Program.Manager;
|
2019-01-03 15:39:22 -05:00
|
|
|
|
}
|
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;
|
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)
|
|
|
|
|
{
|
|
|
|
|
Client = Client ?? new EFClient()
|
2018-03-27 00:54:20 -04:00
|
|
|
|
{
|
2018-04-28 21:11:13 -04:00
|
|
|
|
ClientId = -1,
|
2018-11-05 22:01:29 -05:00
|
|
|
|
Level = EFClient.Permission.User,
|
2019-03-29 22:56:56 -04:00
|
|
|
|
CurrentAlias = new EFAlias() { Name = "Webfront 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
|
|
|
|
|
{
|
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;
|
2019-07-13 21:45:25 -04:00
|
|
|
|
Client.NetworkId = clientId == 1 ? 0 : User.Claims.First(_claim => _claim.Type == ClaimTypes.PrimarySid).Value.ConvertGuidToLong();
|
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
|
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;
|
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;
|
2018-08-03 18:10:20 -04:00
|
|
|
|
|
2018-03-06 02:22:19 -05:00
|
|
|
|
base.OnActionExecuting(context);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|