コード例 #1
0
        /// <summary>
        /// Metodo responsabile della ricerca in base ad una query prestabilita
        /// </summary>
        /// <param name="query">Stringa da cercare</param>
        /// <returns></returns>
        public IList <IUtente> Cerca(string query)
        {
            string         uppercaseQuery = query.ToUpper();
            List <IUtente> utenti         = new List <IUtente>();

            // Effettuo una ricerca CASE-INSENSITIVE sui campi email, nome, cognome ed username
            using (SQLiteCommand cmd = new SQLiteCommand("SELECT * FROM utenti WHERE UPPER(email) LIKE '%' || @query || '%' " +
                                                         "OR UPPER(nome) LIKE '%' || @query || '%' " +
                                                         "OR UPPER(cognome) LIKE '%' || @query || '%' " +
                                                         "OR UPPER(username) LIKE '%' || @query || '%' ", _connection))
            {
                cmd.Prepare();
                cmd.Parameters.AddWithValue("@query", uppercaseQuery);
                using (SQLiteDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        // Converto la entry del database in una stazione
                        IUtente utenteCorrente = Utente.ConvertiRigaInUtente(reader);
                        if (utenteCorrente != null)
                        {
                            utenti.Add(utenteCorrente);
                        }
                    }
                }
            }

            return(utenti);
        }
コード例 #2
0
        /// <summary>
        /// Metodo statico per la scrittura sul file di log.
        /// Parametri: IUtente utente che effettua il logging, string messaggio da scrivere
        /// Ritorno: intero che rappresenta il numero di bytes scritti sul file
        /// </summary>
        /// <param name="utente"> Utente che effettua l'operazione </param>
        /// <param name="messaggio"> Messaggio dell'operazione </param>
        /// <returns></returns>
        public static int Scrivi(IUtente utente, string messaggio)
        {
            // Offset: da UTC a CEST
            var offset = 2 * 60 * 60; // TODO timezone vincolata
            // Aggiungo l'offset per avere coerenza con la timezone. Opportuno decidere altro metodo oppure se va bene anche con timezone UTC
            long timestamp = new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds() + offset;

            // Concateno le differenti parti della linea finale
            string linea = timestamp.ToString() + " " + utente.Username + " " + messaggio;

            FileStream fs = null;

            try
            {
                // Apro il file in append
                fs = new FileStream(_percorso, FileMode.Append);
                using (StreamWriter writer = new StreamWriter(fs))
                {
                    // Scrivo la linea
                    writer.WriteLine(linea);
                }
            }
            finally
            {
                if (fs != null)
                {
                    fs.Dispose();
                }
            }

            // TODO void invece che int come tipo di ritorno?
            // TODO dovrei leggere i byte effettivamente scritti invece che quelli virtuali (lunghezza stringa)
            return(linea.Length);
        }
コード例 #3
0
        public void TestRegistraUtente()
        {
            Utente utente = new UtenteNormale
            {
                Nome        = "tizio",
                Cognome     = "caio",
                Email       = "*****@*****.**",
                LoginRemoto = true,
                Username    = "******"
            };

            utente.ImpostaPasswordDaOriginale("password");

            // Verifico che non esista
            Assert.IsNull(gestioneUtentiController.ValidaCredenziali("tizio", "password"));

            // Lo registro, e verifico che adesso esiste
            Assert.IsTrue(gestioneUtentiController.Registra(utente));
            IUtente utenteValidato = gestioneUtentiController.ValidaCredenziali("tizio", "password");

            Assert.IsNotNull(utenteValidato);

            // Mi assicuro che sia del tipo giusto
            Assert.IsInstanceOfType(utenteValidato, typeof(UtenteNormale), "L'utente è di tipo sbagliato");
        }
