core: simplify auth chain permission callback handling
This commit is contained in:
@@ -61,11 +61,21 @@ free_data (gpointer data)
|
|||||||
g_free (tmp);
|
g_free (tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
default_call_func (NMAuthChain *chain,
|
||||||
|
const char *permission,
|
||||||
|
GError *error,
|
||||||
|
NMAuthCallResult result,
|
||||||
|
gpointer user_data)
|
||||||
|
{
|
||||||
|
if (!error)
|
||||||
|
nm_auth_chain_set_data (chain, permission, GUINT_TO_POINTER (result), NULL);
|
||||||
|
}
|
||||||
|
|
||||||
NMAuthChain *
|
NMAuthChain *
|
||||||
nm_auth_chain_new (PolkitAuthority *authority,
|
nm_auth_chain_new (PolkitAuthority *authority,
|
||||||
DBusGMethodInvocation *context,
|
DBusGMethodInvocation *context,
|
||||||
NMAuthChainResultFunc done_func,
|
NMAuthChainResultFunc done_func,
|
||||||
NMAuthChainCallFunc call_func,
|
|
||||||
gpointer user_data)
|
gpointer user_data)
|
||||||
{
|
{
|
||||||
NMAuthChain *self;
|
NMAuthChain *self;
|
||||||
@@ -75,7 +85,7 @@ nm_auth_chain_new (PolkitAuthority *authority,
|
|||||||
self->authority = g_object_ref (authority);
|
self->authority = g_object_ref (authority);
|
||||||
self->data = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, free_data);
|
self->data = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, free_data);
|
||||||
self->done_func = done_func;
|
self->done_func = done_func;
|
||||||
self->call_func = call_func;
|
self->call_func = /* call_func ? call_func : */ default_call_func;
|
||||||
self->context = context;
|
self->context = context;
|
||||||
self->user_data = user_data;
|
self->user_data = user_data;
|
||||||
return self;
|
return self;
|
||||||
|
@@ -57,7 +57,6 @@ typedef void (*NMAuthChainCallFunc) (NMAuthChain *chain,
|
|||||||
NMAuthChain *nm_auth_chain_new (PolkitAuthority *authority,
|
NMAuthChain *nm_auth_chain_new (PolkitAuthority *authority,
|
||||||
DBusGMethodInvocation *context,
|
DBusGMethodInvocation *context,
|
||||||
NMAuthChainResultFunc done_func,
|
NMAuthChainResultFunc done_func,
|
||||||
NMAuthChainCallFunc call_func,
|
|
||||||
gpointer user_data);
|
gpointer user_data);
|
||||||
|
|
||||||
gpointer nm_auth_chain_get_data (NMAuthChain *chain, const char *tag);
|
gpointer nm_auth_chain_get_data (NMAuthChain *chain, const char *tag);
|
||||||
|
114
src/nm-manager.c
114
src/nm-manager.c
@@ -2823,10 +2823,9 @@ sleep_auth_done_cb (NMAuthChain *chain,
|
|||||||
NMAuthCallResult result;
|
NMAuthCallResult result;
|
||||||
gboolean do_sleep;
|
gboolean do_sleep;
|
||||||
|
|
||||||
result = GPOINTER_TO_UINT (nm_auth_chain_get_data (chain, "result"));
|
|
||||||
do_sleep = GPOINTER_TO_UINT (nm_auth_chain_get_data (chain, "sleep"));
|
|
||||||
|
|
||||||
priv->auth_chains = g_slist_remove (priv->auth_chains, chain);
|
priv->auth_chains = g_slist_remove (priv->auth_chains, chain);
|
||||||
|
|
||||||
|
result = GPOINTER_TO_UINT (nm_auth_chain_get_data (chain, NM_AUTH_PERMISSION_SLEEP_WAKE));
|
||||||
if (error) {
|
if (error) {
|
||||||
nm_log_dbg (LOGD_CORE, "Sleep/wake request failed: %s", error->message);
|
nm_log_dbg (LOGD_CORE, "Sleep/wake request failed: %s", error->message);
|
||||||
ret_error = g_error_new (NM_MANAGER_ERROR,
|
ret_error = g_error_new (NM_MANAGER_ERROR,
|
||||||
@@ -2842,6 +2841,8 @@ sleep_auth_done_cb (NMAuthChain *chain,
|
|||||||
dbus_g_method_return_error (context, ret_error);
|
dbus_g_method_return_error (context, ret_error);
|
||||||
g_error_free (ret_error);
|
g_error_free (ret_error);
|
||||||
} else {
|
} else {
|
||||||
|
/* Auth success */
|
||||||
|
do_sleep = GPOINTER_TO_UINT (nm_auth_chain_get_data (chain, "sleep"));
|
||||||
_internal_sleep (self, do_sleep);
|
_internal_sleep (self, do_sleep);
|
||||||
dbus_g_method_return (context);
|
dbus_g_method_return (context);
|
||||||
}
|
}
|
||||||
@@ -2849,17 +2850,6 @@ sleep_auth_done_cb (NMAuthChain *chain,
|
|||||||
nm_auth_chain_unref (chain);
|
nm_auth_chain_unref (chain);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
|
||||||
sleep_auth_call_done_cb (NMAuthChain *chain,
|
|
||||||
const char *permission,
|
|
||||||
GError *error,
|
|
||||||
NMAuthCallResult result,
|
|
||||||
gpointer user_data)
|
|
||||||
{
|
|
||||||
if (!error)
|
|
||||||
nm_auth_chain_set_data (chain, "result", GUINT_TO_POINTER (result), NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
impl_manager_sleep (NMManager *self,
|
impl_manager_sleep (NMManager *self,
|
||||||
gboolean do_sleep,
|
gboolean do_sleep,
|
||||||
@@ -2903,11 +2893,7 @@ impl_manager_sleep (NMManager *self,
|
|||||||
if (!return_no_pk_error (priv->authority, "Permission", context))
|
if (!return_no_pk_error (priv->authority, "Permission", context))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
chain = nm_auth_chain_new (priv->authority,
|
chain = nm_auth_chain_new (priv->authority, context, sleep_auth_done_cb, self);
|
||||||
context,
|
|
||||||
sleep_auth_done_cb,
|
|
||||||
sleep_auth_call_done_cb,
|
|
||||||
self);
|
|
||||||
g_assert (chain);
|
g_assert (chain);
|
||||||
priv->auth_chains = g_slist_append (priv->auth_chains, chain);
|
priv->auth_chains = g_slist_append (priv->auth_chains, chain);
|
||||||
|
|
||||||
@@ -2959,10 +2945,9 @@ enable_net_done_cb (NMAuthChain *chain,
|
|||||||
NMAuthCallResult result;
|
NMAuthCallResult result;
|
||||||
gboolean enable;
|
gboolean enable;
|
||||||
|
|
||||||
result = GPOINTER_TO_UINT (nm_auth_chain_get_data (chain, "result"));
|
|
||||||
enable = GPOINTER_TO_UINT (nm_auth_chain_get_data (chain, "enable"));
|
|
||||||
|
|
||||||
priv->auth_chains = g_slist_remove (priv->auth_chains, chain);
|
priv->auth_chains = g_slist_remove (priv->auth_chains, chain);
|
||||||
|
|
||||||
|
result = GPOINTER_TO_UINT (nm_auth_chain_get_data (chain, NM_AUTH_PERMISSION_ENABLE_DISABLE_NETWORK));
|
||||||
if (error) {
|
if (error) {
|
||||||
nm_log_dbg (LOGD_CORE, "Enable request failed: %s", error->message);
|
nm_log_dbg (LOGD_CORE, "Enable request failed: %s", error->message);
|
||||||
ret_error = g_error_new (NM_MANAGER_ERROR,
|
ret_error = g_error_new (NM_MANAGER_ERROR,
|
||||||
@@ -2978,6 +2963,8 @@ enable_net_done_cb (NMAuthChain *chain,
|
|||||||
dbus_g_method_return_error (context, ret_error);
|
dbus_g_method_return_error (context, ret_error);
|
||||||
g_error_free (ret_error);
|
g_error_free (ret_error);
|
||||||
} else {
|
} else {
|
||||||
|
/* Auth success */
|
||||||
|
enable = GPOINTER_TO_UINT (nm_auth_chain_get_data (chain, "enable"));
|
||||||
_internal_enable (self, enable);
|
_internal_enable (self, enable);
|
||||||
dbus_g_method_return (context);
|
dbus_g_method_return (context);
|
||||||
}
|
}
|
||||||
@@ -2985,17 +2972,6 @@ enable_net_done_cb (NMAuthChain *chain,
|
|||||||
nm_auth_chain_unref (chain);
|
nm_auth_chain_unref (chain);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
|
||||||
enable_net_call_done_cb (NMAuthChain *chain,
|
|
||||||
const char *permission,
|
|
||||||
GError *error,
|
|
||||||
NMAuthCallResult result,
|
|
||||||
gpointer user_data)
|
|
||||||
{
|
|
||||||
if (!error)
|
|
||||||
nm_auth_chain_set_data (chain, "result", GUINT_TO_POINTER (result), NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
impl_manager_enable (NMManager *self,
|
impl_manager_enable (NMManager *self,
|
||||||
gboolean enable,
|
gboolean enable,
|
||||||
@@ -3039,11 +3015,7 @@ impl_manager_enable (NMManager *self,
|
|||||||
if (!return_no_pk_error (priv->authority, "Permission", context))
|
if (!return_no_pk_error (priv->authority, "Permission", context))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
chain = nm_auth_chain_new (priv->authority,
|
chain = nm_auth_chain_new (priv->authority, context, enable_net_done_cb, self);
|
||||||
context,
|
|
||||||
enable_net_done_cb,
|
|
||||||
enable_net_call_done_cb,
|
|
||||||
self);
|
|
||||||
g_assert (chain);
|
g_assert (chain);
|
||||||
priv->auth_chains = g_slist_append (priv->auth_chains, chain);
|
priv->auth_chains = g_slist_append (priv->auth_chains, chain);
|
||||||
|
|
||||||
@@ -3060,6 +3032,23 @@ pk_authority_changed_cb (GObject *object, gpointer user_data)
|
|||||||
g_signal_emit (NM_MANAGER (user_data), signals[CHECK_PERMISSIONS], 0);
|
g_signal_emit (NM_MANAGER (user_data), signals[CHECK_PERMISSIONS], 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
get_perm_add_result (NMAuthChain *chain, GHashTable *results, const char *permission)
|
||||||
|
{
|
||||||
|
NMAuthCallResult result;
|
||||||
|
|
||||||
|
result = GPOINTER_TO_UINT (nm_auth_chain_get_data (chain, permission));
|
||||||
|
if (result == NM_AUTH_CALL_RESULT_YES)
|
||||||
|
g_hash_table_insert (results, (char *) permission, "yes");
|
||||||
|
else if (result == NM_AUTH_CALL_RESULT_NO)
|
||||||
|
g_hash_table_insert (results, (char *) permission, "no");
|
||||||
|
else if (result == NM_AUTH_CALL_RESULT_AUTH)
|
||||||
|
g_hash_table_insert (results, (char *) permission, "auth");
|
||||||
|
else {
|
||||||
|
nm_log_dbg (LOGD_CORE, "unknown auth chain result %d", result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
get_permissions_done_cb (NMAuthChain *chain,
|
get_permissions_done_cb (NMAuthChain *chain,
|
||||||
GError *error,
|
GError *error,
|
||||||
@@ -3069,6 +3058,7 @@ get_permissions_done_cb (NMAuthChain *chain,
|
|||||||
NMManager *self = NM_MANAGER (user_data);
|
NMManager *self = NM_MANAGER (user_data);
|
||||||
NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
|
NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
|
||||||
GError *ret_error;
|
GError *ret_error;
|
||||||
|
GHashTable *results;
|
||||||
|
|
||||||
priv->auth_chains = g_slist_remove (priv->auth_chains, chain);
|
priv->auth_chains = g_slist_remove (priv->auth_chains, chain);
|
||||||
if (error) {
|
if (error) {
|
||||||
@@ -3080,48 +3070,25 @@ get_permissions_done_cb (NMAuthChain *chain,
|
|||||||
dbus_g_method_return_error (context, ret_error);
|
dbus_g_method_return_error (context, ret_error);
|
||||||
g_error_free (ret_error);
|
g_error_free (ret_error);
|
||||||
} else {
|
} else {
|
||||||
dbus_g_method_return (context, nm_auth_chain_get_data (chain, "results"));
|
results = g_hash_table_new (g_str_hash, g_str_equal);
|
||||||
|
get_perm_add_result (chain, results, NM_AUTH_PERMISSION_ENABLE_DISABLE_NETWORK);
|
||||||
|
get_perm_add_result (chain, results, NM_AUTH_PERMISSION_SLEEP_WAKE);
|
||||||
|
get_perm_add_result (chain, results, NM_AUTH_PERMISSION_ENABLE_DISABLE_WIFI);
|
||||||
|
get_perm_add_result (chain, results, NM_AUTH_PERMISSION_ENABLE_DISABLE_WWAN);
|
||||||
|
get_perm_add_result (chain, results, NM_AUTH_PERMISSION_USE_USER_CONNECTIONS);
|
||||||
|
dbus_g_method_return (context, results);
|
||||||
|
g_hash_table_destroy (results);
|
||||||
}
|
}
|
||||||
|
|
||||||
nm_auth_chain_unref (chain);
|
nm_auth_chain_unref (chain);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
|
||||||
get_permissions_call_done_cb (NMAuthChain *chain,
|
|
||||||
const char *permission,
|
|
||||||
GError *error,
|
|
||||||
guint result,
|
|
||||||
gpointer user_data)
|
|
||||||
{
|
|
||||||
GHashTable *hash;
|
|
||||||
const char *str_result = NULL;
|
|
||||||
|
|
||||||
if (error)
|
|
||||||
return;
|
|
||||||
|
|
||||||
hash = nm_auth_chain_get_data (chain, "results");
|
|
||||||
|
|
||||||
if (result == NM_AUTH_CALL_RESULT_YES)
|
|
||||||
str_result = "yes";
|
|
||||||
else if (result == NM_AUTH_CALL_RESULT_NO)
|
|
||||||
str_result = "no";
|
|
||||||
else if (result == NM_AUTH_CALL_RESULT_AUTH)
|
|
||||||
str_result = "auth";
|
|
||||||
else {
|
|
||||||
nm_log_dbg (LOGD_CORE, "unknown auth chain result %d", result);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (str_result)
|
|
||||||
g_hash_table_insert (hash, g_strdup (permission), g_strdup (str_result));
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
impl_manager_get_permissions (NMManager *self,
|
impl_manager_get_permissions (NMManager *self,
|
||||||
DBusGMethodInvocation *context)
|
DBusGMethodInvocation *context)
|
||||||
{
|
{
|
||||||
NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
|
NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
|
||||||
NMAuthChain *chain;
|
NMAuthChain *chain;
|
||||||
GHashTable *results;
|
|
||||||
|
|
||||||
if (!priv->authority) {
|
if (!priv->authority) {
|
||||||
GError *error;
|
GError *error;
|
||||||
@@ -3134,17 +3101,10 @@ impl_manager_get_permissions (NMManager *self,
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
chain = nm_auth_chain_new (priv->authority,
|
chain = nm_auth_chain_new (priv->authority, context, get_permissions_done_cb, self);
|
||||||
context,
|
|
||||||
get_permissions_done_cb,
|
|
||||||
get_permissions_call_done_cb,
|
|
||||||
self);
|
|
||||||
g_assert (chain);
|
g_assert (chain);
|
||||||
priv->auth_chains = g_slist_append (priv->auth_chains, chain);
|
priv->auth_chains = g_slist_append (priv->auth_chains, chain);
|
||||||
|
|
||||||
results = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
|
|
||||||
nm_auth_chain_set_data (chain, "results", results, (GDestroyNotify) g_hash_table_destroy);
|
|
||||||
|
|
||||||
nm_auth_chain_add_call (chain, NM_AUTH_PERMISSION_ENABLE_DISABLE_NETWORK, FALSE);
|
nm_auth_chain_add_call (chain, NM_AUTH_PERMISSION_ENABLE_DISABLE_NETWORK, FALSE);
|
||||||
nm_auth_chain_add_call (chain, NM_AUTH_PERMISSION_SLEEP_WAKE, FALSE);
|
nm_auth_chain_add_call (chain, NM_AUTH_PERMISSION_SLEEP_WAKE, FALSE);
|
||||||
nm_auth_chain_add_call (chain, NM_AUTH_PERMISSION_ENABLE_DISABLE_WIFI, FALSE);
|
nm_auth_chain_add_call (chain, NM_AUTH_PERMISSION_ENABLE_DISABLE_WIFI, FALSE);
|
||||||
|
Reference in New Issue
Block a user