112 lines
3.0 KiB
C#
112 lines
3.0 KiB
C#
using System.Net;
|
|
using System.Net.Sockets;
|
|
using BlueWest.Data.Application.Users;
|
|
using BlueWest.Data.Auth.Context.Users;
|
|
using CodeLiturgy.Views.Models;
|
|
using CodeLiturgy.Views.Utils;
|
|
using Duende.IdentityServer.Extensions;
|
|
using Microsoft.AspNetCore.Localization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace CodeLiturgy.Views.Controllers;
|
|
|
|
public class UserController : Controller
|
|
{
|
|
protected ILogger<UserController> _logger;
|
|
protected ApplicationUserManager _userManager;
|
|
|
|
private List<RouteRecord> _footerMenu;
|
|
|
|
public UserController(ApplicationUserManager userManager, ILogger<UserController> logger)
|
|
{
|
|
_logger = logger;
|
|
_userManager = userManager;
|
|
_footerMenu = new List<RouteRecord>();
|
|
}
|
|
|
|
public async Task OnEveryAction()
|
|
{
|
|
HandleGlobalization();
|
|
SetFooterMenuViewData();
|
|
await SetUserProfileViewData();
|
|
OnInitialization();
|
|
SetFooterMenuViewData();
|
|
}
|
|
|
|
public virtual void OnInitialization()
|
|
{
|
|
}
|
|
|
|
protected void SetFooterMenuViewData()
|
|
{
|
|
ViewData[FooterMenuViewDataId] = _footerMenu;
|
|
}
|
|
|
|
public void SetFooterMenu(List<RouteRecord> routeRecords)
|
|
{
|
|
_footerMenu = routeRecords;
|
|
}
|
|
|
|
public async Task SetUserProfileViewData()
|
|
{
|
|
if (!ViewData.ContainsKey(UserViewDataId))
|
|
{
|
|
ViewData[UserViewDataId] = await GetLoggedInUser();
|
|
}
|
|
}
|
|
|
|
public async Task<ApplicationUserUnique> GetLoggedInUser()
|
|
{
|
|
if (User.IsAuthenticated())
|
|
{
|
|
ApplicationUser? user = await _userManager.GetUserAsync(User);
|
|
return new ApplicationUserUnique(user);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
|
|
public IpInformation ExtractIpInformation(IPAddress? ipAddress)
|
|
{
|
|
string ipAddressString = "";
|
|
IpType ipType = IpType.Unknown;
|
|
|
|
if (ipAddress != null)
|
|
{
|
|
if (ipAddress.AddressFamily == AddressFamily.InterNetwork)
|
|
{
|
|
ipType = IpType.Ipv4;
|
|
ipAddressString = ipAddress.MapToIPv4().ToString();
|
|
}
|
|
|
|
if (ipAddress.AddressFamily == AddressFamily.InterNetworkV6)
|
|
{
|
|
ipType = IpType.Ipv6;
|
|
ipAddressString = ipAddress
|
|
.MapToIPv6()
|
|
.ToString();
|
|
}
|
|
}
|
|
|
|
|
|
return new IpInformation(ipType, ipAddressString);
|
|
}
|
|
|
|
public void HandleGlobalization()
|
|
{
|
|
var requestCultureFeature = HttpContext.Features.Get<IRequestCultureFeature>();
|
|
string currentCultureName = DefaultCultureName;
|
|
if (requestCultureFeature != null)
|
|
{
|
|
currentCultureName = requestCultureFeature.RequestCulture.Culture.Name;
|
|
}
|
|
|
|
ViewData[LanguageViewStorage] = currentCultureName;
|
|
IPAddress? remoteIpAddress = Request.HttpContext.Connection.RemoteIpAddress;
|
|
IpInformation ipInformation = ExtractIpInformation(Request.HttpContext.Connection.RemoteIpAddress);
|
|
|
|
|
|
ViewData[IpViewStorage] = ipInformation;
|
|
}
|
|
} |