Skip to content

Commit

Permalink
Allow launching with pkexec (elementary#951)
Browse files Browse the repository at this point in the history
* Allow launching with pkexec

* Update Application.vala

Co-authored-by: Jeremy Wootten <[email protected]>
  • Loading branch information
danirabbit and Jeremy Wootten authored Feb 24, 2021
1 parent 881266f commit 4bb9ec5
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
20 changes: 20 additions & 0 deletions data/code.policy.in.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE policyconfig PUBLIC
"-//freedesktop//DTD PolicyKit Policy Configuration 1.0//EN"
"http://www.freedesktop.org/standards/PolicyKit/1.0/policyconfig.dtd">
<policyconfig>
<vendor>elementary</vendor>
<vendor_url>https://github.com/elementary/code</vendor_url>
<icon_name>io.elementary.code</icon_name>
<action id="org.freedesktop.policykit.pkexec.io.elementary.code">
<description>Run Code as Administrator</description>
<message>Authentication is required to run Code as Administrator</message>
<defaults>
<allow_any>auth_admin</allow_any>
<allow_inactive>auth_admin</allow_inactive>
<allow_active>auth_admin</allow_active>
</defaults>
<annotate key="org.freedesktop.policykit.exec.path">@install_prefix@/@bin_dir@/@exec_name@</annotate>
<annotate key="org.freedesktop.policykit.exec.allow_gui">true</annotate>
</action>
</policyconfig>
21 changes: 21 additions & 0 deletions data/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,24 @@ appstream_file = i18n.merge_file(
install_dir: join_paths(get_option('datadir'), 'metainfo'),
install: true
)

config_data = configuration_data()
config_data.set('install_prefix', get_option('prefix'))
config_data.set('bin_dir', get_option('bindir'))
config_data.set('exec_name', meson.project_name())

policy_in = configure_file(
input: 'code.policy.in.in',
output: meson.project_name() + '.policy.in',
configuration: config_data,
install: false,
)

i18n.merge_file(
'policy',
input: policy_in,
output: meson.project_name() + '.policy',
po_dir: join_paths(meson.source_root (), 'po', 'extra'),
install: true,
install_dir: join_paths(get_option('datadir'), 'polkit-1', 'actions'),
)
1 change: 1 addition & 0 deletions po/extra/POTFILES
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
data/io.elementary.code.appdata.xml.in
data/io.elementary.code.desktop.in.in
data/code.policy.in.in
7 changes: 7 additions & 0 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ namespace Scratch {
}

public override int command_line (GLib.ApplicationCommandLine command_line) {
/* Only allow running with root privileges using pkexec, not using sudo */
if (Posix.getuid () == 0 && GLib.Environment.get_variable ("PKEXEC_UID") == null) {
warning ("Running Code using sudo is not possible. Use: pkexec io.elementary.code");
quit ();
return 1;
};

var options = command_line.get_options_dict ();

if (options.contains ("new-tab")) {
Expand Down

0 comments on commit 4bb9ec5

Please sign in to comment.