420 lines
19 KiB
C#
420 lines
19 KiB
C#
using Newtonsoft.Json;
|
|
using SteamWare;
|
|
using System;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
namespace GPW_data
|
|
{
|
|
/// <summary>
|
|
/// proxy accesso ai dati
|
|
/// </summary>
|
|
public class DataProxy
|
|
{
|
|
#region Private Fields
|
|
|
|
private static int cacheTTL = 60 * 5;
|
|
|
|
private static string rkeyDipRow = memLayer.ML.redHash($"LiMan:DipRow");
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Public Fields
|
|
|
|
/// <summary>
|
|
/// Singleton accesso a DataProxy
|
|
/// </summary>
|
|
public static DataProxy DP = new DataProxy();
|
|
|
|
public DS_ApplicazioneTableAdapters.AnagFasiTableAdapter taAF;
|
|
public DS_ApplicazioneTableAdapters.AnagTagFasiTableAdapter taATF;
|
|
public DS_UtilityTableAdapters.AnagKeyValueTableAdapter taAKV;
|
|
public DS_ApplicazioneTableAdapters.AnagDevicesTableAdapter taAnagDev;
|
|
public DS_ApplicazioneTableAdapters.AnagClientiTableAdapter taAnCli;
|
|
public DS_ApplicazioneTableAdapters.AnagOrariTableAdapter taAO;
|
|
public DS_ApplicazioneTableAdapters.AnagProgettiTableAdapter taAP;
|
|
public DS_ApplicazioneTableAdapters.CalendFesteFerieTableAdapter taCFF;
|
|
public DS_ApplicazioneTableAdapters.Dipendenti2GruppiTableAdapter taDip2Gruppi;
|
|
public DS_ApplicazioneTableAdapters.Dipendenti2RuoliTableAdapter taDip2Ruoli;
|
|
public DS_ApplicazioneTableAdapters.stp_DipendentiAndAnomalieTableAdapter taDipAnom;
|
|
public DS_ApplicazioneTableAdapters.DipendentiTableAdapter taDipendenti;
|
|
public DS_exportTableAdapters.RegAtt_pivotTableAdapter taExpComm;
|
|
public DS_exportTableAdapters.TimbratureZucchettiTableAdapter taExpZucc;
|
|
public DS_ApplicazioneTableAdapters.GiustificativiTableAdapter taGiust;
|
|
public DS_ApplicazioneTableAdapters.AnagGruppiTableAdapter taGruppi;
|
|
public DS_ApplicazioneTableAdapters.HistTempTableAdapter taHT;
|
|
public DS_ApplicazioneTableAdapters.v_logCommUtTableAdapter taLCU;
|
|
public DS_UtilityTableAdapters.nextValContatoreTableAdapter taNextVal;
|
|
public DS_ApplicazioneTableAdapters.RegAttivitaTableAdapter taRA;
|
|
public DS_ApplicazioneTableAdapters.regAttDayExplTableAdapter taRAExpl;
|
|
public DS_ApplicazioneTableAdapters.RegistroEventiTableAdapter taRE;
|
|
public DS_ApplicazioneTableAdapters.RilievoTempTableAdapter taRT;
|
|
public DS_ApplicazioneTableAdapters.ListTagDDTableAdapter taListTDD;
|
|
public DS_ApplicazioneTableAdapters.RegistroMalattieTableAdapter taRM;
|
|
public DS_ApplicazioneTableAdapters.RegistroRichiesteTableAdapter taRR;
|
|
public DS_ApplicazioneTableAdapters.TagMeseTableAdapter taTagMese;
|
|
public DS_ApplicazioneTableAdapters.TimbratureTableAdapter taTimb;
|
|
public DS_ApplicazioneTableAdapters.TimbratureExplTableAdapter taTimbExpl;
|
|
public DS_ApplicazioneTableAdapters.TE_RA_ExplTableAdapter taTimbRegAtt;
|
|
public DS_UtilityTableAdapters.v_selDipendentiTableAdapter taVSD;
|
|
public DS_UtilityTableAdapters.v_selProjFasiTableAdapter taVSPF;
|
|
public DS_UtilityTableAdapters.stp_VSProjFasi_searchTableAdapter taVSProjFasiSearch;
|
|
|
|
#endregion Public Fields
|
|
|
|
#region Private Constructors
|
|
|
|
private DataProxy()
|
|
{
|
|
initTA();
|
|
setupConnectionStringBase();
|
|
}
|
|
|
|
#endregion Private Constructors
|
|
|
|
#region Public Properties
|
|
|
|
/// <summary>
|
|
/// Verifica se il dip corrente ha un record valido in payload
|
|
/// </summary>
|
|
public static bool currDipHasPayloadActive => _currDipHasPayloadActive();
|
|
|
|
/// <summary>
|
|
/// Verifica se il dip corrente sia attivo
|
|
/// </summary>
|
|
public static bool currDipIsActive => _currDipIsActive();
|
|
|
|
/// <summary>
|
|
/// idxDipendente in sessione
|
|
/// </summary>
|
|
public static int idxDipendente
|
|
{
|
|
get
|
|
{
|
|
int idx = 0;
|
|
try
|
|
{
|
|
idx = memLayer.ML.IntSessionObj("IdxDipendente");
|
|
}
|
|
catch
|
|
{ }
|
|
return idx;
|
|
}
|
|
set
|
|
{
|
|
memLayer.ML.setSessionVal("idxDipendente", value);
|
|
}
|
|
}
|
|
|
|
#endregion Public Properties
|
|
|
|
#region Private Methods
|
|
|
|
/// <summary>
|
|
/// Verifica se il dip corrente ha un record valido in payload
|
|
/// </summary>
|
|
private static bool _currDipHasPayloadActive()
|
|
{
|
|
bool answ = licenzeGPW.userPayloadOk(idxDipendente);
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Verifica se il dip corrente sia attivo
|
|
/// </summary>
|
|
private static bool _currDipIsActive()
|
|
{
|
|
bool answ = false;
|
|
try
|
|
{
|
|
DS_Applicazione.DipendentiRow rigaDip = DP.getRowDip(idxDipendente);
|
|
answ = rigaDip.attivo;
|
|
}
|
|
catch
|
|
{ }
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Verifica se il dip RICHIESTO sia attivo
|
|
/// </summary>
|
|
/// <param name="_idxDipendente"></param>
|
|
/// <returns></returns>
|
|
private bool checkDipIsActive(int _idxDipendente)
|
|
{
|
|
bool answ = false;
|
|
try
|
|
{
|
|
DS_Applicazione.DipendentiRow rigaDip = DP.getRowDip(_idxDipendente);
|
|
answ = rigaDip.attivo;
|
|
}
|
|
catch
|
|
{ }
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Calcola HASH del codice impiego
|
|
/// </summary>
|
|
/// <param name="rigaDip"></param>
|
|
/// <returns></returns>
|
|
private string getHashCodImpiego(DS_Applicazione.DipendentiRow rigaDip)
|
|
{
|
|
string hash = "";
|
|
string answ = "";
|
|
if (rigaDip != null)
|
|
{
|
|
answ = $"{rigaDip.idxDipendente}|{rigaDip.Cognome}.{rigaDip.Nome}|{rigaDip.CF}|{rigaDip.dataAssunzione:yyyyMMdd}|{rigaDip.email}|{rigaDip.matricola}";
|
|
}
|
|
// hashing!
|
|
using (var md5 = MD5.Create())
|
|
{
|
|
byte[] InputBytes = Encoding.UTF8.GetBytes(answ);
|
|
var byteHash = md5.ComputeHash(InputBytes);
|
|
hash = BitConverter.ToString(byteHash).Replace("-", "").ToLowerInvariant();
|
|
}
|
|
|
|
return hash;
|
|
}
|
|
|
|
/// <summary>
|
|
/// init dei table adapters
|
|
/// </summary>
|
|
private void initTA()
|
|
{
|
|
taAnCli = new DS_ApplicazioneTableAdapters.AnagClientiTableAdapter();
|
|
taListTDD = new DS_ApplicazioneTableAdapters.ListTagDDTableAdapter();
|
|
taTagMese = new DS_ApplicazioneTableAdapters.TagMeseTableAdapter();
|
|
taTimb = new DS_ApplicazioneTableAdapters.TimbratureTableAdapter();
|
|
taTimbExpl = new DS_ApplicazioneTableAdapters.TimbratureExplTableAdapter();
|
|
taAnagDev = new DS_ApplicazioneTableAdapters.AnagDevicesTableAdapter();
|
|
taDipendenti = new DS_ApplicazioneTableAdapters.DipendentiTableAdapter();
|
|
taDip2Gruppi = new DS_ApplicazioneTableAdapters.Dipendenti2GruppiTableAdapter();
|
|
taDip2Ruoli = new DS_ApplicazioneTableAdapters.Dipendenti2RuoliTableAdapter();
|
|
taAP = new DS_ApplicazioneTableAdapters.AnagProgettiTableAdapter();
|
|
taAF = new DS_ApplicazioneTableAdapters.AnagFasiTableAdapter();
|
|
taATF = new DS_ApplicazioneTableAdapters.AnagTagFasiTableAdapter();
|
|
taLCU = new DS_ApplicazioneTableAdapters.v_logCommUtTableAdapter();
|
|
taRA = new DS_ApplicazioneTableAdapters.RegAttivitaTableAdapter();
|
|
taRAExpl = new DS_ApplicazioneTableAdapters.regAttDayExplTableAdapter();
|
|
taAO = new DS_ApplicazioneTableAdapters.AnagOrariTableAdapter();
|
|
taGiust = new DS_ApplicazioneTableAdapters.GiustificativiTableAdapter();
|
|
taGruppi = new DS_ApplicazioneTableAdapters.AnagGruppiTableAdapter();
|
|
taDipAnom = new DS_ApplicazioneTableAdapters.stp_DipendentiAndAnomalieTableAdapter();
|
|
taTimbRegAtt = new DS_ApplicazioneTableAdapters.TE_RA_ExplTableAdapter();
|
|
taRE = new DS_ApplicazioneTableAdapters.RegistroEventiTableAdapter();
|
|
taCFF = new DS_ApplicazioneTableAdapters.CalendFesteFerieTableAdapter();
|
|
taRM = new DS_ApplicazioneTableAdapters.RegistroMalattieTableAdapter();
|
|
taRR = new DS_ApplicazioneTableAdapters.RegistroRichiesteTableAdapter();
|
|
taRT = new DS_ApplicazioneTableAdapters.RilievoTempTableAdapter();
|
|
taHT = new DS_ApplicazioneTableAdapters.HistTempTableAdapter();
|
|
taNextVal = new DS_UtilityTableAdapters.nextValContatoreTableAdapter();
|
|
taAKV = new DS_UtilityTableAdapters.AnagKeyValueTableAdapter();
|
|
taVSD = new DS_UtilityTableAdapters.v_selDipendentiTableAdapter();
|
|
taVSProjFasiSearch = new DS_UtilityTableAdapters.stp_VSProjFasi_searchTableAdapter();
|
|
taVSPF = new DS_UtilityTableAdapters.v_selProjFasiTableAdapter();
|
|
taExpZucc = new DS_exportTableAdapters.TimbratureZucchettiTableAdapter();
|
|
taExpComm = new DS_exportTableAdapters.RegAtt_pivotTableAdapter();
|
|
}
|
|
|
|
/// <summary>
|
|
/// effettua setup dei connection strings da web.config delal singola applicazione
|
|
/// </summary>
|
|
private void setupConnectionStringBase()
|
|
{
|
|
string connGPW = memLayer.ML.confReadString("GPWConnectionString");
|
|
// connections del db
|
|
taAnCli.Connection.ConnectionString = connGPW;
|
|
taListTDD.Connection.ConnectionString = connGPW;
|
|
taTagMese.Connection.ConnectionString = connGPW;
|
|
taTimb.Connection.ConnectionString = connGPW;
|
|
taTimbExpl.Connection.ConnectionString = connGPW;
|
|
taAnagDev.Connection.ConnectionString = connGPW;
|
|
taDipendenti.Connection.ConnectionString = connGPW;
|
|
taDip2Gruppi.Connection.ConnectionString = connGPW;
|
|
taDip2Ruoli.Connection.ConnectionString = connGPW;
|
|
taAP.Connection.ConnectionString = connGPW;
|
|
taAF.Connection.ConnectionString = connGPW;
|
|
taATF.Connection.ConnectionString = connGPW;
|
|
taLCU.Connection.ConnectionString = connGPW;
|
|
taRA.Connection.ConnectionString = connGPW;
|
|
taRAExpl.Connection.ConnectionString = connGPW;
|
|
taAO.Connection.ConnectionString = connGPW;
|
|
taGiust.Connection.ConnectionString = connGPW;
|
|
taGruppi.Connection.ConnectionString = connGPW;
|
|
taDipAnom.Connection.ConnectionString = connGPW;
|
|
taTimbRegAtt.Connection.ConnectionString = connGPW;
|
|
taRE.Connection.ConnectionString = connGPW;
|
|
taCFF.Connection.ConnectionString = connGPW;
|
|
taRM.Connection.ConnectionString = connGPW;
|
|
taRR.Connection.ConnectionString = connGPW;
|
|
taRT.Connection.ConnectionString = connGPW;
|
|
taHT.Connection.ConnectionString = connGPW;
|
|
taNextVal.Connection.ConnectionString = connGPW;
|
|
taAKV.Connection.ConnectionString = connGPW;
|
|
taVSD.Connection.ConnectionString = connGPW;
|
|
taVSProjFasiSearch.Connection.ConnectionString = connGPW;
|
|
taVSPF.Connection.ConnectionString = connGPW;
|
|
taExpZucc.Connection.ConnectionString = connGPW;
|
|
taExpComm.Connection.ConnectionString = connGPW;
|
|
}
|
|
|
|
#endregion Private Methods
|
|
|
|
#region Public Methods
|
|
|
|
public string cognomeNomeByIdx(int idxDip)
|
|
{
|
|
string answ = "";
|
|
var rigaDip = getRowDip(idxDip);
|
|
if (rigaDip != null)
|
|
{
|
|
answ = $"{rigaDip.Cognome} {rigaDip.Nome}";
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Verifica se il dip RICHIESTO sia attivo
|
|
/// </summary>
|
|
/// <param name="_idxDipendente"></param>
|
|
/// <returns></returns>
|
|
public bool dipIsActive(int _idxDipendente) => checkDipIsActive(_idxDipendente);
|
|
|
|
/// <summary>
|
|
/// effettua enroll del device
|
|
/// </summary>
|
|
/// <param name="UserAuthKey"></param>
|
|
/// <param name="IPv4"></param>
|
|
/// <param name="DeviceName"></param>
|
|
/// <param name="Description"></param>
|
|
/// <param name="idxDipendente"></param>
|
|
/// <returns></returns>
|
|
public bool enrollDevice(string UserAuthKey, string IPv4, string DeviceName, string Description, int idxDipendente)
|
|
{
|
|
bool fatto = false;
|
|
// in primis testo se dipendente ed authKey sono validi..
|
|
if (DataProxy.DP.taDipendenti.getByAuthKey(idxDipendente, UserAuthKey).Rows.Count > 0)
|
|
{
|
|
/************************************************
|
|
* Gestione riconoscimento devices
|
|
*
|
|
* - cerco IP del device, testo se è rete interna (A) o extranet/internet (B) cercando substring "localNet" da web.config
|
|
* (A): device interni: c'è un DHCP, e "dhcpLeaseTime" deve essere pari a lease time---
|
|
* - verifico se IP già in uso da un tempo < "dhcpLeaseTime" --> in questo caso segnalo errore e rimbalzo (NON permetto di registrare device a nuovo utente... sospetto uso "improprio" del device
|
|
* - se tempo > dhcpLeaseTime allora può aver cambiato IP: aggiorno IP e descrizione del device e proseguo
|
|
* (B): all'esterno vedo tutta una subnet NATtata con unico IP, non è + discriminante
|
|
* - le timbrature "da esterno" devono essere confermate (instrodurre "tipo timbratura" x cui interne sono autoconfermate, esterne sono "grayed" (da confermare, da admin o ad esempio se si accende secondo device utente entro 5/10 min)
|
|
* - le timbrature ext DOVREBBERO chiedere location (jScript?) e inviarla, visibile in conferma
|
|
*
|
|
*
|
|
* **********************************************/
|
|
|
|
//!!!RIFARE!!! non è + importante device name e description come distinti ma "quasi distinti" gli IP "intranet" o meglio anche ripetuto se a distanza di tempo
|
|
#if false
|
|
// controllo se esiste già device con quel secret code...
|
|
if (DataProxy.DP.taAnagDev.getByDeviceName(DeviceName).Rows.Count > 0)
|
|
{
|
|
// controllo se è inutilizzato da almeno XX minuti (da web.config), min 5 direi...
|
|
bool ip_decaduto = true; // fare cerco IP nei device name e cerco data ultimo utilizzo...
|
|
if (ip_decaduto)
|
|
{
|
|
// in questo caso elimino il record...
|
|
DataProxy.DP.taAnagDev.delByDeviceName(DeviceName);
|
|
}
|
|
else
|
|
{
|
|
}
|
|
}
|
|
#endif
|
|
// calcolo il secret...
|
|
DateTime adesso = DateTime.Now;
|
|
string Secret = string.Format("{0}|{1}|{2}", idxDipendente, DeviceName, adesso);
|
|
string devSecret = SteamCrypto.EncryptString(Secret, "AuthGPW");
|
|
try
|
|
{
|
|
DataProxy.DP.taAnagDev.insertQuery(devSecret, idxDipendente, DeviceName, Description, adesso, IPv4);
|
|
// registro "consumo" della authKey
|
|
DataProxy.DP.taDipendenti.recordAuthKeyUse(idxDipendente);
|
|
// salvo il cookie nel browser
|
|
memLayer.ML.setCookieVal("AuthGPW", devSecret, DateTime.Now.AddYears(5));
|
|
// indico come fatto
|
|
fatto = true;
|
|
}
|
|
catch
|
|
{ }
|
|
}
|
|
return fatto;
|
|
}
|
|
|
|
public DS_Applicazione.DipendentiRow getRowDip(int idxDip)
|
|
{
|
|
DS_Applicazione.DipendentiRow answ = null;
|
|
// solo se dip valido
|
|
if (idxDip > 0)
|
|
{
|
|
// cerco in redis...
|
|
string redKey = $"{rkeyDipRow}:{idxDip}";
|
|
string rawData = memLayer.ML.getRSV(redKey);
|
|
if (string.IsNullOrEmpty(rawData))
|
|
{
|
|
var tabDip = DP.taDipendenti.getByIdx(idxDip);
|
|
if (tabDip != null && tabDip.Rows.Count == 1)
|
|
{
|
|
answ = tabDip[0];
|
|
rawData = JsonConvert.SerializeObject(tabDip);
|
|
memLayer.ML.setRSV(redKey, rawData, cacheTTL);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
answ = JsonConvert.DeserializeObject<DS_Applicazione.DipendentiDataTable>(rawData)[0];
|
|
}
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Calcola HASH del codice impiego
|
|
/// </summary>
|
|
/// <param name="rigaDip"></param>
|
|
/// <returns></returns>
|
|
public string hashCodImpiego(DS_Applicazione.DipendentiRow rigaDip) => getHashCodImpiego(rigaDip);
|
|
|
|
/// <summary>
|
|
/// invia una mail al destinatario x linkare nuovi devices tramite URL
|
|
/// </summary>
|
|
/// <param name="destinatario"></param>
|
|
/// <param name="UserAuthKey"></param>
|
|
/// <param name="idxDipendente"></param>
|
|
/// <returns></returns>
|
|
public bool sendUserAuthEmail(string destinatario, string UserAuthKey, int idxDipendente)
|
|
{
|
|
bool fatto = false;
|
|
string mittente = "";
|
|
string oggetto = "";
|
|
string userUrl = "";
|
|
string baseUrl = "";
|
|
string userWebUrl = "";
|
|
string baseWebUrl = "";
|
|
string corpo = "";
|
|
try
|
|
{
|
|
// compongo la stringa
|
|
mittente = memLayer.ML.confReadString("_fromEmail");
|
|
oggetto = "Link autorizzazione device per GPW";
|
|
baseUrl = memLayer.ML.confReadString("baseUrl");
|
|
baseWebUrl = memLayer.ML.confReadString("baseWebUrl");
|
|
userUrl = string.Format("{2}jumper?UserAuthkey={0}&idxDipendente={1}", UserAuthKey, idxDipendente, baseUrl);
|
|
userWebUrl = string.Format("{2}jumper?UserAuthkey={0}&idxDipendente={1}", UserAuthKey, idxDipendente, baseWebUrl);
|
|
corpo = string.Format("Hai ricevuto questa email su richiesta tua o dell'Admin per poter procedere a registrare un (nuovo) devices con GPW:{0}<br/>Per proseguire clicca sul link seguente(rete interna):<br />{0}{0}{1}{0}{0}<br /><br />oppure sul link seguente (internet):<br />{0}{0}{2}{0}{0}<br /><br />Team GPW Steamware", Environment.NewLine, userUrl, userWebUrl);
|
|
fatto = gestEmail.geAuth.mandaEmail(mittente, destinatario, oggetto, corpo);
|
|
}
|
|
catch
|
|
{ }
|
|
return fatto;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
}
|
|
} |