29079a578f
- spostamento servizio AppAuth i proj generico
167 lines
4.8 KiB
C#
167 lines
4.8 KiB
C#
using Microsoft.AspNetCore.Components;
|
|
using Microsoft.AspNetCore.Components.Authorization;
|
|
using Microsoft.Extensions.Configuration;
|
|
using MP.AppAuth.Models;
|
|
using MP.AppAuth.Services;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MP.Land.Shared
|
|
{
|
|
public partial class NavMenu
|
|
{
|
|
#region Public Properties
|
|
|
|
[Parameter]
|
|
public EventCallback<bool> EC_compressUpdated { get; set; }
|
|
|
|
#endregion Public Properties
|
|
|
|
#region Protected Properties
|
|
|
|
[Inject]
|
|
protected AuthenticationStateProvider AuthStateProvider { get; set; } = null!;
|
|
|
|
[Inject]
|
|
protected AppAuthService DataService { get; set; }
|
|
|
|
protected string hideText { get => showText ? "" : "invisible"; }
|
|
|
|
protected bool IsSuperAdmin
|
|
{
|
|
get => HasRight(AppAuthService.RoleSuperAdmin);
|
|
}
|
|
|
|
[Inject]
|
|
protected NavigationManager NavManager { get; set; } = null!;
|
|
|
|
protected string pageName
|
|
{
|
|
get
|
|
{
|
|
string pName = NavManager.ToBaseRelativePath(NavManager.Uri).ToLower();
|
|
if (pName.Contains("?"))
|
|
{
|
|
pName = pName.Substring(0, pName.IndexOf("?"));
|
|
}
|
|
return pName;
|
|
}
|
|
}
|
|
|
|
protected bool showText { get; set; } = true;
|
|
|
|
protected List<PermessiModel> UserPerm { get; set; } = new List<PermessiModel>();
|
|
|
|
protected List<UserDirittiModel> UserRight { get; set; } = new List<UserDirittiModel>();
|
|
|
|
#endregion Protected Properties
|
|
|
|
#region Protected Methods
|
|
|
|
protected bool HasRight(string codFunz)
|
|
{
|
|
bool answ = false;
|
|
if (UserRight != null && UserRight.Count > 0)
|
|
{
|
|
answ = UserRight
|
|
.Where(x => x.Funzione.Equals(codFunz, System.StringComparison.InvariantCultureIgnoreCase))
|
|
.Count() > 0;
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
await ReloadData();
|
|
}
|
|
|
|
protected void ToggleCompress()
|
|
{
|
|
showText = !showText;
|
|
EC_compressUpdated.InvokeAsync(showText);
|
|
}
|
|
|
|
#endregion Protected Methods
|
|
|
|
#region Private Fields
|
|
|
|
private bool collapseNavMenu = true;
|
|
|
|
private bool onlyIcon = false;
|
|
private string SafePages = "";
|
|
private string userName = "";
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Private Properties
|
|
|
|
[Inject]
|
|
private IConfiguration ConfMan { get; set; } = null!;
|
|
|
|
private bool isLoading { get; set; } = false;
|
|
private string NavMenuCssClass => collapseNavMenu ? "collapse" : null;
|
|
|
|
private string? TextCss => onlyIcon ? "d-none" : "";
|
|
|
|
#endregion Private Properties
|
|
|
|
#region Private Methods
|
|
|
|
private void checkAuth()
|
|
{
|
|
// verifico pagina tra i permessi, se manca --> rimando a pagina unauth... se contiene
|
|
// index --> salto
|
|
if (!pageName.Contains("index"))
|
|
{
|
|
bool isAuth = false;
|
|
if (UserPerm != null)
|
|
{
|
|
isAuth = UserPerm.Where(x => x.Url.ToLower() == pageName).Count() > 0;
|
|
bool pageIsSafe = SafePages.ToLower().Contains($"|{pageName.ToLower()}|");
|
|
if (!pageIsSafe && !isAuth)
|
|
{
|
|
NavManager.NavigateTo("Unauthorized", true);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private async Task ReloadData()
|
|
{
|
|
isLoading = true;
|
|
// sistemo elenco pagine safe...
|
|
SafePages = ConfMan.GetValue<string>("Application:SafePages").ToLower();
|
|
var authState = await AuthStateProvider.GetAuthenticationStateAsync();
|
|
var user = authState.User;
|
|
if (user.Identity != null && user.Identity.IsAuthenticated)
|
|
{
|
|
userName = $"{user.Identity.Name}";
|
|
}
|
|
else
|
|
{
|
|
userName = "N.A.";
|
|
}
|
|
// carico diritti...
|
|
var domUser = userName.Split("\\");
|
|
if (domUser.Length > 0)
|
|
{
|
|
string dominio = domUser[0];
|
|
string uName = domUser[1];
|
|
UserRight = await DataService.DirittiGetByUser(uName);
|
|
UserPerm = await DataService.PermessiGetByUser(uName);
|
|
}
|
|
checkAuth();
|
|
await Task.Delay(1);
|
|
isLoading = false;
|
|
await Task.Delay(1);
|
|
}
|
|
|
|
private void ToggleNavMenu()
|
|
{
|
|
collapseNavMenu = !collapseNavMenu;
|
|
}
|
|
|
|
#endregion Private Methods
|
|
}
|
|
} |