@@ -34,143 +34,143 @@ |
||
| 34 | 34 | use OCP\ILogger; |
| 35 | 35 | |
| 36 | 36 | class MySQL extends AbstractDatabase { |
| 37 | - public $dbprettyname = 'MySQL/MariaDB'; |
|
| 38 | - |
|
| 39 | - public function setupDatabase($username) { |
|
| 40 | - //check if the database user has admin right |
|
| 41 | - $connection = $this->connect(['dbname' => null]); |
|
| 42 | - |
|
| 43 | - // detect mb4 |
|
| 44 | - $tools = new MySqlTools(); |
|
| 45 | - if ($tools->supports4ByteCharset($connection)) { |
|
| 46 | - $this->config->setValue('mysql.utf8mb4', true); |
|
| 47 | - $connection = $this->connect(['dbname' => null]); |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - $this->createSpecificUser($username, $connection); |
|
| 51 | - |
|
| 52 | - //create the database |
|
| 53 | - $this->createDatabase($connection); |
|
| 54 | - |
|
| 55 | - //fill the database if needed |
|
| 56 | - $query='select count(*) from information_schema.tables where table_schema=? AND table_name = ?'; |
|
| 57 | - $connection->executeQuery($query, [$this->dbName, $this->tablePrefix.'users']); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * @param \OC\DB\Connection $connection |
|
| 62 | - */ |
|
| 63 | - private function createDatabase($connection) { |
|
| 64 | - try{ |
|
| 65 | - $name = $this->dbName; |
|
| 66 | - $user = $this->dbUser; |
|
| 67 | - //we can't use OC_DB functions here because we need to connect as the administrative user. |
|
| 68 | - $characterSet = $this->config->getValue('mysql.utf8mb4', false) ? 'utf8mb4' : 'utf8'; |
|
| 69 | - $query = "CREATE DATABASE IF NOT EXISTS `$name` CHARACTER SET $characterSet COLLATE ${characterSet}_bin;"; |
|
| 70 | - $connection->executeUpdate($query); |
|
| 71 | - } catch (\Exception $ex) { |
|
| 72 | - $this->logger->logException($ex, [ |
|
| 73 | - 'message' => 'Database creation failed.', |
|
| 74 | - 'level' => ILogger::ERROR, |
|
| 75 | - 'app' => 'mysql.setup', |
|
| 76 | - ]); |
|
| 77 | - return; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - try { |
|
| 81 | - //this query will fail if there aren't the right permissions, ignore the error |
|
| 82 | - $query="GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, REFERENCES, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, EVENT, TRIGGER ON `$name` . * TO '$user'"; |
|
| 83 | - $connection->executeUpdate($query); |
|
| 84 | - } catch (\Exception $ex) { |
|
| 85 | - $this->logger->logException($ex, [ |
|
| 86 | - 'message' => 'Could not automatically grant privileges, this can be ignored if database user already had privileges.', |
|
| 87 | - 'level' => ILogger::DEBUG, |
|
| 88 | - 'app' => 'mysql.setup', |
|
| 89 | - ]); |
|
| 90 | - } |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * @param IDBConnection $connection |
|
| 95 | - * @throws \OC\DatabaseSetupException |
|
| 96 | - */ |
|
| 97 | - private function createDBUser($connection) { |
|
| 98 | - try{ |
|
| 99 | - $name = $this->dbUser; |
|
| 100 | - $password = $this->dbPassword; |
|
| 101 | - // we need to create 2 accounts, one for global use and one for local user. if we don't specify the local one, |
|
| 102 | - // the anonymous user would take precedence when there is one. |
|
| 103 | - $query = "CREATE USER '$name'@'localhost' IDENTIFIED BY '$password'"; |
|
| 104 | - $connection->executeUpdate($query); |
|
| 105 | - $query = "CREATE USER '$name'@'%' IDENTIFIED BY '$password'"; |
|
| 106 | - $connection->executeUpdate($query); |
|
| 107 | - } |
|
| 108 | - catch (\Exception $ex){ |
|
| 109 | - $this->logger->logException($ex, [ |
|
| 110 | - 'message' => 'Database user creation failed.', |
|
| 111 | - 'level' => ILogger::ERROR, |
|
| 112 | - 'app' => 'mysql.setup', |
|
| 113 | - ]); |
|
| 114 | - } |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * @param $username |
|
| 119 | - * @param IDBConnection $connection |
|
| 120 | - * @return array |
|
| 121 | - */ |
|
| 122 | - private function createSpecificUser($username, $connection) { |
|
| 123 | - try { |
|
| 124 | - //user already specified in config |
|
| 125 | - $oldUser = $this->config->getValue('dbuser', false); |
|
| 126 | - |
|
| 127 | - //we don't have a dbuser specified in config |
|
| 128 | - if ($this->dbUser !== $oldUser) { |
|
| 129 | - //add prefix to the admin username to prevent collisions |
|
| 130 | - $adminUser = substr('oc_' . $username, 0, 16); |
|
| 131 | - |
|
| 132 | - $i = 1; |
|
| 133 | - while (true) { |
|
| 134 | - //this should be enough to check for admin rights in mysql |
|
| 135 | - $query = 'SELECT user FROM mysql.user WHERE user=?'; |
|
| 136 | - $result = $connection->executeQuery($query, [$adminUser]); |
|
| 137 | - |
|
| 138 | - //current dbuser has admin rights |
|
| 139 | - if ($result) { |
|
| 140 | - $data = $result->fetchAll(); |
|
| 141 | - //new dbuser does not exist |
|
| 142 | - if (count($data) === 0) { |
|
| 143 | - //use the admin login data for the new database user |
|
| 144 | - $this->dbUser = $adminUser; |
|
| 145 | - |
|
| 146 | - //create a random password so we don't need to store the admin password in the config file |
|
| 147 | - $this->dbPassword = $this->random->generate(30); |
|
| 148 | - |
|
| 149 | - $this->createDBUser($connection); |
|
| 150 | - |
|
| 151 | - break; |
|
| 152 | - } else { |
|
| 153 | - //repeat with different username |
|
| 154 | - $length = strlen((string)$i); |
|
| 155 | - $adminUser = substr('oc_' . $username, 0, 16 - $length) . $i; |
|
| 156 | - $i++; |
|
| 157 | - } |
|
| 158 | - } else { |
|
| 159 | - break; |
|
| 160 | - } |
|
| 161 | - } |
|
| 162 | - } |
|
| 163 | - } catch (\Exception $ex) { |
|
| 164 | - $this->logger->logException($ex, [ |
|
| 165 | - 'message' => 'Can not create a new MySQL user, will continue with the provided user.', |
|
| 166 | - 'level' => ILogger::INFO, |
|
| 167 | - 'app' => 'mysql.setup', |
|
| 168 | - ]); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - $this->config->setValues([ |
|
| 172 | - 'dbuser' => $this->dbUser, |
|
| 173 | - 'dbpassword' => $this->dbPassword, |
|
| 174 | - ]); |
|
| 175 | - } |
|
| 37 | + public $dbprettyname = 'MySQL/MariaDB'; |
|
| 38 | + |
|
| 39 | + public function setupDatabase($username) { |
|
| 40 | + //check if the database user has admin right |
|
| 41 | + $connection = $this->connect(['dbname' => null]); |
|
| 42 | + |
|
| 43 | + // detect mb4 |
|
| 44 | + $tools = new MySqlTools(); |
|
| 45 | + if ($tools->supports4ByteCharset($connection)) { |
|
| 46 | + $this->config->setValue('mysql.utf8mb4', true); |
|
| 47 | + $connection = $this->connect(['dbname' => null]); |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + $this->createSpecificUser($username, $connection); |
|
| 51 | + |
|
| 52 | + //create the database |
|
| 53 | + $this->createDatabase($connection); |
|
| 54 | + |
|
| 55 | + //fill the database if needed |
|
| 56 | + $query='select count(*) from information_schema.tables where table_schema=? AND table_name = ?'; |
|
| 57 | + $connection->executeQuery($query, [$this->dbName, $this->tablePrefix.'users']); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * @param \OC\DB\Connection $connection |
|
| 62 | + */ |
|
| 63 | + private function createDatabase($connection) { |
|
| 64 | + try{ |
|
| 65 | + $name = $this->dbName; |
|
| 66 | + $user = $this->dbUser; |
|
| 67 | + //we can't use OC_DB functions here because we need to connect as the administrative user. |
|
| 68 | + $characterSet = $this->config->getValue('mysql.utf8mb4', false) ? 'utf8mb4' : 'utf8'; |
|
| 69 | + $query = "CREATE DATABASE IF NOT EXISTS `$name` CHARACTER SET $characterSet COLLATE ${characterSet}_bin;"; |
|
| 70 | + $connection->executeUpdate($query); |
|
| 71 | + } catch (\Exception $ex) { |
|
| 72 | + $this->logger->logException($ex, [ |
|
| 73 | + 'message' => 'Database creation failed.', |
|
| 74 | + 'level' => ILogger::ERROR, |
|
| 75 | + 'app' => 'mysql.setup', |
|
| 76 | + ]); |
|
| 77 | + return; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + try { |
|
| 81 | + //this query will fail if there aren't the right permissions, ignore the error |
|
| 82 | + $query="GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, REFERENCES, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, EVENT, TRIGGER ON `$name` . * TO '$user'"; |
|
| 83 | + $connection->executeUpdate($query); |
|
| 84 | + } catch (\Exception $ex) { |
|
| 85 | + $this->logger->logException($ex, [ |
|
| 86 | + 'message' => 'Could not automatically grant privileges, this can be ignored if database user already had privileges.', |
|
| 87 | + 'level' => ILogger::DEBUG, |
|
| 88 | + 'app' => 'mysql.setup', |
|
| 89 | + ]); |
|
| 90 | + } |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * @param IDBConnection $connection |
|
| 95 | + * @throws \OC\DatabaseSetupException |
|
| 96 | + */ |
|
| 97 | + private function createDBUser($connection) { |
|
| 98 | + try{ |
|
| 99 | + $name = $this->dbUser; |
|
| 100 | + $password = $this->dbPassword; |
|
| 101 | + // we need to create 2 accounts, one for global use and one for local user. if we don't specify the local one, |
|
| 102 | + // the anonymous user would take precedence when there is one. |
|
| 103 | + $query = "CREATE USER '$name'@'localhost' IDENTIFIED BY '$password'"; |
|
| 104 | + $connection->executeUpdate($query); |
|
| 105 | + $query = "CREATE USER '$name'@'%' IDENTIFIED BY '$password'"; |
|
| 106 | + $connection->executeUpdate($query); |
|
| 107 | + } |
|
| 108 | + catch (\Exception $ex){ |
|
| 109 | + $this->logger->logException($ex, [ |
|
| 110 | + 'message' => 'Database user creation failed.', |
|
| 111 | + 'level' => ILogger::ERROR, |
|
| 112 | + 'app' => 'mysql.setup', |
|
| 113 | + ]); |
|
| 114 | + } |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * @param $username |
|
| 119 | + * @param IDBConnection $connection |
|
| 120 | + * @return array |
|
| 121 | + */ |
|
| 122 | + private function createSpecificUser($username, $connection) { |
|
| 123 | + try { |
|
| 124 | + //user already specified in config |
|
| 125 | + $oldUser = $this->config->getValue('dbuser', false); |
|
| 126 | + |
|
| 127 | + //we don't have a dbuser specified in config |
|
| 128 | + if ($this->dbUser !== $oldUser) { |
|
| 129 | + //add prefix to the admin username to prevent collisions |
|
| 130 | + $adminUser = substr('oc_' . $username, 0, 16); |
|
| 131 | + |
|
| 132 | + $i = 1; |
|
| 133 | + while (true) { |
|
| 134 | + //this should be enough to check for admin rights in mysql |
|
| 135 | + $query = 'SELECT user FROM mysql.user WHERE user=?'; |
|
| 136 | + $result = $connection->executeQuery($query, [$adminUser]); |
|
| 137 | + |
|
| 138 | + //current dbuser has admin rights |
|
| 139 | + if ($result) { |
|
| 140 | + $data = $result->fetchAll(); |
|
| 141 | + //new dbuser does not exist |
|
| 142 | + if (count($data) === 0) { |
|
| 143 | + //use the admin login data for the new database user |
|
| 144 | + $this->dbUser = $adminUser; |
|
| 145 | + |
|
| 146 | + //create a random password so we don't need to store the admin password in the config file |
|
| 147 | + $this->dbPassword = $this->random->generate(30); |
|
| 148 | + |
|
| 149 | + $this->createDBUser($connection); |
|
| 150 | + |
|
| 151 | + break; |
|
| 152 | + } else { |
|
| 153 | + //repeat with different username |
|
| 154 | + $length = strlen((string)$i); |
|
| 155 | + $adminUser = substr('oc_' . $username, 0, 16 - $length) . $i; |
|
| 156 | + $i++; |
|
| 157 | + } |
|
| 158 | + } else { |
|
| 159 | + break; |
|
| 160 | + } |
|
| 161 | + } |
|
| 162 | + } |
|
| 163 | + } catch (\Exception $ex) { |
|
| 164 | + $this->logger->logException($ex, [ |
|
| 165 | + 'message' => 'Can not create a new MySQL user, will continue with the provided user.', |
|
| 166 | + 'level' => ILogger::INFO, |
|
| 167 | + 'app' => 'mysql.setup', |
|
| 168 | + ]); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + $this->config->setValues([ |
|
| 172 | + 'dbuser' => $this->dbUser, |
|
| 173 | + 'dbpassword' => $this->dbPassword, |
|
| 174 | + ]); |
|
| 175 | + } |
|
| 176 | 176 | } |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | $this->createDatabase($connection); |
| 54 | 54 | |
| 55 | 55 | //fill the database if needed |
| 56 | - $query='select count(*) from information_schema.tables where table_schema=? AND table_name = ?'; |
|
| 56 | + $query = 'select count(*) from information_schema.tables where table_schema=? AND table_name = ?'; |
|
| 57 | 57 | $connection->executeQuery($query, [$this->dbName, $this->tablePrefix.'users']); |
| 58 | 58 | } |
| 59 | 59 | |
@@ -61,7 +61,7 @@ discard block |
||
| 61 | 61 | * @param \OC\DB\Connection $connection |
| 62 | 62 | */ |
| 63 | 63 | private function createDatabase($connection) { |
| 64 | - try{ |
|
| 64 | + try { |
|
| 65 | 65 | $name = $this->dbName; |
| 66 | 66 | $user = $this->dbUser; |
| 67 | 67 | //we can't use OC_DB functions here because we need to connect as the administrative user. |
@@ -79,7 +79,7 @@ discard block |
||
| 79 | 79 | |
| 80 | 80 | try { |
| 81 | 81 | //this query will fail if there aren't the right permissions, ignore the error |
| 82 | - $query="GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, REFERENCES, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, EVENT, TRIGGER ON `$name` . * TO '$user'"; |
|
| 82 | + $query = "GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, REFERENCES, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, EVENT, TRIGGER ON `$name` . * TO '$user'"; |
|
| 83 | 83 | $connection->executeUpdate($query); |
| 84 | 84 | } catch (\Exception $ex) { |
| 85 | 85 | $this->logger->logException($ex, [ |
@@ -95,7 +95,7 @@ discard block |
||
| 95 | 95 | * @throws \OC\DatabaseSetupException |
| 96 | 96 | */ |
| 97 | 97 | private function createDBUser($connection) { |
| 98 | - try{ |
|
| 98 | + try { |
|
| 99 | 99 | $name = $this->dbUser; |
| 100 | 100 | $password = $this->dbPassword; |
| 101 | 101 | // we need to create 2 accounts, one for global use and one for local user. if we don't specify the local one, |
@@ -105,7 +105,7 @@ discard block |
||
| 105 | 105 | $query = "CREATE USER '$name'@'%' IDENTIFIED BY '$password'"; |
| 106 | 106 | $connection->executeUpdate($query); |
| 107 | 107 | } |
| 108 | - catch (\Exception $ex){ |
|
| 108 | + catch (\Exception $ex) { |
|
| 109 | 109 | $this->logger->logException($ex, [ |
| 110 | 110 | 'message' => 'Database user creation failed.', |
| 111 | 111 | 'level' => ILogger::ERROR, |
@@ -127,7 +127,7 @@ discard block |
||
| 127 | 127 | //we don't have a dbuser specified in config |
| 128 | 128 | if ($this->dbUser !== $oldUser) { |
| 129 | 129 | //add prefix to the admin username to prevent collisions |
| 130 | - $adminUser = substr('oc_' . $username, 0, 16); |
|
| 130 | + $adminUser = substr('oc_'.$username, 0, 16); |
|
| 131 | 131 | |
| 132 | 132 | $i = 1; |
| 133 | 133 | while (true) { |
@@ -144,15 +144,15 @@ discard block |
||
| 144 | 144 | $this->dbUser = $adminUser; |
| 145 | 145 | |
| 146 | 146 | //create a random password so we don't need to store the admin password in the config file |
| 147 | - $this->dbPassword = $this->random->generate(30); |
|
| 147 | + $this->dbPassword = $this->random->generate(30); |
|
| 148 | 148 | |
| 149 | 149 | $this->createDBUser($connection); |
| 150 | 150 | |
| 151 | 151 | break; |
| 152 | 152 | } else { |
| 153 | 153 | //repeat with different username |
| 154 | - $length = strlen((string)$i); |
|
| 155 | - $adminUser = substr('oc_' . $username, 0, 16 - $length) . $i; |
|
| 154 | + $length = strlen((string) $i); |
|
| 155 | + $adminUser = substr('oc_'.$username, 0, 16 - $length).$i; |
|
| 156 | 156 | $i++; |
| 157 | 157 | } |
| 158 | 158 | } else { |