summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorLukas Reschke <lukas@owncloud.com>2014-09-16 18:54:37 +0200
committerLukas Reschke <lukas@owncloud.com>2014-09-16 18:54:37 +0200
commit4ebc11aa8d44a8188d43b9f884bd306b4a2fc7a3 (patch)
treed8cbfb46fdc0c878a3f17695933ce189266b0283 /lib
parent129e2d3c321b89381e87a02f94af3b1a9d430d97 (diff)
parente6609d09707daf78716b649d98e85e2c8eff55e1 (diff)
downloadnextcloud-server-4ebc11aa8d44a8188d43b9f884bd306b4a2fc7a3.tar.gz
nextcloud-server-4ebc11aa8d44a8188d43b9f884bd306b4a2fc7a3.zip
Merge pull request #11102 from owncloud/visit1985-issue_108
mysql setup: if dbuser exists try a different one (owncloud/core#108)
Diffstat (limited to 'lib')
-rw-r--r--lib/private/setup/mysql.php62
1 files changed, 38 insertions, 24 deletions
diff --git a/lib/private/setup/mysql.php b/lib/private/setup/mysql.php
index 3327965fb49..5558a2d1e51 100644
--- a/lib/private/setup/mysql.php
+++ b/lib/private/setup/mysql.php
@@ -12,38 +12,52 @@ class MySQL extends AbstractDatabase {
throw new \DatabaseSetupException($this->trans->t('MySQL/MariaDB username and/or password not valid'),
$this->trans->t('You need to enter either an existing account or the administrator.'));
}
+ //user already specified in config
$oldUser=\OC_Config::getValue('dbuser', false);
- //this should be enough to check for admin rights in mysql
- $query="SELECT user FROM mysql.user WHERE user='$this->dbuser'";
- if(mysql_query($query, $connection)) {
- //use the admin login data for the new database user
+ //we don't have a dbuser specified in config
+ if($this->dbuser!=$oldUser) {
+ //add prefix to the admin username to prevent collisions
+ $adminUser=substr('oc_'.$username, 0, 16);
- //add prefix to the mysql user name to prevent collisions
- $this->dbuser=substr('oc_'.$username, 0, 16);
- if($this->dbuser!=$oldUser) {
- //hash the password so we don't need to store the admin config in the config file
- $this->dbpassword=\OC_Util::generateRandomBytes(30);
+ $i = 1;
+ while(true) {
+ //this should be enough to check for admin rights in mysql
+ $query="SELECT user FROM mysql.user WHERE user='$adminUser'";
- $this->createDBUser($connection);
+ $result = mysql_query($query, $connection);
- \OC_Config::setValue('dbuser', $this->dbuser);
- \OC_Config::setValue('dbpassword', $this->dbpassword);
- }
+ //current dbuser has admin rights
+ if($result) {
+ //new dbuser does not exist
+ if(mysql_num_rows($result) === 0) {
+ //use the admin login data for the new database user
+ $this->dbuser=$adminUser;
- //create the database
- $this->createDatabase($connection);
- }
- else {
- if($this->dbuser!=$oldUser) {
- \OC_Config::setValue('dbuser', $this->dbuser);
- \OC_Config::setValue('dbpassword', $this->dbpassword);
- }
-
- //create the database
- $this->createDatabase($connection);
+ //create a random password so we don't need to store the admin password in the config file
+ $this->dbpassword=\OC_Util::generateRandomBytes(30);
+
+ $this->createDBUser($connection);
+
+ break;
+ } else {
+ //repeat with different username
+ $length=strlen((string)$i);
+ $adminUser=substr('oc_'.$username, 0, 16 - $length).$i;
+ $i++;
+ }
+ } else {
+ break;
+ }
+ };
+
+ \OC_Config::setValue('dbuser', $this->dbuser);
+ \OC_Config::setValue('dbpassword', $this->dbpassword);
}
+ //create the database
+ $this->createDatabase($connection);
+
//fill the database if needed
$query='select count(*) from information_schema.tables'
." where table_schema='".$this->dbname."' AND table_name = '".$this->tableprefix."users';";