[glom/mysql2: 2/13] Privs::get_default_developer_user_name(): Take hosting_mode parameters.



commit 833ac60b4b01ccc331fe28f60d8cd21f8b8241da
Author: Murray Cumming <murrayc murrayc com>
Date:   Thu Jan 3 18:48:20 2013 +0100

    Privs::get_default_developer_user_name(): Take hosting_mode parameters.
    
            * glom/libglom/privs.[h|cc]:
    	get_default_developer_user_name(): Take the hosting_mode as a parameter
    	so we can use a shortened username for MySQL without changing it
    	for PostgreSQL.
            * glom/appwindow.cc:
            * glom/frame_glom.cc:
            * glom/glom_create_from_example.cc:
            * tests/test_selfhosting_utils.cc: Adapted.

 ChangeLog                        |   13 +++++++++++++
 glom/appwindow.cc                |    5 +++--
 glom/frame_glom.cc               |   17 ++++++++++-------
 glom/glom_create_from_example.cc |    2 +-
 glom/libglom/privs.cc            |   17 ++++++++++++-----
 glom/libglom/privs.h             |    8 +++++---
 tests/test_selfhosting_utils.cc  |    2 +-
 7 files changed, 45 insertions(+), 19 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 1490430..7b523ab 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,18 @@
 2013-01-03  Murray Cumming  <murrayc murrayc com>
 
+	Privs::get_default_developer_user_name(): Take hosting_mode parameters.
+
+        * glom/libglom/privs.[h|cc]:
+	get_default_developer_user_name(): Take the hosting_mode as a parameter
+	so we can use a shortened username for MySQL without changing it
+	for PostgreSQL.
+        * glom/appwindow.cc:
+        * glom/frame_glom.cc:
+        * glom/glom_create_from_example.cc:
+        * tests/test_selfhosting_utils.cc: Adapted.
+
+2013-01-03  Murray Cumming  <murrayc murrayc com>
+
         tests: Avoid some copy/pasting.
 
 	* tests/test_selfhosting_utils.[h|cc]:
diff --git a/glom/appwindow.cc b/glom/appwindow.cc
index 35501d0..7cb312a 100644
--- a/glom/appwindow.cc
+++ b/glom/appwindow.cc
@@ -1156,8 +1156,9 @@ bool AppWindow::on_document_load()
           // If the document is centrally hosted, don't pretend to know the
           // username or password, because we don't. The user will enter
           // the login credentials in a dialog.
-          if(pDocument->get_hosting_mode() != Document::HOSTING_MODE_POSTGRES_CENTRAL)
-            m_temp_username = Privs::get_default_developer_user_name(m_temp_password);
+          const Document::HostingMode hosting_mode = pDocument->get_hosting_mode();
+          if(hosting_mode != Document::HOSTING_MODE_POSTGRES_CENTRAL)
+            m_temp_username = Privs::get_default_developer_user_name(m_temp_password, hosting_mode);
         }
 
         bool database_not_found = false;