コード例 #4
0
 public void SegnalaLibro(Libro libro)
 {
     try
     {
         BasicHttpBinding myBinding = new BasicHttpBinding();
         myBinding.MaxReceivedMessageSize = 2147483647;
         myBinding.MaxBufferSize          = 2147483647;
         EndpointAddress          myEndpoint       = new EndpointAddress(_addr);
         ChannelFactory <IUtente> myChannelFactory = new ChannelFactory <IUtente>(myBinding, myEndpoint);
         // Create a channel.
         IUtente           client = myChannelFactory.CreateChannel();
         SegnalazioneLibro segn   = new SegnalazioneLibro()
         {
             Username = _username,
             Libro    = _libro,
             Id       = 1
         };
         client.AddSegnalazioneLibro(segn);
         ((IClientChannel)client).Close();
     }
     catch (Exception)
     {
         //doNothing
     }
 }
コード例 #5
0
        public void DownloadCopertina()
        {
            try
            {
                BasicHttpBinding myBinding = new BasicHttpBinding();
                myBinding.MaxReceivedMessageSize = 2147483647;
                myBinding.MaxBufferSize          = 2147483647;
                EndpointAddress          myEndpoint       = new EndpointAddress(_addr);
                ChannelFactory <IUtente> myChannelFactory = new ChannelFactory <IUtente>(myBinding, myEndpoint);
                // Create a channel.
                IUtente         client      = myChannelFactory.CreateChannel();
                DownloadRequest requestData = new DownloadRequest();

                RemoteFileInfo fileInfo = new RemoteFileInfo();
                requestData.FileName = _libro.FilePath;

                try
                {
                    fileInfo = client.DownloadFile(requestData);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                Console.ReadLine();

                string pathNew        = @"C:\Users\mmart\Desktop\Download\" + _libro.FilePath;
                int    numBytesToRead = (int)fileInfo.Length;
                byte[] bytes          = new byte[numBytesToRead + 1];
                int    bytesRead      = 0;

                while (numBytesToRead > 0)
                {
                    // Read may return anything from 0 to numBytesToRead.
                    int n = fileInfo.FileByteStream.Read(bytes, bytesRead, bytes.Length);

                    // Break when the end of the file is reached.
                    if (n == 0)
                    {
                        break;
                    }

                    bytesRead      += n;
                    numBytesToRead -= n;
                }
                numBytesToRead = bytes.Length;

                // Write the byte array to the other FileStream.
                using (FileStream fsNew = new FileStream(pathNew, FileMode.Create, FileAccess.Write))
                {
                    fsNew.Write(bytes, 0, numBytesToRead);
                }
                ((IClientChannel)client).Close();
            }
            catch (Exception)
            {
                //doNothing
            }
        }
コード例 #6
0
        public bool Autentica(string username, string pass)
        {
            IUtente user = _gestioneUtentiController.ValidaCredenziali(username, pass);

            if (user == null)
            {
                return(false);
            }
            else
            {
                UtenteAttivo = user;
                return(true);
            }
        }
コード例 #7
0
        public void TestEliminaUtente()
        {
            // Ottengo l'admin
            IUtente admin = gestioneUtentiController.ValidaCredenziali("admin", "admin");

            Assert.IsNotNull(admin);

            // Lo elimino
            Assert.IsTrue(gestioneUtentiController.Elimina((Utente)admin));

            // Verifico che sia stato eliminato
            admin = gestioneUtentiController.ValidaCredenziali("admin", "admin");
            Assert.IsNull(admin);
        }
コード例 #8
0
        public TrasmissioneDatiWorker(IGestioneUtentiController gestioneUtentiController, ISensore sensore, SslStream sslStream)
        {
            _gestioneUtentiController = gestioneUtentiController;

            _sensore   = sensore;
            _sslStream = sslStream;

            // Crea i lettori e scrittori binari
            _binaryWriter = new BinaryWriter(sslStream);
            _binaryReader = new BinaryReader(sslStream);

            // Ottengo le credenziali utente
            string user = _binaryReader.ReadString();
            string pass = _binaryReader.ReadString();

            Console.WriteLine("Richiesta connessione per utente :{0}", user);

            // Verifico le credenziali
            // TODO Log della connessione remota
            IUtente utenteRemoto = gestioneUtentiController.ValidaCredenziali(user, pass);

            if (utenteRemoto != null)  // Credenziali accettate
            {
                // Invio la risposta
                _binaryWriter.Write("ACCEPT");

                Console.WriteLine("Connessione accettata");

                // Creo il thread di networking
                _threadNetwork = new Thread(new ThreadStart(Run));

                // Registra il worker come ricevitore del sensore per ricevere i dati
                // in tempo reale
                sensore.RicevitoriDatiSensore += OnRisultatiGrezziDisponibili;
            }
            else  // Credenziali non accettate
            {
                // Invio la risposta
                _binaryWriter.Write("INVALID");

                Console.WriteLine("Connessione non accettata");

                throw new CredenzialiInvalideEccezione("Le credenziali non sono valide");
            }
        }
コード例 #9
0
        public MainForm(IGestioneUtentiController gestioneUtentiController,
                        IGestioneStazioniController gestioneStazioniController,
                        IStoricoController storico,
                        ISorgente sorgente, GestoreEventi gestoreEventi,
                        AutenticazioneController autenticazioneController)
        {
            InitializeComponent();

            this.homeDashboard1.Sorgente      = sorgente;
            this.homeDashboard1.GestoreEventi = gestoreEventi;
            this.homeDashboard1.GestioneStazioniController = gestioneStazioniController;

            this.storico1.StoricoController               = storico;
            this.homeGestioneUtenti1.UtentiController     = gestioneUtentiController;
            this.homeGestioneStazioni1.StazioniController = gestioneStazioniController;

            var materialSkinManager = MaterialSkinManager.Instance;

            materialSkinManager.AddFormToManage(this);
            materialSkinManager.Theme       = MaterialSkinManager.Themes.LIGHT;
            materialSkinManager.ColorScheme = SismioColor.Scheme;
            this.navbar.BackColor           = SismioColor.Scheme.DarkPrimaryColor;
            //this.BackColor = SismioColor.BackColor;

            this.tabControl.SelectedTab = this.tabDashboard;

            this.tabDashboard.BackColor        = SismioColor.BackColor;
            this.tabGestioneStazioni.BackColor = SismioColor.BackColor;
            this.tabGestioneUtenti.BackColor   = SismioColor.BackColor;
            this.tabStorico.BackColor          = SismioColor.BackColor;

            // Nascondo i pulsanti in base ai permessi
            IUtente utenteCorrente = autenticazioneController.UtenteAttivo;

            if (utenteCorrente == null)
            {
                this.navGestioneStazioni.Visible = false;
                this.navGestioneUtenti.Visible   = false;
            }
            else
            {
                this.navGestioneStazioni.Visible = utenteCorrente.PuoModificareStazioni();
                this.navGestioneUtenti.Visible   = utenteCorrente.PuoModificareUtenti();
            }
        }
コード例 #10
0
        public void InserisciLibro(string titolo, string autore, int?anno, string genereString, string filePath)
        {
            try
            {
                BasicHttpBinding myBinding = new BasicHttpBinding();
                myBinding.MaxReceivedMessageSize = 2147483647;
                myBinding.MaxBufferSize          = 2147483647;
                EndpointAddress          myEndpoint       = new EndpointAddress(_addr);
                ChannelFactory <IUtente> myChannelFactory = new ChannelFactory <IUtente>(myBinding, myEndpoint);
                // Create a channel.
                IUtente  client   = myChannelFactory.CreateChannel();
                string[] elements = filePath.Split('\\');
                string   fileName = elements[elements.Length - 1];
                client.InserisciLibroCover(titolo, autore, anno, genereString, fileName, _username);

                //try
                //{
                //    //UPLOAD FILE
                System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);

                RemoteFileInfo uploadRequestInfo = new RemoteFileInfo();

                using (System.IO.FileStream stream =
                           new System.IO.FileStream(filePath,
                                                    System.IO.FileMode.Open, System.IO.FileAccess.Read))
                {
                    uploadRequestInfo.FileName       = fileName;
                    uploadRequestInfo.Length         = fileInfo.Length;
                    uploadRequestInfo.FileByteStream = stream;
                }
                client.UploadFile(uploadRequestInfo);
                //}
                //catch (Exception)
                //{

                //}
                ((IClientChannel)client).Close();
            }
            catch (Exception)
            {
                //doNothing
            }
        }
