From ba9c967435ce62bb418c5c67eeb22184b009b24f Mon Sep 17 00:00:00 2001 From: Sean Comeau Date: Thu, 10 Jan 2013 14:43:08 -0800 Subject: [PATCH 1/3] Throw an exception when creating a MySQL user fails and display exception error text to user --- lib/setup.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/setup.php b/lib/setup.php index fdd10be682..9fa6aaf10b 100644 --- a/lib/setup.php +++ b/lib/setup.php @@ -70,9 +70,10 @@ class OC_Setup { try { self::setupMySQLDatabase($dbhost, $dbuser, $dbpass, $dbname, $dbtableprefix, $username); } catch (Exception $e) { + $msgs = explode('|', $e->getMessage()); $error[] = array( - 'error' => 'MySQL username and/or password not valid', - 'hint' => 'You need to enter either an existing account or the administrator.' + 'error' => $msgs[0], + 'hint' => $msgs[1] ); return($error); } @@ -166,7 +167,7 @@ class OC_Setup { //check if the database user has admin right $connection = @mysql_connect($dbhost, $dbuser, $dbpass); if(!$connection) { - throw new Exception('MySQL username and/or password not valid'); + throw new Exception('MySQL username and/or password not valid|You need to enter either an existing account or the administrator.'); } $oldUser=OC_Config::getValue('dbuser', false); @@ -229,8 +230,14 @@ class OC_Setup { // the anonymous user would take precedence when there is one. $query = "CREATE USER '$name'@'localhost' IDENTIFIED BY '$password'"; $result = mysql_query($query, $connection); + if (!$result) { + throw new Exception("MySQL user '" . "$name" . "'@'localhost' already exists|Delete this user from MySQL."); + } $query = "CREATE USER '$name'@'%' IDENTIFIED BY '$password'"; $result = mysql_query($query, $connection); + if (!$result) { + throw new Exception("MySQL user '" . "$name" . "'@'%' already exists|Delete this user from MySQL."); + } } private static function setupPostgreSQLDatabase($dbhost, $dbuser, $dbpass, $dbname, $dbtableprefix, $username) { From 42cd99626e260b807e859ccd124340927cbfaa47 Mon Sep 17 00:00:00 2001 From: Sean Comeau Date: Mon, 14 Jan 2013 11:57:40 -0800 Subject: [PATCH 2/3] Use a custom exception instead of adding a delimiter to the error message --- lib/setup.php | 41 ++++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/lib/setup.php b/lib/setup.php index 9fa6aaf10b..9da17a5374 100644 --- a/lib/setup.php +++ b/lib/setup.php @@ -1,5 +1,23 @@ hint = $hint; + parent::__construct($message, $code, $previous); + } + + public function __toString() { + return __CLASS__ . ": [{$this->code}]: {$this->message} ({$this->hint})\n"; + } + + public function getHint() { + return $this->hint; + } +} + class OC_Setup { public static function install($options) { $error = array(); @@ -19,9 +37,9 @@ class OC_Setup { if($dbtype=='mysql') $dbprettyname = 'MySQL'; else if($dbtype=='pgsql') - $dbprettyname = 'PostgreSQL'; + $dbprettyname = 'PostgreSQL'; else - $dbprettyname = 'Oracle'; + $dbprettyname = 'Oracle'; if(empty($options['dbuser'])) { @@ -69,11 +87,16 @@ class OC_Setup { try { self::setupMySQLDatabase($dbhost, $dbuser, $dbpass, $dbname, $dbtableprefix, $username); - } catch (Exception $e) { - $msgs = explode('|', $e->getMessage()); + } catch (HintException $e) { $error[] = array( - 'error' => $msgs[0], - 'hint' => $msgs[1] + 'error' => $e->getMessage(), + 'hint' => $e->getHint() + ); + return($error); + } catch (Exception $e) { + $error[] = array( + 'error' => $e->getMessage(), + 'hint' => '' ); return($error); } @@ -167,7 +190,7 @@ class OC_Setup { //check if the database user has admin right $connection = @mysql_connect($dbhost, $dbuser, $dbpass); if(!$connection) { - throw new Exception('MySQL username and/or password not valid|You need to enter either an existing account or the administrator.'); + throw new HintException('MySQL username and/or password not valid','You need to enter either an existing account or the administrator.'); } $oldUser=OC_Config::getValue('dbuser', false); @@ -231,12 +254,12 @@ class OC_Setup { $query = "CREATE USER '$name'@'localhost' IDENTIFIED BY '$password'"; $result = mysql_query($query, $connection); if (!$result) { - throw new Exception("MySQL user '" . "$name" . "'@'localhost' already exists|Delete this user from MySQL."); + throw new HintException("MySQL user '" . "$name" . "'@'localhost' already exists","Delete this user from MySQL."); } $query = "CREATE USER '$name'@'%' IDENTIFIED BY '$password'"; $result = mysql_query($query, $connection); if (!$result) { - throw new Exception("MySQL user '" . "$name" . "'@'%' already exists|Delete this user from MySQL."); + throw new HintException("MySQL user '" . "$name" . "'@'%' already exists","Delete this user from MySQL."); } } From 0132a0b2cf1af95b4d56475dc80b576bccfab0d0 Mon Sep 17 00:00:00 2001 From: Sean Comeau Date: Mon, 14 Jan 2013 12:36:50 -0800 Subject: [PATCH 3/3] Rename TestException to DatabaseSetupException --- lib/setup.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/setup.php b/lib/setup.php index 9da17a5374..8985d7d829 100644 --- a/lib/setup.php +++ b/lib/setup.php @@ -1,6 +1,6 @@ $e->getMessage(), 'hint' => $e->getHint() @@ -190,7 +190,7 @@ class OC_Setup { //check if the database user has admin right $connection = @mysql_connect($dbhost, $dbuser, $dbpass); if(!$connection) { - throw new HintException('MySQL username and/or password not valid','You need to enter either an existing account or the administrator.'); + throw new DatabaseSetupException('MySQL username and/or password not valid','You need to enter either an existing account or the administrator.'); } $oldUser=OC_Config::getValue('dbuser', false); @@ -254,12 +254,12 @@ class OC_Setup { $query = "CREATE USER '$name'@'localhost' IDENTIFIED BY '$password'"; $result = mysql_query($query, $connection); if (!$result) { - throw new HintException("MySQL user '" . "$name" . "'@'localhost' already exists","Delete this user from MySQL."); + throw new DatabaseSetupException("MySQL user '" . "$name" . "'@'localhost' already exists","Delete this user from MySQL."); } $query = "CREATE USER '$name'@'%' IDENTIFIED BY '$password'"; $result = mysql_query($query, $connection); if (!$result) { - throw new HintException("MySQL user '" . "$name" . "'@'%' already exists","Delete this user from MySQL."); + throw new DatabaseSetupException("MySQL user '" . "$name" . "'@'%' already exists","Delete this user from MySQL."); } }