]> saetta.ns0.it Git - bcity/fe/commitdiff
Sending confirmation email to new user.
authorAndrea Zagli <azagli@libero.it>
Sun, 14 Feb 2016 11:13:57 +0000 (12:13 +0100)
committerAndrea Zagli <azagli@libero.it>
Sun, 14 Feb 2016 11:13:57 +0000 (12:13 +0100)
.gitignore
configure.ac
doc/bcity_fe/db/Makefile.am
doc/bcity_fe/db/bcity_fe.sql
doc/bcity_fe/db/zakauthedb.sql [new file with mode: 0644]
doc/bcity_fe/examples/bcity_fe.conf
src/Makefile.am
src/commons.h
src/main.c
src/user.c

index 9e267da25de5c63c39718cf2bc9ab7d7fb7e4974..74ecfbb15c9271935542d5e22af73d13f28c6f9f 100644 (file)
@@ -51,4 +51,5 @@ Rules-quot
 *.exe
 *.csv
 src/bcity_fe
-data/bcity_fe/conf
\ No newline at end of file
+data/bcity_fe/conf
+doc/bcity_fe/db/bcity_fe.backup
\ No newline at end of file
index 8c66f32df5394fa07973ba37f15256f3a2d7bc58..ab52f5f20e18246ae402e2913cc51e758e205b87 100644 (file)
@@ -54,6 +54,11 @@ PKG_CHECK_MODULES(BCITYFE, [glib-2.0 >= 2.36
 AC_SUBST(BCITYFE_CFLAGS)
 AC_SUBST(BCITYFE_LIBS)
 
+AM_PATH_LIBGCRYPT(1.2.1, :, [AC_MSG_ERROR([libgcrypt >= 1.2.1 not found.])])
+
+AC_SUBST(LIBGCRYPT_CFLAGS)
+AC_SUBST(LIBGCRYPT_LIBS)
+
 # Checks for header files.
 AC_HEADER_STDC
 
index 2baf5e0b9a4653a504b53613e8d1184fde7d083f..97a9f8491572a8d7622f28c29b8885da5ebee144 100644 (file)
@@ -1,6 +1,7 @@
 bcity_fe_dbdir = $(docdir)/db
 
 bcity_fe_db_DATA = \
-                   bcity_fe.sql
+                   bcity_fe.sql \
+                   zakauthedb.sql
 
 EXTRA_DIST = $(bcity_fe_db_DATA)
index 9c5737de66169129daff3a0216ece5df42a384dc..d5d08a2671a95fecbcac184d346607a91008b5fe 100644 (file)
@@ -2,8 +2,8 @@
 -- PostgreSQL database dump
 --
 
--- Dumped from database version 9.5.0
--- Dumped by pg_dump version 9.5.0
+-- Dumped from database version 9.5.1
+-- Dumped by pg_dump version 9.5.1
 
 SET statement_timeout = 0;
 SET lock_timeout = 0;
@@ -43,6 +43,18 @@ CREATE TABLE segnalazioni (
 );
 
 
+--
+-- Name: users_to_register; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE users_to_register (
+    link character varying(255) NOT NULL,
+    code character varying(255),
+    expire date,
+    password character varying(8)
+);
+
+
 --
 -- Name: categorie_pkey; Type: CONSTRAINT; Schema: public; Owner: -
 --
@@ -60,6 +72,13 @@ ALTER TABLE ONLY segnalazioni
 
 
 --
--- PostgreSQL database dump complete
+-- Name: users_to_register_pkey; Type: CONSTRAINT; Schema: public; Owner: -
 --
 
+ALTER TABLE ONLY users_to_register
+    ADD CONSTRAINT users_to_register_pkey PRIMARY KEY (link);
+
+
+--
+-- PostgreSQL database dump complete
+--
diff --git a/doc/bcity_fe/db/zakauthedb.sql b/doc/bcity_fe/db/zakauthedb.sql
new file mode 100644 (file)
index 0000000..f6f28ac
--- /dev/null
@@ -0,0 +1,2 @@
+ALTER TABLE zakauthedb.users
+   ALTER COLUMN code TYPE character varying(255);
index 2c36d5103a9e0df81617e47590054044bf53573f..83c019a23ab2454eea69bc5b0baca194094f8467 100644 (file)
@@ -7,3 +7,9 @@ cnc_string=PostgreSQL://postgres:postgres@HOST=localhost;DB_NAME=autoz;SEARCHPAT
 
 [DB]
 cnc_string=PostgreSQL://postgres:postgres@HOST=localhost;DB_NAME=bcity
+
+[MAIL]
+smtp=smtp://smtp.localhost/
+security_method=0
+use_auth=0
+user_registration_from=me@myhome.com
index b71a43005478a516d6b649cec47286261eb5a220..5428e07b13fd16b4b01909d137b2faaeae1b3792 100644 (file)
@@ -4,12 +4,14 @@ formdir = $(datadir)/$(PACKAGE)/form
 imagesdir = $(datadir)/$(PACKAGE)/images
 
 AM_CPPFLAGS = $(BCITYFE_CFLAGS) \
+              $(LIBGCRYPT_CFLAGS) \
               -DCONFIGDIR=\""$(configdir)"\" \
               -DCTPLDIR=\""$(ctpldir)"\" \
               -DFORMDIR=\""$(formdir)"\" \
               -DIMAGESDIR=\""$(imagesdir)"\"
 
 LIBS = $(BCITYFE_LIBS) \
+       $(LIBGCRYPT_LIBS) \
        -export-dynamic
 
 bin_PROGRAMS = bcity_fe
index 4ead6fafdc495b8140e5fbf3c575982974d9a8d2..86df526b6ab96c49618d55515b7c7f87453ea535 100644 (file)
@@ -29,6 +29,7 @@
 #include <libgdaex/libgdaex.h>
 #include <libzakautho/autoz.h>
 #include <libzakautho/role_interface.h>
+#include <libsolipa/libsolipa.h>
 
 typedef struct
        {
@@ -50,6 +51,13 @@ typedef struct
 
                ZakAutho *autho;
                ZakAuthoIRole *role_utente;
+
+               Solipa *solipa;
+
+               gchar *smtp_uri;
+               CamelNetworkSecurityMethod smtp_security_method;
+               gboolean smtp_use_auth;
+               gchar *user_registration_from;
        } Commons;
 
 
index 65e196f3aa5d73cefdfcf7eba6b25b6120223489..de6d6ad5aff800115e16a52c90d2fb5644524700 100644 (file)
@@ -24,6 +24,7 @@
 
 #include <libzakcgi/libzakcgi.h>
 #include <libgdaex/libgdaex.h>
+#include <libzakutils/libzakutils.h>
 
 #include "commons.h"
 #include "index.h"
@@ -47,6 +48,8 @@ main (int argc, char *argv[])
 
        GdaConnection *gdacon;
 
+       gchar *str;
+
        gda_init ();
 
        /* inizializzazione commons */
@@ -57,6 +60,9 @@ main (int argc, char *argv[])
        commons->formdir = g_strdup (FORMDIR);
        commons->imagesdir = g_strdup (IMAGESDIR);
 
+       /* inizializzo solipa */
+       commons->solipa = solipa_new ();
+
        /* leggo la configurazione dal file */
        error = NULL;
        commons->config = g_key_file_new ();
@@ -120,6 +126,46 @@ main (int argc, char *argv[])
        zak_autho_set_role_name_prefix (commons->autho, g_key_file_get_value (commons->config, "ZAKAUTHO", "role_name_prefix", NULL));
        zak_autho_set_resource_name_prefix (commons->autho, g_key_file_get_value (commons->config, "ZAKAUTHO", "resource_name_prefix", NULL));
 
+       /* leggo i parametri per l'invio delle mail */
+       error = NULL;
+       commons->smtp_uri = g_key_file_get_value (commons->config, "MAIL", "smtp", &error);
+       if (commons->smtp_uri == NULL)
+               {
+                       g_warning ("Impossibile leggere l'indirizzo del server smtp: %s.\n",
+                                  error != NULL && error->message != NULL ? error->message : "nessun dettaglio");
+               }
+
+       error = NULL;
+       str = g_key_file_get_value (commons->config, "MAIL", "security_method", &error);
+       if (str == NULL)
+               {
+                       commons->smtp_security_method = CAMEL_NETWORK_SECURITY_METHOD_NONE;
+               }
+       else
+               {
+                       commons->smtp_security_method = strtol (str, NULL, 10);
+               }
+
+       error = NULL;
+       str = g_key_file_get_value (commons->config, "MAIL", "use_auth", &error);
+       if (str == NULL)
+               {
+                       commons->smtp_use_auth = FALSE;
+               }
+       else
+               {
+                       commons->smtp_use_auth = zak_utils_string_to_boolean (str);
+               }
+
+       error = NULL;
+       commons->user_registration_from = g_key_file_get_value (commons->config, "MAIL", "user_registration_from", &error);
+       if (commons->user_registration_from == NULL
+               || g_strcmp0 (g_strstrip (commons->user_registration_from), "") == 0)
+               {
+                       g_warning ("Impossibile leggere l'indirizzo del mittente per le mail di conferma registrazione utenti: %s.\n",
+                                  error != NULL && error->message != NULL ? error->message : "nessun dettaglio");
+               }
+
        commons->zcgi_main = zak_cgi_main_new ();
 
        commons->out = g_string_new ("");
@@ -142,7 +188,6 @@ main (int argc, char *argv[])
        zak_cgi_url_connect (zcgi_url, "/login/index", (ZakCgiUrlConnectedFunction)login_index, commons);
        zak_cgi_url_connect (zcgi_url, "/login/logout", (ZakCgiUrlConnectedFunction)login_logout, commons);
 
-
        zak_cgi_url_connect (zcgi_url, "/user[/]?", (ZakCgiUrlConnectedFunction)user_register, commons);
        zak_cgi_url_connect (zcgi_url, "/user/register", (ZakCgiUrlConnectedFunction)user_register, commons);
 
@@ -156,5 +201,7 @@ main (int argc, char *argv[])
        g_string_free (header, TRUE);
        g_string_free (commons->out, TRUE);
 
+       g_object_unref (commons->solipa);
+
        return 0;
 }
index 0f5c6c635e56cae8802033c78b8b95d44c561b2e..b60005b2ed04a83c13b7717d61c954fc241866bd 100644 (file)
        #include <config.h>
 #endif
 
+#include <gcrypt.h>
+
+#include <glib/gprintf.h>
+#include <libzakutils/libzakutils.h>
 #include <libzakform/libzakform.h>
 #include <libzakformgdaex/libzakformgdaex.h>
+#include <libsolipa/mail.h>
 
 #include "user.h"
 
@@ -44,6 +49,17 @@ user_register (GMatchInfo *minfo, gpointer user_data)
        gchar *cnc_string;
        GdaEx *gdaex_authedb;
 
+       SolipaMail *smail;
+       CamelInternetAddress *addr;
+       GSList *addrs;
+
+       gchar *password;
+       gchar *link_conferma;
+       gchar *body;
+       gchar *body_html;
+
+       ZakFormElement *form_element;
+
        Commons *commons = (Commons *)user_data;
 
        if (get_is_logged (commons))
@@ -76,7 +92,7 @@ user_register (GMatchInfo *minfo, gpointer user_data)
                                                                {
                                                                        g_warning ("Impossibile leggere la stringa di connessione per libzakauthe dal file di configurazione: %s.",
                                                                                           error != NULL && error->message != NULL ? error->message : "nessun dettaglio");
-                                                                       ctpl_environ_push_string (env, "form", "Form is valid!!! NOT SAVED!!!");
+                                                                       ctpl_environ_push_string (env, "form", "Errore durante il salvataggio.");
                                                                }
                                                        else
                                                                {
@@ -84,25 +100,156 @@ user_register (GMatchInfo *minfo, gpointer user_data)
                                                                        if (gdaex_authedb == NULL)
                                                                                {
                                                                                        g_warning ("Errore nella connessione al database libzakauthe: %s", cnc_string);
-                                                                                       ctpl_environ_push_string (env, "form", "Form is valid!!! NOT SAVED!!!");
+                                                                                       ctpl_environ_push_string (env, "form", "Errore durante il salvataggio.");
                                                                                }
                                                                        else
                                                                                {
                                                                                        form_provider = zak_form_gdaex_provider_new (gdaex_authedb, "users");
                                                                                        if (form_provider == NULL)
                                                                                                {
-                                                                                                       g_warning ("Errore nella creazione dewl form_provider.");
-                                                                                                       ctpl_environ_push_string (env, "form", "Form is valid!!! NOT SAVED!!!");
+                                                                                                       g_warning ("Errore nella creazione del form_provider.");
+                                                                                                       ctpl_environ_push_string (env, "form", "Errore durante il salvataggio.");
                                                                                                }
                                                                                        else
                                                                                                {
                                                                                                        if (zak_form_form_insert (ZAK_FORM_FORM (form), ZAK_FORM_IPROVIDER (form_provider)))
                                                                                                                {
-                                                                                                                       ctpl_environ_push_string (env, "form", "Form is valid!!! SAVED WITH SUCCESS!!!");
+                                                                                                                       form_element = zak_form_form_get_element_by_id (ZAK_FORM_FORM (form), "code");
+
+                                                                                                                       /* genero la password */
+                                                                                                                       gchar *str;
+                                                                                                                       gchar digest[17] = "";
+                                                                                                                       gchar pwd_gcrypt[33] = "";
+
+                                                                                                                       str = g_strdup_printf ("%f", g_random_double ());
+
+                                                                                                                       gcry_md_hash_buffer (GCRY_MD_MD5, &digest, str, strlen (str));
+                                                                                                                       for (i = 0; i < 16; i++)
+                                                                                                                               {
+                                                                                                                                       g_sprintf (pwd_gcrypt + (i * 2), "%02x", digest[i] & 0xFF);
+                                                                                                                               }
+                                                                                                                       pwd_gcrypt[32] = '\0';
+
+                                                                                                                       password = g_strndup (&pwd_gcrypt[0], 8);
+
+                                                                                                                       g_free (str);
+
+                                                                                                                       /* genero il link conferma */
+                                                                                                                       str = g_strdup_printf ("%f", g_random_double ());
+
+                                                                                                                       gcry_md_hash_buffer (GCRY_MD_MD5, &digest, str, strlen (str));
+                                                                                                                       for (i = 0; i < 16; i++)
+                                                                                                                               {
+                                                                                                                                       g_sprintf (pwd_gcrypt + (i * 2), "%02x", digest[i] & 0xFF);
+                                                                                                                               }
+                                                                                                                       pwd_gcrypt[32] = '\0';
+
+                                                                                                                       link_conferma = g_strdup_printf ("http://localhost/bcity/bcity_fe/user/confirm/%s",
+                                                                                                                                                                                        &pwd_gcrypt[0]);
+
+                                                                                                                       g_free (str);
+
+                                                                                                                       /* salvo l'attesa di registrazione nel db */
+                                                                                                                       GValue *gval;
+                                                                                                                       GdaExSqlBuilder *sqlb;
+                                                                                                                       GDateTime *gdt_now;
+                                                                                                                       GDateTime *gdt;
+
+                                                                                                                       sqlb = gdaex_sql_builder_new (GDA_SQL_STATEMENT_INSERT);
+
+                                                                                                                       gdaex_sql_builder_from (sqlb, "users_to_register", "");
+
+                                                                                                                       gval = zak_utils_gvalue_new_string (&pwd_gcrypt[0]);
+                                                                                                                       gdaex_sql_builder_fields (sqlb,
+                                                                                                                                                                         "users_to_register", "link", "", gval,
+                                                                                                                                                                         NULL);
+                                                                                                                       g_value_unset (gval);
+
+                                                                                                                       gval = zak_utils_gvalue_new_string (zak_form_element_get_value (form_element));
+                                                                                                                       gdaex_sql_builder_fields (sqlb,
+                                                                                                                                                                         "users_to_register", "code", "", gval,
+                                                                                                                                                                         NULL);
+                                                                                                                       g_value_unset (gval);
+
+                                                                                                                       gdt_now = g_date_time_new_now_local ();
+                                                                                                                       gdt = g_date_time_add_days (gdt_now, 15);
+                                                                                                                       gval = zak_utils_gvalue_new_string (zak_utils_gdatetime_to_sql (gdt, NULL));
+                                                                                                                       gdaex_sql_builder_fields (sqlb,
+                                                                                                                                                                         "users_to_register", "expire", "", gval,
+                                                                                                                                                                         NULL);
+                                                                                                                       g_value_unset (gval);
+                                                                                                                       g_date_time_unref (gdt_now);
+                                                                                                                       g_date_time_unref (gdt);
+
+                                                                                                                       gval = zak_utils_gvalue_new_string (password);
+                                                                                                                       gdaex_sql_builder_fields (sqlb,
+                                                                                                                                                                         "users_to_register", "password", "", gval,
+                                                                                                                                                                         NULL);
+                                                                                                                       g_value_unset (gval);
+
+                                                                                                                       if (gdaex_sql_builder_execute (sqlb, commons->gdaex, NULL) < 0)
+                                                                                                                               {
+                                                                                                                                       g_warning ("Errore durante il salvataggio dell'attesa registrazione.");
+                                                                                                                               }
+
+                                                                                                                       /* invio la mail */
+                                                                                                                       smail = solipa_mail_new (commons->solipa);
+                                                                                                                       solipa_mail_set_security_method (smail, commons->smtp_security_method);
+
+                                                                                                                       solipa_mail_set_subject (smail, "Conferma iscrizione a B-City");
+
+                                                                                                                       addr = camel_internet_address_new ();
+
+                                                                                                                       addrs = solipa_mail_get_addresses_from_string (commons->user_registration_from);
+                                                                                                                       camel_internet_address_add (addr,
+                                                                                                                                                                               g_hash_table_lookup ((GHashTable *)addrs->data, "name"),
+                                                                                                                                                                               g_hash_table_lookup ((GHashTable *)addrs->data, "address"));
+                                                                                                                       solipa_mail_set_from (smail, addr);
+
+                                                                                                                       camel_address_remove (CAMEL_ADDRESS (addr), -1);
+
+                                                                                                                       addrs = solipa_mail_get_addresses_from_string (zak_form_element_get_value (form_element));
+                                                                                                                       camel_internet_address_add (addr,
+                                                                                                                                                                               g_hash_table_lookup ((GHashTable *)addrs->data, "name"),
+                                                                                                                                                                               g_hash_table_lookup ((GHashTable *)addrs->data, "address"));
+                                                                                                                       solipa_mail_add_recipient (smail, addr, SOLIPA_MAIL_RECIPIENT_TYPE_TO);
+                                                                                                                       g_object_unref (addr);
+
+                                                                                                                       body = g_strdup_printf ("Conferma la registrazione facendo clic sul seguente collegamento (valido per 15 giorni).\n\n"
+                                                                                                                                                                       "%s\n\n"
+                                                                                                                                                                       "La password a te assegnata è: %s",
+                                                                                                                                                                       link_conferma,
+                                                                                                                                                                       password);
+                                                                                                                       body_html = g_strdup_printf ("<html>\n"
+                                                                                                                                                                                "<head>\n"
+                                                                                                                                                                                "<title>B-City: conferma registrazione</title\n"
+                                                                                                                                                                                "</head>\n"
+                                                                                                                                                                                "<body>\n"
+                                                                                                                                                                                "<div>Conferma la registrazione facendo clic sul seguente collegamento (valido per 15 giorni).</div>\n"
+                                                                                                                                                                                "<div>\n"
+                                                                                                                                                                                "<a href=\"%s\">%s</a>"
+                                                                                                                                                                                "</div>\n"
+                                                                                                                                                                                "<div>La password a te assegnata è: %s</div>\n"
+                                                                                                                                                                                "</body>\n"
+                                                                                                                                                                                "</html>",
+                                                                                                                                                                                link_conferma,
+                                                                                                                                                                                link_conferma,
+                                                                                                                                                                                password);
+
+                                                                                                                       solipa_mail_set_body (smail,
+                                                                                                                                                                 body,
+                                                                                                                                                                 body_html);
+
+                                                                                                                       solipa_mail_send (smail, commons->smtp_uri);
+
+                                                                                                                       g_free (body);
+                                                                                                                       g_free (body_html);
+
+                                                                                                                       ctpl_environ_push_string (env, "form", "Utente registrato con successo. È stata inviata una mail per la procedura di conferma con la password assegnata.");
                                                                                                                }
                                                                                                        else
                                                                                                                {
-                                                                                                                       ctpl_environ_push_string (env, "form", "Form is valid!!! NOT SAVED!!!");
+                                                                                                                       ctpl_environ_push_string (env, "form", "Errore durante il salvataggio.");
                                                                                                                }
                                                                                                }
                                                                                }