コード例 #11
0
 public void PubblicaRecensione(int punteggio, string commento)
 {
     try
     {
         BasicHttpBinding myBinding = new BasicHttpBinding();
         myBinding.MaxReceivedMessageSize = 2147483647;
         myBinding.MaxBufferSize          = 2147483647;
         EndpointAddress          myEndpoint       = new EndpointAddress(_addr);
         ChannelFactory <IUtente> myChannelFactory = new ChannelFactory <IUtente>(myBinding, myEndpoint);
         // Create a channel.
         IUtente client = myChannelFactory.CreateChannel();
         client.PubblicaRecensione(punteggio, commento, _libro, _username);
         ((IClientChannel)client).Close();
     }
     catch (Exception)
     {
         //doNothing
     }
 }
コード例 #12
0
 public void ModificaListaPersonale(Lista lista)
 {
     try
     {
         BasicHttpBinding myBinding = new BasicHttpBinding();
         myBinding.MaxReceivedMessageSize = 2147483647;
         myBinding.MaxBufferSize          = 2147483647;
         EndpointAddress          myEndpoint       = new EndpointAddress(_addr);
         ChannelFactory <IUtente> myChannelFactory = new ChannelFactory <IUtente>(myBinding, myEndpoint);
         // Create a channel.
         IUtente client = myChannelFactory.CreateChannel();
         client.ModificaListaPersonale(lista);
         ((IClientChannel)client).Close();
     }
     catch (Exception)
     {
         //doNothing
     }
 }
