532 lines
17 KiB
C#
532 lines
17 KiB
C#
using GWMS.Data;
|
|
using GWMS.UI.Data;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Components;
|
|
using Microsoft.AspNetCore.Components.Authorization;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.JSInterop;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace GWMS.UI.Pages
|
|
{
|
|
[Authorize(Roles = "SuperAdmin, Admin")]
|
|
public partial class UserAdmin : ComponentBase, IDisposable
|
|
{
|
|
#region Private Fields
|
|
|
|
/// <summary>
|
|
/// Elenco CLAIMS da mostrare durante editing
|
|
/// </summary>
|
|
private List<string> ClaimsList = new List<string>() { STD_CLAIM, "PlantId", "SupplierId", "TransporterId" };
|
|
|
|
/// <summary>
|
|
/// User corrente
|
|
/// </summary>
|
|
private IdentityUser objUser = new IdentityUser();
|
|
|
|
/// <summary>
|
|
/// Collezione utenti
|
|
/// </summary>
|
|
private List<IdentityUser> RawList = new List<IdentityUser>();
|
|
|
|
/// <summary>
|
|
/// Elenco ROLES da mostrare in dropdown durante editing (1 solo? usare DB?!?)
|
|
/// </summary>
|
|
private List<string> RolesList = new List<string>() { "Undef", "ExtTransp", "ExtUser", "User", "Admin", "SuperAdmin" };
|
|
|
|
// To enable showing the Popup
|
|
private bool ShowPopup = false;
|
|
|
|
// To hold any possible errors
|
|
private string strError = "";
|
|
|
|
/// <summary>
|
|
/// Collezione utenti
|
|
/// </summary>
|
|
private List<UserData> UsersList = new List<UserData>();
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Public Fields
|
|
|
|
public const string ADMIN_ROLE = "SuperAdmin";
|
|
public const string STD_CLAIM = "None";
|
|
public const string UNDEF_ROLE = "Undef";
|
|
|
|
#endregion Public Fields
|
|
|
|
#region Private Properties
|
|
|
|
private int _currPage { get; set; } = 1;
|
|
|
|
private int _numRecord { get; set; } = 10;
|
|
|
|
[CascadingParameter]
|
|
private Task<AuthenticationState> authenticationStateTask { get; set; }
|
|
|
|
private System.Security.Claims.Claim CurrentUserClaim
|
|
{
|
|
get => new System.Security.Claims.Claim(CurrentUserClaimType, CurrentUserClaimVal);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Claim di default (Tipo)
|
|
/// </summary>
|
|
private string CurrentUserClaimType { get; set; } = "None";
|
|
|
|
/// <summary>
|
|
/// Claim di default (valore)
|
|
/// </summary>
|
|
private string CurrentUserClaimVal { get; set; } = "0";
|
|
|
|
/// <summary>
|
|
/// Ruolo di default (User!!!)
|
|
/// </summary>
|
|
private string CurrentUserRole { get; set; } = "User";
|
|
|
|
private int currPage
|
|
{
|
|
get => _currPage;
|
|
set
|
|
{
|
|
if (_currPage != value)
|
|
{
|
|
_currPage = value;
|
|
var pUpd = Task.Run(async () => await ReloadData());
|
|
pUpd.Wait();
|
|
}
|
|
}
|
|
}
|
|
|
|
private bool isLoading { get; set; } = false;
|
|
|
|
[Inject]
|
|
private MessageService MessageService { get; set; }
|
|
|
|
private int numRecord
|
|
{
|
|
get => _numRecord;
|
|
set
|
|
{
|
|
if (_numRecord != value)
|
|
{
|
|
_numRecord = value;
|
|
var pUpd = Task.Run(async () => await ReloadData());
|
|
pUpd.Wait();
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion Private Properties
|
|
|
|
#region Protected Properties
|
|
|
|
[Inject]
|
|
protected RoleManager<IdentityRole> _RoleManager { get; set; }
|
|
|
|
[Inject]
|
|
protected UserManager<IdentityUser> _UserManager { get; set; }
|
|
|
|
[Inject]
|
|
protected MessageService AppMService { get; set; }
|
|
|
|
[Inject]
|
|
protected AuthenticationStateProvider AuthenticationStateProvider { get; set; }
|
|
|
|
[Inject]
|
|
protected IJSRuntime JSRuntime { get; set; }
|
|
|
|
protected int totalCount
|
|
{
|
|
get
|
|
{
|
|
int answ = 0;
|
|
if (RawList != null)
|
|
{
|
|
answ = RawList.Count;
|
|
}
|
|
return answ;
|
|
}
|
|
}
|
|
|
|
#endregion Protected Properties
|
|
|
|
#region Public Properties
|
|
|
|
public string searchVal
|
|
{
|
|
get
|
|
{
|
|
return MessageService.SearchVal;
|
|
}
|
|
}
|
|
|
|
#endregion Public Properties
|
|
|
|
#region Private Methods
|
|
|
|
private void AddNewUser()
|
|
{
|
|
// Make new user
|
|
objUser = new IdentityUser();
|
|
objUser.PasswordHash = "*****";
|
|
// Set Id to blank so we know it is a new record
|
|
objUser.Id = "";
|
|
// Open the Popup
|
|
ShowPopup = true;
|
|
}
|
|
|
|
private async Task CheckSuperAdmin()
|
|
{
|
|
// se non ci fosse --> creo samuele come superadmin
|
|
string superUser = "samuele@steamware.net";
|
|
string superPwd = "viaDante16!";
|
|
var user = await _UserManager.FindByEmailAsync(superUser);
|
|
if (user == null)
|
|
{
|
|
// Insert new user
|
|
var NewUser =
|
|
new IdentityUser
|
|
{
|
|
UserName = superUser,
|
|
Email = superUser,
|
|
EmailConfirmed = true
|
|
};
|
|
var CreateResult = await _UserManager.CreateAsync(NewUser, superPwd);
|
|
if (CreateResult.Succeeded)
|
|
{
|
|
user = await _UserManager.FindByEmailAsync(superUser);
|
|
}
|
|
}
|
|
|
|
// verifico ruoli...
|
|
if (user != null)
|
|
{
|
|
// Gestione salvataggio ruoli... SE VARIATO...
|
|
var UserRoles = await _UserManager.GetRolesAsync(user);
|
|
if (UserRoles != null && UserRoles.Count > 0)
|
|
{
|
|
var oldRole = UserRoles.Where(x => x == ADMIN_ROLE).FirstOrDefault();
|
|
if (oldRole == null)
|
|
{
|
|
// aggiungo a ruolo admin
|
|
await _UserManager.AddToRoleAsync(user, ADMIN_ROLE);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private async Task ClosePopup()
|
|
{
|
|
// Close the Popup
|
|
ShowPopup = false;
|
|
// Refresh Users
|
|
await GetUsers();
|
|
}
|
|
|
|
private async Task DeleteUser(IdentityUser _IdentityUser)
|
|
{
|
|
// Close the Popup
|
|
ShowPopup = false;
|
|
|
|
if (!await JSRuntime.InvokeAsync<bool>("confirm", "Sicuro di voler eliminare l'utente selezionato??"))
|
|
return;
|
|
|
|
// Get the user
|
|
var user = await _UserManager.FindByIdAsync(_IdentityUser.Id);
|
|
if (user != null)
|
|
{
|
|
// Delete the user
|
|
await _UserManager.DeleteAsync(user);
|
|
}
|
|
// Refresh Users
|
|
await GetUsers();
|
|
}
|
|
|
|
private void EditUser(IdentityUser _IdentityUser)
|
|
{
|
|
// selezione come current user
|
|
objUser = _IdentityUser;
|
|
var pUpd = Task.Run(async () =>
|
|
{
|
|
// Get the user
|
|
var user = await _UserManager.FindByIdAsync(objUser.Id);
|
|
if (user != null)
|
|
{
|
|
// salvo role
|
|
var UserRoles = await _UserManager.GetRolesAsync(user);
|
|
if (UserRoles != null)
|
|
{
|
|
CurrentUserRole = UserRoles.FirstOrDefault();
|
|
}
|
|
else
|
|
{
|
|
CurrentUserRole = UNDEF_ROLE;
|
|
}
|
|
|
|
// salvo claim
|
|
var UserClaims = await _UserManager.GetClaimsAsync(user);
|
|
if (UserClaims != null && UserClaims.Count > 0)
|
|
{
|
|
var CurrentUserClaim = UserClaims.FirstOrDefault();
|
|
CurrentUserClaimType = CurrentUserClaim.Type;
|
|
CurrentUserClaimVal = CurrentUserClaim.Value;
|
|
}
|
|
else
|
|
{
|
|
CurrentUserClaimType = STD_CLAIM;
|
|
CurrentUserClaimVal = "0";
|
|
}
|
|
}
|
|
});
|
|
pUpd.Wait();
|
|
// Open the Popup
|
|
ShowPopup = true;
|
|
}
|
|
|
|
private async Task ReloadData()
|
|
{
|
|
isLoading = true;
|
|
await GetUsers();
|
|
isLoading = false;
|
|
}
|
|
|
|
private async Task SaveUser()
|
|
{
|
|
try
|
|
{
|
|
// Is this an existing user?
|
|
if (objUser.Id != "")
|
|
{
|
|
// Get the user
|
|
var user = await _UserManager.FindByIdAsync(objUser.Id);
|
|
// Update Email + check email
|
|
user.Email = objUser.Email;
|
|
user.EmailConfirmed = objUser.EmailConfirmed;
|
|
// Update the user
|
|
await _UserManager.UpdateAsync(user);
|
|
// Only update password if the current value
|
|
// is not the default value
|
|
if (objUser.PasswordHash != "*****")
|
|
{
|
|
var resetToken =
|
|
await _UserManager.GeneratePasswordResetTokenAsync(user);
|
|
var passworduser =
|
|
await _UserManager.ResetPasswordAsync(
|
|
user,
|
|
resetToken,
|
|
objUser.PasswordHash);
|
|
if (!passworduser.Succeeded)
|
|
{
|
|
if (passworduser.Errors.FirstOrDefault() != null)
|
|
{
|
|
strError =
|
|
passworduser
|
|
.Errors
|
|
.FirstOrDefault()
|
|
.Description;
|
|
}
|
|
else
|
|
{
|
|
strError = "Password error";
|
|
}
|
|
// Keep the popup opened
|
|
return;
|
|
}
|
|
}
|
|
// Gestione salvataggio ruoli... SE VARIATO...
|
|
var UserRoles = await _UserManager.GetRolesAsync(user);
|
|
if (UserRoles != null && UserRoles.Count > 0)
|
|
{
|
|
var oldRole = UserRoles.FirstOrDefault();
|
|
if (!CurrentUserRole.Equals(oldRole))
|
|
{
|
|
// recupero il ruolo attuale e lo rimuovo
|
|
await _UserManager.RemoveFromRoleAsync(user, oldRole);
|
|
}
|
|
// aggiungo il nuovo ruolo
|
|
await _UserManager.AddToRoleAsync(user, CurrentUserRole);
|
|
}
|
|
else
|
|
{
|
|
// aggiungo il nuovo ruolo
|
|
await _UserManager.AddToRoleAsync(user, CurrentUserRole);
|
|
}
|
|
|
|
// gestione salvataggio Claims
|
|
var UserClaims = await _UserManager.GetClaimsAsync(user);
|
|
if (UserClaims != null && UserClaims.Count > 0)
|
|
{
|
|
var oldClaim = UserClaims.FirstOrDefault();
|
|
if (!oldClaim.Equals(CurrentUserClaim))
|
|
{
|
|
// recupero il ruolo attuale e lo rimuovo
|
|
await _UserManager.RemoveClaimAsync(user, oldClaim);
|
|
}
|
|
// aggiungo il nuovo ruolo
|
|
await _UserManager.AddClaimAsync(user, CurrentUserClaim);
|
|
}
|
|
else
|
|
{
|
|
// aggiungo il nuovo ruolo
|
|
await _UserManager.AddClaimAsync(user, CurrentUserClaim);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// Insert new user
|
|
var NewUser =
|
|
new IdentityUser
|
|
{
|
|
UserName = objUser.Email,
|
|
Email = objUser.Email
|
|
};
|
|
var CreateResult = await _UserManager.CreateAsync(NewUser, objUser.PasswordHash);
|
|
if (!CreateResult.Succeeded)
|
|
{
|
|
if (CreateResult
|
|
.Errors
|
|
.FirstOrDefault() != null)
|
|
{
|
|
strError =
|
|
CreateResult
|
|
.Errors
|
|
.FirstOrDefault()
|
|
.Description;
|
|
}
|
|
else
|
|
{
|
|
strError = "Create error";
|
|
}
|
|
// Keep the popup opened
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
// aggiungo a ruolo undef
|
|
await _UserManager.AddToRoleAsync(NewUser, UNDEF_ROLE);
|
|
// aggiungo claim undef...
|
|
await _UserManager.AddClaimAsync(NewUser, CurrentUserClaim);
|
|
}
|
|
}
|
|
// Close the Popup
|
|
ShowPopup = false;
|
|
// Refresh Users
|
|
await GetUsers();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
strError = ex.GetBaseException().Message;
|
|
}
|
|
}
|
|
|
|
#endregion Private Methods
|
|
|
|
#region Protected Methods
|
|
|
|
protected void ForceReload(int newNum)
|
|
{
|
|
numRecord = newNum;
|
|
}
|
|
|
|
protected void ForceReloadPage(int newNum)
|
|
{
|
|
currPage = newNum;
|
|
}
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
AppMService.ShowSearch = true;
|
|
AppMService.PageName = "Gestione Utenti";
|
|
AppMService.PageIcon = "fas fa-users pr-2";
|
|
AppMService.EA_SearchUpdated += OnSeachUpdated;
|
|
// lettura dati
|
|
await GetUsers();
|
|
await CheckSuperAdmin();
|
|
}
|
|
|
|
#endregion Protected Methods
|
|
|
|
#region Public Methods
|
|
|
|
public string checkSem(bool valore)
|
|
{
|
|
return valore ? "badge-success" : "badge-danger";
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
AppMService.EA_SearchUpdated -= OnSeachUpdated;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera elenco utenti
|
|
/// </summary>
|
|
public async Task GetUsers()
|
|
{
|
|
// clear any error messages
|
|
strError = "";
|
|
// Collection to hold users
|
|
UsersList = new List<UserData>();
|
|
// get users from _UserManager
|
|
var allData = _UserManager.Users.ToList();
|
|
if (!string.IsNullOrEmpty(searchVal))
|
|
{
|
|
RawList = allData.Where(x => x.NormalizedEmail.Contains(searchVal.ToUpper()) || x.NormalizedUserName.Contains(searchVal.ToUpper())).ToList();
|
|
}
|
|
else
|
|
{
|
|
RawList = allData;
|
|
}
|
|
var user = RawList.Skip(numRecord * (currPage - 1)).Take(numRecord).Select(x => new IdentityUser
|
|
{
|
|
Id = x.Id,
|
|
UserName = x.UserName,
|
|
Email = x.Email,
|
|
PhoneNumber = x.PhoneNumber,
|
|
PasswordHash = "*****",
|
|
EmailConfirmed = x.EmailConfirmed
|
|
}).ToList();
|
|
|
|
foreach (var item in user)
|
|
{
|
|
var UserRoles = await _UserManager.GetRolesAsync(item);
|
|
var UserClaims = await _UserManager.GetClaimsAsync(item);
|
|
|
|
var newItem = new UserData()
|
|
{
|
|
Identity = item,
|
|
Roles = UserRoles.ToList(),
|
|
Claims = UserClaims.ToList()
|
|
};
|
|
UsersList.Add(newItem);
|
|
}
|
|
}
|
|
|
|
public async void OnSeachUpdated()
|
|
{
|
|
await GetUsers();
|
|
await InvokeAsync(() =>
|
|
{
|
|
StateHasChanged();
|
|
});
|
|
}
|
|
|
|
public string ShowClaims(List<System.Security.Claims.Claim> ClaimList)
|
|
{
|
|
string answ = string.Join(",", ClaimList);
|
|
return answ;
|
|
}
|
|
|
|
public string ShowRoles(List<string> RoleList)
|
|
{
|
|
string answ = string.Join(",", RoleList);
|
|
return answ;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
}
|
|
} |