server/lib/private/Setup/PostgreSQL.php

158 lines
5.5 KiB
PHP
Raw Normal View History

<?php
2015-03-26 10:44:34 +00:00
/**
2016-07-21 15:07:57 +00:00
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
2015-03-26 10:44:34 +00:00
* @author Bart Visscher <bartv@thisnet.nl>
* @author eduardo <eduardo@vnexu.net>
2016-07-21 15:07:57 +00:00
* @author Joas Schilling <coding@schilljs.com>
2016-01-12 14:02:16 +00:00
* @author Morris Jobke <hey@morrisjobke.de>
2016-07-21 16:13:36 +00:00
* @author Robin Appelman <robin@icewind.nl>
2016-07-21 15:07:57 +00:00
* @author Roeland Jago Douma <roeland@famdouma.nl>
2015-10-05 18:54:56 +00:00
* @author Thomas Müller <thomas.mueller@tmit.eu>
2015-03-26 10:44:34 +00:00
*
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OC\Setup;
2016-07-12 11:50:54 +00:00
use OC\DatabaseException;
use OC\DB\QueryBuilder\Literal;
use OCP\IDBConnection;
2013-04-02 20:01:23 +00:00
class PostgreSQL extends AbstractDatabase {
public $dbprettyname = 'PostgreSQL';
2013-04-02 20:01:23 +00:00
public function setupDatabase($username) {
2016-07-12 11:50:54 +00:00
$connection = $this->connect([
'dbname' => 'postgres'
]);
//check for roles creation rights in postgresql
$builder = $connection->getQueryBuilder();
$builder->automaticTablePrefix(false);
$query = $builder
->select('rolname')
->from('pg_roles')
->where($builder->expr()->eq('rolcreaterole', new Literal('TRUE')))
->andWhere($builder->expr()->eq('rolname', $builder->createNamedParameter($this->dbUser)));
try {
$result = $query->execute();
$canCreateRoles = $result->rowCount() > 0;
} catch (DatabaseException $e) {
$canCreateRoles = false;
}
2016-07-12 11:50:54 +00:00
if($canCreateRoles) {
//use the admin login data for the new database user
//add prefix to the postgresql user name to prevent collisions
$this->dbUser='oc_'.$username;
//create a new password so we don't need to store the admin config in the config file
$this->dbPassword = \OC::$server->getSecureRandom()->generate(30, \OCP\Security\ISecureRandom::CHAR_LOWER.\OCP\Security\ISecureRandom::CHAR_DIGITS);
2013-04-02 20:01:23 +00:00
$this->createDBUser($connection);
}
2016-07-12 11:50:54 +00:00
$systemConfig = $this->config->getSystemConfig();
2015-12-03 13:35:15 +00:00
$systemConfig->setValues([
'dbuser' => $this->dbUser,
'dbpassword' => $this->dbPassword,
]);
//create the database
$this->createDatabase($connection);
2016-07-12 11:50:54 +00:00
$query = $connection->prepare("select count(*) FROM pg_class WHERE relname=? limit 1");
$query->execute([$this->tablePrefix . "users"]);
$tablesSetup = $query->fetchColumn() > 0;
// the connection to dbname=postgres is not needed anymore
2016-07-12 11:50:54 +00:00
$connection->close();
2013-04-02 20:01:23 +00:00
// connect to the ownCloud database (dbname=$this->dbname) and check if it needs to be filled
2015-12-03 13:35:15 +00:00
$this->dbUser = $systemConfig->getValue('dbuser');
$this->dbPassword = $systemConfig->getValue('dbpassword');
2016-07-12 11:50:54 +00:00
$connection = $this->connect();
try {
$connection->connect();
} catch (\Exception $e) {
$this->logger->logException($e);
throw new \OC\DatabaseSetupException($this->trans->t('PostgreSQL username and/or password not valid'),
2016-07-12 11:50:54 +00:00
$this->trans->t('You need to enter either an existing account or the administrator.'));
}
2016-07-12 11:50:54 +00:00
if(!$tablesSetup) {
2013-06-27 18:19:51 +00:00
\OC_DB::createDbFromStructure($this->dbDefinitionFile);
}
}
2016-07-12 11:50:54 +00:00
private function createDatabase(IDBConnection $connection) {
if(!$this->databaseExists($connection)) {
//The database does not exists... let's create it
2016-07-12 11:50:54 +00:00
$query = $connection->prepare("CREATE DATABASE " . addslashes($this->dbName) . " OWNER " . addslashes($this->dbUser));
try {
$query->execute();
} catch (DatabaseException $e) {
$this->logger->error('Error while trying to create database');
$this->logger->logException($e);
}
2016-07-12 11:50:54 +00:00
} else {
$query = $connection->prepare("REVOKE ALL PRIVILEGES ON DATABASE " . addslashes($this->dbName) . " FROM PUBLIC");
try {
$query->execute();
} catch (DatabaseException $e) {
$this->logger->error('Error while trying to restrict database permissions');
$this->logger->logException($e);
}
}
}
2016-07-12 11:50:54 +00:00
private function userExists(IDBConnection $connection) {
$builder = $connection->getQueryBuilder();
$builder->automaticTablePrefix(false);
$query = $builder->select('*')
->from('pg_roles')
->where($builder->expr()->eq('rolname', $builder->createNamedParameter($this->dbUser)));
$result = $query->execute();
return $result->rowCount() > 0;
}
2016-07-12 11:50:54 +00:00
private function databaseExists(IDBConnection $connection) {
$builder = $connection->getQueryBuilder();
$builder->automaticTablePrefix(false);
$query = $builder->select('datname')
->from('pg_database')
->where($builder->expr()->eq('datname', $builder->createNamedParameter($this->dbName)));
$result = $query->execute();
return $result->rowCount() > 0;
}
private function createDBUser(IDBConnection $connection) {
try {
if ($this->userExists($connection)) {
2016-07-12 11:50:54 +00:00
// change the password
$query = $connection->prepare("ALTER ROLE " . addslashes($this->dbUser) . " CREATEDB WITH PASSWORD '" . addslashes($this->dbPassword) . "'");
2016-07-12 11:50:54 +00:00
} else {
// create the user
$query = $connection->prepare("CREATE USER " . addslashes($this->dbUser) . " CREATEDB PASSWORD '" . addslashes($this->dbPassword) . "'");
}
2016-07-12 11:50:54 +00:00
$query->execute();
} catch (DatabaseException $e) {
$this->logger->error('Error while trying to create database user');
$this->logger->logException($e);
}
}
}