[gnome-multi-writer] Check for authentication before copying



commit fd56e71ba0636e45fbf2a60e862d332908138ee2
Author: Richard Hughes <richard hughsie com>
Date:   Mon Aug 29 09:09:29 2016 +0100

    Check for authentication before copying
    
    The polkit auth times out, so we have to check each time.

 configure.ac                       |    1 +
 contrib/gnome-multi-writer.spec.in |    1 +
 src/Makefile.am                    |    2 ++
 src/gmw-main.c                     |   12 +++++++++---
 4 files changed, 13 insertions(+), 3 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 594df47..b399ab3 100644
--- a/configure.ac
+++ b/configure.ac
@@ -86,6 +86,7 @@ PKG_CHECK_MODULES(UDISKS, udisks2)
 PKG_CHECK_MODULES(GUDEV, gudev-1.0)
 PKG_CHECK_MODULES(GTK, gtk+-3.0 >= 3.11.2)
 PKG_CHECK_MODULES(CANBERRA, libcanberra-gtk3 >= 0.10)
+PKG_CHECK_MODULES(POLKIT, polkit-gobject-1)
 AC_PATH_PROG(APPSTREAM_UTIL, [appstream-util], [unfound])
 
 dnl ---------------------------------------------------------------------------
diff --git a/contrib/gnome-multi-writer.spec.in b/contrib/gnome-multi-writer.spec.in
index 62e8ec7..c5046aa 100644
--- a/contrib/gnome-multi-writer.spec.in
+++ b/contrib/gnome-multi-writer.spec.in
@@ -20,6 +20,7 @@ BuildRequires: libgusb-devel >= 0.2.4
 BuildRequires: libudisks2-devel
 BuildRequires: libgudev1-devel
 BuildRequires: libappstream-glib
+BuildRequires: polkit-devel
 
 Requires:      gnome-icon-theme-extras
 
diff --git a/src/Makefile.am b/src/Makefile.am
index a63dafd..756f99b 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -5,6 +5,7 @@ AM_CPPFLAGS =                                           \
        $(GUDEV_CFLAGS)                                 \
        $(GUSB_CFLAGS)                                  \
        $(SOUP_CFLAGS)                                  \
+       $(POLKIT_CFLAGS)                                \
        $(UDISKS_CFLAGS)                                \
        -DG_LOG_DOMAIN=\"GnomeMultiWriter\"             \
        -DG_USB_API_IS_SUBJECT_TO_CHANGE                \
@@ -19,6 +20,7 @@ gnome_multi_writer_LDADD =                            \
        $(GLIB_LIBS)                                    \
        $(GTK_LIBS)                                     \
        $(GUSB_LIBS)                                    \
+       $(POLKIT_LIBS)                                  \
        $(UDISKS_LIBS)
 
 gnome_multi_writer_CFLAGS =                            \
diff --git a/src/gmw-main.c b/src/gmw-main.c
index 7598e8e..f50d6cf 100644
--- a/src/gmw-main.c
+++ b/src/gmw-main.c
@@ -56,7 +56,6 @@ typedef struct {
        UDisksClient            *udisks_client;
        GThreadPool             *thread_pool;
        GMutex                   thread_pool_mutex;
-       gboolean                 done_polkit_auth;
        guint                    inhibit_id;
        guint                    throughput_id;
        guint                    idle_id;
@@ -1213,8 +1212,10 @@ gmw_start_copy (GmwPrivate *priv)
 {
        GmwDevice *device;
        GtkWindow *window;
+       const gchar *action_id = "org.freedesktop.udisks2.open-device";
        guint i;
        g_autoptr(GError) error = NULL;
+       g_autoptr(GPermission) permission = NULL;
 
        /* if nothing already set, request this now */
        if (priv->image_file == NULL)
@@ -1233,8 +1234,14 @@ gmw_start_copy (GmwPrivate *priv)
                }
        }
 
+
        /* do a dummy call to get the PolicyKit auth */
-       if (!priv->done_polkit_auth) {
+       permission = polkit_permission_new_sync (action_id, NULL, NULL, &error);
+       if (permission == NULL) {
+               gmw_error_dialog (priv, "Failed to get permission", error->message);
+               return;
+       }
+       if (!g_permission_get_allowed (permission)) {
                device = g_ptr_array_index (priv->devices, 0);
                if (!gmw_auth_dummy_restore (priv, device, &error)) {
                        g_dbus_error_strip_remote_error (error);
@@ -1245,7 +1252,6 @@ gmw_start_copy (GmwPrivate *priv)
                                          error->message);
                        return;
                }
-               priv->done_polkit_auth = TRUE;
        }
 
        /* update the global stats */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]