]> saetta.ns0.it Git - zakauthe/plugins/smbldap/commitdiff
Enabled group adding.
authorAndrea Zagli <azagli@libero.it>
Sun, 5 Sep 2010 10:32:44 +0000 (12:32 +0200)
committerAndrea Zagli <azagli@libero.it>
Sun, 5 Sep 2010 10:32:44 +0000 (12:32 +0200)
data/libaute-smbldap/gui/autesmbldap.gui
src/aute_smbldap.c
src/aute_smbldap.h
src/group.c
src/group.h

index acda08fec88ef23c8facaa35665585e3daea8587..e510878d397fe01681f3013d5c4bea1c0fbd6e32 100644 (file)
     <property name="title" translatable="yes">Group</property>
     <property name="modal">True</property>
     <property name="window_position">center-on-parent</property>
+    <property name="default_width">500</property>
     <property name="destroy_with_parent">True</property>
     <child>
       <object class="GtkVBox" id="vbox2">
         <child>
           <object class="GtkTable" id="table4">
             <property name="visible">True</property>
-            <property name="n_rows">3</property>
+            <property name="n_rows">2</property>
             <property name="n_columns">2</property>
             <property name="column_spacing">5</property>
             <property name="row_spacing">5</property>
                 <property name="y_options">GTK_FILL</property>
               </packing>
             </child>
-            <child>
-              <object class="GtkLabel" id="label16">
-                <property name="label" translatable="yes">lbl_cn_key</property>
-              </object>
-              <packing>
-                <property name="top_attach">2</property>
-                <property name="bottom_attach">3</property>
-                <property name="x_options">GTK_FILL</property>
-                <property name="y_options">GTK_FILL</property>
-              </packing>
-            </child>
-            <child>
-              <placeholder/>
-            </child>
             <child>
               <object class="GtkLabel" id="label17">
                 <property name="visible">True</property>
index 692ffd230f4f36eda6fb7634643b91f386c6242c..29b887a24a9f6fc853c04f5659b427b5bb694528 100644 (file)
 #include <ldap.h>
 #include <libssh/libssh.h>
 
-#include <libgtkform/fielddatetime.h>
-
 #ifdef HAVE_LIBCONFI
        #include <libconfi.h>
 #endif
 
+#include "aute_smbldap.h"
 #include "user.h"
 #include "group.h"
 
 static GtkBuilder *gtkbuilder;
 
 static gchar *guifile;
-static gchar *formdir;
 
 static LDAP *ldap;
 static gchar *base_dn;
@@ -141,8 +139,8 @@ static GtkWindow
        return w;
 }
 
-static void
-exec_ssh_command (const gchar *host, const gchar *command)
+void
+autesmbldap_exec_ssh_command (const gchar *command)
 {
        int rc;
        char buf[4096];
@@ -165,16 +163,16 @@ exec_ssh_command (const gchar *host, const gchar *command)
                                }
 
                        /*g_message("HOST SSH: %s",host_ssh);*/
-                       if (ssh_options_set (session, SSH_OPTIONS_HOST, host) < 0)
+                       if (ssh_options_set (session, SSH_OPTIONS_HOST, host_ssh) < 0)
                                {
-                                       g_warning ("Unable to set the SSH host (%s).", host);
+                                       g_warning ("Unable to set the SSH host (%s).", host_ssh);
                                        ssh_disconnect (session);
                                        session = NULL;
                                        return;
                                }
                        if (ssh_connect (session))
                                {
-                                       g_warning ("SSH connection failed to host «%s» (%s).", host, ssh_get_error (session));
+                                       g_warning ("SSH connection failed to host «%s» (%s).", host_ssh, ssh_get_error (session));
                                        ssh_disconnect (session);
                                        session = NULL;
                                        return;
@@ -447,7 +445,7 @@ static gchar
                                        /* cambio la password */
                                        gchar *command = g_strdup_printf ("echo -e \"%s\\n\" | sudo -k -S echo -e \"%s\\\\n%s\\\\n\" | sudo /usr/sbin/smbldap-passwd \"%s\"", password, password_nuova, password_nuova, utente);
 
-                                       exec_ssh_command (host_ssh, command);
+                                       autesmbldap_exec_ssh_command (command);
                                }
                }
 
