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 /// /// gestione evento richiesta nuovo valore (mostra footer, ...) /// /// /// 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(); } /// /// reset della selezione /// /// /// protected void btnReset_Click(object sender, EventArgs e) { isTicketReq = false; showTickets = false; resetSelezione(); } protected void chkshowAll_CheckedChanged(object sender, EventArgs e) { grView.DataBind(); raiseReset(); } /// /// elenco colonne del datagrid /// /// protected DataColumnCollection colonneObj() { DataColumnCollection colonne = null; using (DS_Applicazione.DipendentiDataTable tabella = new DS_Applicazione.DipendentiDataTable()) { colonne = tabella.Columns; } return colonne; } /// /// traduce gli header delle colonne /// /// /// 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(); } /// /// annulla inserimento nuovo valore da footer /// /// /// protected void lblCanc_click(object sender, EventArgs e) { // annullo inserimento: nascondo footer, bind controlli... grView.FooterRow.Visible = false; } /// /// inserisce nuovo valore da footer /// /// /// 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 CodList = new Dictionary(); // 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; } } /// /// Richiesta selezione utente /// /// /// 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(); } /// /// Richiesta inserimento ticket supporto /// /// /// protected void lbtTicket_Click(object sender, EventArgs e) { isTicketReq = true; showTickets = false; } /// /// check licenze in fase di eliminazione... /// /// /// 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 ParamList = new Dictionary(); 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; } } } } } /// /// check licenze in fase di update... /// /// /// 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 ParamList = new Dictionary(); 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(); } } } } } /// /// caricamento /// /// /// protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { isTicketReq = false; showTickets = false; //fullRefresh(); } fixVisibility(); } /// /// recupera i dati di un nuovo record contenuti nel footer di un gridView; /// questi devono essere opportunamente nominati (es: txt{0}, dl{0}, ...) /// /// /// 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 /// /// Verifica data assunzione valida = ultimi 60 anni... /// /// /// 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; } /// /// Verifica licenza (se sia libera x modifica o prima associazione) /// /// /// 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; } /// /// Verifica licenza (se sia validata/associata a quelle in memoria) /// /// /// 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; } /// /// determina se sia eliminabile il record (=non usato) /// /// /// 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(); } /// /// resetta la selezione dei valori in caso di modifiche su altri controlli /// public void resetSelezione() { grView.SelectedIndex = -1; grView.DataBind(); raiseReset(); } #endregion Public Methods } }