From f61ac3f4a0155ddc3420445a5ff959c906c3a743 Mon Sep 17 00:00:00 2001 From: Romain Quetiez Date: Thu, 28 Jan 2010 17:10:39 +0000 Subject: [PATCH] Fixed two bugs: - Trac#80 Misleading messages when using the back button during the setup (added back button in any error case) - Trac#72 Revised the setup to consider any installation scenario regarding Database and user privileges (user rights tracked into the log file) SVN:trunk[291] --- application/application.inc.php | 1 - core/cmdbsource.class.inc.php | 32 ++++++++++++++++-- core/metamodel.class.php | 55 +++++++++++++++++++++++-------- core/test.class.inc.php | 4 +-- pages/ITopConsultant.php | 4 +-- setup/index.php | 57 ++++++++++++++++++++++----------- 6 files changed, 114 insertions(+), 39 deletions(-) diff --git a/application/application.inc.php b/application/application.inc.php index d15464c19..f613ff1e6 100644 --- a/application/application.inc.php +++ b/application/application.inc.php @@ -4,7 +4,6 @@ require_once('../application/applicationcontext.class.inc.php'); require_once('../application/usercontext.class.inc.php'); require_once('../application/cmdbabstract.class.inc.php'); require_once('../application/displayblock.class.inc.php'); -require_once('../application/iotask.class.inc.php'); require_once('../application/audit.category.class.inc.php'); require_once('../application/audit.rule.class.inc.php'); //require_once('../application/menunode.class.inc.php'); diff --git a/core/cmdbsource.class.inc.php b/core/cmdbsource.class.inc.php index 3543349e6..8c0e72409 100644 --- a/core/cmdbsource.class.inc.php +++ b/core/cmdbsource.class.inc.php @@ -20,6 +20,7 @@ class MySQLException extends CoreException public function __construct($sIssue, $aContext) { $aContext['mysql_error'] = mysql_error(); + $aContext['mysql_errno'] = mysql_errno(); parent::__construct($sIssue, $aContext); } } @@ -41,13 +42,13 @@ class CMDBSource self::$m_sDBName = $sSource; if (!self::$m_resDBLink = @mysql_pconnect($sServer, $sUser, $sPwd)) { - throw new MySQLException('Could not connect to the DB server', array('host'=>$sServer)); + throw new MySQLException('Could not connect to the DB server', array('host'=>$sServer, 'user'=>$sUser)); } if (!empty($sSource)) { if (!mysql_select_db($sSource, self::$m_resDBLink)) { - throw new MySQLException('Could not select DB', array('db_name'=>$sSource)); + throw new MySQLException('Could not select DB', array('host'=>$sServer, 'user'=>$sUser, 'db_name'=>$sSource)); } } } @@ -464,6 +465,33 @@ class CMDBSource } return $result; } + + + /** + * Returns the privileges of the current user + * @return string privileges in a raw format + */ + public static function GetRawPrivileges() + { + try + { + $result = self::Query('SHOW GRANTS'); // [ FOR CURRENT_USER()] + } + catch(MySQLException $e) + { + return "Current user not allowed to see his own privileges (could not access to the database 'mysql' - $iCode)"; + } + + $aRes = array(); + while ($aRow = mysql_fetch_array($result, MYSQL_NUM)) + { + // so far, only one column... + $aRes[] = implode('/', $aRow); + } + mysql_free_result($result); + // so far, only one line... + return implode(', ', $aRes); + } } diff --git a/core/metamodel.class.php b/core/metamodel.class.php index 1725daaab..6e053ea56 100644 --- a/core/metamodel.class.php +++ b/core/metamodel.class.php @@ -1941,29 +1941,56 @@ abstract class MetaModel echo "\n"; } - public static function DBExists() + public static function DBExists($bMustBeComplete = true) { - // returns true if at least one table exists (taking into account the DB sharing) - // then some tables might be missing, but that is made in DBCheckFormat + // returns true if at least one table exists // - if (empty(self::$m_sTablePrefix)) - { - return CMDBSource::IsDB(self::$m_sDBName); - } - // DB sharing - // Check if there is at least one table with the prefix - // if (!CMDBSource::IsDB(self::$m_sDBName)) { return false; } CMDBSource::SelectDB(self::$m_sDBName); - // If a table matches the prefix, then consider that the database already exists - $sSQL = "SHOW TABLES LIKE '".strtolower(self::$m_sTablePrefix)."%' "; - $result = CMDBSource::Query($sSQL); - return (CMDBSource::NbRows($result) > 0); + $aFound = array(); + $aMissing = array(); + foreach (self::DBEnumTables() as $sTable => $aClasses) + { + if (CMDBSource::IsTable($sTable)) + { + $aFound[] = $sTable; + } + else + { + $aMissing[] = $sTable; + } + } + + if (count($aFound) == 0) + { + // no expected table has been found + return false; + } + else + { + if (count($aMissing) == 0) + { + // the database is complete (still, could be some fields missing!) + return true; + } + else + { + // not all the tables, could be an older version + if ($bMustBeComplete) + { + return false; + } + else + { + return true; + } + } + } } public static function DBDrop() diff --git a/core/test.class.inc.php b/core/test.class.inc.php index 87af7e165..1b1a6cdf0 100644 --- a/core/test.class.inc.php +++ b/core/test.class.inc.php @@ -461,7 +461,7 @@ abstract class TestBizModel extends TestHandler protected function ResetDB() { - if (MetaModel::DBExists()) + if (MetaModel::DBExists(false)) { MetaModel::DBDrop(); } @@ -527,7 +527,7 @@ abstract class TestBizModelGeneric extends TestBizModel { parent::DoPrepare(); - if (!MetaModel::DBExists()) + if (!MetaModel::DBExists(false)) { MetaModel::DBCreate(); } diff --git a/pages/ITopConsultant.php b/pages/ITopConsultant.php index 68dcb6dca..022788c8c 100644 --- a/pages/ITopConsultant.php +++ b/pages/ITopConsultant.php @@ -151,9 +151,9 @@ function ShowDatabaseInfo() function CreateDB() { $sRes = "

