162 lines
4.9 KiB
C#
162 lines
4.9 KiB
C#
using LiMan.DB.DBModels;
|
|
using LiMan.UI.Data;
|
|
using Microsoft.AspNetCore.Components;
|
|
using Microsoft.AspNetCore.Components.Authorization;
|
|
using Org.BouncyCastle.Bcpg;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace LiMan.UI.Components
|
|
{
|
|
// Vedere anche:
|
|
// https://docs.microsoft.com/en-us/aspnet/core/blazor/security/?view=aspnetcore-5.0#:~:text=Blazor%20uses%20the%20existing%20ASP.NET%20Core%20authentication%20mechanisms,all%20client-side%20code%20can%20be%20modified%20by%20users https://docs.microsoft.com/en-us/aspnet/core/blazor/security/?view=aspnetcore-5.0
|
|
public partial class CmpTop
|
|
{
|
|
#region Public Methods
|
|
|
|
public void Dispose()
|
|
{
|
|
MServ.EA_PageUpdated -= OnPageUpdate;
|
|
}
|
|
|
|
public void OnPageUpdate()
|
|
{
|
|
PageName = MServ.PageName;
|
|
PageIcon = MServ.PageIcon;
|
|
InvokeAsync(() =>
|
|
{
|
|
StateHasChanged();
|
|
});
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Protected Properties
|
|
|
|
[Inject]
|
|
protected MessageService MServ { get; set; } = null!;
|
|
|
|
[Inject]
|
|
protected LiManDataService DataService { get; set; } = null!;
|
|
|
|
protected List<AuthClaimModel> listClaims { get; set; } = new List<AuthClaimModel>();
|
|
|
|
protected int userId { get; set; } = 0;
|
|
|
|
#endregion Protected Properties
|
|
|
|
#region Protected Methods
|
|
|
|
protected override void OnInitialized()
|
|
{
|
|
MServ.EA_PageUpdated += OnPageUpdate;
|
|
}
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
await forceReload();
|
|
}
|
|
|
|
#endregion Protected Methods
|
|
|
|
#region Private Properties
|
|
|
|
[CascadingParameter]
|
|
private Task<AuthenticationState> AuthenticationStateTask { get; set; }
|
|
|
|
private string PageIcon { get; set; } = "";
|
|
|
|
private string PageName { get; set; } = "";
|
|
|
|
[CascadingParameter(Name = "ShowSearch")]
|
|
private bool ShowSearch { get; set; }
|
|
|
|
private string userName
|
|
{
|
|
get => MServ.UserName;
|
|
set => MServ.UserName = value;
|
|
}
|
|
|
|
#endregion Private Properties
|
|
|
|
#region Private Methods
|
|
|
|
private async Task checkAuthUser()
|
|
{
|
|
// verifico user su DB
|
|
var listUser = await DataService.AuthUserGetFilt(userName);
|
|
// se nullo --> creo!
|
|
if (listUser == null || listUser.Count == 0)
|
|
{
|
|
string adDomain = "Domain";
|
|
string adUser = "User";
|
|
if (userName.Contains("\\"))
|
|
{
|
|
var adInfo = userName.Split("\\");
|
|
adDomain = adInfo[0];
|
|
adUser = adInfo[1];
|
|
}
|
|
// creo record
|
|
AuthUserModel newRec = new AuthUserModel()
|
|
{
|
|
AD_Domain = adDomain,
|
|
AD_User = adUser,
|
|
Username = userName
|
|
};
|
|
// faccio upsert!
|
|
bool userOk = await DataService.AuthUserUpsert(newRec);
|
|
if (userOk)
|
|
{
|
|
// rileggo!
|
|
listUser = await DataService.AuthUserGetFilt(userName);
|
|
}
|
|
}
|
|
// recupero UserID...
|
|
userId = (listUser == null || listUser.Count == 0) ? 0 : listUser.FirstOrDefault().UserID;
|
|
|
|
// carico Claims utente da DB per averli in cache redis...
|
|
listClaims = await DataService.AuthClaimByUserName(userName);
|
|
// se fosse nulla --> aggiungo USER base!
|
|
if (listClaims == null || listClaims.Count == 0 && userId > 0)
|
|
{
|
|
// recupero user...
|
|
AuthClaimModel newClaim = new AuthClaimModel()
|
|
{
|
|
ClaimID = 0,
|
|
DtIns = DateTime.Now,
|
|
DtMod = DateTime.Now,
|
|
RoleID = 1,
|
|
UserID = userId
|
|
};
|
|
bool claimOk = await DataService.AuthClaimUpsert(newClaim);
|
|
if (claimOk)
|
|
{
|
|
listClaims = await DataService.AuthClaimByUserName(userName);
|
|
}
|
|
}
|
|
}
|
|
|
|
[Inject]
|
|
protected AuthenticationStateProvider AutheStateProvider { get; set; } = null!;
|
|
|
|
private async Task forceReload()
|
|
{
|
|
var authState = await AutheStateProvider.GetAuthenticationStateAsync();
|
|
var user = authState.User;
|
|
|
|
if (user.Identity.IsAuthenticated)
|
|
{
|
|
userName = $"{user.Identity.Name}";
|
|
await checkAuthUser();
|
|
}
|
|
else
|
|
{
|
|
userName = "N.A.";
|
|
}
|
|
}
|
|
|
|
#endregion Private Methods
|
|
}
|
|
} |