コード例 #13
0
 public void RimuoviRecensione(Recensione r)
 {
     try
     {
         BasicHttpBinding myBinding = new BasicHttpBinding();
         myBinding.MaxReceivedMessageSize = 2147483647;
         myBinding.MaxBufferSize          = 2147483647;
         EndpointAddress          myEndpoint       = new EndpointAddress(_addr);
         ChannelFactory <IUtente> myChannelFactory = new ChannelFactory <IUtente>(myBinding, myEndpoint);
         // Create a channel.
         IUtente client = myChannelFactory.CreateChannel();
         client.RimuoviRecensione(r, _username);
         ((IClientChannel)client).Close();
     }
     catch (Exception)
     {
         //doNothing
     }
 }
コード例 #14
0
        public void InserisciLibro(string titolo, string autore, int?anno, string genereString)
        {
            try
            {
                BasicHttpBinding myBinding = new BasicHttpBinding();
                myBinding.MaxReceivedMessageSize = 2147483647;
                myBinding.MaxBufferSize          = 2147483647;
                EndpointAddress          myEndpoint       = new EndpointAddress(_addr);
                ChannelFactory <IUtente> myChannelFactory = new ChannelFactory <IUtente>(myBinding, myEndpoint);
                // Create a channel.
                IUtente client = myChannelFactory.CreateChannel();
                client.InserisciLibro(titolo, autore, anno, genereString, _username);

                ((IClientChannel)client).Close();
            }
            catch (Exception)
            {
                //doNothing
            }
        }
コード例 #15
0
 public List <Recensione> GetLeMieRecensioni()
 {
     try
     {
         BasicHttpBinding myBinding = new BasicHttpBinding();
         myBinding.MaxReceivedMessageSize = 2147483647;
         myBinding.MaxBufferSize          = 2147483647;
         EndpointAddress          myEndpoint       = new EndpointAddress(_addr);
         ChannelFactory <IUtente> myChannelFactory = new ChannelFactory <IUtente>(myBinding, myEndpoint);
         // Create a channel.
         IUtente client = myChannelFactory.CreateChannel();
         _leMieRecensioni = client.GetRecensioniScritte(_username).ToList <Recensione>();
         ((IClientChannel)client).Close();
     }
     catch (Exception)
     {
         //doNothing
     }
     return(_leMieRecensioni);
 }
