@@ -31,136 +31,136 @@ |
||
| 31 | 31 | use OCP\IDBConnection; |
| 32 | 32 | |
| 33 | 33 | class PostgreSQL extends AbstractDatabase { |
| 34 | - public $dbprettyname = 'PostgreSQL'; |
|
| 34 | + public $dbprettyname = 'PostgreSQL'; |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * @param string $username |
|
| 38 | - * @throws \OC\DatabaseSetupException |
|
| 39 | - * @suppress SqlInjectionChecker |
|
| 40 | - */ |
|
| 41 | - public function setupDatabase($username) { |
|
| 42 | - try { |
|
| 43 | - $connection = $this->connect([ |
|
| 44 | - 'dbname' => 'postgres' |
|
| 45 | - ]); |
|
| 46 | - //check for roles creation rights in postgresql |
|
| 47 | - $builder = $connection->getQueryBuilder(); |
|
| 48 | - $builder->automaticTablePrefix(false); |
|
| 49 | - $query = $builder |
|
| 50 | - ->select('rolname') |
|
| 51 | - ->from('pg_roles') |
|
| 52 | - ->where($builder->expr()->eq('rolcreaterole', new Literal('TRUE'))) |
|
| 53 | - ->andWhere($builder->expr()->eq('rolname', $builder->createNamedParameter($this->dbUser))); |
|
| 36 | + /** |
|
| 37 | + * @param string $username |
|
| 38 | + * @throws \OC\DatabaseSetupException |
|
| 39 | + * @suppress SqlInjectionChecker |
|
| 40 | + */ |
|
| 41 | + public function setupDatabase($username) { |
|
| 42 | + try { |
|
| 43 | + $connection = $this->connect([ |
|
| 44 | + 'dbname' => 'postgres' |
|
| 45 | + ]); |
|
| 46 | + //check for roles creation rights in postgresql |
|
| 47 | + $builder = $connection->getQueryBuilder(); |
|
| 48 | + $builder->automaticTablePrefix(false); |
|
| 49 | + $query = $builder |
|
| 50 | + ->select('rolname') |
|
| 51 | + ->from('pg_roles') |
|
| 52 | + ->where($builder->expr()->eq('rolcreaterole', new Literal('TRUE'))) |
|
| 53 | + ->andWhere($builder->expr()->eq('rolname', $builder->createNamedParameter($this->dbUser))); |
|
| 54 | 54 | |
| 55 | - try { |
|
| 56 | - $result = $query->execute(); |
|
| 57 | - $canCreateRoles = $result->rowCount() > 0; |
|
| 58 | - } catch (DatabaseException $e) { |
|
| 59 | - $canCreateRoles = false; |
|
| 60 | - } |
|
| 55 | + try { |
|
| 56 | + $result = $query->execute(); |
|
| 57 | + $canCreateRoles = $result->rowCount() > 0; |
|
| 58 | + } catch (DatabaseException $e) { |
|
| 59 | + $canCreateRoles = false; |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - if ($canCreateRoles) { |
|
| 63 | - //use the admin login data for the new database user |
|
| 62 | + if ($canCreateRoles) { |
|
| 63 | + //use the admin login data for the new database user |
|
| 64 | 64 | |
| 65 | - //add prefix to the postgresql user name to prevent collisions |
|
| 66 | - $this->dbUser = 'oc_' . strtolower($username); |
|
| 67 | - //create a new password so we don't need to store the admin config in the config file |
|
| 68 | - $this->dbPassword = \OC::$server->getSecureRandom()->generate(30, \OCP\Security\ISecureRandom::CHAR_LOWER . \OCP\Security\ISecureRandom::CHAR_DIGITS); |
|
| 65 | + //add prefix to the postgresql user name to prevent collisions |
|
| 66 | + $this->dbUser = 'oc_' . strtolower($username); |
|
| 67 | + //create a new password so we don't need to store the admin config in the config file |
|
| 68 | + $this->dbPassword = \OC::$server->getSecureRandom()->generate(30, \OCP\Security\ISecureRandom::CHAR_LOWER . \OCP\Security\ISecureRandom::CHAR_DIGITS); |
|
| 69 | 69 | |
| 70 | - $this->createDBUser($connection); |
|
| 71 | - } |
|
| 70 | + $this->createDBUser($connection); |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - $this->config->setValues([ |
|
| 74 | - 'dbuser' => $this->dbUser, |
|
| 75 | - 'dbpassword' => $this->dbPassword, |
|
| 76 | - ]); |
|
| 73 | + $this->config->setValues([ |
|
| 74 | + 'dbuser' => $this->dbUser, |
|
| 75 | + 'dbpassword' => $this->dbPassword, |
|
| 76 | + ]); |
|
| 77 | 77 | |
| 78 | - //create the database |
|
| 79 | - $this->createDatabase($connection); |
|
| 80 | - // the connection to dbname=postgres is not needed anymore |
|
| 81 | - $connection->close(); |
|
| 82 | - } catch (\Exception $e) { |
|
| 83 | - $this->logger->logException($e); |
|
| 84 | - $this->logger->warning('Error trying to connect as "postgres", assuming database is setup and tables need to be created'); |
|
| 85 | - $this->config->setValues([ |
|
| 86 | - 'dbuser' => $this->dbUser, |
|
| 87 | - 'dbpassword' => $this->dbPassword, |
|
| 88 | - ]); |
|
| 89 | - } |
|
| 78 | + //create the database |
|
| 79 | + $this->createDatabase($connection); |
|
| 80 | + // the connection to dbname=postgres is not needed anymore |
|
| 81 | + $connection->close(); |
|
| 82 | + } catch (\Exception $e) { |
|
| 83 | + $this->logger->logException($e); |
|
| 84 | + $this->logger->warning('Error trying to connect as "postgres", assuming database is setup and tables need to be created'); |
|
| 85 | + $this->config->setValues([ |
|
| 86 | + 'dbuser' => $this->dbUser, |
|
| 87 | + 'dbpassword' => $this->dbPassword, |
|
| 88 | + ]); |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - // connect to the database (dbname=$this->dbname) and check if it needs to be filled |
|
| 92 | - $this->dbUser = $this->config->getValue('dbuser'); |
|
| 93 | - $this->dbPassword = $this->config->getValue('dbpassword'); |
|
| 94 | - $connection = $this->connect(); |
|
| 95 | - try { |
|
| 96 | - $connection->connect(); |
|
| 97 | - } catch (\Exception $e) { |
|
| 98 | - $this->logger->logException($e); |
|
| 99 | - throw new \OC\DatabaseSetupException($this->trans->t('PostgreSQL username and/or password not valid'), |
|
| 100 | - $this->trans->t('You need to enter details of an existing account.')); |
|
| 101 | - } |
|
| 102 | - } |
|
| 91 | + // connect to the database (dbname=$this->dbname) and check if it needs to be filled |
|
| 92 | + $this->dbUser = $this->config->getValue('dbuser'); |
|
| 93 | + $this->dbPassword = $this->config->getValue('dbpassword'); |
|
| 94 | + $connection = $this->connect(); |
|
| 95 | + try { |
|
| 96 | + $connection->connect(); |
|
| 97 | + } catch (\Exception $e) { |
|
| 98 | + $this->logger->logException($e); |
|
| 99 | + throw new \OC\DatabaseSetupException($this->trans->t('PostgreSQL username and/or password not valid'), |
|
| 100 | + $this->trans->t('You need to enter details of an existing account.')); |
|
| 101 | + } |
|
| 102 | + } |
|
| 103 | 103 | |
| 104 | - private function createDatabase(IDBConnection $connection) { |
|
| 105 | - if (!$this->databaseExists($connection)) { |
|
| 106 | - //The database does not exists... let's create it |
|
| 107 | - $query = $connection->prepare("CREATE DATABASE " . addslashes($this->dbName) . " OWNER " . addslashes($this->dbUser)); |
|
| 108 | - try { |
|
| 109 | - $query->execute(); |
|
| 110 | - } catch (DatabaseException $e) { |
|
| 111 | - $this->logger->error('Error while trying to create database'); |
|
| 112 | - $this->logger->logException($e); |
|
| 113 | - } |
|
| 114 | - } else { |
|
| 115 | - $query = $connection->prepare("REVOKE ALL PRIVILEGES ON DATABASE " . addslashes($this->dbName) . " FROM PUBLIC"); |
|
| 116 | - try { |
|
| 117 | - $query->execute(); |
|
| 118 | - } catch (DatabaseException $e) { |
|
| 119 | - $this->logger->error('Error while trying to restrict database permissions'); |
|
| 120 | - $this->logger->logException($e); |
|
| 121 | - } |
|
| 122 | - } |
|
| 123 | - } |
|
| 104 | + private function createDatabase(IDBConnection $connection) { |
|
| 105 | + if (!$this->databaseExists($connection)) { |
|
| 106 | + //The database does not exists... let's create it |
|
| 107 | + $query = $connection->prepare("CREATE DATABASE " . addslashes($this->dbName) . " OWNER " . addslashes($this->dbUser)); |
|
| 108 | + try { |
|
| 109 | + $query->execute(); |
|
| 110 | + } catch (DatabaseException $e) { |
|
| 111 | + $this->logger->error('Error while trying to create database'); |
|
| 112 | + $this->logger->logException($e); |
|
| 113 | + } |
|
| 114 | + } else { |
|
| 115 | + $query = $connection->prepare("REVOKE ALL PRIVILEGES ON DATABASE " . addslashes($this->dbName) . " FROM PUBLIC"); |
|
| 116 | + try { |
|
| 117 | + $query->execute(); |
|
| 118 | + } catch (DatabaseException $e) { |
|
| 119 | + $this->logger->error('Error while trying to restrict database permissions'); |
|
| 120 | + $this->logger->logException($e); |
|
| 121 | + } |
|
| 122 | + } |
|
| 123 | + } |
|
| 124 | 124 | |
| 125 | - private function userExists(IDBConnection $connection) { |
|
| 126 | - $builder = $connection->getQueryBuilder(); |
|
| 127 | - $builder->automaticTablePrefix(false); |
|
| 128 | - $query = $builder->select('*') |
|
| 129 | - ->from('pg_roles') |
|
| 130 | - ->where($builder->expr()->eq('rolname', $builder->createNamedParameter($this->dbUser))); |
|
| 131 | - $result = $query->execute(); |
|
| 132 | - return $result->rowCount() > 0; |
|
| 133 | - } |
|
| 125 | + private function userExists(IDBConnection $connection) { |
|
| 126 | + $builder = $connection->getQueryBuilder(); |
|
| 127 | + $builder->automaticTablePrefix(false); |
|
| 128 | + $query = $builder->select('*') |
|
| 129 | + ->from('pg_roles') |
|
| 130 | + ->where($builder->expr()->eq('rolname', $builder->createNamedParameter($this->dbUser))); |
|
| 131 | + $result = $query->execute(); |
|
| 132 | + return $result->rowCount() > 0; |
|
| 133 | + } |
|
| 134 | 134 | |
| 135 | - private function databaseExists(IDBConnection $connection) { |
|
| 136 | - $builder = $connection->getQueryBuilder(); |
|
| 137 | - $builder->automaticTablePrefix(false); |
|
| 138 | - $query = $builder->select('datname') |
|
| 139 | - ->from('pg_database') |
|
| 140 | - ->where($builder->expr()->eq('datname', $builder->createNamedParameter($this->dbName))); |
|
| 141 | - $result = $query->execute(); |
|
| 142 | - return $result->rowCount() > 0; |
|
| 143 | - } |
|
| 135 | + private function databaseExists(IDBConnection $connection) { |
|
| 136 | + $builder = $connection->getQueryBuilder(); |
|
| 137 | + $builder->automaticTablePrefix(false); |
|
| 138 | + $query = $builder->select('datname') |
|
| 139 | + ->from('pg_database') |
|
| 140 | + ->where($builder->expr()->eq('datname', $builder->createNamedParameter($this->dbName))); |
|
| 141 | + $result = $query->execute(); |
|
| 142 | + return $result->rowCount() > 0; |
|
| 143 | + } |
|
| 144 | 144 | |
| 145 | - private function createDBUser(IDBConnection $connection) { |
|
| 146 | - $dbUser = $this->dbUser; |
|
| 147 | - try { |
|
| 148 | - $i = 1; |
|
| 149 | - while ($this->userExists($connection)) { |
|
| 150 | - $i++; |
|
| 151 | - $this->dbUser = $dbUser . $i; |
|
| 152 | - } |
|
| 145 | + private function createDBUser(IDBConnection $connection) { |
|
| 146 | + $dbUser = $this->dbUser; |
|
| 147 | + try { |
|
| 148 | + $i = 1; |
|
| 149 | + while ($this->userExists($connection)) { |
|
| 150 | + $i++; |
|
| 151 | + $this->dbUser = $dbUser . $i; |
|
| 152 | + } |
|
| 153 | 153 | |
| 154 | - // create the user |
|
| 155 | - $query = $connection->prepare("CREATE USER " . addslashes($this->dbUser) . " CREATEDB PASSWORD '" . addslashes($this->dbPassword) . "'"); |
|
| 156 | - $query->execute(); |
|
| 157 | - if ($this->databaseExists($connection)) { |
|
| 158 | - $query = $connection->prepare('GRANT CONNECT ON DATABASE ' . addslashes($this->dbName) . ' TO '.addslashes($this->dbUser)); |
|
| 159 | - $query->execute(); |
|
| 160 | - } |
|
| 161 | - } catch (DatabaseException $e) { |
|
| 162 | - $this->logger->error('Error while trying to create database user'); |
|
| 163 | - $this->logger->logException($e); |
|
| 164 | - } |
|
| 165 | - } |
|
| 154 | + // create the user |
|
| 155 | + $query = $connection->prepare("CREATE USER " . addslashes($this->dbUser) . " CREATEDB PASSWORD '" . addslashes($this->dbPassword) . "'"); |
|
| 156 | + $query->execute(); |
|
| 157 | + if ($this->databaseExists($connection)) { |
|
| 158 | + $query = $connection->prepare('GRANT CONNECT ON DATABASE ' . addslashes($this->dbName) . ' TO '.addslashes($this->dbUser)); |
|
| 159 | + $query->execute(); |
|
| 160 | + } |
|
| 161 | + } catch (DatabaseException $e) { |
|
| 162 | + $this->logger->error('Error while trying to create database user'); |
|
| 163 | + $this->logger->logException($e); |
|
| 164 | + } |
|
| 165 | + } |
|
| 166 | 166 | } |
@@ -63,9 +63,9 @@ discard block |
||
| 63 | 63 | //use the admin login data for the new database user |
| 64 | 64 | |
| 65 | 65 | //add prefix to the postgresql user name to prevent collisions |
| 66 | - $this->dbUser = 'oc_' . strtolower($username); |
|
| 66 | + $this->dbUser = 'oc_'.strtolower($username); |
|
| 67 | 67 | //create a new password so we don't need to store the admin config in the config file |
| 68 | - $this->dbPassword = \OC::$server->getSecureRandom()->generate(30, \OCP\Security\ISecureRandom::CHAR_LOWER . \OCP\Security\ISecureRandom::CHAR_DIGITS); |
|
| 68 | + $this->dbPassword = \OC::$server->getSecureRandom()->generate(30, \OCP\Security\ISecureRandom::CHAR_LOWER.\OCP\Security\ISecureRandom::CHAR_DIGITS); |
|
| 69 | 69 | |
| 70 | 70 | $this->createDBUser($connection); |
| 71 | 71 | } |
@@ -104,7 +104,7 @@ discard block |
||
| 104 | 104 | private function createDatabase(IDBConnection $connection) { |
| 105 | 105 | if (!$this->databaseExists($connection)) { |
| 106 | 106 | //The database does not exists... let's create it |
| 107 | - $query = $connection->prepare("CREATE DATABASE " . addslashes($this->dbName) . " OWNER " . addslashes($this->dbUser)); |
|
| 107 | + $query = $connection->prepare("CREATE DATABASE ".addslashes($this->dbName)." OWNER ".addslashes($this->dbUser)); |
|
| 108 | 108 | try { |
| 109 | 109 | $query->execute(); |
| 110 | 110 | } catch (DatabaseException $e) { |
@@ -112,7 +112,7 @@ discard block |
||
| 112 | 112 | $this->logger->logException($e); |
| 113 | 113 | } |
| 114 | 114 | } else { |
| 115 | - $query = $connection->prepare("REVOKE ALL PRIVILEGES ON DATABASE " . addslashes($this->dbName) . " FROM PUBLIC"); |
|
| 115 | + $query = $connection->prepare("REVOKE ALL PRIVILEGES ON DATABASE ".addslashes($this->dbName)." FROM PUBLIC"); |
|
| 116 | 116 | try { |
| 117 | 117 | $query->execute(); |
| 118 | 118 | } catch (DatabaseException $e) { |
@@ -148,14 +148,14 @@ discard block |
||
| 148 | 148 | $i = 1; |
| 149 | 149 | while ($this->userExists($connection)) { |
| 150 | 150 | $i++; |
| 151 | - $this->dbUser = $dbUser . $i; |
|
| 151 | + $this->dbUser = $dbUser.$i; |
|
| 152 | 152 | } |
| 153 | 153 | |
| 154 | 154 | // create the user |
| 155 | - $query = $connection->prepare("CREATE USER " . addslashes($this->dbUser) . " CREATEDB PASSWORD '" . addslashes($this->dbPassword) . "'"); |
|
| 155 | + $query = $connection->prepare("CREATE USER ".addslashes($this->dbUser)." CREATEDB PASSWORD '".addslashes($this->dbPassword)."'"); |
|
| 156 | 156 | $query->execute(); |
| 157 | 157 | if ($this->databaseExists($connection)) { |
| 158 | - $query = $connection->prepare('GRANT CONNECT ON DATABASE ' . addslashes($this->dbName) . ' TO '.addslashes($this->dbUser)); |
|
| 158 | + $query = $connection->prepare('GRANT CONNECT ON DATABASE '.addslashes($this->dbName).' TO '.addslashes($this->dbUser)); |
|
| 159 | 159 | $query->execute(); |
| 160 | 160 | } |
| 161 | 161 | } catch (DatabaseException $e) { |