Skip to content

Commit

Permalink
Moving mysql setup code over to Doctrine
Browse files Browse the repository at this point in the history
  • Loading branch information
DeepDiver1975 committed Jul 29, 2015
1 parent aff11d7 commit 114f128
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 95 deletions.
2 changes: 1 addition & 1 deletion lib/private/setup.php
Expand Up @@ -249,7 +249,7 @@ public function install($options) {

$class = self::$dbSetupClasses[$dbType];
/** @var \OC\Setup\AbstractDatabase $dbSetup */
$dbSetup = new $class($l, 'db_structure.xml');
$dbSetup = new $class($l, 'db_structure.xml', $this->config);
$error = array_merge($error, $dbSetup->validate($options));

// validate the data directory
Expand Down
37 changes: 23 additions & 14 deletions lib/private/setup/abstractdatabase.php
Expand Up @@ -22,22 +22,31 @@
*/
namespace OC\Setup;

use OCP\IConfig;

abstract class AbstractDatabase {

/**
* @var \OC_L10N
*/
/** @var \OC_L10N */
protected $trans;
/** @var string */
protected $dbDefinitionFile;
protected $dbuser;
protected $dbpassword;
protected $dbname;
protected $dbhost;
protected $tableprefix;
/** @var string */
protected $dbUser;
/** @var string */
protected $dbPassword;
/** @var string */
protected $dbName;
/** @var string */
protected $dbHost;
/** @var string */
protected $tablePrefix;
/** @var IConfig */
protected $config;

public function __construct($trans, $dbDefinitionFile) {
public function __construct($trans, $dbDefinitionFile, IConfig $config) {
$this->trans = $trans;
$this->dbDefinitionFile = $dbDefinitionFile;
$this->config = $config;
}

public function validate($config) {
Expand Down Expand Up @@ -67,11 +76,11 @@ public function initialize($config) {
'dbtableprefix' => $dbTablePrefix,
]);

$this->dbuser = $dbUser;
$this->dbpassword = $dbPass;
$this->dbname = $dbName;
$this->dbhost = $dbHost;
$this->tableprefix = $dbTablePrefix;
$this->dbUser = $dbUser;
$this->dbPassword = $dbPass;
$this->dbName = $dbName;
$this->dbHost = $dbHost;
$this->tablePrefix = $dbTablePrefix;
}

abstract public function setupDatabase($userName);
Expand Down
87 changes: 46 additions & 41 deletions lib/private/setup/mysql.php
Expand Up @@ -23,40 +23,38 @@
*/
namespace OC\Setup;

use OC\DB\ConnectionFactory;

class MySQL extends AbstractDatabase {
public $dbprettyname = 'MySQL/MariaDB';

public function setupDatabase($username) {
//check if the database user has admin right
$connection = @mysql_connect($this->dbhost, $this->dbuser, $this->dbpassword);
if(!$connection) {
throw new \OC\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.'));
}
$connection = $this->connect();
//user already specified in config
$oldUser=\OC_Config::getValue('dbuser', false);

//we don't have a dbuser specified in config
if($this->dbuser!=$oldUser) {
if($this->dbUser!=$oldUser) {
//add prefix to the admin username to prevent collisions
$adminUser=substr('oc_'.$username, 0, 16);

$i = 1;
while(true) {
//this should be enough to check for admin rights in mysql
$query="SELECT user FROM mysql.user WHERE user='$adminUser'";

$result = mysql_query($query, $connection);
$result = $connection->executeQuery($query);

//current dbuser has admin rights
if($result) {
$data = $result->fetchAll();
//new dbuser does not exist
if(mysql_num_rows($result) === 0) {
if(count($data) === 0) {
//use the admin login data for the new database user
$this->dbuser=$adminUser;
$this->dbUser=$adminUser;

//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->dbPassword=\OC_Util::generateRandomBytes(30);

$this->createDBUser($connection);

Expand All @@ -73,8 +71,8 @@ public function setupDatabase($username) {
};

\OC_Config::setValues([
'dbuser' => $this->dbuser,
'dbpassword' => $this->dbpassword,
'dbuser' => $this->dbUser,
'dbpassword' => $this->dbPassword,
]);
}

Expand All @@ -83,50 +81,57 @@ public function setupDatabase($username) {

//fill the database if needed
$query='select count(*) from information_schema.tables'
." where table_schema='".$this->dbname."' AND table_name = '".$this->tableprefix."users';";
$result = mysql_query($query, $connection);
if($result) {
$row=mysql_fetch_row($result);
}
." where table_schema='".$this->dbName."' AND table_name = '".$this->tablePrefix."users';";
$result = $connection->executeQuery($query);
$row = $result->fetch();
if(!$result or $row[0]==0) {
\OC_DB::createDbFromStructure($this->dbDefinitionFile);
}
mysql_close($connection);
}

/**
* @param \OC\DB\Connection $connection
*/
private function createDatabase($connection) {
$name = $this->dbname;
$user = $this->dbuser;
$name = $this->dbName;
$user = $this->dbUser;
//we cant use OC_BD functions here because we need to connect as the administrative user.
$query = "CREATE DATABASE IF NOT EXISTS `$name` CHARACTER SET utf8 COLLATE utf8_bin;";
$result = mysql_query($query, $connection);
if(!$result) {
$entry = $this->trans->t('DB Error: "%s"', array(mysql_error($connection))) . '<br />';
$entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />';
\OCP\Util::writeLog('setup.mysql', $entry, \OCP\Util::WARN);
}
$query="GRANT ALL PRIVILEGES ON `$name` . * TO '$user'";
$connection->executeUpdate($query);

//this query will fail if there aren't the right permissions, ignore the error
mysql_query($query, $connection);
$query="GRANT ALL PRIVILEGES ON `$name` . * TO '$user'";
$connection->executeUpdate($query);
}

/**
* @param \OC\DB\Connection $connection
* @throws \OC\DatabaseSetupException
*/
private function createDBUser($connection) {
$name = $this->dbuser;
$password = $this->dbpassword;
$name = $this->dbUser;
$password = $this->dbPassword;
// we need to create 2 accounts, one for global use and one for local user. if we don't specify the local one,
// 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 \OC\DatabaseSetupException($this->trans->t("MySQL/MariaDB user '%s'@'localhost' exists already.", array($name)),
$this->trans->t("Drop this user from MySQL/MariaDB", array($name)));
}
$connection->executeUpdate($query);
$query = "CREATE USER '$name'@'%' IDENTIFIED BY '$password'";
$result = mysql_query($query, $connection);
if (!$result) {
throw new \OC\DatabaseSetupException($this->trans->t("MySQL/MariaDB user '%s'@'%%' already exists", array($name)),
$this->trans->t("Drop this user from MySQL/MariaDB."));
}
$connection->executeUpdate($query);
}

/**
* @return \OC\DB\Connection
* @throws \OC\DatabaseSetupException
*/
private function connect() {
$type = 'mysql';
$connectionParams = array(
'host' => $this->dbHost,
'user' => $this->dbUser,
'password' => $this->dbPassword,
'tablePrefix' => $this->tablePrefix,
);
$cf = new ConnectionFactory();
return $cf->getConnection($type, $connectionParams);
}
}
38 changes: 19 additions & 19 deletions lib/private/setup/oci.php
Expand Up @@ -38,10 +38,10 @@ public function initialize($config) {
$this->dbtablespace = 'USERS';
}
// allow empty hostname for oracle
$this->dbhost = $config['dbhost'];
$this->dbHost = $config['dbhost'];

\OC_Config::setValues([
'dbhost' => $this->dbhost,
'dbhost' => $this->dbHost,
'dbtablespace' => $this->dbtablespace,
]);
}
Expand All @@ -58,16 +58,16 @@ public function validate($config) {
}

public function setupDatabase($username) {
$e_host = addslashes($this->dbhost);
$e_dbname = addslashes($this->dbname);
$e_host = addslashes($this->dbHost);
$e_dbname = addslashes($this->dbName);
//check if the database user has admin right
if ($e_host == '') {
$easy_connect_string = $e_dbname; // use dbname as easy connect name
} else {
$easy_connect_string = '//'.$e_host.'/'.$e_dbname;
}
\OCP\Util::writeLog('setup oracle', 'connect string: ' . $easy_connect_string, \OCP\Util::DEBUG);
$connection = @oci_connect($this->dbuser, $this->dbpassword, $easy_connect_string);
$connection = @oci_connect($this->dbUser, $this->dbPassword, $easy_connect_string);
if(!$connection) {
$errorMessage = $this->getLastError();
if ($errorMessage) {
Expand Down Expand Up @@ -103,23 +103,23 @@ public function setupDatabase($username) {
//use the admin login data for the new database user

//add prefix to the oracle user name to prevent collisions
$this->dbuser='oc_'.$username;
$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_Util::generateRandomBytes(30);
$this->dbPassword=\OC_Util::generateRandomBytes(30);

//oracle passwords are treated as identifiers:
// must start with alphanumeric char
// needs to be shortened to 30 bytes, as the two " needed to escape the identifier count towards the identifier length.
$this->dbpassword=substr($this->dbpassword, 0, 30);
$this->dbPassword=substr($this->dbPassword, 0, 30);

$this->createDBUser($connection);
}
}

\OC_Config::setValues([
'dbuser' => $this->dbuser,
'dbname' => $this->dbname,
'dbpassword' => $this->dbpassword,
'dbuser' => $this->dbUser,
'dbname' => $this->dbName,
'dbpassword' => $this->dbPassword,
]);

//create the database not necessary, oracle implies user = schema
Expand All @@ -131,26 +131,26 @@ public function setupDatabase($username) {
oci_close($connection);

// connect to the oracle database (schema=$this->dbuser) an check if the schema needs to be filled
$this->dbuser = \OC_Config::getValue('dbuser');
$this->dbUser = \OC_Config::getValue('dbuser');
//$this->dbname = \OC_Config::getValue('dbname');
$this->dbpassword = \OC_Config::getValue('dbpassword');
$this->dbPassword = \OC_Config::getValue('dbpassword');

$e_host = addslashes($this->dbhost);
$e_dbname = addslashes($this->dbname);
$e_host = addslashes($this->dbHost);
$e_dbname = addslashes($this->dbName);

if ($e_host == '') {
$easy_connect_string = $e_dbname; // use dbname as easy connect name
} else {
$easy_connect_string = '//'.$e_host.'/'.$e_dbname;
}
$connection = @oci_connect($this->dbuser, $this->dbpassword, $easy_connect_string);
$connection = @oci_connect($this->dbUser, $this->dbPassword, $easy_connect_string);
if(!$connection) {
throw new \OC\DatabaseSetupException($this->trans->t('Oracle username and/or password not valid'),
$this->trans->t('You need to enter either an existing account or the administrator.'));
}
$query = "SELECT count(*) FROM user_tables WHERE table_name = :un";
$stmt = oci_parse($connection, $query);
$un = $this->tableprefix.'users';
$un = $this->tablePrefix.'users';
oci_bind_by_name($stmt, ':un', $un);
if (!$stmt) {
$entry = $this->trans->t('DB Error: "%s"', array($this->getLastError($connection))) . '<br />';
Expand All @@ -171,8 +171,8 @@ public function setupDatabase($username) {
* @param resource $connection
*/
private function createDBUser($connection) {
$name = $this->dbuser;
$password = $this->dbpassword;
$name = $this->dbUser;
$password = $this->dbPassword;
$query = "SELECT * FROM all_users WHERE USERNAME = :un";
$stmt = oci_parse($connection, $query);
if (!$stmt) {
Expand Down

0 comments on commit 114f128

Please sign in to comment.