From 051408b6eb96eeb07083a3f09275184dd55d5d9d Mon Sep 17 00:00:00 2001 From: Leo Date: Mon, 29 Jan 2024 02:11:12 +0900 Subject: [PATCH] Allow adding new system connections without admin prompt (#314) --- .github/workflows/main.yml | 2 +- README.md | 1 + data/meson.build | 7 +++++++ data/network.policy.in | 21 +++++++++++++++++++++ meson.build | 3 +++ src/Widgets/WifiInterface.vala | 13 +++++++++++++ 6 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 data/network.policy.in diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 1afe89c3..48692455 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -23,7 +23,7 @@ jobs: - name: Install Dependencies run: | apt update - apt install -y gobject-introspection libgranite-dev libnm-dev libnma-dev libwingpanel-dev meson valac + apt install -y gobject-introspection libgranite-dev libnm-dev libnma-dev libwingpanel-dev libpolkit-gobject-1-dev meson valac - name: Build env: DESTDIR: out diff --git a/README.md b/README.md index 42648cd3..f411f4e2 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ You'll need the following dependencies: * libgranite-dev * libnm-dev * libnma-dev +* libpolkit-gobject-1-dev * libwingpanel-dev * meson * valac diff --git a/data/meson.build b/data/meson.build index 3430fe1e..802309c2 100644 --- a/data/meson.build +++ b/data/meson.build @@ -6,3 +6,10 @@ i18n.merge_file( install: true, install_dir: join_paths(get_option('datadir'), 'metainfo'), ) + +configure_file( + input: 'network.policy.in', + output: 'io.elementary.wingpanel.network.policy', + configuration: config_data, + install_dir: polkit_dep.get_pkgconfig_variable('policydir', define_variable: ['prefix', prefix]) +) diff --git a/data/network.policy.in b/data/network.policy.in new file mode 100644 index 00000000..3ed2f2a7 --- /dev/null +++ b/data/network.policy.in @@ -0,0 +1,21 @@ + + + + elementary + https://elementary.io/ + + + Modify network connections for all users + Authentication is required to manage global network settings + preferences-system-network + + auth_admin + auth_admin + yes + + org.freedesktop.NetworkManager.settings.modify.system + + + \ No newline at end of file diff --git a/meson.build b/meson.build index 7f66febd..9eefc926 100644 --- a/meson.build +++ b/meson.build @@ -38,6 +38,8 @@ gresource = gnome.compile_resources( source_dir: 'data' ) +polkit_dep = dependency ('polkit-gobject-1') + shared_module( meson.project_name(), config_file, @@ -61,6 +63,7 @@ shared_module( dependency('gtk+-3.0'), dependency('libnm', version: '>=1.24'), dependency('libnma'), + polkit_dep, meson.get_compiler('vala').find_library('posix'), wingpanel_dep ], diff --git a/src/Widgets/WifiInterface.vala b/src/Widgets/WifiInterface.vala index 9ad321fa..8ffc6390 100644 --- a/src/Widgets/WifiInterface.vala +++ b/src/Widgets/WifiInterface.vala @@ -17,6 +17,8 @@ */ public class Network.WifiInterface : Network.WidgetNMInterface { + private static Polkit.Permission? permission = null; + public NM.Client nm_client { get; construct; } public NM.DeviceWifi? wifi_device; @@ -238,6 +240,17 @@ public class Network.WifiInterface : Network.WidgetNMInterface { return; } + if (permission == null) { + try { + permission = new Polkit.Permission.sync ( + "io.elementary.wingpanel.network.administration", + new Polkit.UnixProcess (Posix.getpid ()) + ); + } catch (Error e) { + warning ("Can't get permission to add Wi-Fi network without prompting for admin: %s", e.message); + } + } + // See if we already have a connection configured for this AP and try connecting if so var connections = nm_client.get_connections (); var device_connections = wifi_device.filter_connections (connections);