Files
GPW/GPW_Admin/WebUserControls/mod_adminDipendenti.ascx.cs

661 lines
24 KiB
C#

using GPW_data;
using SteamWare;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace GPW_Admin.WebUserControls
{
public partial class mod_adminDipendenti : BaseUserControl
{
#region Public Properties
public int attivazioniOnline
{
get => licenzeGPW.attivazioniOnline;
}
public int IdxDipSel
{
get
{
int answ = 0;
if (grView.SelectedIndex >= 0)
{
var rawIdx = grView.SelectedValue.ToString();
int.TryParse(rawIdx, out answ);
}
return answ;
}
}
public bool isTicketReq
{
get
{
bool answ = false;
bool.TryParse(hfReqTicket.Value, out answ);
return answ;
}
set
{
hfReqTicket.Value = $"{value}";
}
}
public int numLicenze
{
get => licenzeGPW.licenzeAttive;
}
public int numLicenzeOnline
{
get => licenzeGPW.licenzeOnline;
}
public int numTickets
{
get => licenzeGPW.numTickets;
}
public bool showAll
{
get
{
return chkshowAll.Checked;
}
}
public bool showTickets
{
get
{
bool answ = false;
bool.TryParse(hfShowTickets.Value, out answ);
return answ;
}
set
{
hfShowTickets.Value = $"{value}";
}
}
public int utentiAttivi
{
get => licenzeGPW.utentiAttivi;
}
#endregion Public Properties
#region Private Methods
private static void fullRefresh()
{
licenzeGPW.resetLicenseData();
// eseguo call di recupero da online
bool refreshApp = licenzeGPW.RefreshAppInfo().Result;
bool refreshAct = licenzeGPW.RefreshActInfo().Result;
bool refreshPayload = licenzeGPW.RefreshPayload().Result;
// chiama update di TUTTE le authKey verso il server online
var listaUtenti = DataProxy.DP.taDipendenti.getAttivi(false);
foreach (var item in listaUtenti)
{
var CodImpiego = DataProxy.DP.hashCodImpiego(item);
bool fatto = licenzeGPW.tryRefreshActivation(CodImpiego, item.authKey).Result;
}
}
private void fixVisibility()
{
lbtFixMissing.Visible = (numLicenzeOnline == numLicenze && attivazioniOnline != utentiAttivi);
grView.PageSize = utils.pageSize;
}
#endregion Private Methods
#region Protected Methods
/// <summary>
/// gestione evento richiesta nuovo valore (mostra footer, ...)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnNew_Click(object sender, EventArgs e)
{
// reset selezione...
resetSelezione();
// mostro il footer oppure la riga dei dettagli x nuovo...
if (grView.FooterRow != null)
{
grView.FooterRow.Visible = true;
}
// sollevo evento nuovo valore...
//raiseAddNew();
}
/// <summary>
/// reset della selezione
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnReset_Click(object sender, EventArgs e)
{
isTicketReq = false;
showTickets = false;
resetSelezione();
}
protected void chkshowAll_CheckedChanged(object sender, EventArgs e)
{
grView.DataBind();
raiseReset();
}
/// <summary>
/// elenco colonne del datagrid
/// </summary>
/// <returns></returns>
protected DataColumnCollection colonneObj()
{
DataColumnCollection colonne = null;
using (DS_Applicazione.DipendentiDataTable tabella = new DS_Applicazione.DipendentiDataTable())
{
colonne = tabella.Columns;
}
return colonne;
}
/// <summary>
/// traduce gli header delle colonne
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void grView_DataBound(object sender, EventArgs e)
{
if (grView.Rows.Count > 0)
{
LinkButton lb;
// aggiorno gli headers
foreach (TableCell cella in grView.HeaderRow.Cells)
{
try
{
lb = (LinkButton)cella.Controls[0];
lb.Text = traduci(lb.Text);
}
catch
{ }
}
int totRecord = grView.Rows.Count + grView.PageSize * (grView.PageCount - 1);
lblNumRec.Text = string.Format("{0} records of ~ {1}", grView.Rows.Count, totRecord);
}
else
{
lblNumRec.Text = "";
}
}
protected void grView_RowEditing(object sender, GridViewEditEventArgs e)
{
if (grView.SelectedIndex >= 0)
{
resetSelezione();
}
}
protected void grView_SelectedIndexChanged(object sender, EventArgs e)
{
// altrimenti mostro gestione licenza DIP
raiseAddNew();
}
/// <summary>
/// annulla inserimento nuovo valore da footer
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lblCanc_click(object sender, EventArgs e)
{
// annullo inserimento: nascondo footer, bind controlli...
grView.FooterRow.Visible = false;
}
/// <summary>
/// inserisce nuovo valore da footer
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lblIns_click(object sender, EventArgs e)
{
// click su inserimento, chiamo il metodo insert dell'ObjectDataSource
ods.Insert();
}
protected void lbtFixMissing_Click(object sender, EventArgs e)
{
// procedo SOLO SE il numero online/offline è differente in primis...
if (numLicenzeOnline == numLicenze && attivazioniOnline != utentiAttivi)
{
// ciclo tutti gli utenti attivi
var localUserList = licenzeGPW.getDipAttivi();
// verifico SE sia disponibile licenza...
var activationsList = licenzeGPW.ListaAttivazioni;
Dictionary<string, string> CodList = new Dictionary<string, string>();
// ciclo x ogni dip attivo...
foreach (var item in localUserList)
{
// calcolo codImpiego
string currCodImp = DataProxy.DP.hashCodImpiego(item);
// cerco se abbia attivazione
var currActiv = activationsList.Where(x => x.CodImpiego == currCodImp).FirstOrDefault();
if (currActiv == null || currActiv.CodImpiego != currCodImp)
{
CodList.Add(currCodImp, item.authKey);
}
}
// provo attivazione in blocco
licenzeGPW.tryActivationMult(CodList).ConfigureAwait(false);
if (CodList.Count > 0)
{
raiseReset();
}
// cmq faccio refresh
fullRefresh();
}
}
protected void lbtRefresh_Click(object sender, EventArgs e)
{
fullRefresh();
grView.DataBind();
}
protected void lbtRegenUserKey_Click(object sender, EventArgs e)
{
// rigenera TUTTE le userkey utente
var elencoDip = licenzeGPW.getDipAttivi().ToList();
// ciclo x ogni utente...
foreach (var dipendente in elencoDip)
{
DateTime adesso = DateTime.Now;
string newKey = $"{dipendente.CF}-{adesso:yyMMdd-HHmmss}";
string md5UserAuthKey = SteamCrypto.EncryptString(newKey, "AuthGPW");
// aggiorno su DB
DataProxy.DP.taDipendenti.stp_Dip_setAuthKey(dipendente.idxDipendente, md5UserAuthKey);
// chiama resync dati licenza (cod impiego / codAuth)
bool fatto = licenzeGPW.tryRefreshActivation(DataProxy.DP.hashCodImpiego(dipendente), md5UserAuthKey).Result;
}
}
/// <summary>
/// Richiesta selezione utente
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbtSelect_Click(object sender, EventArgs e)
{
isTicketReq = false;
showTickets = false;
}
protected void lbtShowTickets_Click(object sender, EventArgs e)
{
// indico richiesta showTickets
isTicketReq = false;
showTickets = true;
// sollevo evento
raiseEvent();
}
/// <summary>
/// Richiesta inserimento ticket supporto
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbtTicket_Click(object sender, EventArgs e)
{
isTicketReq = true;
showTickets = false;
}
/// <summary>
/// check licenze in fase di eliminazione...
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ods_Deleting(object sender, ObjectDataSourceMethodEventArgs e)
{
if (e != null)
{
// cerco!
if (e.InputParameters["Original_idxDipendente"] != null)
{
string origIdxDip = $"{e.InputParameters["Original_idxDipendente"]}";
int idxDip = 0;
int.TryParse(origIdxDip, out idxDip);
if (idxDip > 0)
{
// disattivo la licenza corrente...
var localUserList = licenzeGPW.getDipAttivi();
var currUser = localUserList.Where(x => x.idxDipendente == idxDip).FirstOrDefault();
// verifico SE sia disponibile licenza...
var activationsList = licenzeGPW.ListaAttivazioni;
var currHash = DataProxy.DP.hashCodImpiego(currUser);
var currAct = activationsList.Where(x => x.CodImpiego == currHash).FirstOrDefault();
bool fatto = true;
if (currAct != null)
{
// riesegue controllo ed disattiva
Dictionary<string, string> ParamList = new Dictionary<string, string>();
ParamList.Add(currAct.CodImpiego, currAct.Chiave);
fatto = licenzeGPW.tryDeactivation(ParamList).Result;
}
licenzeGPW.resetDipAttivi();
// se NON ha disattivato NON elimina dipendente...
if (!fatto)
{
e.Cancel = true;
}
}
}
}
}
/// <summary>
/// check licenze in fase di update...
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ods_Updating(object sender, ObjectDataSourceMethodEventArgs e)
{
if (e != null)
{
if (!licenzeGPW.checkLicenze)
{
// annullo update se licenze sforate...
e.Cancel = true;
grView.EditIndex = -1;
grView.DataBind();
}
else
{
// cerco!
if (e.InputParameters["Original_idxDipendente"] != null)
{
string origIdxDip = $"{e.InputParameters["Original_idxDipendente"]}";
int idxDip = 0;
int.TryParse(origIdxDip, out idxDip);
if (idxDip > 0)
{
// disattivo la licenza corrente...
var localUserList = licenzeGPW.getDipAttivi();
var currUser = localUserList.Where(x => x.idxDipendente == idxDip).FirstOrDefault();
// verifico SE sia disponibile licenza...
var activationsList = licenzeGPW.ListaAttivazioni;
var currHash = DataProxy.DP.hashCodImpiego(currUser);
var currAct = activationsList.Where(x => x.CodImpiego == currHash).FirstOrDefault();
bool fatto = true;
if (currAct != null)
{
// riesegue controllo ed disattiva
Dictionary<string, string> ParamList = new Dictionary<string, string>();
ParamList.Add(currAct.CodImpiego, currAct.Chiave);
fatto = licenzeGPW.tryDeactivation(ParamList).Result;
}
// indico disattivato...
e.InputParameters["attivo"] = "false";
// trimmo campi "a rischio"
e.InputParameters["dominio"] = e.InputParameters["dominio"].ToString().Trim();
e.InputParameters["utente"] = e.InputParameters["utente"].ToString().Trim();
e.InputParameters["Cognome"] = e.InputParameters["Cognome"].ToString().Trim();
e.InputParameters["Nome"] = e.InputParameters["Nome"].ToString().Trim();
e.InputParameters["email"] = e.InputParameters["email"].ToString().Trim();
licenzeGPW.resetDipAttivi();
}
}
}
}
}
/// <summary>
/// caricamento
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
isTicketReq = false;
showTickets = false;
//fullRefresh();
}
fixVisibility();
}
/// <summary>
/// recupera i dati di un nuovo record contenuti nel footer di un gridView;
/// questi devono essere opportunamente nominati (es: txt{0}, dl{0}, ...)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void recuperaFooter(object sender, ObjectDataSourceMethodEventArgs e)
{
if (e != null)
{
if (chkLicOk)
{
//recupero la riga footer...
DataColumnCollection colonne = colonneObj();
string nomeCol;
string tipoColonna = "";
foreach (DataColumn colonna in colonne)
{
nomeCol = colonna.ColumnName;
// cerco un textbox o quello che sia...
if (grView.FooterRow.FindControl(string.Format("txt{0}", nomeCol)) != null)
{
tipoColonna = "textBox";
}
else if (grView.FooterRow.FindControl(string.Format("dl{0}", nomeCol)) != null)
{
tipoColonna = "dropDownList";
}
else if (grView.FooterRow.FindControl(string.Format("chk{0}", nomeCol)) != null)
{
tipoColonna = "checkBox";
}
// in base al tipo salvo negli inputparameters dell'ODS
switch (tipoColonna)
{
case "textBox":
e.InputParameters[nomeCol] = ((TextBox)grView.FooterRow.FindControl(string.Format("txt{0}", nomeCol))).Text.Trim();
break;
case "dropDownList":
e.InputParameters[nomeCol] = ((DropDownList)grView.FooterRow.FindControl(string.Format("dl{0}", nomeCol))).SelectedValue;
break;
case "checkBox":
e.InputParameters[nomeCol] = ((CheckBox)grView.FooterRow.FindControl(string.Format("chk{0}", nomeCol))).Checked;
break;
default:
break;
}
tipoColonna = "";
}
// attivo imposto a FALSE!
e.InputParameters["attivo"] = "false";
// imposto la sua authKey
string codCF = e.InputParameters["CF"].ToString();
if (string.IsNullOrEmpty(codCF))
{
codCF = "ABCDEF12G34H567I";
}
DateTime adesso = DateTime.Now;
string md5UserAuthKey = SteamCrypto.EncryptString($"{codCF}.{adesso:yyMMddHHmmss}", "AuthGPW");
e.InputParameters["authKey"] = $"{md5UserAuthKey}";
// fix confersione dt
DateTime dtNasc = DateTime.Today;
try
{
dtNasc = DateTime.Parse($"{e.InputParameters["dataNascita"]}");
}
catch
{ }
e.InputParameters["dataNascita"] = $"{dtNasc:yyyy-MM-dd}";
}
else
{
// annullo insert se licenze sforate...
e.Cancel = true;
grView.DataBind();
}
}
}
#endregion Protected Methods
#region Public Methods
/// <summary>
/// Verifica data assunzione valida = ultimi 60 anni...
/// </summary>
/// <param name="dtAss"></param>
/// <returns></returns>
public bool checkDataAssunzione(object dtAss)
{
bool answ = false;
DateTime oggi = DateTime.Today;
DateTime dataAss = oggi.AddYears(-100);
DateTime.TryParse($"{dtAss}", out dataAss);
answ = (oggi.Subtract(dataAss).TotalDays < 365 * 60);
return answ;
}
/// <summary>
/// Verifica licenza (se sia libera x modifica o prima associazione)
/// </summary>
/// <param name="idxDIp"></param>
/// <returns></returns>
public bool checkFreeEdit(object idxDip)
{
bool answ = false;
int IdxDip = 0;
int.TryParse($"{idxDip}", out IdxDip);
if (IdxDip > 0)
{
var localUserList = licenzeGPW.getDipAttivi();
// cerco!
var currUser = localUserList.Where(x => x.idxDipendente == IdxDip).FirstOrDefault();
// verifico SE sia disponibile licenza...
var activationsList = licenzeGPW.ListaAttivazioni;
var currHash = DataProxy.DP.hashCodImpiego(currUser);
var currAct = activationsList.Where(x => x.CodImpiego == currHash).FirstOrDefault();
// se NON c'è licenza è ok (editabile)
answ = (currAct == null);
// ora verifico: se NON ha licenza OK, altrimenti deve essere scaduta...
if (!answ)
{
answ = currAct.VetoUnlock <= DateTime.Today;
}
}
return answ;
}
/// <summary>
/// Verifica licenza (se sia validata/associata a quelle in memoria)
/// </summary>
/// <param name="idxDIp"></param>
/// <returns></returns>
public bool checkLic(object idxDip)
{
bool answ = false;
int IdxDip = 0;
int.TryParse($"{idxDip}", out IdxDip);
if (IdxDip > 0)
{
var localUserList = licenzeGPW.getDipAttivi();
// cerco!
var currUser = localUserList.Where(x => x.idxDipendente == IdxDip).FirstOrDefault();
// verifico SE sia disponibile licenza...
var activationsList = licenzeGPW.ListaAttivazioni;
var currHash = DataProxy.DP.hashCodImpiego(currUser);
var currAct = activationsList.Where(x => x.CodImpiego == currHash).FirstOrDefault();
answ = (currAct != null && currAct.CodImpiego == currHash);
}
return answ;
}
/// <summary>
/// determina se sia eliminabile il record (=non usato)
/// </summary>
/// <param name="idxMaker"></param>
/// <returns></returns>
public bool delEnabled(object idxObj)
{
bool answ = true;
// solo se ha diritti scrittura controllo
if (idxObj != null)
{
DateTime oggi = DateTime.Today.AddDays(1);
int timbTrovate = 0;
int raTrovate = 0;
int idxDip = 0;
bool dipAttivo = true;
_ = int.TryParse(idxObj.ToString(), out idxDip);
timbTrovate = DataProxy.DP.taTimb.getLastByDip(idxDip).Count;
raTrovate = DataProxy.DP.taRA.getByDipData(idxDip, oggi.AddYears(-10), oggi).Count;
var dipRow = DataProxy.DP.taDipendenti.getByIdx(idxDip);
if (dipRow != null && dipRow.Count > 0)
{
dipAttivo = dipRow[0].attivo;
}
// controllo se ci sono record correlati...
if ((timbTrovate + raTrovate) > 0 || dipAttivo)
{
answ = false;
}
}
return answ;
}
public string cssByActive(object isActive)
{
bool isAct = false;
bool.TryParse($"{isActive}", out isAct);
return isAct ? "text-dark" : "text-danger";
}
public void doUpdate()
{
fullRefresh();
fixVisibility();
grView.DataBind();
}
/// <summary>
/// resetta la selezione dei valori in caso di modifiche su altri controlli
/// </summary>
public void resetSelezione()
{
grView.SelectedIndex = -1;
grView.DataBind();
raiseReset();
}
#endregion Public Methods
}
}