up-device: polkit check when setting charge strategy

The set operation should be allowed by polkit.
This commit is contained in:
Kate Hsuan 2026-01-22 16:43:10 +08:00
parent 69e4cb8011
commit 0ca7766243
4 changed files with 19 additions and 6 deletions

View file

@ -29,4 +29,12 @@ file are instantly applied.
</defaults>
</action>
<action id="org.freedesktop.UPower.set-charge-strategy">
<description>Change the battery charging strategy</description>
<message>Authentication is required to set battery charging strategy.</message>
<defaults>
<allow_inactive>no</allow_inactive>
<allow_active>yes</allow_active>
</defaults>
</action>
</policyconfig>

View file

@ -669,7 +669,7 @@ up_device_battery_set_charge_threshold (UpExportedDevice *skeleton,
return FALSE;
}
if (!up_device_polkit_is_allowed (device, invocation)) {
if (!up_device_polkit_is_allowed (device, invocation, "org.freedesktop.UPower.enable-charging-limit")) {
g_dbus_method_invocation_return_error (invocation,
UP_DAEMON_ERROR, UP_DAEMON_ERROR_GENERAL,
"Operation is not allowed.");
@ -805,8 +805,10 @@ up_device_battery_set_charge_strategy (UpExportedDevice *skeleton,
return FALSE;
}
if (!up_device_polkit_is_allowed (device, invocation)) {
if (!up_device_polkit_is_allowed (device, invocation, "org.freedesktop.UPower.set-charge-strategy")) {
g_dbus_method_invocation_return_error (invocation,
UP_DAEMON_ERROR, UP_DAEMON_ERROR_GENERAL,
"Operation is not allowed.");
return TRUE;
}

View file

@ -407,12 +407,14 @@ up_device_get_daemon (UpDevice *device)
* up_device_polkit_is_allowed
**/
gboolean
up_device_polkit_is_allowed (UpDevice *device, GDBusMethodInvocation *invocation)
up_device_polkit_is_allowed (UpDevice *device, GDBusMethodInvocation *invocation, const gchar *action_id)
{
UpDevicePrivate *priv = up_device_get_instance_private (device);
g_return_val_if_fail (UP_IS_DEVICE (device), FALSE);
g_return_val_if_fail (action_id != NULL, FALSE);
if (!up_daemon_polkit_is_allowed (priv->daemon,
"org.freedesktop.UPower.enable-charging-limit",
action_id,
invocation))
return FALSE;

View file

@ -69,7 +69,8 @@ gboolean up_device_get_online (UpDevice *device,
gboolean *online);
const gchar *up_device_get_state_dir_override (UpDevice *device);
gboolean up_device_polkit_is_allowed (UpDevice *device,
GDBusMethodInvocation *invocation);
GDBusMethodInvocation *invocation,
const gchar *action_id);
void up_device_sibling_discovered (UpDevice *device,
GObject *sibling);
gboolean up_device_refresh_internal (UpDevice *device,