2019-01-03 14:39:22 -06:00
|
|
|
|
using Microsoft.AspNetCore.Authentication;
|
2019-08-04 20:38:55 -05:00
|
|
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2018-03-06 01:22:19 -06:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.Filters;
|
2018-04-08 01:44:42 -05:00
|
|
|
|
using SharedLibraryCore.Database;
|
|
|
|
|
using SharedLibraryCore.Database.Models;
|
2019-12-02 15:52:36 -06:00
|
|
|
|
using SharedLibraryCore.Dtos;
|
2018-04-08 13:48:40 -05:00
|
|
|
|
using SharedLibraryCore.Interfaces;
|
2019-01-03 14:39:22 -06:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Security.Claims;
|
2019-08-04 20:38:55 -05:00
|
|
|
|
using System.Threading.Tasks;
|
2018-03-06 01:22:19 -06:00
|
|
|
|
|
2019-12-02 15:52:36 -06:00
|
|
|
|
namespace SharedLibraryCore
|
2018-03-06 01:22:19 -06:00
|
|
|
|
{
|
|
|
|
|
public class BaseController : Controller
|
|
|
|
|
{
|
2019-08-04 20:38:55 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// life span in months
|
|
|
|
|
/// </summary>
|
|
|
|
|
private const int COOKIE_LIFESPAN = 3;
|
|
|
|
|
|
2019-12-02 15:52:36 -06:00
|
|
|
|
public IManager Manager { get; private set; }
|
2018-04-08 01:44:42 -05:00
|
|
|
|
protected readonly DatabaseContext Context;
|
2018-06-26 20:17:24 -05:00
|
|
|
|
protected bool Authorized { get; set; }
|
2018-05-05 15:36:26 -05:00
|
|
|
|
protected SharedLibraryCore.Localization.Index Localization { get; private set; }
|
2018-04-26 01:13:04 -05:00
|
|
|
|
protected EFClient Client { get; private set; }
|
2018-08-03 17:10:20 -05:00
|
|
|
|
private static readonly byte[] LocalHost = { 127, 0, 0, 1 };
|
2018-05-05 17:52:04 -05:00
|
|
|
|
private static string SocialLink;
|
|
|
|
|
private static string SocialTitle;
|
2019-01-03 14:39:22 -06:00
|
|
|
|
protected List<Page> Pages;
|
2018-03-06 01:22:19 -06:00
|
|
|
|
|
2019-12-02 15:52:36 -06:00
|
|
|
|
public BaseController(IManager manager)
|
2018-03-06 01:22:19 -06:00
|
|
|
|
{
|
2019-12-02 15:52:36 -06:00
|
|
|
|
Manager = manager;
|
2018-05-05 17:52:04 -05:00
|
|
|
|
|
|
|
|
|
if (Localization == null)
|
2019-01-03 14:39:22 -06:00
|
|
|
|
{
|
|
|
|
|
Localization = Utilities.CurrentLocalization.LocalizationIndex;
|
|
|
|
|
}
|
2018-05-05 17:52:04 -05:00
|
|
|
|
|
2018-05-07 23:58:46 -05:00
|
|
|
|
if (Manager.GetApplicationSettings().Configuration().EnableSocialLink && SocialLink == null)
|
2018-04-26 01:13:04 -05:00
|
|
|
|
{
|
2018-05-07 23:58:46 -05:00
|
|
|
|
SocialLink = Manager.GetApplicationSettings().Configuration().SocialLinkAddress;
|
2018-05-05 17:52:04 -05:00
|
|
|
|
SocialTitle = Manager.GetApplicationSettings().Configuration().SocialLinkTitle;
|
2018-04-26 01:13:04 -05:00
|
|
|
|
}
|
2018-09-16 15:34:16 -05:00
|
|
|
|
|
2019-01-03 14:39:22 -06:00
|
|
|
|
Pages = Manager.GetPageList().Pages
|
|
|
|
|
.Select(page => new Page
|
|
|
|
|
{
|
|
|
|
|
Name = page.Key,
|
|
|
|
|
Location = page.Value
|
|
|
|
|
}).ToList();
|
|
|
|
|
|
2018-09-16 15:34:16 -05:00
|
|
|
|
ViewBag.Version = Manager.Version;
|
2019-07-05 20:53:03 -05:00
|
|
|
|
ViewBag.IsFluid = false;
|
2019-08-02 18:04:34 -05:00
|
|
|
|
ViewBag.EnableColorCodes = Manager.GetApplicationSettings().Configuration().EnableColorCodes;
|
2018-04-26 01:13:04 -05:00
|
|
|
|
}
|
2018-03-26 23:54:20 -05:00
|
|
|
|
|
2019-08-04 20:38:55 -05: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 01:13:04 -05:00
|
|
|
|
public override void OnActionExecuting(ActionExecutingContext context)
|
|
|
|
|
{
|
|
|
|
|
Client = Client ?? new EFClient()
|
2018-03-26 23:54:20 -05:00
|
|
|
|
{
|
2018-04-28 20:11:13 -05:00
|
|
|
|
ClientId = -1,
|
2018-11-05 21:01:29 -06:00
|
|
|
|
Level = EFClient.Permission.User,
|
2019-03-29 21:56:56 -05:00
|
|
|
|
CurrentAlias = new EFAlias() { Name = "Webfront Guest" }
|
2018-03-26 23:54:20 -05:00
|
|
|
|
};
|
|
|
|
|
|
2018-04-26 01:13:04 -05:00
|
|
|
|
if (!HttpContext.Connection.RemoteIpAddress.GetAddressBytes().SequenceEqual(LocalHost))
|
2018-03-26 23:54:20 -05:00
|
|
|
|
{
|
2018-04-09 22:33:42 -05:00
|
|
|
|
try
|
|
|
|
|
{
|
2019-04-07 20:14:59 -05:00
|
|
|
|
int clientId = Convert.ToInt32(User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid)?.Value ?? "-1");
|
|
|
|
|
|
|
|
|
|
if (clientId > 0)
|
|
|
|
|
{
|
|
|
|
|
Client.ClientId = clientId;
|
2019-07-13 20:45:25 -05:00
|
|
|
|
Client.NetworkId = clientId == 1 ? 0 : User.Claims.First(_claim => _claim.Type == ClaimTypes.PrimarySid).Value.ConvertGuidToLong();
|
2019-04-07 20:14:59 -05: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 22:33:42 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (InvalidOperationException)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
2018-04-28 16:39:45 -05:00
|
|
|
|
|
2019-03-24 21:34:20 -05:00
|
|
|
|
catch (KeyNotFoundException)
|
2018-04-28 16:39:45 -05:00
|
|
|
|
{
|
|
|
|
|
// force the "banned" client to be signed out
|
2018-05-14 12:55:10 -05:00
|
|
|
|
HttpContext.SignOutAsync().Wait(5000);
|
2018-04-28 16:39:45 -05:00
|
|
|
|
}
|
2018-03-26 23:54:20 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-25 20:00:36 -06:00
|
|
|
|
// give the local host full access
|
2018-04-09 22:33:42 -05:00
|
|
|
|
else
|
2018-03-26 23:54:20 -05:00
|
|
|
|
{
|
2018-04-26 01:13:04 -05:00
|
|
|
|
Client.ClientId = 1;
|
2018-11-05 21:01:29 -06:00
|
|
|
|
Client.Level = EFClient.Permission.Console;
|
2018-04-26 01:13:04 -05:00
|
|
|
|
Client.CurrentAlias = new EFAlias() { Name = "IW4MAdmin" };
|
2018-11-25 20:00:36 -06:00
|
|
|
|
Authorized = true;
|
2019-08-04 20:38:55 -05:00
|
|
|
|
var claims = new[]
|
2019-07-29 12:08:25 -05:00
|
|
|
|
{
|
2019-08-04 20:38:55 -05: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-26 23:54:20 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-09 02:01:12 -06:00
|
|
|
|
ViewBag.Authorized = Authorized;
|
2019-01-03 14:39:22 -06:00
|
|
|
|
ViewBag.Url = Manager.GetApplicationSettings().Configuration().WebfrontUrl;
|
2018-04-26 01:13:04 -05:00
|
|
|
|
ViewBag.User = Client;
|
2018-05-05 17:52:04 -05:00
|
|
|
|
ViewBag.SocialLink = SocialLink ?? "";
|
|
|
|
|
ViewBag.SocialTitle = SocialTitle;
|
2019-01-03 14:39:22 -06:00
|
|
|
|
ViewBag.Pages = Pages;
|
2019-05-17 09:02:09 -05:00
|
|
|
|
ViewBag.Localization = Utilities.CurrentLocalization.LocalizationIndex;
|
2019-07-27 15:23:45 -05:00
|
|
|
|
ViewBag.CustomBranding = Manager.GetApplicationSettings().Configuration().WebfrontCustomBranding ?? "IW4MAdmin";
|
2019-08-02 18:04:34 -05:00
|
|
|
|
ViewBag.EnableColorCodes = Manager.GetApplicationSettings().Configuration().EnableColorCodes;
|
2018-08-03 17:10:20 -05:00
|
|
|
|
|
2018-03-06 01:22:19 -06:00
|
|
|
|
base.OnActionExecuting(context);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|