diff --git a/glom/frame_glom.cc b/glom/frame_glom.cc
index a7ff651..e35f5d6 100644
--- a/glom/frame_glom.cc
+++ b/glom/frame_glom.cc
@@ -845,7 +845,7 @@ void Frame_Glom::on_menu_file_toggle_share(const Glib::RefPtr<Gtk::ToggleAction>
       shared = true;
 
       //Ask for a user/password if none is set:
-      const bool real_user_exists = Privs::get_developer_user_exists_with_password();
+      const bool real_user_exists = Privs::get_developer_user_exists_with_password(document->get_hosting_mode());
       if(!real_user_exists)
       {
         //Ask for an initial user:
@@ -888,7 +888,8 @@ void Frame_Glom::on_menu_file_toggle_share(const Glib::RefPtr<Gtk::ToggleAction>
         //Remove the default no-password user, because that would be a security hole:
         //We do this after adding/using the non-default user, because we can't
         //remove a currently-used user.
-        const bool default_user_exists = Privs::get_default_developer_user_exists();
+        const Document::HostingMode hosting_mode = document->get_hosting_mode();
+        const bool default_user_exists = Privs::get_default_developer_user_exists(hosting_mode);
         if(default_user_exists)
         {
           //Force a reconnection with the new password:
@@ -897,7 +898,7 @@ void Frame_Glom::on_menu_file_toggle_share(const Glib::RefPtr<Gtk::ToggleAction>
           //Remove it, after stopping it from being the database owner:
           bool disabled = true;
           Glib::ustring default_password;
-          const Glib::ustring default_user = Privs::get_default_developer_user_name(default_password);
+          const Glib::ustring default_user = Privs::get_default_developer_user_name(default_password, hosting_mode);
 
           ConnectionPool* connectionpool = ConnectionPool::get_instance();
           const bool reowned = set_database_owner_user(connectionpool->get_user());
@@ -950,12 +951,13 @@ void Frame_Glom::on_menu_file_toggle_share(const Glib::RefPtr<Gtk::ToggleAction>
       shared = false;
 
       //Make sure the default no-password user exists:
-      const bool default_user_exists = Privs::get_default_developer_user_exists();
+      const Document::HostingMode hosting_mode = document->get_hosting_mode();
+      const bool default_user_exists = Privs::get_default_developer_user_exists(hosting_mode);
       if(!default_user_exists)
       {
         //Add it:
         Glib::ustring default_password;
-        const Glib::ustring default_user = Privs::get_default_developer_user_name(default_password);
+        const Glib::ustring default_user = Privs::get_default_developer_user_name(default_password, hosting_mode);
 
         const bool added = DbUtils::add_user(document, default_user, default_password, GLOM_STANDARD_GROUP_NAME_DEVELOPER);
         if(!added)
@@ -1920,7 +1922,8 @@ bool Frame_Glom::connection_request_password_and_choose_new_database_name()
     add_view(m_pDialogConnection); //Also a composite view.
   }
 
-  switch(document->get_hosting_mode())
+  const Document::HostingMode hosting_mode = document->get_hosting_mode();
+  switch(hosting_mode)
   {
     case Document::HOSTING_MODE_POSTGRES_SELF:
     {
@@ -1935,7 +1938,7 @@ bool Frame_Glom::connection_request_password_and_choose_new_database_name()
       else
       {
         //Use the default user because we are not network shared:
-        user = Privs::get_default_developer_user_name(password);
+        user = Privs::get_default_developer_user_name(password, hosting_mode);
       }
 
       // Create the requested self-hosting database:
diff --git a/glom/glom_create_from_example.cc b/glom/glom_create_from_example.cc
index 2baa517..7cf67c6 100644
--- a/glom/glom_create_from_example.cc
+++ b/glom/glom_create_from_example.cc
@@ -410,7 +410,7 @@ int main(int argc, char* argv[])
   if(self_hosting)
   {
     Glib::ustring password;
-    const Glib::ustring user = Glom::Privs::get_default_developer_user_name(password);
+    const Glib::ustring user = Glom::Privs::get_default_developer_user_name(password, document.get_hosting_mode());
     connection_pool->set_user(user);
     connection_pool->set_password(password);
   }
diff --git a/glom/libglom/privs.cc b/glom/libglom/privs.cc
index 83a3edd..a41a76a 100644
--- a/glom/libglom/privs.cc
+++ b/glom/libglom/privs.cc
@@ -58,10 +58,10 @@ Privs::type_vec_strings Privs::get_database_groups()
   return result;
 }
 
-bool Privs::get_default_developer_user_exists()
+bool Privs::get_default_developer_user_exists(Document::HostingMode hosting_mode)
 {
   Glib::ustring default_password;
-  const Glib::ustring default_user = get_default_developer_user_name(default_password);
+  const Glib::ustring default_user = get_default_developer_user_name(default_password, hosting_mode);
 
   const type_vec_strings users = get_database_users();
   type_vec_strings::const_iterator iterFind = std::find(users.begin(), users.end(), default_user);
@@ -71,10 +71,10 @@ bool Privs::get_default_developer_user_exists()
   return false; //The default user is not there.
 }
 
-bool Privs::get_developer_user_exists_with_password()
+bool Privs::get_developer_user_exists_with_password(Document::HostingMode hosting_mode)
 {
   Glib::ustring default_password;
-  const Glib::ustring default_user = get_default_developer_user_name(default_password);
+  const Glib::ustring default_user = get_default_developer_user_name(default_password, hosting_mode);
 
   const type_vec_strings users = get_database_users();
   for(type_vec_strings::const_iterator iter = users.begin(); iter != users.end(); ++iter)
@@ -90,9 +90,16 @@ bool Privs::get_developer_user_exists_with_password()
   return false;
 }
 
-Glib::ustring Privs::get_default_developer_user_name(Glib::ustring& password)
+Glib::ustring Privs::get_default_developer_user_name(Glib::ustring& password, Document::HostingMode hosting_mode)
 {
   password = "glom_default_developer_password";
+
+  if((hosting_mode == Document::HOSTING_MODE_MYSQL_CENTRAL) ||
+   (hosting_mode == Document::HOSTING_MODE_MYSQL_SELF))
+  {
+    return "glom_dev_user"; //MySQL restricts user names to 16 characters.
+  }
+
   return "glom_default_developer_user";
 }
 
diff --git a/glom/libglom/privs.h b/glom/libglom/privs.h
index 1a7c774..8641732 100644
--- a/glom/libglom/privs.h
+++ b/glom/libglom/privs.h
@@ -57,18 +57,20 @@ public:
    * instead of just a default glom user and default password,
    * The user should be forced to choose a user/password when network sharing is active.
    */
-  static bool get_developer_user_exists_with_password();
+  static bool get_developer_user_exists_with_password(Document::HostingMode hosting_mode);
 
   /** Discover whether the default developer user exists (which has a known password).
    * The user should be forced to choose a user/password when network sharing is active,
    * and this default user should no longer exist in that case.
    */
-  static bool get_default_developer_user_exists();
+  static bool get_default_developer_user_exists(Document::HostingMode hosting_mode);
 
   /** Get the standard username and password used for the no-password user,
    * which should only be used when network sharing is not active.
+   *
+   * @param hosting_mode So we can use a shorter name for MySQL to avoid an error about the name being too long.
    */
-  static Glib::ustring get_default_developer_user_name(Glib::ustring& password);
+  static Glib::ustring get_default_developer_user_name(Glib::ustring& password, Document::HostingMode hosting_mode);
 
   static Privileges get_table_privileges(const Glib::ustring& group_name, const Glib::ustring& table_name);
   static bool set_table_privileges(const Glib::ustring& group_name, const Glib::ustring& table_name, const Privileges& privs, bool developer_privs = false);
diff --git a/tests/test_selfhosting_utils.cc b/tests/test_selfhosting_utils.cc
index 37c465a..ce550bb 100644
--- a/tests/test_selfhosting_utils.cc
+++ b/tests/test_selfhosting_utils.cc
@@ -212,7 +212,7 @@ bool test_create_and_selfhost_new_empty(Glom::Document& document, Glom::Document
 
   //We must specify a default username and password:
   Glib::ustring password;
-  const Glib::ustring user = Glom::Privs::get_default_developer_user_name(password);
+  const Glib::ustring user = Glom::Privs::get_default_developer_user_name(password, hosting_mode);
   connection_pool->set_user(user);
   connection_pool->set_password(password);
 



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