[glom] Limit user and group name lengths.



commit 739a0452a8a9efa2b30d4c5919dce65e561b2dab
Author: Murray Cumming <murrayc murrayc com>
Date:   Wed Feb 1 15:15:18 2012 +0100

    Limit user and group name lengths.
    
    * glom/libglom/privs.[h|cc]: Add a MAX_ROLE_SIZE enum constants.
    I cannot find any PostgreSQL documentation of this 63 character limit.
    * glom/mode_design/users/dialog_new_group.cc:
    * glom/mode_design/users/dialog_user.cc: Use it to set maximum characters
    for the entry boxes.
    * tests/test_selfhosting_new_empty_then_users.cc: Show that the problem
    found so far was caused by too-long names, not spaces.
    I still need to check parsing of table permissions.

 ChangeLog                                      |   13 +++++++++++++
 glom/libglom/privs.cc                          |    2 ++
 glom/libglom/privs.h                           |   10 ++++++++++
 glom/mode_design/users/dialog_new_group.cc     |    1 +
 glom/mode_design/users/dialog_user.cc          |    4 ++++
 tests/test_selfhosting_new_empty_then_users.cc |   18 ++++++++++++++----
 6 files changed, 44 insertions(+), 4 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 6a7af5c..8889a9a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,18 @@
 2012-02-01  Murray Cumming  <murrayc murrayc com>
 
+	Limit user and group name lengths.
+
+	* glom/libglom/privs.[h|cc]: Add a MAX_ROLE_SIZE enum constants.
+	I cannot find any PostgreSQL documentation of this 63 character limit.
+	* glom/mode_design/users/dialog_new_group.cc:
+	* glom/mode_design/users/dialog_user.cc: Use it to set maximum characters
+	for the entry boxes.
+	* tests/test_selfhosting_new_empty_then_users.cc: Show that the problem
+	found so far was caused by too-long names, not spaces.
+	I still need to check parsing of table permissions.
+
+2012-02-01  Murray Cumming  <murrayc murrayc com>
+
 	test_selfhosting_new_empty_then_users: Test user adding with all group names.
 
 	* tests/test_selfhosting_new_empty_then_users.cc: Use for loops to 
diff --git a/glom/libglom/privs.cc b/glom/libglom/privs.cc
index c93f147..6d65171 100644
--- a/glom/libglom/privs.cc
+++ b/glom/libglom/privs.cc
@@ -133,6 +133,7 @@ Privs::type_vec_strings Privs::get_database_users(const Glib::ustring& group_nam
       builder->add_cond(Gnome::Gda::SQL_OPERATOR_TYPE_EQ,
         builder->add_field_id("groname", "pg_group"),
         builder->add_expr(group_name)));
