From 3f3bb6ee54e26eee3c160f003c15861a6b7999b1 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Tue, 19 Mar 2019 16:55:11 +0000 Subject: [PATCH] lib: Change allow_interactive_authorization bool to flags This is an API break for the mct_{get,set}_app_filter() APIs, but makes them a bit clearer to use, and a little more future proof. This includes updates to all the tests and documentation. Signed-off-by: Philip Withnall --- libmalcontent/app-filter.c | 85 ++++++++++++-------------- libmalcontent/app-filter.h | 42 +++++++++++-- libmalcontent/tests/app-filter.c | 45 +++++++++----- malcontent-client/malcontent-client.py | 14 ++++- 4 files changed, 120 insertions(+), 66 deletions(-) diff --git a/libmalcontent/app-filter.c b/libmalcontent/app-filter.c index 140b628..914e7c8 100644 --- a/libmalcontent/app-filter.c +++ b/libmalcontent/app-filter.c @@ -563,22 +563,21 @@ accounts_find_user_by_id (GDBusConnection *connection, * @connection: (nullable): a #GDBusConnection to the system bus, or %NULL to * use the default * @user_id: ID of the user to query, typically coming from getuid() - * @allow_interactive_authorization: %TRUE to allow interactive polkit - * authorization dialogues to be displayed during the call; %FALSE otherwise + * @flags: flags to affect the behaviour of the call * @cancellable: (nullable): a #GCancellable, or %NULL * @error: return location for a #GError, or %NULL * * Synchronous version of mct_get_app_filter_async(). * * Returns: (transfer full): app filter for the queried user - * Since: 0.2.0 + * Since: 0.3.0 */ MctAppFilter * -mct_get_app_filter (GDBusConnection *connection, - uid_t user_id, - gboolean allow_interactive_authorization, - GCancellable *cancellable, - GError **error) +mct_get_app_filter (GDBusConnection *connection, + uid_t user_id, + MctGetAppFilterFlags flags, + GCancellable *cancellable, + GError **error) { g_autofree gchar *object_path = NULL; g_autoptr(GVariant) result_variant = NULL; @@ -603,7 +602,7 @@ mct_get_app_filter (GDBusConnection *connection, return NULL; object_path = accounts_find_user_by_id (connection, user_id, - allow_interactive_authorization, + (flags & MCT_GET_APP_FILTER_FLAGS_INTERACTIVE), cancellable, error); if (object_path == NULL) return NULL; @@ -616,7 +615,7 @@ mct_get_app_filter (GDBusConnection *connection, "GetAll", g_variant_new ("(s)", "com.endlessm.ParentalControls.AppFilter"), G_VARIANT_TYPE ("(a{sv})"), - allow_interactive_authorization + (flags & MCT_GET_APP_FILTER_FLAGS_INTERACTIVE) ? G_DBUS_CALL_FLAGS_ALLOW_INTERACTIVE_AUTHORIZATION : G_DBUS_CALL_FLAGS_NONE, -1, /* timeout, ms */ @@ -700,7 +699,7 @@ typedef struct { GDBusConnection *connection; /* (nullable) (owned) */ uid_t user_id; - gboolean allow_interactive_authorization; + MctGetAppFilterFlags flags; } GetAppFilterData; static void @@ -717,8 +716,7 @@ G_DEFINE_AUTOPTR_CLEANUP_FUNC (GetAppFilterData, get_app_filter_data_free) * @connection: (nullable): a #GDBusConnection to the system bus, or %NULL to * use the default * @user_id: ID of the user to query, typically coming from getuid() - * @allow_interactive_authorization: %TRUE to allow interactive polkit - * authorization dialogues to be displayed during the call; %FALSE otherwise + * @flags: flags to affect the behaviour of the call * @cancellable: (nullable): a #GCancellable, or %NULL * @callback: a #GAsyncReadyCallback * @user_data: user data to pass to @callback @@ -733,12 +731,12 @@ G_DEFINE_AUTOPTR_CLEANUP_FUNC (GetAppFilterData, get_app_filter_data_free) * On failure, an #MctAppFilterError, a #GDBusError or a #GIOError will be * returned. * - * Since: 0.2.0 + * Since: 0.3.0 */ void mct_get_app_filter_async (GDBusConnection *connection, uid_t user_id, - gboolean allow_interactive_authorization, + MctGetAppFilterFlags flags, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data) @@ -755,7 +753,7 @@ mct_get_app_filter_async (GDBusConnection *connection, data = g_new0 (GetAppFilterData, 1); data->connection = (connection != NULL) ? g_object_ref (connection) : NULL; data->user_id = user_id; - data->allow_interactive_authorization = allow_interactive_authorization; + data->flags = flags; g_task_set_task_data (task, g_steal_pointer (&data), (GDestroyNotify) get_app_filter_data_free); @@ -773,7 +771,7 @@ get_app_filter_thread_cb (GTask *task, g_autoptr(GError) local_error = NULL; filter = mct_get_app_filter (data->connection, data->user_id, - data->allow_interactive_authorization, + data->flags, cancellable, &local_error); if (local_error != NULL) @@ -810,23 +808,22 @@ mct_get_app_filter_finish (GAsyncResult *result, * use the default * @user_id: ID of the user to set the filter for, typically coming from getuid() * @app_filter: (transfer none): the app filter to set for the user - * @allow_interactive_authorization: %TRUE to allow interactive polkit - * authorization dialogues to be displayed during the call; %FALSE otherwise + * @flags: flags to affect the behaviour of the call * @cancellable: (nullable): a #GCancellable, or %NULL * @error: return location for a #GError, or %NULL * * Synchronous version of mct_set_app_filter_async(). * * Returns: %TRUE on success, %FALSE otherwise - * Since: 0.2.0 + * Since: 0.3.0 */ gboolean -mct_set_app_filter (GDBusConnection *connection, - uid_t user_id, - MctAppFilter *app_filter, - gboolean allow_interactive_authorization, - GCancellable *cancellable, - GError **error) +mct_set_app_filter (GDBusConnection *connection, + uid_t user_id, + MctAppFilter *app_filter, + MctSetAppFilterFlags flags, + GCancellable *cancellable, + GError **error) { g_autofree gchar *object_path = NULL; g_autoptr(GVariant) app_filter_variant = NULL; @@ -851,7 +848,7 @@ mct_set_app_filter (GDBusConnection *connection, return FALSE; object_path = accounts_find_user_by_id (connection, user_id, - allow_interactive_authorization, + (flags & MCT_SET_APP_FILTER_FLAGS_INTERACTIVE), cancellable, error); if (object_path == NULL) return FALSE; @@ -873,7 +870,7 @@ mct_set_app_filter (GDBusConnection *connection, "AppFilter", g_steal_pointer (&app_filter_variant)), G_VARIANT_TYPE ("()"), - allow_interactive_authorization + (flags & MCT_SET_APP_FILTER_FLAGS_INTERACTIVE) ? G_DBUS_CALL_FLAGS_ALLOW_INTERACTIVE_AUTHORIZATION : G_DBUS_CALL_FLAGS_NONE, -1, /* timeout, ms */ @@ -896,7 +893,7 @@ mct_set_app_filter (GDBusConnection *connection, "OarsFilter", g_steal_pointer (&oars_filter_variant)), G_VARIANT_TYPE ("()"), - allow_interactive_authorization + (flags & MCT_SET_APP_FILTER_FLAGS_INTERACTIVE) ? G_DBUS_CALL_FLAGS_ALLOW_INTERACTIVE_AUTHORIZATION : G_DBUS_CALL_FLAGS_NONE, -1, /* timeout, ms */ @@ -919,7 +916,7 @@ mct_set_app_filter (GDBusConnection *connection, "AllowUserInstallation", g_steal_pointer (&allow_user_installation_variant)), G_VARIANT_TYPE ("()"), - allow_interactive_authorization + (flags & MCT_SET_APP_FILTER_FLAGS_INTERACTIVE) ? G_DBUS_CALL_FLAGS_ALLOW_INTERACTIVE_AUTHORIZATION : G_DBUS_CALL_FLAGS_NONE, -1, /* timeout, ms */ @@ -942,7 +939,7 @@ mct_set_app_filter (GDBusConnection *connection, "AllowSystemInstallation", g_steal_pointer (&allow_system_installation_variant)), G_VARIANT_TYPE ("()"), - allow_interactive_authorization + (flags & MCT_SET_APP_FILTER_FLAGS_INTERACTIVE) ? G_DBUS_CALL_FLAGS_ALLOW_INTERACTIVE_AUTHORIZATION : G_DBUS_CALL_FLAGS_NONE, -1, /* timeout, ms */ @@ -967,7 +964,7 @@ typedef struct GDBusConnection *connection; /* (nullable) (owned) */ uid_t user_id; MctAppFilter *app_filter; /* (owned) */ - gboolean allow_interactive_authorization; + MctSetAppFilterFlags flags; } SetAppFilterData; static void @@ -986,8 +983,7 @@ G_DEFINE_AUTOPTR_CLEANUP_FUNC (SetAppFilterData, set_app_filter_data_free) * use the default * @user_id: ID of the user to set the filter for, typically coming from getuid() * @app_filter: (transfer none): the app filter to set for the user - * @allow_interactive_authorization: %TRUE to allow interactive polkit - * authorization dialogues to be displayed during the call; %FALSE otherwise + * @flags: flags to affect the behaviour of the call * @cancellable: (nullable): a #GCancellable, or %NULL * @callback: a #GAsyncReadyCallback * @user_data: user data to pass to @callback @@ -1002,16 +998,16 @@ G_DEFINE_AUTOPTR_CLEANUP_FUNC (SetAppFilterData, set_app_filter_data_free) * On failure, an #MctAppFilterError, a #GDBusError or a #GIOError will be * returned. The user’s app filter settings will be left in an undefined state. * - * Since: 0.2.0 + * Since: 0.3.0 */ void -mct_set_app_filter_async (GDBusConnection *connection, - uid_t user_id, - MctAppFilter *app_filter, - gboolean allow_interactive_authorization, - GCancellable *cancellable, - GAsyncReadyCallback callback, - gpointer user_data) +mct_set_app_filter_async (GDBusConnection *connection, + uid_t user_id, + MctAppFilter *app_filter, + MctSetAppFilterFlags flags, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data) { g_autoptr(GTask) task = NULL; g_autoptr(SetAppFilterData) data = NULL; @@ -1028,7 +1024,7 @@ mct_set_app_filter_async (GDBusConnection *connection, data->connection = (connection != NULL) ? g_object_ref (connection) : NULL; data->user_id = user_id; data->app_filter = mct_app_filter_ref (app_filter); - data->allow_interactive_authorization = allow_interactive_authorization; + data->flags = flags; g_task_set_task_data (task, g_steal_pointer (&data), (GDestroyNotify) set_app_filter_data_free); @@ -1046,8 +1042,7 @@ set_app_filter_thread_cb (GTask *task, g_autoptr(GError) local_error = NULL; success = mct_set_app_filter (data->connection, data->user_id, - data->app_filter, - data->allow_interactive_authorization, + data->app_filter, data->flags, cancellable, &local_error); if (local_error != NULL) diff --git a/libmalcontent/app-filter.h b/libmalcontent/app-filter.h index 67bf8e7..dd22788 100644 --- a/libmalcontent/app-filter.h +++ b/libmalcontent/app-filter.h @@ -76,6 +76,40 @@ typedef enum MCT_APP_FILTER_OARS_VALUE_INTENSE, } MctAppFilterOarsValue; +/** + * MctGetAppFilterFlags: + * @MCT_GET_APP_FILTER_FLAGS_NONE: No flags set. + * @MCT_GET_APP_FILTER_FLAGS_INTERACTIVE: Allow interactive polkit dialogs when + * requesting authorization. + * + * Flags to control the behaviour of mct_get_app_filter() and + * mct_get_app_filter_async(). + * + * Since: 0.3.0 + */ +typedef enum +{ + MCT_GET_APP_FILTER_FLAGS_NONE = 0, + MCT_GET_APP_FILTER_FLAGS_INTERACTIVE, +} MctGetAppFilterFlags; + +/** + * MctSetAppFilterFlags: + * @MCT_SET_APP_FILTER_FLAGS_NONE: No flags set. + * @MCT_SET_APP_FILTER_FLAGS_INTERACTIVE: Allow interactive polkit dialogs when + * requesting authorization. + * + * Flags to control the behaviour of mct_set_app_filter() and + * mct_set_app_filter_async(). + * + * Since: 0.3.0 + */ +typedef enum +{ + MCT_SET_APP_FILTER_FLAGS_NONE = 0, + MCT_SET_APP_FILTER_FLAGS_INTERACTIVE, +} MctSetAppFilterFlags; + /** * MctAppFilter: * @@ -116,12 +150,12 @@ gboolean mct_app_filter_is_system_installation_allowed (MctAppFil MctAppFilter *mct_get_app_filter (GDBusConnection *connection, uid_t user_id, - gboolean allow_interactive_authorization, + MctGetAppFilterFlags flags, GCancellable *cancellable, GError **error); void mct_get_app_filter_async (GDBusConnection *connection, uid_t user_id, - gboolean allow_interactive_authorization, + MctGetAppFilterFlags flags, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data); @@ -131,13 +165,13 @@ MctAppFilter *mct_get_app_filter_finish (GAsyncResult *result, gboolean mct_set_app_filter (GDBusConnection *connection, uid_t user_id, MctAppFilter *app_filter, - gboolean allow_interactive_authorization, + MctSetAppFilterFlags flags, GCancellable *cancellable, GError **error); void mct_set_app_filter_async (GDBusConnection *connection, uid_t user_id, MctAppFilter *app_filter, - gboolean allow_interactive_authorization, + MctSetAppFilterFlags flags, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data); diff --git a/libmalcontent/tests/app-filter.c b/libmalcontent/tests/app-filter.c index a4ff981..c4b08c4 100644 --- a/libmalcontent/tests/app-filter.c +++ b/libmalcontent/tests/app-filter.c @@ -555,7 +555,8 @@ test_app_filter_bus_get (BusFixture *fixture, mct_get_app_filter_async (gt_dbus_queue_get_client_connection (fixture->queue), fixture->valid_uid, - FALSE, NULL, async_result_cb, &result); + MCT_GET_APP_FILTER_FLAGS_NONE, NULL, + async_result_cb, &result); while (result == NULL) g_main_context_iteration (NULL, TRUE); @@ -565,7 +566,8 @@ test_app_filter_bus_get (BusFixture *fixture, { app_filter = mct_get_app_filter (gt_dbus_queue_get_client_connection (fixture->queue), fixture->valid_uid, - FALSE, NULL, &local_error); + MCT_GET_APP_FILTER_FLAGS_NONE, NULL, + &local_error); } g_assert_no_error (local_error); @@ -608,7 +610,8 @@ test_app_filter_bus_get_whitelist (BusFixture *fixture, app_filter = mct_get_app_filter (gt_dbus_queue_get_client_connection (fixture->queue), fixture->valid_uid, - FALSE, NULL, &local_error); + MCT_GET_APP_FILTER_FLAGS_NONE, NULL, + &local_error); g_assert_no_error (local_error); g_assert_nonnull (app_filter); @@ -658,7 +661,8 @@ test_app_filter_bus_get_all_oars_values (BusFixture *fixture, app_filter = mct_get_app_filter (gt_dbus_queue_get_client_connection (fixture->queue), fixture->valid_uid, - FALSE, NULL, &local_error); + MCT_GET_APP_FILTER_FLAGS_NONE, NULL, + &local_error); g_assert_no_error (local_error); g_assert_nonnull (app_filter); @@ -705,7 +709,8 @@ test_app_filter_bus_get_defaults (BusFixture *fixture, app_filter = mct_get_app_filter (gt_dbus_queue_get_client_connection (fixture->queue), fixture->valid_uid, - FALSE, NULL, &local_error); + MCT_GET_APP_FILTER_FLAGS_NONE, NULL, + &local_error); g_assert_no_error (local_error); g_assert_nonnull (app_filter); @@ -736,7 +741,8 @@ test_app_filter_bus_get_error_invalid_user (BusFixture *fixture, mct_get_app_filter_async (gt_dbus_queue_get_client_connection (fixture->queue), fixture->missing_uid, - FALSE, NULL, async_result_cb, &result); + MCT_GET_APP_FILTER_FLAGS_NONE, NULL, + async_result_cb, &result); /* Handle the FindUserById() call and claim the user doesn’t exist. */ gint64 user_id; @@ -780,7 +786,8 @@ test_app_filter_bus_get_error_permission_denied (BusFixture *fixture, mct_get_app_filter_async (gt_dbus_queue_get_client_connection (fixture->queue), fixture->valid_uid, - FALSE, NULL, async_result_cb, &result); + MCT_GET_APP_FILTER_FLAGS_NONE, NULL, + async_result_cb, &result); /* Handle the FindUserById() call. */ gint64 user_id; @@ -835,7 +842,8 @@ test_app_filter_bus_get_error_permission_denied_missing (BusFixture *fixture, mct_get_app_filter_async (gt_dbus_queue_get_client_connection (fixture->queue), fixture->valid_uid, - FALSE, NULL, async_result_cb, &result); + MCT_GET_APP_FILTER_FLAGS_NONE, NULL, + async_result_cb, &result); /* Handle the FindUserById() call. */ gint64 user_id; @@ -888,7 +896,8 @@ test_app_filter_bus_get_error_unknown (BusFixture *fixture, mct_get_app_filter_async (gt_dbus_queue_get_client_connection (fixture->queue), fixture->valid_uid, - FALSE, NULL, async_result_cb, &result); + MCT_GET_APP_FILTER_FLAGS_NONE, NULL, + async_result_cb, &result); /* Handle the FindUserById() call and return a bogus error. */ gint64 user_id; @@ -1068,7 +1077,8 @@ test_app_filter_bus_set (BusFixture *fixture, mct_set_app_filter_async (gt_dbus_queue_get_client_connection (fixture->queue), fixture->valid_uid, app_filter, - FALSE, NULL, async_result_cb, &result); + MCT_SET_APP_FILTER_FLAGS_NONE, NULL, + async_result_cb, &result); while (result == NULL) g_main_context_iteration (NULL, TRUE); @@ -1078,7 +1088,8 @@ test_app_filter_bus_set (BusFixture *fixture, { success = mct_set_app_filter (gt_dbus_queue_get_client_connection (fixture->queue), fixture->valid_uid, app_filter, - FALSE, NULL, &local_error); + MCT_SET_APP_FILTER_FLAGS_NONE, NULL, + &local_error); } g_assert_no_error (local_error); @@ -1106,7 +1117,8 @@ test_app_filter_bus_set_error_invalid_user (BusFixture *fixture, mct_set_app_filter_async (gt_dbus_queue_get_client_connection (fixture->queue), fixture->missing_uid, app_filter, - FALSE, NULL, async_result_cb, &result); + MCT_SET_APP_FILTER_FLAGS_NONE, NULL, + async_result_cb, &result); /* Handle the FindUserById() call and claim the user doesn’t exist. */ gint64 user_id; @@ -1160,7 +1172,8 @@ test_app_filter_bus_set_error_permission_denied (BusFixture *fixture, success = mct_set_app_filter (gt_dbus_queue_get_client_connection (fixture->queue), fixture->valid_uid, app_filter, - FALSE, NULL, &local_error); + MCT_SET_APP_FILTER_FLAGS_NONE, NULL, + &local_error); g_assert_error (local_error, MCT_APP_FILTER_ERROR, MCT_APP_FILTER_ERROR_PERMISSION_DENIED); @@ -1197,7 +1210,8 @@ test_app_filter_bus_set_error_unknown (BusFixture *fixture, success = mct_set_app_filter (gt_dbus_queue_get_client_connection (fixture->queue), fixture->valid_uid, app_filter, - FALSE, NULL, &local_error); + MCT_SET_APP_FILTER_FLAGS_NONE, NULL, + &local_error); g_assert_error (local_error, G_IO_ERROR, G_IO_ERROR_DBUS_ERROR); g_assert_false (success); @@ -1239,7 +1253,8 @@ test_app_filter_bus_set_error_invalid_property (BusFixture *fixture, success = mct_set_app_filter (gt_dbus_queue_get_client_connection (fixture->queue), fixture->valid_uid, app_filter, - FALSE, NULL, &local_error); + MCT_SET_APP_FILTER_FLAGS_NONE, NULL, + &local_error); g_assert_error (local_error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS); g_assert_false (success); diff --git a/malcontent-client/malcontent-client.py b/malcontent-client/malcontent-client.py index ebbf5c2..49834e4 100644 --- a/malcontent-client/malcontent-client.py +++ b/malcontent-client/malcontent-client.py @@ -37,9 +37,14 @@ def __get_app_filter(user_id, interactive): If `interactive` is `True`, interactive polkit authorisation dialogues will be allowed. An exception will be raised on failure.""" + if interactive: + flags = Malcontent.GetAppFilterFlags.INTERACTIVE + else: + flags = Malcontent.GetAppFilterFlags.NONE + return Malcontent.get_app_filter( connection=None, user_id=user_id, - allow_interactive_authorization=interactive, cancellable=None) + flags=flags, cancellable=None) def __get_app_filter_or_error(user_id, interactive): @@ -58,9 +63,14 @@ def __set_app_filter(user_id, app_filter, interactive): If `interactive` is `True`, interactive polkit authorisation dialogues will be allowed. An exception will be raised on failure.""" + if interactive: + flags = Malcontent.GetAppFilterFlags.INTERACTIVE + else: + flags = Malcontent.GetAppFilterFlags.NONE + Malcontent.set_app_filter( connection=None, user_id=user_id, app_filter=app_filter, - allow_interactive_authorization=interactive, cancellable=None) + flags=flags, cancellable=None) def __set_app_filter_or_error(user_id, app_filter, interactive):