[gnome-system-tools] Move convenience users/groups functions to liboobs
- From: Milan Bouchet-Valat <milanbv src gnome org>
- To: svn-commits-list gnome org
- Cc:
- Subject: [gnome-system-tools] Move convenience users/groups functions to liboobs
- Date: Sat, 5 Dec 2009 18:30:04 +0000 (UTC)
commit 4694a92f0805d75d308bf7e04bf62da9c3d0bc69
Author: Milan Bouchet-Valat <nalimilan club fr>
Date: Sat Dec 5 13:54:43 2009 +0100
Move convenience users/groups functions to liboobs
Remove user_settings_get_user_from_name(), group_settings_get_group_from_name(), is_login_used(), uid_exists(), group_exists(), user_settings_find_new_uid(), group_settings_find_new_gid(), user_settings_is_user_in_group(), is_user_root() and is_group_root(). Replace them with their new and improved equivalents in liboobs.
src/users/group-settings.c | 109 ++++---------------------
src/users/user-profiles.c | 16 +++-
src/users/user-settings.c | 194 +++++++++++---------------------------------
3 files changed, 73 insertions(+), 246 deletions(-)
---
diff --git a/src/users/group-settings.c b/src/users/group-settings.c
index 5cae1c8..2dfdb24 100644
--- a/src/users/group-settings.c
+++ b/src/users/group-settings.c
@@ -119,47 +119,16 @@ group_delete (GtkTreeModel *model, GtkTreePath *path)
}
-gid_t
-group_settings_find_new_gid (void)
-{
- OobsGroupsConfig *config;
- OobsList *list;
- OobsListIter list_iter;
- GObject *group;
- gboolean valid;
- gid_t new_gid, gid, gid_min, gid_max;
-
- config = OOBS_GROUPS_CONFIG (GST_USERS_TOOL (tool)->groups_config);
- list = oobs_groups_config_get_groups (config);
- valid = oobs_list_get_iter_first (list, &list_iter);
-
- gid_min = GST_USERS_TOOL (tool)->minimum_gid;
- gid_max = GST_USERS_TOOL (tool)->maximum_gid;
-
- new_gid = gid_min - 1;
-
- while (valid) {
- group = oobs_list_get (list, &list_iter);
- gid = oobs_group_get_gid (OOBS_GROUP (group));
- g_object_unref (group);
-
- if (gid <= gid_max && gid >= gid_min && new_gid < gid)
- new_gid = gid;
-
- valid = oobs_list_iter_next (list, &list_iter);
- }
-
- new_gid++;
-
- return new_gid;
-}
-
GtkWidget*
group_settings_dialog_new (OobsGroup *group)
{
+ OobsGroupsConfig *config;
GtkWidget *dialog, *widget;
const gchar *name;
gchar *title;
+ gid_t gid;
+
+ config = OOBS_GROUPS_CONFIG (GST_USERS_TOOL (tool)->groups_config);
dialog = gst_dialog_get_widget (tool->main_dialog, "group_settings_dialog");
name = oobs_group_get_name (group);
@@ -173,8 +142,7 @@ group_settings_dialog_new (OobsGroup *group)
gtk_window_set_title (GTK_WINDOW (dialog), _("New group"));
widget = gst_dialog_get_widget (tool->main_dialog, "group_settings_gid");
- gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget),
- group_settings_find_new_gid ());
+ gid = oobs_groups_config_find_free_gid (config, 0, 0);
} else {
g_object_set_data (G_OBJECT (dialog), "is_new", GINT_TO_POINTER (FALSE));
@@ -183,9 +151,11 @@ group_settings_dialog_new (OobsGroup *group)
g_free (title);
widget = gst_dialog_get_widget (tool->main_dialog, "group_settings_gid");
- gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), oobs_group_get_gid (group));
+ gid = oobs_group_get_gid (group);
}
+ gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), gid);
+
widget = gst_dialog_get_widget (tool->main_dialog, "group_settings_name");
gtk_entry_set_text (GTK_ENTRY (widget), (name) ? name : "");
gtk_widget_set_sensitive (widget, (name == NULL));
@@ -207,59 +177,6 @@ group_settings_dialog_group_is_new (void)
return is_new;
}
-static gboolean
-is_group_root (OobsGroup *group)
-{
- const gchar *name = oobs_group_get_name (group);
-
- if (!name)
- return FALSE;
-
- return (strcmp (name, "root") == 0);
-}
-
-/* Get the OobsGroup corresponding to a name, or NULL if it does not exist.
- * Don't forget to unref the group when valid. */
-OobsGroup *
-group_settings_get_group_from_name (const gchar *name)
-{
- OobsGroupsConfig *config;
- OobsList *groups_list;
- OobsListIter iter;
- OobsGroup *group;
- gboolean valid;
- const gchar *group_name;
-
- config = OOBS_GROUPS_CONFIG (GST_USERS_TOOL (tool)->groups_config);
- groups_list = oobs_groups_config_get_groups (config);
- valid = oobs_list_get_iter_first (groups_list, &iter);
-
- while (valid) {
- group = OOBS_GROUP (oobs_list_get (groups_list, &iter));
- group_name = oobs_group_get_name (group);
-
- if (group_name && strcmp (name, group_name) == 0)
- return group;
-
- valid = oobs_list_iter_next (groups_list, &iter);
- }
-
- return NULL;
-}
-
-gboolean
-group_settings_group_exists (const gchar *name)
-{
- OobsGroup *group = group_settings_get_group_from_name (name);
-
- if (group) {
- g_object_unref (group);
- return TRUE;
- }
- else
- return FALSE;
-}
-
/* FIXME: this function is duplicated in user-settings.c */
static gboolean
is_valid_name (const gchar *name)
@@ -284,17 +201,20 @@ is_valid_name (const gchar *name)
static void
check_name (gchar **primary_text, gchar **secondary_text, gpointer data)
{
+ OobsGroupsConfig *config;
OobsGroup *group = OOBS_GROUP (data);
GtkWidget *widget;
const gchar *name;
+ config = OOBS_GROUPS_CONFIG (GST_USERS_TOOL (tool)->groups_config);
+
widget = gst_dialog_get_widget (tool->main_dialog, "group_settings_name");
name = gtk_entry_get_text (GTK_ENTRY (widget));
if (strlen (name) < 1) {
*primary_text = g_strdup (_("Group name is empty"));
*secondary_text = g_strdup (_("A group name must be specified."));
- } else if (is_group_root (group) && strcmp (name, "root") != 0) {
+ } else if (oobs_group_is_root (group) && strcmp (name, "root") != 0) {
*primary_text = g_strdup (_("Group name of the administrator group user should not be modified"));
*secondary_text = g_strdup (_("This would leave the system unusable."));
} else if (!is_valid_name (name)) {
@@ -302,7 +222,8 @@ check_name (gchar **primary_text, gchar **secondary_text, gpointer data)
*secondary_text = g_strdup (_("Please set a valid group name consisting of "
"a lower case letter followed by lower case "
"letters and numbers."));
- } else if (group_settings_dialog_group_is_new () && group_settings_group_exists (name)) {
+ } else if (group_settings_dialog_group_is_new ()
+ && oobs_groups_config_is_name_used (config, name)) {
*primary_text = g_strdup_printf (_("Group \"%s\" already exists"), name);
*secondary_text = g_strdup (_("Please choose a different group name."));
}
@@ -318,7 +239,7 @@ check_gid (gchar **primary_text, gchar **secondary_text, gpointer data)
widget = gst_dialog_get_widget (tool->main_dialog, "group_settings_gid");
gid = gtk_spin_button_get_value (GTK_SPIN_BUTTON (widget));
- if (is_group_root (group) && gid != 0) {
+ if (oobs_group_is_root (group) && gid != 0) {
*primary_text = g_strdup (_("Group ID of the Administrator account should not be modified"));
*secondary_text = g_strdup (_("This would leave the system unusable."));
}
diff --git a/src/users/user-profiles.c b/src/users/user-profiles.c
index 594b83c..c5b0e3e 100644
--- a/src/users/user-profiles.c
+++ b/src/users/user-profiles.c
@@ -26,6 +26,8 @@
#include "user-settings.h"
#include "group-settings.h"
+extern GstTool *tool;
+
#define PROFILES_FILE "/etc/gnome-system-tools/users/profiles"
#define GST_USER_PROFILES_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GST_TYPE_USER_PROFILES, GstUserProfilesPrivate))
@@ -240,6 +242,7 @@ gst_user_profiles_get_for_user (GstUserProfiles *profiles,
GstUserProfilesPrivate *priv;
GstUserProfile *profile;
GstUserProfile *matched;
+ OobsGroupsConfig *groups_config;
GFile *file_home, *file_prefix;
const gchar *shell, *home;
gint uid;
@@ -256,6 +259,7 @@ gst_user_profiles_get_for_user (GstUserProfiles *profiles,
uid = oobs_user_get_uid (user);
priv = GST_USER_PROFILES_GET_PRIVATE (profiles);
+ groups_config = OOBS_GROUPS_CONFIG (GST_USERS_TOOL (tool)->groups_config);
matched = NULL;
for (l = priv->profiles; l; l = l->next) {
@@ -300,8 +304,8 @@ gst_user_profiles_get_for_user (GstUserProfiles *profiles,
group_name++;
}
- group = group_settings_get_group_from_name ((char *) m->data);
- in_group = user_settings_is_user_in_group (user, group);
+ group = oobs_groups_config_get_from_name (groups_config, (char *) m->data);
+ in_group = oobs_user_is_in_group (user, group);
if ((in_profile && !in_group) || (!in_profile && in_group)) {
if (group)
g_object_unref (group);
@@ -335,6 +339,8 @@ gst_user_profiles_apply (GstUserProfiles *profiles,
gboolean new_user)
{
GstUserProfilesPrivate *priv;
+ OobsUsersConfig *users_config;
+ OobsGroupsConfig *groups_config;
gint uid;
char *home;
char **group_name;
@@ -349,6 +355,7 @@ gst_user_profiles_apply (GstUserProfiles *profiles,
g_return_if_fail (oobs_user_get_login_name (user) != NULL);
priv = GST_USER_PROFILES_GET_PRIVATE (profiles);
+ groups_config = OOBS_GROUPS_CONFIG (GST_USERS_TOOL (tool)->groups_config);
/* default shell */
oobs_user_set_shell (user, profile->shell);
@@ -364,7 +371,7 @@ gst_user_profiles_apply (GstUserProfiles *profiles,
group_name++;
}
- group = group_settings_get_group_from_name ((char *) l->data);
+ group = oobs_groups_config_get_from_name (groups_config, (char *) l->data);
if (in_profile)
oobs_group_add_user (group, user);
else
@@ -375,7 +382,8 @@ gst_user_profiles_apply (GstUserProfiles *profiles,
return;
/* default UID */
- uid = user_settings_find_new_uid (profile->uid_min, profile->uid_max);
+ users_config = OOBS_USERS_CONFIG (GST_USERS_TOOL (tool)->users_config);
+ uid = oobs_users_config_find_free_uid (users_config, profile->uid_min, profile->uid_max);
oobs_user_set_uid (user, uid);
/* default home prefix */
diff --git a/src/users/user-settings.c b/src/users/user-settings.c
index 162e52a..33cde21 100644
--- a/src/users/user-settings.c
+++ b/src/users/user-settings.c
@@ -246,16 +246,18 @@ set_main_group (OobsUser *user)
model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo));
if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combo), &iter)) {
- group = group_settings_get_group_from_name (name);
+ config = OOBS_GROUPS_CONFIG (GST_USERS_TOOL (tool)->groups_config);
+ group = oobs_groups_config_get_from_name (config, name);
+
/* Most standard case when creating user:
* no group using the login already exists, so create it */
if (group == NULL) {
is_group_new = TRUE;
group = oobs_group_new (name);
- oobs_group_set_gid (group, group_settings_find_new_gid ());
+ oobs_group_set_gid (group,
+ oobs_groups_config_find_free_gid (config, 0, 0));
/* FIXME: this should be in a generic function */
- config = OOBS_GROUPS_CONFIG (GST_USERS_TOOL (tool)->groups_config);
groups_list = oobs_groups_config_get_groups (config);
oobs_list_append (groups_list, &list_iter);
oobs_list_set (groups_list, &list_iter, group);
@@ -316,104 +318,6 @@ get_no_passwd_login_group ()
return NULL;
}
-gboolean
-user_settings_is_user_in_group (OobsUser *user,
- OobsGroup *group)
-{
- OobsUser *tmp_user;
- GList *users = NULL;
- GList *l;
-
- if (!user || !group)
- return FALSE;
-
- users = oobs_group_get_users (group);
- for (l = users; l; l = l->next) {
- tmp_user = l->data;
- if (tmp_user == user)
- break;
- }
- g_list_free (users);
-
- return l != NULL;
-}
-
-static gboolean
-is_user_root (OobsUser *user)
-{
- const gchar *login;
-
- if (!user)
- return FALSE;
-
- login = oobs_user_get_login_name (user);
-
- if (!login)
- return FALSE;
-
- return (strcmp (login, "root") == 0);
-}
-
-static gboolean
-uid_exists (uid_t uid)
-{
- OobsUsersConfig *config;
- OobsList *list;
- OobsListIter list_iter;
- GObject *user;
- gboolean valid;
- uid_t user_uid;
-
- config = OOBS_USERS_CONFIG (GST_USERS_TOOL (tool)->users_config);
- list = oobs_users_config_get_users (config);
- valid = oobs_list_get_iter_first (list, &list_iter);
-
- while (valid) {
- user = oobs_list_get (list, &list_iter);
- user_uid = oobs_user_get_uid (OOBS_USER (user));
- g_object_unref (user);
-
- if (user_uid == uid)
- return TRUE;
-
- valid = oobs_list_iter_next (list, &list_iter);
- }
-
- return FALSE;
-}
-
-uid_t
-user_settings_find_new_uid (gint uid_min,
- gint uid_max)
-{
- OobsUsersConfig *config;
- OobsList *list;
- OobsListIter list_iter;
- GObject *user;
- gboolean valid;
- uid_t new_uid, uid;
-
- config = OOBS_USERS_CONFIG (GST_USERS_TOOL (tool)->users_config);
- list = oobs_users_config_get_users (config);
- valid = oobs_list_get_iter_first (list, &list_iter);
- new_uid = uid_min - 1;
-
- while (valid) {
- user = oobs_list_get (list, &list_iter);
- uid = oobs_user_get_uid (OOBS_USER (user));
- g_object_unref (user);
-
- if (uid <= uid_max && uid >= uid_min && new_uid < uid)
- new_uid = uid;
-
- valid = oobs_list_iter_next (list, &list_iter);
- }
-
- new_uid++;
-
- return new_uid;
-}
-
static void
set_login_length (GtkWidget *entry)
{
@@ -471,8 +375,9 @@ user_settings_set (OobsUser *user)
oobs_users_config_get_default_shell (config));
widget = gst_dialog_get_widget (tool->main_dialog, "user_settings_uid");
- uid = user_settings_find_new_uid (GST_USERS_TOOL (tool)->minimum_uid,
- GST_USERS_TOOL (tool)->maximum_uid);
+ uid = oobs_users_config_find_free_uid (config,
+ GST_USERS_TOOL (tool)->minimum_uid,
+ GST_USERS_TOOL (tool)->maximum_uid);
gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), uid);
gst_dialog_try_set_sensitive (tool->main_dialog, widget, TRUE);
gtk_widget_hide (notice);
@@ -486,7 +391,7 @@ user_settings_set (OobsUser *user)
gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), oobs_user_get_uid (user));
/* Show a notice if the user is logged in,
* except if we don't have the required permissions to edit UID anyway */
- if (is_user_root (user)) {
+ if (oobs_user_is_root (user)) {
gst_dialog_try_set_sensitive (tool->main_dialog, widget, FALSE);
gtk_widget_hide (notice);
}
@@ -531,7 +436,7 @@ user_settings_set (OobsUser *user)
widget2 = gst_dialog_get_widget (tool->main_dialog, "edit_user_profile_button");
profile = gst_user_profiles_get_for_user (GST_USERS_TOOL (tool)->profiles,
user, FALSE);
- if (is_user_root (user)) {
+ if (oobs_user_is_root (user)) {
gtk_widget_set_sensitive (widget2, FALSE);
gtk_label_set_text (GTK_LABEL (widget), _("Superuser"));
}
@@ -546,34 +451,6 @@ user_settings_set (OobsUser *user)
}
}
-static gboolean
-login_exists (const gchar *login)
-{
- OobsUsersConfig *config;
- OobsList *users_list;
- OobsListIter iter;
- GObject *user;
- gboolean valid;
- const gchar *user_login;
-
- config = OOBS_USERS_CONFIG (GST_USERS_TOOL (tool)->users_config);
- users_list = oobs_users_config_get_users (config);
- valid = oobs_list_get_iter_first (users_list, &iter);
-
- while (valid) {
- user = oobs_list_get (users_list, &iter);
- user_login = oobs_user_get_login_name (OOBS_USER (user));
- g_object_unref (user);
-
- if (user_login && strcmp (login, user_login) == 0)
- return TRUE;
-
- valid = oobs_list_iter_next (users_list, &iter);
- }
-
- return FALSE;
-}
-
static void
check_comments (gchar **primary_text, gchar **secondary_text, gpointer data)
{
@@ -626,23 +503,33 @@ check_home (gchar **primary_text, gchar **secondary_text, gpointer data)
static void
check_uid (gchar **primary_text, gchar **secondary_text, gpointer data)
{
+ OobsUsersConfig *config;
OobsUser *user;
+ OobsUser *uid_user;
GtkWidget *widget;
gint uid;
widget = gst_dialog_get_widget (tool->main_dialog, "user_settings_uid");
uid = gtk_spin_button_get_value (GTK_SPIN_BUTTON (widget));
- user = g_object_get_data (G_OBJECT (data), "user");
+ user = users_table_get_current ();
- if (is_user_root (user) && uid != 0) {
+ if (oobs_user_is_root (user) && uid != 0) {
*primary_text = g_strdup (_("Administrator account's user ID should not be modified"));
*secondary_text = g_strdup (_("This would leave the system unusable."));
}
- if (!user && uid_exists (uid)) {
- *primary_text = g_strdup_printf (_("User ID %d is already used"), uid);
- *secondary_text = g_strdup (_("Please choose a different numeric identifier for the new user."));
+ config = OOBS_USERS_CONFIG (GST_USERS_TOOL (tool)->users_config);
+ uid_user = oobs_users_config_get_from_uid (config, uid);
+
+ if (uid_user != user) {
+ *primary_text = g_strdup_printf (_("User ID %d is already used by user %s"),
+ uid, oobs_user_get_login_name (uid_user));
+ *secondary_text = g_strdup_printf (_("Please choose a different numeric identifier for %s."),
+ oobs_user_get_login_name (user));
}
+
+ g_object_unref (user);
+ g_object_unref (uid_user);
}
static void
@@ -733,11 +620,13 @@ user_settings_dialog_run (GtkWidget *dialog)
void
on_user_new_name_changed (GtkEditable *user_name, gpointer user_data)
{
+ OobsUsersConfig *config;
GtkWidget *validate_button;
GtkWidget *user_login;
GtkTreeModel *model;
gboolean valid_login;
gboolean valid_name;
+ gboolean used_login;
const char *name;
char *lc_name, *ascii_name, *stripped_name;
char **words1;
@@ -884,27 +773,35 @@ on_user_new_name_changed (GtkEditable *user_name, gpointer user_data)
item3 = g_string_append (item3, first_word->str);
item4 = g_string_prepend (item4, last_word->str);
- if (nwords2 > 0 && !login_exists (item1->str) && !isdigit(item1->str[0]))
+ config = OOBS_USERS_CONFIG (GST_USERS_TOOL (tool)->users_config);
+
+ used_login = oobs_users_config_is_login_used (config, item1->str);
+ if (nwords2 > 0 && !used_login && !isdigit(item1->str[0]))
gtk_combo_box_append_text (GTK_COMBO_BOX (user_login), item1->str);
/* if there's only one word, would be the same as item1 */
if (nwords2 > 1) {
/* add other items */
- if (!login_exists (item2->str) && !isdigit(item2->str[0]))
+ used_login = oobs_users_config_is_login_used (config, item2->str);
+ if (!used_login && !isdigit(item2->str[0]))
gtk_combo_box_append_text (GTK_COMBO_BOX (user_login), item2->str);
- if (!login_exists (item3->str) && !isdigit(item3->str[0]))
+ used_login = oobs_users_config_is_login_used (config, item3->str);
+ if (!used_login && !isdigit(item3->str[0]))
gtk_combo_box_append_text (GTK_COMBO_BOX (user_login), item3->str);
- if (!login_exists (item4->str) && !isdigit(item4->str[0]))
+ used_login = oobs_users_config_is_login_used (config, item4->str);
+ if (!used_login && !isdigit(item4->str[0]))
gtk_combo_box_append_text (GTK_COMBO_BOX (user_login), item4->str);
/* add the last word */
- if (!login_exists (last_word->str) && !isdigit(last_word->str[0]))
+ used_login = oobs_users_config_is_login_used (config, last_word->str);
+ if (!used_login && !isdigit(last_word->str[0]))
gtk_combo_box_append_text (GTK_COMBO_BOX (user_login), last_word->str);
/* ...and the first one */
- if (!login_exists (first_word->str) && !isdigit(first_word->str[0]))
+ used_login = oobs_users_config_is_login_used (config, first_word->str);
+ if (!used_login && !isdigit(first_word->str[0]))
gtk_combo_box_append_text (GTK_COMBO_BOX (user_login), first_word->str);
}
@@ -949,7 +846,8 @@ on_user_new_login_changed (GtkComboBox *login_combo, gpointer user_data)
login_notice = gst_dialog_get_widget (tool->main_dialog, "user_new_login_notice");
letter_notice = gst_dialog_get_widget (tool->main_dialog, "user_new_login_letter_notice");
- used_login = login_exists (login);
+ used_login = oobs_users_config_is_login_used (OOBS_USERS_CONFIG (GST_USERS_TOOL (tool)->users_config),
+ login);
empty_login = (strlen (login) <= 0);
valid_login = TRUE;
@@ -1193,14 +1091,14 @@ on_edit_user_passwd (GtkButton *button, gpointer user_data)
no_passwd_login_group = get_no_passwd_login_group ();
/* root should not be allowed to login without password,
* and we disable the feature if the group does not exist */
- if (is_user_root (user) || no_passwd_login_group == NULL) {
+ if (oobs_user_is_root (user) || no_passwd_login_group == NULL) {
gtk_widget_set_sensitive (nocheck_toggle, FALSE);
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (nocheck_toggle),
FALSE);
}
else {
gst_dialog_try_set_sensitive (tool->main_dialog, nocheck_toggle, TRUE);
- if (user_settings_is_user_in_group (user, no_passwd_login_group))
+ if (oobs_user_is_in_group (user, no_passwd_login_group))
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (nocheck_toggle),
TRUE);
else
@@ -1239,7 +1137,7 @@ on_edit_user_passwd (GtkButton *button, gpointer user_data)
/* check whether user is allowed to login without password */
no_passwd_login_group = get_no_passwd_login_group ();
- if (!is_user_root (user) && no_passwd_login_group != NULL) {
+ if (!oobs_user_is_root (user) && no_passwd_login_group != NULL) {
if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (nocheck_toggle)))
oobs_group_add_user (no_passwd_login_group, user);
else
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]