using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using SteamWare; namespace CMS_SC.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) { txtSearch.Visible = pageHasSearch; Page.Title = Request.Url.ToString(); // se ho cambiato pagina registro...) if (devicesAuthProxy.pagPrecedente != titolo) { devicesAuthProxy.pagPrecedente = devicesAuthProxy.pagCorrente; } // salvo pagina corrente devicesAuthProxy.pagCorrente = titolo; // fix logo imgLogo.Visible = logoVisible; //PagCorrente(); // tolto 2015.02.24 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"); } } #if false liGestVoc.Visible = userIsSuperAdmin; liListValues.Visible = userIsSuperAdmin; liImport.Visible = userIsSuperUser; liUserAdmin.Visible = userIsSuperAdmin; liDivider.Visible = userIsSuperAdmin; liUpdateVoc.Visible = userIsSuperAdmin; #endif } /// /// 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 = checkAuthCookieCMS_SC(); if (!allOk) { ResetUser(); } } return allOk; } /// /// verifica la presenza di un cookie VALIDO per autorizzare il device ed avvia utente... /// private bool checkAuthCookieCMS_SC() { bool answ = false; try { HttpCookie cookie = Request.Cookies[devicesAuthProxy.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 = devicesAuthProxy.stObj.taAnagDev.getByDeviceSecret(devSecret)[0]; email = device.USER_NAME; } catch(Exception exc) { logger.lg.scriviLog(string.Format("{0}", exc), tipoLog.EXCEPTION); } 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(devicesAuthProxy.AuthCookieName, ""); memLayer.ML.emptyCookieVal(devicesAuthProxy.AuthCookieName); } /// /// 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; } // tolto 2015.02.24 #if false /// /// 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.pagCorrente = titolo; user_std.pagPrecedente = Request.Url.ToString(); } #endif /// /// wrapper traduzione /// /// /// public string traduci(object lemma) { return user_std.UtSn.Traduci(lemma.ToString()); } /// /// verifica se utente sia SuperAdmin /// public bool userIsSuperAdmin { get { return devicesAuthProxy.stObj.userHasRight("SuperAdmin"); } } /// /// verifica se utente sia SuperUser /// public bool userIsSuperUser { get { return devicesAuthProxy.stObj.userHasRight("SuperUser"); } } /// /// verifica se la pagina abbia abilitato il search /// public bool pageHasSearch { get; set; } #if false public bool pageHasSearch { get { return memLayer.ML.BoolSessionObj("pageHasSearch"); } } #endif /// /// modifica campo ricerca... /// /// /// protected void txtSearch_TextChanged(object sender, EventArgs e) { // salvo in sessione memLayer.ML.setSessionVal("searchVal", txtSearch.Text.Trim()); } } }