@@ -674,16 +672,39 @@ static void
 autesmbldap_on_btn_new_clicked (GtkButton *button,
                            gpointer user_data)
 {
-       /*GtkWidget *w;
+       GtkWidget *dialog;
+       GtkWidget *w;
 
-       User *u = user_new (gtkbuilder, gdaex, guifile, formdir, NULL);
+       if (gtk_notebook_get_current_page (GTK_NOTEBOOK (notebook)) == 0)
+               {
+                       /*User *u = user_new (gtkbuilder, gdaex, guifile, formdir, NULL);
 
-       g_signal_connect (G_OBJECT (u), "aggiornato",
-                         G_CALLBACK (autesmbldap_on_user_aggiornato), NULL);
+                       g_signal_connect (G_OBJECT (u), "aggiornato",
+                                                 G_CALLBACK (autesmbldap_on_user_aggiornato), NULL);
+
+                       w = user_get_widget (u);*/
+               }
+       else if (gtk_notebook_get_current_page (GTK_NOTEBOOK (notebook)) == 1)
+               {
+                       Group *u = group_new (gtkbuilder, guifile, ldap, "", groups_ou, base_dn);
+
+                       g_signal_connect (G_OBJECT (u), "aggiornato",
+                                                 G_CALLBACK (autesmbldap_on_group_aggiornato), NULL);
+
+                       w = group_get_widget (u);
+               }
+       else
+               {
+                       dialog = gtk_message_dialog_new (autesmbldap_get_gtkwidget_parent_gtkwindow (w_users),
+                                                                                        GTK_DIALOG_DESTROY_WITH_PARENT,
+                                                                                        GTK_MESSAGE_WARNING,
+                                                                                        GTK_BUTTONS_OK,
+                                                                                        "Wrong notebook page.");
+                       return;
+               }
 
-       w = user_get_widget (u);
        gtk_window_set_transient_for (GTK_WINDOW (w), autesmbldap_get_gtkwidget_parent_gtkwindow (w_users));
-       gtk_widget_show (w);*/
+       gtk_widget_show (w);
 }
   
 static void
@@ -753,7 +774,7 @@ autesmbldap_on_btn_delete_clicked (GtkButton *button,
                                                                                        col_cn, &cn,
                                                                                        -1);
 
-                                       exec_ssh_command (host_ssh, g_strdup_printf ("%s%s", command, cn));
+                                       autesmbldap_exec_ssh_command (g_strdup_printf ("%s%s", command, cn));
 
                                        (*load_list) ();
                                }
@@ -814,16 +835,6 @@ gchar
        GUIDIR = g_build_filename (g_win32_get_package_installation_directory_of_module (NULL), "share", "libaute-smbldap", "gui", NULL);
 #endif
 