Creating the DB...

\n"; - if (MetaModel::DBExists()) + if (MetaModel::DBExists(false)) { - $sRes .= "

It appears that the DB already exists.

\n"; + $sRes .= "

It appears that the DB already exists (at least one table).

\n"; } else { diff --git a/setup/index.php b/setup/index.php index 722cd012f..07abf9f49 100644 --- a/setup/index.php +++ b/setup/index.php @@ -184,6 +184,9 @@ function CheckServerConnection(SetupWebPage $oP, $sDBServer, $sDBUser, $sDBPwd) $oDBSource = new CMDBSource; $oDBSource->Init($sDBServer, $sDBUser, $sDBPwd); $oP->ok("Connection to '$sDBServer' as '$sDBUser' successful."); + + $oP->log("Info - User privileges: ".($oDBSource->GetRawPrivileges())); + $sDBVersion = $oDBSource->GetDBVersion(); if (version_compare($sDBVersion, MYSQL_MIN_VERSION, '>=')) { @@ -262,19 +265,33 @@ function InitDataModel(SetupWebPage $oP, $sConfigFileName, $bAllowMissingDatabas function CreateDatabaseStructure(SetupWebPage $oP, Config $oConfig, $sDBName, $sDBPrefix) { InitDataModel($oP, TMP_CONFIG_FILE, true); // Allow the DB to NOT exist since we're about to create it ! + $oP->log('Info - CreateDatabaseStructure'); - $oP->info("Creating the structure in '$sDBName' (prefix = '$sDBPrefix')."); - //MetaModel::CheckDefinitions(); - if (!MetaModel::DBExists()) + if (strlen($sDBPrefix) > 0) { - MetaModel::DBCreate(); - $oP->ok("Database structure created in '$sDBName' (prefix = '$sDBPrefix')."); + $oP->info("Creating the structure in '$sDBName' (table names prefixed by '$sDBPrefix')."); } else { - $oP->error("Error: database '$sDBName' (prefix = '$sDBPrefix') already exists."); - $oP->p("Tables with conflicting names already exist in the database. - Try selecting another database instance or specifiy a prefix to prevent conflicting table names."); + $oP->info("Creating the structure in '$sDBName'."); + } + + //MetaModel::CheckDefinitions(); + if (!MetaModel::DBExists(/* bMustBeComplete */ false)) + { + MetaModel::DBCreate(); + $oP->ok("Database structure successfuly created."); + } + else + { + if (strlen($sDBPrefix) > 0) + { + $oP->error("Error: found iTop tables into the database '$sDBName' (prefix: '$sDBPrefix'). Please, try selecting another database instance or specify another prefix to prevent conflicting table names."); + } + else + { + $oP->error("Error: found iTop tables into the database '$sDBName'. Please, try selecting another database instance or specify a prefix to prevent conflicting table names."); + } return false; } return true; @@ -380,8 +397,9 @@ function DisplayStep1(SetupWebPage $oP) $oP->add("
Database connection\n"); $aForm = array(); $aForm[] = array('label' => "Server name$sRedStar:", 'input' => "", - 'help' => 'E.g. "localhost", "dbserver.mycompany.com" or "192.142.10.23".'); - $aForm[] = array('label' => "User name$sRedStar:", 'input' => ""); + 'help' => 'E.g. "localhost", "dbserver.mycompany.com" or "192.142.10.23"'); + $aForm[] = array('label' => "User name$sRedStar:", 'input' => "", + 'help' => 'The account must have the following privileges: SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER'); $aForm[] = array('label' => 'Password:', 'input' => ""); $oP->form($aForm); $oP->add("
\n"); @@ -407,7 +425,7 @@ function DisplayStep2(SetupWebPage $oP, Config $oConfig, $sDBServer, $sDBUser, $ if ($aDatabases === false) { // Connection failed, invalid credentials ? Go back - $oP->add("\n"); + $oP->add("\n"); } else { @@ -441,7 +459,7 @@ function DisplayStep2(SetupWebPage $oP, Config $oConfig, $sDBServer, $sDBUser, $ $oP->form($aForm); $oP->add("\n"); - $oP->add("\n"); + $oP->add("\n"); $oP->add("    \n"); $oP->add("\n"); } @@ -477,13 +495,13 @@ function DisplayStep3(SetupWebPage $oP, Config $oConfig, $sDBName, $sDBPrefix) $oP->form($aForm); $oP->add("\n"); $oP->add("\n"); - $oP->add("\n"); + $oP->add("\n"); $oP->add("    \n"); $oP->add("\n"); } else { - $oP->add("\n"); + $oP->add("\n"); } // Form goes here $oP->add("\n"); @@ -509,14 +527,14 @@ function DisplayStep4(SetupWebPage $oP, Config $oConfig, $sAdminUser, $sAdminPwd $oP->p(" Yes, for testing purposes, populate the database with sample data.\n"); $oP->p(" No, this is a production system, load only the data required by the application.\n"); $oP->p("\n"); - $oP->add("\n"); + $oP->add("\n"); $oP->add("    \n"); $oP->add("\n"); } else { // Creation failed - $oP->add("\n"); + $oP->add("\n"); } // End of visible form $oP->add("\n"); @@ -561,7 +579,7 @@ function DisplayStep5(SetupWebPage $oP, Config $oConfig, $sAuthUser, $sAuthPwd) $oP->add("
\n"); $oP->ok("The initialization completed successfully."); // Form goes here - $oP->add("\n"); + $oP->add("\n"); $oP->add("    \n"); $oP->add("\n"); $oP->add("
\n"); @@ -575,7 +593,7 @@ function DisplayStep5(SetupWebPage $oP, Config $oConfig, $sAuthUser, $sAuthPwd) $oP->error("Error: Failed to login for user: '$sAuthUser'\n"); $oP->add("
\n"); - $oP->add("\n"); + $oP->add("\n"); $oP->add("    \n"); $oP->add("
\n"); } @@ -585,6 +603,7 @@ function DisplayStep5(SetupWebPage $oP, Config $oConfig, $sAuthUser, $sAuthPwd) $oP->error("Error: unable to create the configuration file."); $oP->p($e->getHtmlDesc()); $oP->p("Did you forget to remove the previous (read-only) configuration file ?"); + $oP->add("\n"); } } /** @@ -686,10 +705,12 @@ try catch(Exception $e) { $oP->error("Error: '".$e->getMessage()."'"); + $oP->add("\n"); } catch(CoreException $e) { $oP->error("Error: '".$e->getHtmlDesc()."'"); + $oP->add("\n"); } $oP->output(); ?>