Преглед на файлове

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)

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@291 a333f486-631f-4898-b8df-5754b55c2be0

romainq преди 15 години
родител
ревизия
d115df743f
променени са 6 файла, в които са добавени 112 реда и са изтрити 37 реда
  1. 0 1
      application/application.inc.php
  2. 30 2
      core/cmdbsource.class.inc.php
  3. 41 14
      core/metamodel.class.php
  4. 2 2
      core/test.class.inc.php
  5. 2 2
      pages/ITopConsultant.php
  6. 37 16
      setup/index.php

+ 0 - 1
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');

+ 30 - 2
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);
+	}
 }
 
 

+ 41 - 14
core/metamodel.class.php

@@ -1941,29 +1941,56 @@ abstract class MetaModel
 		echo "</form>\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()

+ 2 - 2
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();
 		}

+ 2 - 2
pages/ITopConsultant.php

@@ -151,9 +151,9 @@ function ShowDatabaseInfo()
 function CreateDB()
 {
 	$sRes = "<p>Creating the DB...</p>\n";
-	if (MetaModel::DBExists())
+	if (MetaModel::DBExists(false))
 	{
-		$sRes .= "<p>It appears that the DB already exists.</p>\n";
+		$sRes .= "<p>It appears that the DB already exists (at least one table).</p>\n";
 	}
 	else
 	{

+ 37 - 16
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').");
+	if (strlen($sDBPrefix) > 0)
+	{
+		$oP->info("Creating the structure in '$sDBName' (table names prefixed by '$sDBPrefix').");
+	}
+	else
+	{
+		$oP->info("Creating the structure in '$sDBName'.");
+	}
+
 	//MetaModel::CheckDefinitions();
-	if (!MetaModel::DBExists())
+	if (!MetaModel::DBExists(/* bMustBeComplete */ false))
 	{
 		MetaModel::DBCreate();
-		$oP->ok("Database structure created in '$sDBName' (prefix = '$sDBPrefix').");
+		$oP->ok("Database structure successfuly created.");
 	}
 	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.");
+		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("<fieldset><legend>Database connection</legend>\n");
 		$aForm = array();
 		$aForm[] = array('label' => "Server name$sRedStar:", 'input' => "<input id=\"db_server\" type=\"text\" name=\"db_server\" value=\"\">",
-						 'help' => 'E.g. "localhost", "dbserver.mycompany.com" or "192.142.10.23".');
-		$aForm[] = array('label' => "User name$sRedStar:", 'input' => "<input id=\"db_user\" type=\"text\" name=\"db_user\" value=\"\">");
+						'help' => 'E.g. "localhost", "dbserver.mycompany.com" or "192.142.10.23"');
+		$aForm[] = array('label' => "User name$sRedStar:", 'input' => "<input id=\"db_user\" type=\"text\" name=\"db_user\" value=\"\">",
+						'help' => 'The account must have the following privileges: SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER');
 		$aForm[] = array('label' => 'Password:', 'input' => "<input id=\"db_pwd\" type=\"password\" name=\"db_pwd\" value=\"\">");
 		$oP->form($aForm);
 		$oP->add("</fieldset>\n");
@@ -407,7 +425,7 @@ function DisplayStep2(SetupWebPage $oP, Config $oConfig, $sDBServer, $sDBUser, $
 	if ($aDatabases === false)
 	{
 		// Connection failed, invalid credentials ? Go back
-		$oP->add("<button onClick=\"window.history.back();\"><< Back</button>\n");
+		$oP->add("<button type=\"button\" onClick=\"window.history.back();\"><< Back</button>\n");
 	}
 	else
 	{
@@ -441,7 +459,7 @@ function DisplayStep2(SetupWebPage $oP, Config $oConfig, $sDBServer, $sDBUser, $
 		$oP->form($aForm);
 
 		$oP->add("<input type=\"hidden\" name=\"operation\" value=\"$sNextOperation\">\n");
-		$oP->add("<button onClick=\"window.history.back();\"><< Back</button>\n");
+		$oP->add("<button type=\"button\" onClick=\"window.history.back();\"><< Back</button>\n");
 		$oP->add("&nbsp;&nbsp;&nbsp;&nbsp;\n");
 		$oP->add("<button type=\"submit\">Next >></button>\n");
 	}
@@ -477,13 +495,13 @@ function DisplayStep3(SetupWebPage $oP, Config $oConfig, $sDBName, $sDBPrefix)
 		$oP->form($aForm);
 		$oP->add("</fieldset>\n");
 		$oP->add("<input type=\"hidden\" name=\"operation\" value=\"$sNextOperation\">\n");
-		$oP->add("<button onClick=\"window.history.back();\"><< Back</button>\n");
+		$oP->add("<button type=\"button\" onClick=\"window.history.back();\"><< Back</button>\n");
 		$oP->add("&nbsp;&nbsp;&nbsp;&nbsp;\n");
 		$oP->add("<button type=\"submit\">Next >></button>\n");
 	}
 	else
 	{
-		$oP->add("<button onClick=\"window.history.back();\"><< Back</button>\n");
+		$oP->add("<button type=\"button\" onClick=\"window.history.back();\"><< Back</button>\n");
 	}
 	// Form goes here
 	$oP->add("</form>\n");
@@ -509,14 +527,14 @@ function DisplayStep4(SetupWebPage $oP, Config $oConfig, $sAdminUser, $sAdminPwd
 		$oP->p("<input type=\"radio\" id=\"sample_data\" name=\"sample_data\" checked value=\"yes\"> Yes, for testing purposes, populate the database with sample data.\n");
 		$oP->p("<input type=\"radio\" name=\"sample_data\" unchecked value=\"no\"> No, this is a production system, load only the data required by the application.\n");
 		$oP->p("</fieldset>\n");
-		$oP->add("<button onClick=\"window.history.back();\"><< Back</button>\n");
+		$oP->add("<button type=\"button\" onClick=\"window.history.back();\"><< Back</button>\n");
 		$oP->add("&nbsp;&nbsp;&nbsp;&nbsp;\n");
 		$oP->add("<button onclick=\"DoSubmit('Finalizing configuration and loading data...', 4);\">Finish</button>\n");
 	}
 	else
 	{
 		// Creation failed
-		$oP->add("<button onClick=\"window.history.back();\"><< Back</button>\n");
+		$oP->add("<button type=\"button\" onClick=\"window.history.back();\"><< Back</button>\n");
 	}
 	// End of visible form
 	$oP->add("</form>\n");
@@ -561,7 +579,7 @@ function DisplayStep5(SetupWebPage $oP, Config $oConfig, $sAuthUser, $sAuthPwd)
 			$oP->add("<form method=\"get\" action=\"../index.php\">\n");
 			$oP->ok("The initialization completed successfully.");
 			// Form goes here
-			$oP->add("<button onClick=\"window.history.back();\"><< Back</button>\n");
+			$oP->add("<button type=\"button\" onClick=\"window.history.back();\"><< Back</button>\n");
 			$oP->add("&nbsp;&nbsp;&nbsp;&nbsp;\n");
 			$oP->add("<button type=\"submit\">Enter iTop</button>\n");
 			$oP->add("</form>\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("<form method=\"get\" action=\"../index.php\">\n");
-			$oP->add("<button onClick=\"window.history.back();\"><< Back</button>\n");
+			$oP->add("<button type=\"button\" onClick=\"window.history.back();\"><< Back</button>\n");
 			$oP->add("&nbsp;&nbsp;&nbsp;&nbsp;\n");
 			$oP->add("</form>\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("<button type=\"button\" onClick=\"window.history.back();\"><< Back</button>\n");
 	}
 }
 /**
@@ -686,10 +705,12 @@ try
 catch(Exception $e)
 {
 	$oP->error("Error: '".$e->getMessage()."'");	
+	$oP->add("<button type=\"button\" onClick=\"window.history.back();\"><< Back</button>\n");
 }
 catch(CoreException $e)
 {
 	$oP->error("Error: '".$e->getHtmlDesc()."'");	
+	$oP->add("<button type=\"button\" onClick=\"window.history.back();\"><< Back</button>\n");
 }
 $oP->output();
 ?>