-#ifdef G_OS_WIN32
-#undef FORMDIR
-
-       gchar *FORMDIR;
-
-       FORMDIR = g_build_filename (g_win32_get_package_installation_directory_of_module (NULL), "share", "libaute-smbldap", "form", NULL);
-#endif
-
-       formdir = g_strdup (FORMDIR);
-
        guifile = g_build_filename (GUIDIR, "autesmbldap.gui", NULL);
        if (!gtk_builder_add_objects_from_file (gtkbuilder, guifile,
                                                g_strsplit ("diag_main", "|", -1),
index 7c2d8702fad71f2090d9f2eb6510899af57047b5..3db7e60c137431f89222cf27eb5fb71d0eb7b5bd 100644 (file)
@@ -22,6 +22,7 @@
 
 G_BEGIN_DECLS
 
+void autesmbldap_exec_ssh_command (const gchar *command);
 
 G_END_DECLS
 
index 8e09784a880f5aae1bf18601d981d243ecd33141..7893322f29968fffab2a3017e1caa371c290c5b6 100644 (file)
@@ -17,8 +17,6 @@
  *
  */
 
-#include <libgtkform/form.h>
-
 #include "group.h"
 #include "aute_smbldap.h"
 
@@ -28,8 +26,6 @@ static void group_init (Group *group);
 static void group_carica (Group *group);
 static void group_salva (Group *group);
 
-static gboolean group_conferma_chiusura (Group *group);
-
 static void group_set_property (GObject *object,
                                      guint property_id,
                                      const GValue *value,
@@ -39,10 +35,6 @@ static void group_get_property (GObject *object,
                                      GValue *value,
                                      GParamSpec *pspec);
 
-static gboolean group_on_w_group_delete_event (GtkWidget *widget,
-                               GdkEvent *event,
-                               gpointer group_data);
-
 static void group_on_btn_annulla_clicked (GtkButton *button,
                                     gpointer group_data);
 static void group_on_btn_salva_clicked (GtkButton *button,
@@ -50,27 +42,21 @@ static void group_on_btn_salva_clicked (GtkButton *button,
 
 #define GROUP_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TYPE_GROUP, GroupPrivate))
 
-enum
-{
-       TXT_CODE,
-       LBL_PASSWORD
-};
-
 typedef struct _GroupPrivate GroupPrivate;
 struct _GroupPrivate
        {
                GtkBuilder *gtkbuilder;
-               GdaEx *gdaex;
                gchar *guifile;
-               gchar *formdir;
-
-               GtkForm *form;
+               LDAP *ldap;
 
                GtkWidget *w;
 
-               GObject **objects;
+               GtkWidget *txt_cn;
+               GtkWidget *lbl_gid;
 
-               gchar *code;
+               gchar *cn;
+               gchar *groups_ou;
+               gchar *base_dn;
        };
 
 G_DEFINE_TYPE (Group, group, G_TYPE_OBJECT)
@@ -106,33 +92,37 @@ group_init (Group *group)
 {
        GroupPrivate *priv = GROUP_GET_PRIVATE (group);
 
-       priv->code = NULL;
+       priv->cn = NULL;
+       priv->groups_ou = NULL;
+       priv->base_dn = NULL;
 }
 
 /**
  * group_new:
  * @gtkbuilder:
- * @gdaex:
  * @guifile:
- * @formdir:
- * @code:
+ * @ldap:
+ * @cn:
+ * @groups_ou:
+ * @base_dn:
  *
  * Returns: the newly created #Group object.
  */
 Group
-*group_new (GtkBuilder *gtkbuilder, GdaEx *gdaex,
-           const gchar *guifile, const gchar *formdir, const gchar *code)
+*group_new (GtkBuilder *gtkbuilder, const gchar *guifile, LDAP *ldap,
+            const gchar *cn, const gchar *groups_ou, const gchar *base_dn)
 {
        GError *error;
 
+       g_return_val_if_fail (cn != NULL && groups_ou != NULL && base_dn != NULL, NULL);
+
        Group *a = GROUP (g_object_new (group_get_type (), NULL));
 
        GroupPrivate *priv = GROUP_GET_PRIVATE (a);
 
        priv->gtkbuilder = gtkbuilder;
-       priv->gdaex = gdaex;
        priv->guifile = g_strdup (guifile);
-       priv->formdir = g_strdup (formdir);
+       priv->ldap = ldap;
 
        error = NULL;
        gtk_builder_add_objects_from_file (priv->gtkbuilder, priv->guifile,
@@ -144,40 +134,23 @@ Group
                        return NULL;
                }
 
-       priv->form = gtk_form_new ();
-       g_object_set (priv->form, "gdaex", priv->gdaex, NULL);
-       gtk_form_load_from_file (priv->form, g_build_filename (priv->formdir, "group.form", NULL), priv->gtkbuilder);
-
-       g_object_set (priv->form, "gdaex", priv->gdaex, NULL);
-
        priv->w = GTK_WIDGET (gtk_builder_get_object (priv->gtkbuilder, "w_group"));
+       priv->txt_cn = GTK_WIDGET (gtk_builder_get_object (priv->gtkbuilder, "entry5"));
+       priv->lbl_gid = GTK_WIDGET (gtk_builder_get_object (priv->gtkbuilder, "label18"));
 
-       priv->objects = gtk_form_get_objects_by_name (priv->form,
-                                                     "entry1",
-                                                     "label14",
-                                                     NULL);
-
-       g_signal_connect (priv->w,
-                         "delete-event", G_CALLBACK (group_on_w_group_delete_event), (gpointer *)a);
-
-       g_signal_connect (gtk_builder_get_object (priv->gtkbuilder, "button5"),
+       g_signal_connect (gtk_builder_get_object (priv->gtkbuilder, "button7"),
                          "clicked", G_CALLBACK (group_on_btn_annulla_clicked), (gpointer *)a);
-       g_signal_connect (gtk_builder_get_object (priv->gtkbuilder, "button6"),
+       g_signal_connect (gtk_builder_get_object (priv->gtkbuilder, "button8"),
                          "clicked", G_CALLBACK (group_on_btn_salva_clicked), (gpointer *)a);
 
-       if (code != NULL)
-               {
-                       priv->code = g_strstrip (g_strdup (code));
-               }
-       if (priv->code == NULL || g_strcmp0 (priv->code, "") == 0)
-               {
-                       gtk_form_clear (priv->form);
-               }
-       else
-               {
-                       gtk_entry_set_text (GTK_ENTRY (priv->objects[TXT_CODE]), priv->code);
-                       gtk_editable_set_editable (GTK_EDITABLE (priv->objects[TXT_CODE]), FALSE);
+       priv->cn = g_strstrip (g_strdup (cn));
+       priv->groups_ou = g_strstrip (g_strdup (groups_ou));
+       priv->base_dn = g_strstrip (g_strdup (base_dn));
 
+       gtk_entry_set_text (GTK_ENTRY (priv->txt_cn), "");
+       gtk_label_set_text (GTK_LABEL (priv->lbl_gid), "");
+       if (g_strcmp0 (priv->cn, "") != 0)
+               {
                        group_carica (a);
                }
 
@@ -203,45 +176,90 @@ group_carica (Group *group)
 {
        GroupPrivate *priv = GROUP_GET_PRIVATE (group);
 
-       if (gtk_form_fill_from_table (priv->form))
+       int retldap;
+       LDAPMessage *result;
+       LDAPMessage *entry;
+       gchar **attrs;
+       gchar *base;
+
+       BerElement *ber;
+       gchar *attr;
+       struct berval **vals;
+
+       attrs = g_strsplit ("cn|gidNumber", "|", -1);
+
+       result = NULL;
+       base = g_strdup_printf ("%s,%s", priv->groups_ou, priv->base_dn);
+       retldap = ldap_search_ext_s (priv->ldap, base, LDAP_SCOPE_ONELEVEL,
+                                    g_strdup_printf ("(cn=%s)", priv->cn), attrs, 0, NULL, NULL, LDAP_NO_LIMIT,
+                                    LDAP_NO_LIMIT, &result);
+       if (retldap != LDAP_SUCCESS)
                {
+                       g_warning ("Errore nel search: %s", ldap_err2string (retldap));
+                       return;
+               }
+
+       entry = ldap_first_entry (priv->ldap, result);
+       if (entry != NULL)
+               {
+                       for (attr = ldap_first_attribute (priv->ldap, entry, &ber);
+                            attr != NULL; attr = ldap_next_attribute (priv->ldap, entry, ber))
+                               {
+                                       vals = ldap_get_values_len (priv->ldap, entry, attr);
+                                       if (vals != NULL)
+                                               {
+                                                       if (vals[0] != NULL)
+                                                               {
+                                                                       if (g_strcmp0 (attr, "cn") == 0)
+                                                                               {
+                                                                                       gtk_entry_set_text (GTK_ENTRY (priv->txt_cn), vals[0]->bv_val);
+                                                                               }
+                                                                       else if (g_strcmp0 (attr, "gidNumber") == 0)
+                                                                               {
+                                                                                       gtk_label_set_text (GTK_LABEL (priv->lbl_gid), vals[0]->bv_val);
+                                                                               }
+                                                               }
+                                                       ldap_value_free_len (vals);
+                                               }
+                                       ldap_memfree (attr);
+                               }
+                       if (ber != NULL)
+                               {
+                                       ber_free (ber, 0);
+                               }
+               }
+       else
+               {
+                       g_warning ("Group «%s» not found.", g_strdup_printf ("cn=%s,%s", base, priv->cn));
                }
 }
 
 static void
 group_salva (Group *group)
 {
-       GError *error = NULL;
-       gchar *sql;
        GtkWidget *dialog;
 
-       GDate *da;
-       GDate *a;
-
        GroupClass *klass = GROUP_GET_CLASS (group);
 
        GroupPrivate *priv = GROUP_GET_PRIVATE (group);
 
-       if (!gtk_form_check (priv->form, FALSE, NULL, TRUE, priv->w, TRUE))
-               {
-                       return;
-               }
-
-       if (priv->code == NULL || g_strcmp0 (priv->code, "") == 0)
-               {
-                       sql = gtk_form_get_sql (priv->form, GTK_FORM_SQL_INSERT);
-               }
-       else
-               {
-                       sql = gtk_form_get_sql (priv->form, GTK_FORM_SQL_UPDATE);
-               }
+       /*if (gdaex_execute (priv->gdaex, sql) == 1)
+               {*/
+                       if (g_strcmp0 (priv->cn, "") == 0)
+                               {
+                                       autesmbldap_exec_ssh_command (g_strdup_printf ("sudo /usr/sbin/smbldap-groupadd -a %s",
+                                                                 gtk_entry_get_text (GTK_ENTRY (priv->txt_cn))));
+                                       priv->cn = g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->txt_cn)));
+                                       group_carica (group);
+                               }
+                       else
+                               {
+                                       /* TODO */
+                                       return;
+                               }
 
-       if (gdaex_execute (priv->gdaex, sql) == 1)
-               {
                        g_signal_emit (group, klass->aggiornato_signal_id, 0);
 
-                       gtk_form_set_as_origin (priv->form);
-
                        dialog = gtk_message_dialog_new (GTK_WINDOW (priv->w),
                                                                                         GTK_DIALOG_DESTROY_WITH_PARENT,
                                                                                         GTK_MESSAGE_INFO,
@@ -249,7 +267,7 @@ group_salva (Group *group)
                                                                                         "Salvataggio eseguito con successo.");
                        gtk_dialog_run (GTK_DIALOG (dialog));
                        gtk_widget_destroy (dialog);
-               }
+               /*}
        else
                {
                        dialog = gtk_message_dialog_new (GTK_WINDOW (priv->w),
@@ -259,7 +277,7 @@ group_salva (Group *group)
                                                                                         "Errore durante il salvataggio.");
                        gtk_dialog_run (GTK_DIALOG (dialog));
                        gtk_widget_destroy (dialog);
-               }
+               }*/
 }
 
 static void
@@ -292,42 +310,7 @@ group_get_property (GObject *object, guint property_id, GValue *value, GParamSpe
                }
 }
 
-static gboolean
-group_conferma_chiusura (Group *group)
-{
-       GtkWidget *dialog;
-
-       gboolean ret;
-
-       GroupPrivate *priv = GROUP_GET_PRIVATE (group);
-
-       ret = TRUE;
-       if (gtk_form_is_changed (priv->form))
-               {
-                       dialog = gtk_message_dialog_new (GTK_WINDOW (priv->w),
-                                                                GTK_DIALOG_DESTROY_WITH_PARENT,
-                                                                GTK_MESSAGE_QUESTION,
-                                                                GTK_BUTTONS_YES_NO,
-                                                                "Sicuro di voler chiudere senza salvare?");
-                       if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_NO)
-                               {
-                                       ret = FALSE;
-                               }
-                       gtk_widget_destroy (dialog);
-               }
-
-       return ret;
-}
-
 /* CALLBACK */
-static gboolean
-group_on_w_group_delete_event (GtkWidget *widget,
-                               GdkEvent *event,
-                               gpointer group_data)
-{
-       return !group_conferma_chiusura ((Group *)group_data);
-}
-
 static void
 group_on_btn_annulla_clicked (GtkButton *button,
                         gpointer group_data)
@@ -336,7 +319,7 @@ group_on_btn_annulla_clicked (GtkButton *button,
 
        GroupPrivate *priv = GROUP_GET_PRIVATE (group);
 
-       if (group_conferma_chiusura (group)) gtk_widget_destroy (priv->w);
+       gtk_widget_destroy (priv->w);
 }
 
 static void
index 6d2cd20c5d7e4cbb8517938ad8e2476e5067dd37..1010c918f1d0f31131d6eca327065edcaa8a32d9 100644 (file)
@@ -24,7 +24,7 @@
 #include <glib-object.h>
 
 #include <gtk/gtk.h>
-#include <libgdaex.h>
+#include <ldap.h>
 
 G_BEGIN_DECLS
 
@@ -54,8 +54,9 @@ struct _GroupClass
 
 GType group_get_type (void) G_GNUC_CONST;
 
-Group *group_new (GtkBuilder *gtkbuilder, GdaEx *gdaex,
-                const gchar *guifile, const gchar *formdir, const gchar *code);
+Group *group_new (GtkBuilder *gtkbuilder, const gchar *guifile,
+                  LDAP *ldap,
+                  const gchar *cn, const gchar *groups_ou, const gchar *base_dn);
 
 GtkWidget *group_get_widget (Group *group);