using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using SteamWare; namespace C2P.WebUserControls { public partial class mod_testata : System.Web.UI.UserControl { ///// ///// nome della pagina correntemente caricata ///// //public string _paginaCorrente; ///// ///// memorizza la pagina precedente (ovvero la corrente ma non da page-object ma in session... ///// //public string _paginaPrecedente; /// /// calcola currentPage /// protected string currPage { get { return Request.Url.LocalPath.Split('/').Last(); } } /// /// definisce visibilità logo: sempre SE la pagina non è quella default (su cui il logo è visibile in grande...) /// public bool logoVisible { get { return currPage != "Default"; } } /// /// titolo pagina /// public string titolo { get { return devicesAuthProxy.getPage(Request.Url); } } /// /// caricamento pagina /// /// /// protected void Page_Load(object sender, EventArgs e) { // altri controlli if (!Page.IsPostBack) { Page.Title = Request.Url.ToString(); // se ho cambiato pagina registro...) if (devicesAuthProxy.pagPrecedente != titolo) { devicesAuthProxy.pagPrecedente = devicesAuthProxy.pagCorrente; } // salvo apgina corrente devicesAuthProxy.pagCorrente = titolo; // fix logo imgLogo.Visible = logoVisible; PagCorrente(); memLayer.ML.emptySessionVal("searchVal"); } // SOLO se la pagina NON E' "safe"... if (memLayer.ML.confReadString("PageNoIndex").IndexOf(titolo) < 0) { bool userOk = checkUser(); bool pageOk = checkPageIsAuth(); // controllo se c'è utente in sessione.. if ((userOk)) { // ora controllo pagina... if (!pageOk) { if (devicesAuthProxy.stObj.isPageEnabled(devicesAuthProxy.pagPrecedente)) { Response.Redirect(devicesAuthProxy.pagPrecedente); } else { Response.Redirect("Home"); } } } else { // rimando pagina x registrazione devices Response.Redirect("UserAdmin"); } } } /// /// verifica che la pagina sia tra quelle autorizzate x l'utente /// /// private bool checkPageIsAuth() { bool allOk = false; bool pageSafe = (memLayer.ML.confReadString("PageNoIndex").IndexOf(titolo) >= 0); bool pageAuth = devicesAuthProxy.stObj.isPageEnabled(titolo); allOk = (pageSafe || pageAuth); return allOk; } /// /// verifica presenza utente autorizzato in sessione /// private bool checkUser() { bool allOk = devicesAuthProxy.stObj.isAuth; if (!devicesAuthProxy.stObj.isAuth) { // controllo cookie device... allOk = checkAuthCookieC2P(); if (!allOk) { ResetUser(); } } return allOk; } /// /// verifica la presenza di un cookie VALIDO per autorizzare il device ed avvia utente... /// private bool checkAuthCookieC2P() { bool answ = false; try { HttpCookie cookie = Request.Cookies["AuthDevice"]; 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 = devicesAuthProxy.stObj.taAnagDev.getByDeviceSecret(devSecret)[0]; email = device.USER_NAME; } catch { } if (email != "") { // aggiorno descrizione (user agent) ed IP... userAgent = Request.UserAgent; postazione_IP = 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 devicesAuthProxy.stObj.taAnagDev.updateIP(device.IdxDevice, DateTime.Now, postazione_IP, userAgent); } // salvo in sessione utente memLayer.ML.setSessionVal("email", email); // avvio utente... devicesAuthProxy.stObj.startUpUtente(email); // salvo gruppo... if (devicesAuthProxy.stObj.isAuth) { // se tutto ok memLayer.ML.setSessionVal("Gruppo", devicesAuthProxy.stObj.rigaUtente.CodGruppo); answ = true; } } } } catch (Exception exc) { logger.lg.scriviLog(string.Format("Errore in checkAuthCookie:{0}{1}", Environment.NewLine, exc), tipoLog.EXCEPTION); } return answ; } /// /// resetta utente /// private void ResetUser() { devicesAuthProxy.stObj.clearAllUserData(); // svuoto cookie... memLayer.ML.setCookieVal("AuthDevice", ""); memLayer.ML.emptyCookieVal("AuthDevice"); } /// /// classe grafica da pagina corrente /// /// /// public string liClass(string nomeElem) { string answ = ""; if (user_std.pagCorrente != "") { try { if (nomeElem.IndexOf(user_std.pagCorrente) >= 0) answ = "active"; } catch { } } //else //{ // try // { // if (nomeElem == "default") answ = "active"; // } // catch // { } //} return answ; } /// /// salva in variabile pagina il nome della pagina corrente /// protected void PagCorrente() { Uri MyUrl = Request.Url; string delimStr = "/"; char[] delimiter = delimStr.ToCharArray(); string[] finalUrl = MyUrl.LocalPath.ToString().Split(delimiter); int n = finalUrl.Length; user_std.pagCorrente = finalUrl[n - 1].ToString(); user_std.pagPrecedente = Request.Url.ToString(); } /// /// wrapper traduzione /// /// /// public string traduci(object lemma) { return user_std.UtSn.Traduci(lemma.ToString()); } #if false /// /// modifica campo ricerca... /// /// /// protected void txtSearch_TextChanged(object sender, EventArgs e) { // salvo in sessione memLayer.ML.setSessionVal("searchVal", txtSearch.Text.Trim()); } #endif } }