From 5383adceeb35ff4add993e6b6287ed7edd60f7a3 Mon Sep 17 00:00:00 2001
From: Andrea Zagli <azagli@libero.it>
Date: Sat, 6 Feb 2016 10:38:09 +0100
Subject: [PATCH] Use of libzakautho.

---
 configure.ac                        | 13 ++++++-----
 doc/bcity_fe/examples/bcity_fe.conf |  3 +++
 src/commons.h                       |  5 ++++
 src/index.c                         |  2 +-
 src/login.c                         | 17 +++++++++++---
 src/main.c                          | 36 +++++++++++++++++++++++++++++
 6 files changed, 66 insertions(+), 10 deletions(-)

diff --git a/configure.ac b/configure.ac
index 3302de9..1d1248b 100644
--- a/configure.ac
+++ b/configure.ac
@@ -41,13 +41,14 @@ PKG_CHECK_MODULES(BCITYFE, [glib-2.0 >= 2.36
                             gobject-2.0 >= 2.36
                             gmodule-2.0 >= 2.0.0
                             libxml-2.0 >= 2.0.0
-							libgdaex >= 0.5.0
-							libsolipa >= 0.5.0
-							ctpl >= 0.3.0
-							libzakauthe >= 0.5.0
+                            libgdaex >= 0.5.0
+                            libsolipa >= 0.5.0
+                            ctpl >= 0.3.0
+                            libzakauthe >= 0.5.0
+                            libzakautho >= 0.0.5
                             libzakutils
-							libzakform
-							libzakcgi])
+                            libzakform
+                            libzakcgi])
 
 AC_SUBST(BCITYFE_CFLAGS)
 AC_SUBST(BCITYFE_LIBS)
diff --git a/doc/bcity_fe/examples/bcity_fe.conf b/doc/bcity_fe/examples/bcity_fe.conf
index 5bc008f..2c36d51 100644
--- a/doc/bcity_fe/examples/bcity_fe.conf
+++ b/doc/bcity_fe/examples/bcity_fe.conf
@@ -2,5 +2,8 @@
 plugin=/usr/local/lib/libzakautke/plugins/libzakauthedb.so
 cnc_string=PostgreSQL://postgres:postgres@HOST=localhost;DB_NAME=bcity;SEARCHPATH=zakauthedb
 
+[ZAKAUTHO]
+cnc_string=PostgreSQL://postgres:postgres@HOST=localhost;DB_NAME=autoz;SEARCHPATH=zakautho
+
 [DB]
 cnc_string=PostgreSQL://postgres:postgres@HOST=localhost;DB_NAME=bcity
diff --git a/src/commons.h b/src/commons.h
index e302ff1..f328484 100644
--- a/src/commons.h
+++ b/src/commons.h
@@ -27,6 +27,8 @@
 #include <ctpl/ctpl.h>
 #include <libzakcgi/libzakcgi.h>
 #include <libgdaex/libgdaex.h>
+#include <libzakautho/autoz.h>
+#include <libzakautho/role_interface.h>
 
 typedef struct
 	{
@@ -46,6 +48,9 @@ typedef struct
 		ZakCgiSession *zcgi_session;
 
 		GString *out;
+
+		ZakAutho *autho;
+		ZakAuthoIRole *role_utente;
 	} Commons;
 
 
diff --git a/src/index.c b/src/index.c
index c1197f4..d0dc695 100644
--- a/src/index.c
+++ b/src/index.c
@@ -256,7 +256,7 @@ index_save (GMatchInfo *minfo, gpointer user_data)
 			return;
 		}
 
-	newid = gdaex_get_new_id(commons->gdaex, "segnalazioni", "id", NULL);
+	newid = gdaex_get_new_id (commons->gdaex, "segnalazioni", "id", NULL);
 
 	sql = g_strdup_printf ("INSERT INTO segnalazioni"
 						   " (id, utente, latitudine, longitudine, id_categorie)"
diff --git a/src/login.c b/src/login.c
index 9a201a4..f392fa8 100644
--- a/src/login.c
+++ b/src/login.c
@@ -99,9 +99,20 @@ login_index (GMatchInfo *minfo, gpointer user_data)
 			username = login_post (commons);
 			if (username != NULL)
 				{
-					zak_cgi_session_set_value (commons->zcgi_session, "username", username);
-					zak_cgi_main_redirect (commons->zcgi_main, "/bcity/bcity_fe/index/index");
-					return;
+					commons->role_utente = zak_autho_get_role_from_id (commons->autho, username);
+					if (commons->role_utente == NULL
+						|| !zak_autho_is_allowed (commons->autho, commons->role_utente, zak_autho_get_resource_from_id (commons->autho, "ticket"), FALSE))
+						{
+							msg = g_strdup ("<div class=\"alert alert-danger\" role=\"alert\">Utente non autorizzato.</div>\n");
+							ctpl_environ_push_string (env, "msg", msg);
+							g_free (msg);
+						}
+					else
+						{
+							zak_cgi_session_set_value (commons->zcgi_session, "username", username);
+							zak_cgi_main_redirect (commons->zcgi_main, "/bcity/bcity_fe/index/index");
+							return;
+						}
 				}
 			else
 				{
diff --git a/src/main.c b/src/main.c
index 397770f..84a16d4 100644
--- a/src/main.c
+++ b/src/main.c
@@ -44,6 +44,8 @@ main (int argc, char *argv[])
 
 	gchar *filename;
 
+	GdaConnection *gdacon;
+
 	gda_init ();
 
 	/* inizializzazione commons */
@@ -82,6 +84,40 @@ main (int argc, char *argv[])
 			g_error ("Errore nella connessione al database: %s", cnc_string);
 		}
 
+	/* leggo i parametri per libzakautho */
+	error = NULL;
+	cnc_string = g_key_file_get_value (commons->config, "ZAKAUTHO", "cnc_string", &error);
+	if (cnc_string == NULL || error != NULL)
+		{
+			g_warning ("Impossibile leggere la stringa di connessione per libzakautho dal file di configurazione: %s.",
+			           error != NULL && error->message != NULL ? error->message : "nessun dettaglio");
+			return 0;
+		}
+
+	error = NULL;
+	gdacon = gda_connection_open_from_string (NULL, cnc_string, NULL, 0, &error);
+	if (gdacon == NULL || error != NULL)
+		{
+			g_warning ("Impossibile creare la connessione al db per libzakautho: %s.",
+			           error != NULL && error->message != NULL ? error->message : "nessun dettaglio");
+			return 0;
+		}
+
+	commons->autho = zak_autho_new ();
+	if (commons->autho == NULL)
+		{
+			g_warning ("Impossibile creare l'oggetto per libzakautho.");
+			return 0;
+		}
+	if (!zak_autho_load_from_db (commons->autho, gdacon, NULL, TRUE))
+		{
+			g_warning ("Impossibile caricare la configurazione per libzakautho dal db.");
+			return 0;
+		}
+
+	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));
+
 	commons->zcgi_main = zak_cgi_main_new ();
 
 	commons->out = g_string_new ("");
-- 
2.49.0