+    //TODO: Show SQL.
     Glib::RefPtr<Gnome::Gda::DataModel> data_model = DbUtils::query_execute_select(builder);
     if(data_model && data_model->get_n_rows())
     {
@@ -162,6 +163,7 @@ Privs::type_vec_strings Privs::get_database_users(const Glib::ustring& group_nam
           if(data_model && data_model->get_n_rows() && data_model->get_n_columns())
           {
             const Gnome::Gda::Value value = data_model->get_value_at(0, 0);
+            //std::cout << G_STRFUNC << "DEBUG:  username=" << value.get_string() << std::endl; 
             result.push_back(value.get_string());
           }
           else
diff --git a/glom/libglom/privs.h b/glom/libglom/privs.h
index 33a256f..38d97d7 100644
--- a/glom/libglom/privs.h
+++ b/glom/libglom/privs.h
@@ -32,6 +32,16 @@ class Privs : public GlomPostgres
 {
 public:
 
+  /** This is apparently undocumented in PostgreSQL,
+   * but if we try to create a user or group
+   * with more characters (or bytes?) than this then
+   * a truncated version of it will be read back.
+   */
+  enum constant
+  {
+     MAX_ROLE_SIZE = 63
+  };
+
   /** Get the groups with access to the database.
    */
   static type_vec_strings get_database_groups();
diff --git a/glom/mode_design/users/dialog_new_group.cc b/glom/mode_design/users/dialog_new_group.cc
index 497ff93..664646a 100644
--- a/glom/mode_design/users/dialog_new_group.cc
+++ b/glom/mode_design/users/dialog_new_group.cc
@@ -31,6 +31,7 @@ Dialog_NewGroup::Dialog_NewGroup(BaseObjectType* cobject, const Glib::RefPtr<Gtk
   m_entry_name(0)
 {
   builder->get_widget("entry_group_name", m_entry_name);
+  m_entry_name.set_max_length(Privs::MAX_ROLE_SIZE);
 
   //m_entry_name->signal_changed().connect( sigc::mem_fun(*this, &Dialog_NewGroup::on_entry_name_changed) );
 }
diff --git a/glom/mode_design/users/dialog_user.cc b/glom/mode_design/users/dialog_user.cc
index e7978d8..52674d9 100644
--- a/glom/mode_design/users/dialog_user.cc
+++ b/glom/mode_design/users/dialog_user.cc
@@ -35,9 +35,13 @@ Dialog_User::Dialog_User(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builde
   //builder->get_widget("label_table_name", m_label_table_name);
 
   builder->get_widget("entry_user", m_entry_user);
+  m_entry_name.set_max_length(Privs::MAX_ROLE_SIZE);
   builder->get_widget_derived("combobox_group", m_combo_group);
+
   builder->get_widget("entry_password", m_entry_password);
+  m_entry_password.set_max_length(Privs::MAX_ROLE_SIZE); //Let's assume that this has a similar (undocumented in PostgreSQL) max size as the user.
   builder->get_widget("entry_password_confirm", m_entry_password_confirm);
+  m_entry_password_confirm.set_max_length(Privs::MAX_ROLE_SIZE);
 
   show_all_children();
 }
diff --git a/tests/test_selfhosting_new_empty_then_users.cc b/tests/test_selfhosting_new_empty_then_users.cc
index 6fdd80d..a7d126e 100644
--- a/tests/test_selfhosting_new_empty_then_users.cc
+++ b/tests/test_selfhosting_new_empty_then_users.cc
@@ -99,8 +99,9 @@ static bool test(Glom::Document::HostingMode hosting_mode)
   typedef std::vector<Glib::ustring> type_vec_strings;
   type_vec_strings table_names;
   table_names.push_back("sometable1");
-  table_names.push_back("sometable with space characters1");
+  table_names.push_back("sometable with space characters");
   table_names.push_back("sometable with a \" quote character");
+  table_names.push_back("sometablewithaverylongnameyaddayaddayaddayaddayaddyaddayaddayaddayaddayaddayaddayaddayaddayaddayaddayaddayadda");
 
   //Add some tables, for the groups to have rights for:
   for(type_vec_strings::const_iterator iter = table_names.begin(); iter != table_names.end(); ++iter)
@@ -131,8 +132,11 @@ static bool test(Glom::Document::HostingMode hosting_mode)
   //Add groups:
   type_vec_strings group_names;
   group_names.push_back("somegroup1");
-  group_names.push_back("somegroup with space characters1");
+  group_names.push_back("somegroup with space characters");
   group_names.push_back("somegroup with a \" quote character");
+  group_names.push_back("somegroupwithaverylongnameyaddayaddayaddayaddayaddyaddayaddayad"); //Almost too big.
+  //We expect this to fail because of an apparently-undocumented max pg_user size of 63 characters in PostgreSQL:
+  //group_names.push_back("somegroupwithaverylongnameyaddayaddayaddayaddayaddyaddayaddayadd");
 
   //Add groups:
   for(type_vec_strings::const_iterator iter = group_names.begin(); iter != group_names.end(); ++iter)
@@ -147,15 +151,21 @@ static bool test(Glom::Document::HostingMode hosting_mode)
   //TODO: Test strange passwords.
   type_vec_strings user_names;
   user_names.push_back("someuser1");
-  user_names.push_back("someuser with space characters1");
+  user_names.push_back("someuser with space characters");
   user_names.push_back("someuser with a \" quote character");
+  user_names.push_back("someuserwithaverylongnameyaddayaddayaddayaddayaddyaddayadda"); //Almost too big, with space for the numeric suffix below.
+  //We expect this to fail because of an apparently-undocumented max pg_user size of 63 characters in PostgreSQL:
+  //user_names.push_back("someuserwithaverylongnameyaddayaddayaddayaddayaddyaddayaddayadd");
+
+  guint i = 0;
   for(type_vec_strings::const_iterator iter_user = user_names.begin(); iter_user != user_names.end(); ++iter_user)
   {
     for(type_vec_strings::const_iterator iter_group = group_names.begin(); iter_group != group_names.end(); ++iter_group)
     {
-      const Glib::ustring username = *iter_user + "for_" + *iter_group; //Make sure the username is unique.
+      const Glib::ustring username = Glib::ustring::compose("%1%2", *iter_user, i); //Make sure the username is unique.
       if(!test_add_user(document, username, *iter_group))
         return false;
+      ++i;
     }
   }
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]