コード例 #16
0
        public IList <IUtente> ListaTutti()
        {
            List <IUtente> utenti = new List <IUtente>();

            using (SQLiteCommand cmd = new SQLiteCommand("SELECT * FROM utenti", _connection))
            {
                using (SQLiteDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        IUtente utenteCorrente = Utente.ConvertiRigaInUtente(reader);
                        if (utenteCorrente != null)
                        {
                            utenti.Add(utenteCorrente);
                        }
                    }
                }
            }

            return(utenti);
        }
コード例 #17
0
 public Libro[] GetClassificaAnno(int anno)
 {
     Libro[] libri = null;
     try
     {
         BasicHttpBinding myBinding = new BasicHttpBinding();
         myBinding.MaxReceivedMessageSize = 2147483647;
         myBinding.MaxBufferSize          = 2147483647;
         EndpointAddress          myEndpoint       = new EndpointAddress(_addr);
         ChannelFactory <IUtente> myChannelFactory = new ChannelFactory <IUtente>(myBinding, myEndpoint);
         // Create a channel.
         IUtente client = myChannelFactory.CreateChannel();
         libri = client.GetClassificaAnno(anno);
         ((IClientChannel)client).Close();
     }
     catch (Exception)
     {
         //doNothing
     }
     return(libri);
 }
コード例 #18
0
        public List <Libro> CercaGenere(string genereString)
        {
            List <Libro> libri = null;

            try
            {
                BasicHttpBinding myBinding = new BasicHttpBinding();
                myBinding.MaxReceivedMessageSize = 2147483647;
                myBinding.MaxBufferSize          = 2147483647;
                EndpointAddress          myEndpoint       = new EndpointAddress(_addr);
                ChannelFactory <IUtente> myChannelFactory = new ChannelFactory <IUtente>(myBinding, myEndpoint);
                // Create a channel.
                IUtente client = myChannelFactory.CreateChannel();
                libri = client.CercaGenere(genereString).ToList <Libro>();
                ((IClientChannel)client).Close();
            }
            catch (Exception)
            {
                //doNothing
            }
            return(libri);
        }
コード例 #19
0
        public List <Recensione> CercaRecensioniLibro(Libro libro)
        {
            List <Recensione> recensioni = null;

            try
            {
                BasicHttpBinding myBinding = new BasicHttpBinding();
                myBinding.MaxReceivedMessageSize = 2147483647;
                myBinding.MaxBufferSize          = 2147483647;
                EndpointAddress          myEndpoint       = new EndpointAddress(_addr);
                ChannelFactory <IUtente> myChannelFactory = new ChannelFactory <IUtente>(myBinding, myEndpoint);
                // Create a channel.
                IUtente client = myChannelFactory.CreateChannel();
                recensioni = client.CercaRecensioniLibro(libro).ToList <Recensione>();
                ((IClientChannel)client).Close();
            }
            catch (Exception)
            {
                //doNothing
            }
            return(recensioni);
        }
コード例 #20
0
        public string CercaUtente(string username)
        {
            string userTrovato = null;

            try
            {
                BasicHttpBinding myBinding = new BasicHttpBinding();
                myBinding.MaxReceivedMessageSize = 2147483647;
                myBinding.MaxBufferSize          = 2147483647;
                EndpointAddress          myEndpoint       = new EndpointAddress(_addr);
                ChannelFactory <IUtente> myChannelFactory = new ChannelFactory <IUtente>(myBinding, myEndpoint);
                // Create a channel.
                IUtente client = myChannelFactory.CreateChannel();
                userTrovato = client.CercaUtente(username);
                ((IClientChannel)client).Close();
            }
            catch (Exception)
            {
                //doNothing
            }
            return(userTrovato);
        }
コード例 #21
0
        public IUtente ValidaCredenziali(string username, string pass)
        {
            IUtente outputUtente = null;

            using (SQLiteCommand cmd = new SQLiteCommand("SELECT * FROM utenti WHERE username = @Username", _connection))
            {
                cmd.Prepare();
                cmd.Parameters.AddWithValue("@Username", username);

                using (SQLiteDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        outputUtente = Utente.ConvertiRigaInUtente(reader);
                    }
                }
            }

            // Controllo che l'utente esista e la password corrisponda
            if (outputUtente == null)
            {
                return(null);
            }
            else
            {
                // Controllo che la password corrisponda
                string hashPrevisto = HashUtil.GeneraSHA256(pass + outputUtente.SaltPass);
                if (hashPrevisto == outputUtente.HashPass)
                {
                    return(outputUtente);
                }
                else
                {
                    return(null);
                }
            }
        }
