CodeLiturgy.Dashboard/CodeLiturgy.Views/Controllers/LoggedInUserController.cs

140 lines
3.7 KiB
C#
Raw Normal View History

2022-10-27 20:13:02 +03:00
using System.Net;
using System.Net.Sockets;
2022-11-18 03:15:53 +03:00
using CodeLiturgy.Data.Application.Users;
using CodeLiturgy.Data.Auth.Context.Users;
2022-10-30 19:48:24 +03:00
using CodeLiturgy.Views.Models;
using CodeLiturgy.Views.Utils;
2022-10-27 20:13:02 +03:00
using Duende.IdentityServer.Extensions;
using Microsoft.AspNetCore.Localization;
using Microsoft.AspNetCore.Mvc;
2022-10-30 19:48:24 +03:00
namespace CodeLiturgy.Views.Controllers;
2022-10-27 20:13:02 +03:00
/// <summary>
/// Base controller class for Views.
/// </summary>
public class LoggedInUserController : Controller
2022-10-27 20:13:02 +03:00
{
protected ILogger<LoggedInUserController> _logger;
2022-10-27 20:13:02 +03:00
protected ApplicationUserManager _userManager;
2022-11-17 00:17:44 +03:00
private List<Url> _footerMenu;
private List<Url> _headerMenu;
2022-10-27 20:13:02 +03:00
public LoggedInUserController(ApplicationUserManager userManager, ILogger<LoggedInUserController> logger)
2022-10-27 20:13:02 +03:00
{
_logger = logger;
_userManager = userManager;
2022-11-17 00:17:44 +03:00
_footerMenu = new List<Url>();
_headerMenu = new List<Url>();
2022-10-27 20:13:02 +03:00
}
public async Task OnEveryAction()
{
HandleGlobalization();
await SetUserProfileViewData();
OnInitialization();
SetFooterMenuViewData();
2022-11-17 00:17:44 +03:00
SetHeaderMenuViewData();
2022-10-27 20:13:02 +03:00
}
public virtual void OnInitialization()
{
}
protected void SetFooterMenuViewData()
{
ViewData[FooterMenuViewDataId] = _footerMenu;
}
2022-11-17 00:17:44 +03:00
protected void SetHeaderMenuViewData()
{
ViewData[HeaderMenuId] = _headerMenu;
}
public void SetFooterMenu(List<Url> urls)
{
_footerMenu = urls;
}
public void SetHeaderMenu(List<Url> urls)
2022-10-27 20:13:02 +03:00
{
2022-11-17 00:17:44 +03:00
_headerMenu = urls;
2022-10-27 20:13:02 +03:00
}
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);
if (user != null)
{
var dto = new ApplicationUserUnique()
{
Email = user.Email,
EmailConfirmed = user.EmailConfirmed,
NormalizedEmail = user.NormalizedEmail,
UserName = user.UserName,
LockoutEnabled = user.LockoutEnabled
};
return dto;
}
2022-10-27 20:13:02 +03:00
}
return null;
}
public IpInformation ExtractIpInformation(IPAddress ipAddress)
2022-10-27 20:13:02 +03:00
{
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;
2022-10-27 20:13:02 +03:00
IpInformation ipInformation = ExtractIpInformation(Request.HttpContext.Connection.RemoteIpAddress);
ViewData[IpViewStorage] = ipInformation;
}
}