1112 lines
34 KiB
C#
1112 lines
34 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Web;
|
|
|
|
namespace SteamWare
|
|
{
|
|
/// <summary>
|
|
/// classe gestione auth dispositivi (new 2014)
|
|
/// </summary>
|
|
public class devicesAuthProxy
|
|
{
|
|
#region area table adapters
|
|
/// <summary>
|
|
/// TableAdapter devices
|
|
/// </summary>
|
|
public DS_AuthTableAdapters.AnagDevicesTableAdapter taAnagDev;
|
|
/// <summary>
|
|
/// TableAdapter Utenti
|
|
/// </summary>
|
|
public DS_AuthTableAdapters.UtentiTableAdapter taUtenti;
|
|
/// <summary>
|
|
/// TableAdapter Diritti
|
|
/// </summary>
|
|
public DS_AuthTableAdapters.DIRITTITableAdapter taDiritti;
|
|
/// <summary>
|
|
/// TableAdapter permessi
|
|
/// </summary>
|
|
public DS_AuthTableAdapters.PermessiTableAdapter taPermessi;
|
|
/// <summary>
|
|
/// TableAdapter funzione
|
|
/// </summary>
|
|
public DS_AuthTableAdapters.FUNZIONETableAdapter taFunzione;
|
|
/// <summary>
|
|
/// TableAdapter permessi2fuzione
|
|
/// </summary>
|
|
public DS_AuthTableAdapters.Permessi2FunzioneTableAdapter taPermessi2Funzione;
|
|
|
|
/// <summary>
|
|
/// init dei table adapters
|
|
/// </summary>
|
|
protected void initTA()
|
|
{
|
|
taAnagDev = new DS_AuthTableAdapters.AnagDevicesTableAdapter();
|
|
taUtenti = new DS_AuthTableAdapters.UtentiTableAdapter();
|
|
taDiritti = new DS_AuthTableAdapters.DIRITTITableAdapter();
|
|
taPermessi = new DS_AuthTableAdapters.PermessiTableAdapter();
|
|
taFunzione = new DS_AuthTableAdapters.FUNZIONETableAdapter();
|
|
taPermessi2Funzione = new DS_AuthTableAdapters.Permessi2FunzioneTableAdapter();
|
|
}
|
|
/// <summary>
|
|
/// effettua setup dei connection strings da web.config delal singola applicazione
|
|
/// </summary>
|
|
protected virtual void setupConnectionStringBase()
|
|
{
|
|
// connections del db
|
|
string connString = memLayer.ML.confReadString("DevicesAuthConnectionString");
|
|
taAnagDev.Connection.ConnectionString = connString;
|
|
taUtenti.Connection.ConnectionString = connString;
|
|
taDiritti.Connection.ConnectionString = connString;
|
|
taPermessi.Connection.ConnectionString = connString;
|
|
taFunzione.Connection.ConnectionString = connString;
|
|
taPermessi2Funzione.Connection.ConnectionString = connString;
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
/* Classe che si occupa di:
|
|
* ----------------------------------------
|
|
* 1) gestione username
|
|
* 2) gestione ruoli da anagrafica
|
|
* 3) gestione declinazione ruoli in permessi
|
|
*
|
|
* in Standard GENERICO
|
|
*/
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="authProxy"/> class.
|
|
/// </summary>
|
|
protected devicesAuthProxy()
|
|
{
|
|
initTA();
|
|
setupConnectionStringBase();
|
|
}
|
|
/// <summary>
|
|
/// Singleton accesso a devicesAuthProxy (static object)
|
|
/// </summary>
|
|
public static devicesAuthProxy stObj = new devicesAuthProxy();
|
|
|
|
#region metodi verifica authKey, email, ...
|
|
|
|
/// <summary>
|
|
/// cifra in MD5 la stringa in chiaro
|
|
/// </summary>
|
|
/// <param name="chiaro"></param>
|
|
/// <returns></returns>
|
|
public static string encodeKey(string chiaro)
|
|
{
|
|
string answ = chiaro;
|
|
try
|
|
{
|
|
answ = SteamCrypto.EncryptString(chiaro, memLayer.ML.CRS("CodModulo"));
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
logger.lg.scriviLog(exc.ToString(), tipoLog.EXCEPTION);
|
|
}
|
|
return answ;
|
|
}
|
|
/// <summary>
|
|
/// decifra la stringa MD5 in chiaro
|
|
/// </summary>
|
|
/// <param name="cifrato"></param>
|
|
/// <returns></returns>
|
|
public static string decodeKey(string cifrato)
|
|
{
|
|
string answ = cifrato;
|
|
try
|
|
{
|
|
answ = SteamCrypto.DecryptString(cifrato, memLayer.ML.CRS("CodModulo"));
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
logger.lg.scriviLog(exc.ToString(), tipoLog.EXCEPTION);
|
|
}
|
|
return answ;
|
|
}
|
|
/// <summary>
|
|
/// verifica una email per appartenenza ad un utente VALIDO dell'elenco
|
|
/// </summary>
|
|
/// <param name="email"></param>
|
|
/// <returns></returns>
|
|
public bool checkUserEmail(string email)
|
|
{
|
|
bool answ = false;
|
|
try
|
|
{
|
|
answ = taUtenti.getByEmail(email).Rows.Count > 0;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
logger.lg.scriviLog(string.Format("Errore check user email: {0}", exc), tipoLog.EXCEPTION);
|
|
}
|
|
return answ;
|
|
}
|
|
/// <summary>
|
|
/// verifica una email + AuthKey per appartenenza ad un utente VALIDO dell'elenco
|
|
/// </summary>
|
|
/// <param name="email"></param>
|
|
/// <param name="AuthKey"></param>
|
|
/// <returns></returns>
|
|
public bool checkUserEmailAK(string email, string AuthKey)
|
|
{
|
|
bool answ = false;
|
|
int trovati = 0;
|
|
if (email != "" && AuthKey != "")
|
|
{
|
|
string md5UserAuthKey = encodeKey(AuthKey);
|
|
try
|
|
{
|
|
trovati += taUtenti.getByEmailAK(email, md5UserAuthKey).Rows.Count;
|
|
// se abilitato anche plain controlla pure li...
|
|
if (memLayer.ML.CRB("enablePlain"))
|
|
{
|
|
trovati += taUtenti.getByEmailAK(email, AuthKey).Rows.Count;
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
logger.lg.scriviLog(exc.ToString(), tipoLog.EXCEPTION);
|
|
}
|
|
answ = trovati > 0;
|
|
}
|
|
return answ;
|
|
}
|
|
/// <summary>
|
|
/// registra su DB la richiesta di reset della auth key dell'utente ed opzionalmente invia email ad admin
|
|
/// </summary>
|
|
/// <param name="email"></param>
|
|
/// <param name="adminEmail">opzionale, se != "" invia email all'indirizzo dell'admin x reset</param>
|
|
public void reqResetAuthKey(string email, string adminEmail)
|
|
{
|
|
// registro richiesta di reset KEY...
|
|
taUtenti.reqAuthKeyReset(email);
|
|
|
|
// se ho email admin...
|
|
if (adminEmail != "")
|
|
{
|
|
// mando email ad admin x reset utente...
|
|
string oggetto = string.Format("L'utente {0} ha richiesto il reset della sua AuthKey.<br/><br/>Seguire il <a href=\"{1}/{2}\">link seguente</a> per approvare o rifiutare la richiesta", email, memLayer.ML.CRS("baseUrl"), memLayer.ML.CRS("urlGestUtenti"));
|
|
gestEmail.geAuth.mandaEmail(email, adminEmail, "Richiesta reset AuthKey utente", oggetto);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// invia email ad utente con url x reset hash password
|
|
/// </summary>
|
|
/// <param name="email">destinatario</param>
|
|
/// <param name="hashPasswd">hashPasswd ATTUALE (in chiaro)</param>
|
|
/// <param name="webAppName"></param>
|
|
public void sendEmailResetHashPassword(string email, string hashPasswd, string webAppName)
|
|
{
|
|
// se ho email admin...
|
|
if (email != "")
|
|
{
|
|
// calcolo chiave MD5...
|
|
string md5hashPasswd = encodeKey(hashPasswd);
|
|
// mando email ad admin x reset utente...
|
|
string oggetto = string.Format("Hi,<br/><br/>This is an automatic message generated by {3} platform on behalf of platform admin.<br/><br/><a href=\"{0}/resetPassword?hash={1}&email={2}\">Please click on following link (or cut/paste to your preferred browser)</a> to reset your password to {3} login.<br/><br/>Regards.", memLayer.ML.CRS("baseUrl"), HttpUtility.UrlEncode(md5hashPasswd), email, webAppName);
|
|
gestEmail.geAuth.mandaEmail(memLayer.ML.CRS("_fromEmail"), email, webAppName, oggetto);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// invia email ad utente con url x enroll
|
|
/// </summary>
|
|
/// <param name="email"></param>
|
|
/// <param name="AuthKey">chiave (in chiaro)</param>
|
|
/// <param name="SiteName">nome del sito</param>
|
|
public void sendEmailAuthKey(string email, string AuthKey, string SiteName)
|
|
{
|
|
string body = "Hi,<br/><br/>This is an automatic message generated by {3} platform on behalf of platform admin.<br/><br/><a href=\"{0}/jumper?UserAuthkey={1}&USER_NAME={2}\">Please click on following link (or cut/paste to your preferred browser)</a> to enable current device to {3} login.<br/><br/>Regards.";
|
|
string subject = string.Format("AUTH access to {0}", SiteName);
|
|
sendEmailAuthKey(email, AuthKey, SiteName, subject, body);
|
|
}
|
|
/// <summary>
|
|
/// invia email ad utente con url x enroll (utilizzando il parametro baseURL x il sito)
|
|
/// </summary>
|
|
/// <param name="email"></param>
|
|
/// <param name="AuthKey">chiave (in chiaro)</param>
|
|
/// <param name="SiteName">nome del sito</param>
|
|
/// <param name="subject">chiave (in chiaro)</param>
|
|
/// <param name="body">body (in formato da completare string.format con 4 parametri che verrano inseriti come i seguenti:
|
|
/// {0} = baseURL - memLayer.ML.CRS("baseUrl")
|
|
/// {1} = chaive encoded - HttpUtility.UrlEncode(md5UserAuthKey)
|
|
/// {2} = email destinatario
|
|
/// {3} = subject
|
|
/// {4} = site name
|
|
/// </param>
|
|
public void sendEmailAuthKey(string email, string AuthKey, string SiteName, string subject, string body)
|
|
{
|
|
// se ho email admin...
|
|
if (email != "")
|
|
{
|
|
// calcolo chiave MD5...
|
|
string md5UserAuthKey = encodeKey(AuthKey);
|
|
// mando email ad admin x reset utente...
|
|
string oggetto = string.Format(body, memLayer.ML.CRS("baseUrl"), HttpUtility.UrlEncode(md5UserAuthKey), email, SiteName);
|
|
gestEmail.geAuth.mandaEmail(memLayer.ML.CRS("_fromEmail"), email, subject, oggetto);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// numero massimo auth concesse (se non trovo su conf prendo 1000)
|
|
/// </summary>
|
|
protected int maxAuth
|
|
{
|
|
get
|
|
{
|
|
int answ = memLayer.ML.CRI("maxAuth");
|
|
if (answ < 0)
|
|
{
|
|
answ = 1000;
|
|
}
|
|
|
|
return answ;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// effettua enroll del device x l'utente con l'email indicata
|
|
/// </summary>
|
|
/// <param name="UserAuthKey"></param>
|
|
/// <param name="IPv4"></param>
|
|
/// <param name="DeviceName"></param>
|
|
/// <param name="Description"></param>
|
|
/// <param name="email"></param>
|
|
/// <returns></returns>
|
|
public bool enrollDevice(string UserAuthKey, string IPv4, string DeviceName, string Description, string email)
|
|
{
|
|
bool fatto = false;
|
|
if (numAuth(UserAuthKey, email) > 0)
|
|
{
|
|
// calcolo il secret...
|
|
DateTime adesso = DateTime.Now;
|
|
string Secret = string.Format("{0}|{1}|{2}", email, DeviceName, adesso);
|
|
string devSecret = SteamCrypto.EncryptString(Secret, passphrase(memLayer.ML.confReadString("CodModulo"), UserAuthKey));
|
|
try
|
|
{
|
|
// registro chiave x il device
|
|
taAnagDev.Insert(devSecret, email, DeviceName, Description, adesso, adesso, IPv4);
|
|
// se condizione NORMALE (ovvero numAuth < maxAuth, altrimenti NON consuma...)
|
|
if (numAuth(UserAuthKey, email) <= maxAuth)
|
|
{
|
|
// registro "consumo" della authKey (-1 numAuth)
|
|
taUtenti.recordAuthKeyUse(email);
|
|
}
|
|
// salvo il cookie nel browser x 2 anni
|
|
memLayer.ML.setCookieVal(AuthCookieName, devSecret, DateTime.Now.AddYears(2));
|
|
// indico come fatto
|
|
fatto = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
logger.lg.scriviLog(string.Format("Eccezione in fase di enroll devices: parametri{0}UserAuthKey: {1}{0}IPv4: {2}{0}DeviceName: {3}{0}devSecret: {4}{0}email: {5}{0}Description: {6}{0}adesso: {7}{0}eccezione: {8}{0}", Environment.NewLine, UserAuthKey, IPv4, DeviceName, devSecret, email, Description, adesso, exc), tipoLog.EXCEPTION);
|
|
}
|
|
}
|
|
//esce...
|
|
return fatto;
|
|
}
|
|
/// <summary>
|
|
/// restituisce nome cookie di auth (o default...)
|
|
/// </summary>
|
|
public static string AuthCookieName
|
|
{
|
|
get
|
|
{
|
|
string answ = "AuthDevice";
|
|
try
|
|
{
|
|
answ = memLayer.ML.CRS("AuthCookieName");
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
logger.lg.scriviLog(exc.ToString(), tipoLog.EXCEPTION);
|
|
}
|
|
return answ;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// genera la passphrase utente a partire dai parametri richiesti
|
|
/// </summary>
|
|
/// <param name="CodMod"></param>
|
|
/// <param name="userKey"></param>
|
|
/// <returns></returns>
|
|
public string passphrase(string CodMod, string userKey)
|
|
{
|
|
return string.Format("{0}#{1}", CodMod, userKey);
|
|
}
|
|
/// <summary>
|
|
/// Restituisce il numero di attivazioni rimaste x utente dato email e key
|
|
/// </summary>
|
|
/// <param name="UserAuthKey"></param>
|
|
/// <param name="email"></param>
|
|
/// <returns></returns>
|
|
public int numAuth(string UserAuthKey, string email)
|
|
{
|
|
DS_Auth.UtentiRow rowUtenti;
|
|
int numAuth = 0;
|
|
try
|
|
{
|
|
// decodifica al volo la cifra...
|
|
rowUtenti = taUtenti.getByEmailAK(email, UserAuthKey)[0];
|
|
numAuth = rowUtenti.numAuth;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
logger.lg.scriviLog(exc.ToString(), tipoLog.EXCEPTION);
|
|
}
|
|
// ritorno
|
|
return numAuth;
|
|
}
|
|
/// <summary>
|
|
/// Restituisce il numero di attivazioni rimaste x utente dato email
|
|
/// </summary>
|
|
/// <param name="email"></param>
|
|
/// <returns></returns>
|
|
public int numAuth(string email)
|
|
{
|
|
DS_Auth.UtentiRow rowUtenti;
|
|
int numAuth = 0;
|
|
try
|
|
{
|
|
// decodifica al volo la cifra...
|
|
rowUtenti = taUtenti.getByEmail(email)[0];
|
|
numAuth = rowUtenti.numAuth;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
logger.lg.scriviLog(exc.ToString(), tipoLog.EXCEPTION);
|
|
}
|
|
// ritorno
|
|
return numAuth;
|
|
}
|
|
/// <summary>
|
|
/// Generazione stringa casuale di caratteri...
|
|
/// </summary>
|
|
/// <param name="size"></param>
|
|
/// <param name="lowerCase"></param>
|
|
/// <returns></returns>
|
|
public static string RandomString(int size, bool lowerCase)
|
|
{
|
|
StringBuilder builder = new StringBuilder();
|
|
Random random = new Random();
|
|
char ch;
|
|
for (int i = 0; i < size; i++)
|
|
{
|
|
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
|
|
builder.Append(ch);
|
|
}
|
|
if (lowerCase)
|
|
return builder.ToString().ToLower();
|
|
return builder.ToString();
|
|
}
|
|
|
|
/// <summary>
|
|
/// verifica la presenza di un cookie VALIDO per autorizzare il device e se lo trova avvia utente IN SESSIONE...
|
|
/// </summary>
|
|
public bool checkAuthCookie()
|
|
{
|
|
bool answ = false;
|
|
try
|
|
{
|
|
HttpCookie cookie = HttpContext.Current.Request.Cookies[AuthCookieName];
|
|
if (!(cookie == null || cookie.Value == ""))
|
|
{
|
|
// ricavo utente da cookie...
|
|
string userAgent = "";
|
|
string postazione_IP = "";
|
|
string devSecret = cookie.Value;
|
|
DS_Auth.AnagDevicesRow device = null;
|
|
// cerco il device...ogni dipendente può averne + di 1 registrato a suo nome...
|
|
string email = "";
|
|
try
|
|
{
|
|
device = taAnagDev.getByDeviceSecret(devSecret)[0];
|
|
email = device.USER_NAME;
|
|
}
|
|
catch
|
|
{ }
|
|
if (email != "")
|
|
{
|
|
// aggiorno descrizione (user agent) ed IP...
|
|
userAgent = HttpContext.Current.Request.UserAgent;
|
|
postazione_IP = HttpContext.Current.Request.UserHostAddress;
|
|
// controllo IP e DeviceDescription x eventuale update
|
|
if ((device.lastIPv4 != postazione_IP) || (device.Description != userAgent))
|
|
{
|
|
// salvo ultimo "contatto" del device aggiornando descrizione ed IP
|
|
taAnagDev.updateIP(device.IdxDevice, DateTime.Now, postazione_IP, userAgent);
|
|
}
|
|
// salvo in sessione utente
|
|
memLayer.ML.setSessionVal("email", email);
|
|
// avvio utente...
|
|
startUpUtente(email);
|
|
// salvo gruppo...
|
|
if (isAuth)
|
|
{
|
|
// se tutto ok
|
|
memLayer.ML.setSessionVal("Gruppo", rigaUtente.CodGruppo);
|
|
answ = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
logger.lg.scriviLog(string.Format("Errore in checkAuthCookie:{0}{1}", Environment.NewLine, exc), tipoLog.EXCEPTION);
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region area utente
|
|
|
|
/// <summary>
|
|
/// cancella da session l'utente
|
|
/// </summary>
|
|
public void clearAllUserData()
|
|
{
|
|
memLayer.ML.emptySessionVal("USER_NAME");
|
|
memLayer.ML.emptySessionVal("email");
|
|
memLayer.ML.emptySessionVal("dirittiUtente");
|
|
memLayer.ML.emptySessionVal("permessiUtente");
|
|
memLayer.ML.emptySessionVal("permessiUtenteWrite");
|
|
memLayer.ML.emptySessionVal("rigaUtente");
|
|
}
|
|
/// <summary>
|
|
/// restituisce la tabella diritti da session
|
|
/// </summary>
|
|
public DS_Auth.DIRITTIDataTable diritti
|
|
{
|
|
get
|
|
{
|
|
if (memLayer.ML.serializeSession)
|
|
{
|
|
return DataSetAdapter<DS_Auth.DIRITTIDataTable>.convert(memLayer.ML.dsSessionObj("dirittiUtente"));
|
|
}
|
|
else
|
|
{
|
|
return (DS_Auth.DIRITTIDataTable)memLayer.ML.objSessionObj("dirittiUtente");
|
|
}
|
|
}
|
|
set
|
|
{
|
|
if (memLayer.ML.serializeSession)
|
|
{
|
|
memLayer.ML.setSessionDataTable("dirittiUtente", value);
|
|
}
|
|
else
|
|
{
|
|
memLayer.ML.setSessionVal("dirittiUtente", value);
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// tabella dei permessi utente
|
|
/// </summary>
|
|
public DS_Auth.PermessiDataTable permessi
|
|
{
|
|
get
|
|
{
|
|
DS_Auth.PermessiDataTable tabPermessi = null;
|
|
try
|
|
{
|
|
if (memLayer.ML.serializeSession)
|
|
{
|
|
tabPermessi = DataSetAdapter<DS_Auth.PermessiDataTable>.convert(memLayer.ML.dsSessionObj("permessiUtente"));
|
|
}
|
|
else
|
|
{
|
|
tabPermessi = (DS_Auth.PermessiDataTable)memLayer.ML.objSessionObj("permessiUtente");
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
logger.lg.scriviLog(String.Format("Errore recupero permessi!{0}{1}", Environment.NewLine, exc), tipoLog.EXCEPTION);
|
|
}
|
|
return tabPermessi;
|
|
}
|
|
set
|
|
{
|
|
if (memLayer.ML.serializeSession)
|
|
{
|
|
memLayer.ML.setSessionDataTable("permessiUtente", value);
|
|
}
|
|
else
|
|
{
|
|
memLayer.ML.setSessionVal("permessiUtente", value);
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// tabella dei permessi utente di tipo "WRITE" enabled
|
|
/// </summary>
|
|
public DS_Auth.PermessiDataTable permessiWrite
|
|
{
|
|
get
|
|
{
|
|
if (memLayer.ML.serializeSession)
|
|
{
|
|
return DataSetAdapter<DS_Auth.PermessiDataTable>.convert(memLayer.ML.dsSessionObj("permessiUtenteWrite"));
|
|
}
|
|
else
|
|
{
|
|
return (DS_Auth.PermessiDataTable)memLayer.ML.objSessionObj("permessiUtenteWrite");
|
|
}
|
|
}
|
|
set
|
|
{
|
|
if (memLayer.ML.serializeSession)
|
|
{
|
|
memLayer.ML.setSessionDataTable("permessiUtenteWrite", value);
|
|
}
|
|
else
|
|
{
|
|
memLayer.ML.setSessionVal("permessiUtenteWrite", value);
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// oggetto utente con metodi get/set
|
|
/// </summary>
|
|
public string utente
|
|
{
|
|
get
|
|
{
|
|
return memLayer.ML.StringSessionObj("USER_NAME");
|
|
}
|
|
set
|
|
{
|
|
memLayer.ML.setSessionVal("USER_NAME", value, true);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// oggetto email con metodi get/set
|
|
/// </summary>
|
|
public string email
|
|
{
|
|
get
|
|
{
|
|
return memLayer.ML.StringSessionObj("email");
|
|
}
|
|
set
|
|
{
|
|
memLayer.ML.setSessionVal("email", value, true);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// oggetto DeviceSecret IN SESSIONE con metodi get/set
|
|
/// </summary>
|
|
public string DeviceSecret
|
|
{
|
|
get
|
|
{
|
|
return memLayer.ML.getCookieVal(AuthCookieName);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// oggetto modulo IN SESSIONE con metodi get/set
|
|
/// </summary>
|
|
public string modulo
|
|
{
|
|
get
|
|
{
|
|
return memLayer.ML.CRS("CodModulo");
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// restituisce i valori della riga utente da db
|
|
/// </summary>
|
|
public DS_Auth.UtentiRow rigaUtente
|
|
{
|
|
get
|
|
{
|
|
if (memLayer.ML.serializeSession)
|
|
{
|
|
DS_Auth.UtentiDataTable table = DataSetAdapter<DS_Auth.UtentiDataTable>.convert(memLayer.ML.dsSessionObj("rigaUtente"));
|
|
return table[0];
|
|
}
|
|
else
|
|
{
|
|
return (DS_Auth.UtentiRow)memLayer.ML.objSessionObj("rigaUtente");
|
|
}
|
|
}
|
|
set
|
|
{
|
|
if (memLayer.ML.serializeSession)
|
|
{
|
|
memLayer.ML.setSessionDataTable("rigaUtente", value.Table);
|
|
}
|
|
else
|
|
{
|
|
memLayer.ML.setSessionVal("rigaUtente", value);
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// restituisce una stringa formattata con cognome e nome
|
|
/// </summary>
|
|
public string CognomeNome
|
|
{
|
|
get
|
|
{
|
|
string answ = "";
|
|
if (isAuth)
|
|
{
|
|
answ = string.Format("{0} {1}", rigaUtente.cognome, rigaUtente.nome);
|
|
}
|
|
return answ;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// è un boolean che indica se in session ci siano user/email e DeviceSecret (cookie) e quindi utente autenticato in precedenza...
|
|
/// </summary>
|
|
public bool isAuth
|
|
{
|
|
get
|
|
{
|
|
return ((utente != "" || email != "") && DeviceSecret != "");
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// conta il numero di permessi utente per la pagina attuale e restituisce true se ne trova almeno 1
|
|
/// </summary>
|
|
/// <param name="pagina"></param>
|
|
/// <returns></returns>
|
|
public bool isPageEnabled(string pagina)
|
|
{
|
|
bool answ = false;
|
|
try
|
|
{
|
|
if (permessi != null)
|
|
{
|
|
// verifico ANCHE se ci sia una versione ".aspx" in +...
|
|
System.Data.DataRow[] righe = permessi.Select(string.Format("URL = '{0}.aspx' OR URL = '{0}'", pagina));
|
|
answ = (righe.Length >= 1);
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
logger.lg.scriviLog(exc.ToString(), tipoLog.EXCEPTION);
|
|
}
|
|
return answ;
|
|
}
|
|
/// <summary>
|
|
/// Verifica se l'utente sia noto al sistema...
|
|
/// </summary>
|
|
/// <param name="_username"></param>
|
|
/// <returns></returns>
|
|
public bool userIsKnown(string _username)
|
|
{
|
|
bool answ = false;
|
|
try
|
|
{
|
|
answ = taUtenti.getByEmail(_username).Rows.Count > 0;
|
|
}
|
|
catch
|
|
{ }
|
|
return answ;
|
|
}
|
|
/// <summary>
|
|
/// conta il numero di permessi utente per la pagina attuale e restituisce true se ne trova almeno 1
|
|
/// </summary>
|
|
/// <param name="pagina"></param>
|
|
/// <returns></returns>
|
|
public bool isPageSafe(string pagina)
|
|
{
|
|
bool answ = false;
|
|
try
|
|
{
|
|
string _safePages = memLayer.ML.CRS("_safePages");
|
|
answ = (_safePages.IndexOf(pagina) >= 0);
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
logger.lg.scriviLog(exc.ToString(), tipoLog.EXCEPTION);
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// verifica se il permesso utente per la pagina attuale sia write per almeno 1 diritto assegnato (restituisce true se ne trova almeno 1 con permessi2funzione.readwrite='S')
|
|
/// </summary>
|
|
/// <param name="pagina"></param>
|
|
/// <returns></returns>
|
|
public bool isPageWriteEnabled(string pagina)
|
|
{
|
|
bool answ = false;
|
|
try
|
|
{
|
|
if (permessiWrite != null)
|
|
{
|
|
System.Data.DataRow[] righe = permessiWrite.Select(string.Format("URL = '{0}.aspx' OR URL = '{0}'", pagina));
|
|
answ = (righe.Length >= 1);
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
logger.lg.scriviLog(exc.ToString(), tipoLog.EXCEPTION);
|
|
}
|
|
return answ;
|
|
}
|
|
/// <summary>
|
|
/// Procedura da chiamare DOPO aver messo in session i dati utente/email x caricare gli altri dati
|
|
/// </summary>
|
|
/// <param name="_username"></param>
|
|
/// <returns></returns>
|
|
public bool startUpUtente(string _username)
|
|
{
|
|
bool risultato = false;
|
|
try
|
|
{
|
|
clearAllUserData();
|
|
if (_username != "")
|
|
{
|
|
utente = _username;
|
|
email = _username; // !!!HARD CODED, user = email...
|
|
setupRiga();
|
|
setupDirittiPermessi();
|
|
setupMappaSito();
|
|
setupLingua();
|
|
risultato = true;
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
logger.lg.scriviLog(string.Format("Errore in fase di startUpUtente:{0}{1}", Environment.NewLine, exc), tipoLog.EXCEPTION);
|
|
}
|
|
return risultato;
|
|
}
|
|
/// <summary>
|
|
/// costruisce la mappa del sito per l'utente
|
|
/// </summary>
|
|
protected virtual void setupMappaSito()
|
|
{
|
|
string _mappa = "";
|
|
_mappa += "<mainMenu>";
|
|
try
|
|
{
|
|
// partiamo dai permessi utente di numero "0" (sono intestazioni menù...)
|
|
DS_Auth.PermessiRow[] vociMenu = (DS_Auth.PermessiRow[])permessi.Select("NUMERO ='0'", "GRUPPO");
|
|
foreach (DS_Auth.PermessiRow voce in vociMenu)
|
|
{
|
|
_mappa += formattaNodo(voce.NOME, voce.DESCRIZIONE, voce.URL, siteNodeType.startContainer);
|
|
// per ogni livello riempiamo con i permessi figli
|
|
DS_Auth.PermessiRow[] pagine = (DS_Auth.PermessiRow[])permessi.Select(string.Format("GRUPPO ='{0}' AND NUMERO > 0", voce.GRUPPO), "NUMERO");
|
|
foreach (DS_Auth.PermessiRow pagina in pagine)
|
|
{
|
|
_mappa += formattaNodo(pagina.NOME, pagina.DESCRIZIONE, pagina.URL, siteNodeType.leaf);
|
|
}
|
|
_mappa += formattaNodo(voce.NOME, voce.DESCRIZIONE, voce.URL, siteNodeType.endContainer);
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
logger.lg.scriviLog(exc.ToString(), tipoLog.EXCEPTION);
|
|
}
|
|
// salva in session...
|
|
_mappa += "</mainMenu>";
|
|
mappaSito = _mappa;
|
|
}
|
|
/// <summary>
|
|
/// formatta un nodo in modo corretto dai dati indicati
|
|
/// </summary>
|
|
/// <param name="titolo"></param>
|
|
/// <param name="descrizione"></param>
|
|
/// <param name="url"></param>
|
|
/// <param name="tipoNodo"></param>
|
|
protected string formattaNodo(string titolo, string descrizione, string url, siteNodeType tipoNodo)
|
|
{
|
|
string _out = "";
|
|
switch (tipoNodo)
|
|
{
|
|
case siteNodeType.startContainer:
|
|
_out = string.Format("<menu title=\"{0}\" description=\"{1}\" url=\"~/{2}\">", traduci(titolo), traduci(descrizione), url);
|
|
break;
|
|
case siteNodeType.endContainer:
|
|
_out = "</menu>";
|
|
break;
|
|
case siteNodeType.leaf:
|
|
_out = string.Format("<voce title=\"{0}\" description=\"{1}\" url=\"~/{2}\" />", traduci(titolo), traduci(descrizione), url);
|
|
break;
|
|
}
|
|
return _out;
|
|
}
|
|
|
|
/// <summary>
|
|
/// ricarica e ri-traduce la mappa sito per l'utente...
|
|
/// </summary>
|
|
public void ricaricaMappaSito()
|
|
{
|
|
memLayer.ML.emptySessionVal("dirittiUtente");
|
|
memLayer.ML.emptySessionVal("permessiUtente");
|
|
setupDirittiPermessi();
|
|
setupMappaSito();
|
|
}
|
|
/// <summary>
|
|
/// wrapper traduzione
|
|
/// </summary>
|
|
/// <param name="lemma"></param>
|
|
/// <returns></returns>
|
|
public string traduci(object lemma)
|
|
{
|
|
string answ = "";
|
|
if (lemma != null)
|
|
{
|
|
if (lemma.ToString() != "")
|
|
{
|
|
answ = user_std.UtSn.Traduci(lemma.ToString());
|
|
}
|
|
}
|
|
else
|
|
{
|
|
answ = "--";
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// carica la riga dati utente
|
|
/// </summary>
|
|
protected virtual void setupRiga()
|
|
{
|
|
try
|
|
{
|
|
rigaUtente = ((DS_Auth.UtentiRow)taUtenti.getByEmail(email).Rows[0]);
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
logger.lg.scriviLog(exc.ToString(), tipoLog.EXCEPTION);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Carica la tabella diritti dell'utente da db e salva in session
|
|
/// </summary>
|
|
protected virtual void setupDirittiPermessi()
|
|
{
|
|
try
|
|
{
|
|
diritti = taDiritti.getByUserModulo(utente, modulo); // salvo in session i diritti..
|
|
if (diritti.Count > 0)
|
|
{
|
|
setPermessiDaDiritti();
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
logger.lg.scriviLog(string.Format("Errore in fase di setupDirittiPermessi:{0}{1}", Environment.NewLine, exc), tipoLog.EXCEPTION);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Effettua setup dei permessi una volta salvati i diritti
|
|
/// </summary>
|
|
protected virtual void setPermessiDaDiritti()
|
|
{
|
|
// proseguo coi permessi
|
|
DS_Auth.PermessiDataTable allPermessi = taPermessi.GetData();
|
|
DS_Auth.Permessi2FunzioneDataTable allPerm2Funz = taPermessi2Funzione.GetData();
|
|
DS_Auth.PermessiDataTable _permessiUtente = new DS_Auth.PermessiDataTable();
|
|
DS_Auth.PermessiDataTable _permessiUtenteWrite = new DS_Auth.PermessiDataTable();
|
|
string filtroFunz, filtroPerm, filtroPermWrite;
|
|
filtroPerm = " COD_PERMESSO IN (";
|
|
filtroPermWrite = " COD_PERMESSO IN (";
|
|
// filtro i diritti utente x non avere duplicati...
|
|
Dictionary<string, string> funzioniUtente = new Dictionary<string, string>();
|
|
foreach (DS_Auth.DIRITTIRow riga in diritti)
|
|
{
|
|
try
|
|
{
|
|
funzioniUtente.Add(riga.COD_FUNZIONE, riga.COD_FUNZIONE);
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
logger.lg.scriviLog(exc.ToString(), tipoLog.EXCEPTION);
|
|
}
|
|
}
|
|
foreach (KeyValuePair<string, string> kvp in funzioniUtente)
|
|
{
|
|
filtroFunz = string.Format("COD_FUNZIONE='{0}'", kvp.Value);
|
|
// recupero le righe dei righe2perm
|
|
DS_Auth.Permessi2FunzioneRow[] righe_p2f = (DS_Auth.Permessi2FunzioneRow[])allPerm2Funz.Select(filtroFunz);
|
|
foreach (DS_Auth.Permessi2FunzioneRow riga_p2f in righe_p2f)
|
|
{
|
|
filtroPerm += string.Format("'{0}', ", riga_p2f.COD_PERMESSO);
|
|
// se è write metto in tab relativa...
|
|
try
|
|
{
|
|
if (riga_p2f.READWRITE == "S")
|
|
{
|
|
filtroPermWrite += string.Format("'{0}', ", riga_p2f.COD_PERMESSO);
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
logger.lg.scriviLog(exc.ToString(), tipoLog.EXCEPTION);
|
|
}
|
|
}
|
|
}
|
|
if (filtroPerm == " COD_PERMESSO IN (")
|
|
{
|
|
filtroPerm += "'PermessiNonTrovati' ";
|
|
}
|
|
filtroPerm = filtroPerm.Remove(filtroPerm.Length - 2);
|
|
filtroPerm += ") ";
|
|
DS_Auth.PermessiRow[] righePerm = (DS_Auth.PermessiRow[])allPermessi.Select(filtroPerm, "GRUPPO, NUMERO");
|
|
foreach (DS_Auth.PermessiRow rigaPerm in righePerm)
|
|
{
|
|
_permessiUtente.ImportRow(rigaPerm);
|
|
}
|
|
permessi = _permessiUtente;
|
|
// salvo, se ci sono, permessi write...
|
|
if (filtroPermWrite != " COD_PERMESSO IN (")
|
|
{
|
|
filtroPermWrite = filtroPermWrite.Remove(filtroPermWrite.Length - 2);
|
|
filtroPermWrite += ") ";
|
|
DS_Auth.PermessiRow[] righePermW = (DS_Auth.PermessiRow[])allPermessi.Select(filtroPermWrite, "GRUPPO, NUMERO");
|
|
foreach (DS_Auth.PermessiRow rigaPerm in righePermW)
|
|
{
|
|
_permessiUtenteWrite.ImportRow(rigaPerm);
|
|
}
|
|
}
|
|
permessiWrite = _permessiUtenteWrite;
|
|
}
|
|
/// <summary>
|
|
/// verifica nella tab diritti se l'utente abbia il right richiesto e fornisce bool in risposta
|
|
/// </summary>
|
|
/// <param name="diritto"></param>
|
|
/// <returns></returns>
|
|
public bool userHasRight(string diritto)
|
|
{
|
|
bool _answ = false;
|
|
try
|
|
{
|
|
if (diritti != null)
|
|
{
|
|
if (diritti.Select(String.Format("COD_FUNZIONE ='{0}'", diritto)).Length > 0)
|
|
{
|
|
_answ = true;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
logger.lg.scriviLog("userHasRight " + exc.ToString(), tipoLog.EXCEPTION);
|
|
}
|
|
return _answ;
|
|
}
|
|
/// <summary>
|
|
/// imposta la lingua utente dal valore della riga DB
|
|
/// </summary>
|
|
protected virtual void setupLingua()
|
|
{
|
|
string _lingua = "";
|
|
try
|
|
{
|
|
_lingua = rigaUtente.CodGruppo;
|
|
// se contiene "#" prendo primo dei 2 come codice...
|
|
if (_lingua.IndexOf("#") >= 0)
|
|
{
|
|
string[] dati = _lingua.Split('#');
|
|
_lingua = dati[0];
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
logger.lg.scriviLog(exc.ToString(), tipoLog.EXCEPTION);
|
|
}
|
|
if (string.IsNullOrEmpty(_lingua))
|
|
{
|
|
_lingua = "EN";
|
|
}
|
|
|
|
lingua = _lingua;
|
|
}
|
|
|
|
/// <summary>
|
|
/// oggetto lingua utente con metodi get/set
|
|
/// </summary>
|
|
public string lingua
|
|
{
|
|
get
|
|
{
|
|
return memLayer.ML.StringSessionObj("Lingua").ToUpper();
|
|
}
|
|
set
|
|
{
|
|
memLayer.ML.setSessionVal("Lingua", value);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// fornisce un file XML della mappa del sito abilitato per l'utente...
|
|
/// </summary>
|
|
public string mappaSito
|
|
{
|
|
get
|
|
{
|
|
return memLayer.ML.StringSessionObj("mappaSito");
|
|
}
|
|
set
|
|
{
|
|
memLayer.ML.setSessionVal("mappaSito", value);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region gestione pagina
|
|
|
|
/// <summary>
|
|
/// pagina correntemente visualizzata (URL in sessione)
|
|
/// </summary>
|
|
public static string pagCorrente
|
|
{
|
|
get
|
|
{
|
|
return memLayer.ML.StringSessionObj("pagCorrente");
|
|
}
|
|
set
|
|
{
|
|
memLayer.ML.setSessionVal("pagCorrente", value);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// pagina precedentemente visualizzata (URL in sessione)
|
|
/// </summary>
|
|
public static string pagPrecedente
|
|
{
|
|
get
|
|
{
|
|
return memLayer.ML.StringSessionObj("pagPrecedente");
|
|
}
|
|
set
|
|
{
|
|
memLayer.ML.setSessionVal("pagPrecedente", value);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// restituisce il nome della pagina corrente
|
|
/// </summary>
|
|
public static string getPage(Uri MyUrl)
|
|
{
|
|
string answ = "";
|
|
try
|
|
{
|
|
answ = MyUrl.LocalPath.Split('/').Last();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
logger.lg.scriviLog(exc.ToString(), tipoLog.EXCEPTION);
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
|
}
|