コード例 #22
0
        public SchedaLibroView(string addr, string username, Libro libro)
        {
            InitializeComponent();

            _addr     = addr;
            _username = username;
            _libro    = libro;

            _schedaLibro   = new SchedaLibro(_libro, addr, username);
            _laMiaLibreria = new LaMiaLibreria(_username, _addr);

            textBoxTitoloLibro.Text = _libro.Titolo;
            textBoxAutoreLibro.Text = _libro.Autore;
            textBoxAnnoLibro.Text   = _libro.Anno.ToString();
            textBoxGenereLibro.Text = _libro.Gen.ToString();

            if (_libro.FilePath == null)
            {
            }
            else
            {
                _schedaLibro.DownloadCopertina();
                pictureBoxCopertina.SizeMode = PictureBoxSizeMode.StretchImage;

                if (!File.Exists("C:\\Users\\mmart\\Desktop\\Download\\" + _libro.FilePath))
                {
                    try
                    {
                        BasicHttpBinding myBinding = new BasicHttpBinding();
                        myBinding.MaxReceivedMessageSize = 2147483647;
                        myBinding.MaxBufferSize          = 2147483647;
                        EndpointAddress          myEndpoint       = new EndpointAddress(_addr);
                        ChannelFactory <IUtente> myChannelFactory = new ChannelFactory <IUtente>(myBinding, myEndpoint);
                        // Create a channel.
                        IUtente         client      = myChannelFactory.CreateChannel();
                        DownloadRequest requestData = new DownloadRequest();

                        RemoteFileInfo fileInfo = new RemoteFileInfo();
                        requestData.FileName = _libro.FilePath;

                        try
                        {
                            fileInfo = client.DownloadFile(requestData);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.Message);
                        }
                        Console.ReadLine();

                        string pathNew        = @"C:\Users\mmart\Desktop\Download\" + _libro.FilePath;
                        int    numBytesToRead = (int)fileInfo.Length;
                        byte[] buffer         = new byte[483647];
                        byte[] bytes          = new byte[numBytesToRead];
                        int    bytesRead      = 0;

                        while (numBytesToRead > 0)
                        {
                            // Read may return anything from 0 to numBytesToRead.
                            int n = fileInfo.FileByteStream.Read(buffer, bytesRead, buffer.Length);

                            // Break when the end of the file is reached.
                            if (n == 0)
                            {
                                break;
                            }
                            Array.Copy(buffer, 0, bytes, bytesRead, n);
                            bytesRead      += n;
                            numBytesToRead -= n;
                        }
                        numBytesToRead = bytes.Length;

                        // Write the byte array to the other FileStream.
                        using (FileStream fsNew = new FileStream(pathNew, FileMode.Create, FileAccess.Write))
                        {
                            fsNew.Write(bytes, 0, numBytesToRead);
                        }

                        ((IClientChannel)client).Close();
                    }
                    catch
                    {
                    }
                }

                pictureBoxCopertina.Image = Image.FromFile("C:\\Users\\mmart\\Desktop\\Download\\" + _libro.FilePath);
            }
        }
コード例 #23
0
 public bool Disconnetti()
 {
     UtenteAttivo = null;
     return(true);
 }
コード例 #24
0
        public void TestAdminCreatoCorrettamente()
        {
            IUtente admin = gestioneUtentiController.ValidaCredenziali("admin", "admin");

            Assert.IsNotNull(admin);
        }
コード例 #25
0
        public void TestValidaCredenzialiDovrebbeRestituireNull()
        {
            IUtente admin = gestioneUtentiController.ValidaCredenziali("admin", "sbagliata");

            Assert.IsNull(admin);
        }