Skip to content
Snippets Groups Projects
Commit 8baa8970 authored by Thijs Kinkhorst's avatar Thijs Kinkhorst
Browse files

Merge branch 'feature/pdo-options-support' of...

Merge branch 'feature/pdo-options-support' of https://github.com/ElijahLynn/simplesamlphp into ElijahLynn-feature/pdo-options-support
parents cbe0a548 057526fd
No related branches found
No related tags found
No related merge requests found
<?php
/*
/*
* The configuration of SimpleSAMLphp
*
*
*/
$config = array(
......@@ -60,7 +60,7 @@ $config = array(
* - 'temdir': Saving temporary files. SimpleSAMLphp will attempt to create
* this directory if it doesn't exist.
* When specified as a relative path, this is relative to the SimpleSAMLphp
* root directory.
* root directory.
*/
'certdir' => 'cert/',
'loggingdir' => 'log/',
......@@ -377,6 +377,7 @@ $config = array(
*/
'database.username' => 'simplesamlphp',
'database.password' => 'secret',
'database.options' => array(),
/*
* (Optional) Table prefix
......
......@@ -253,9 +253,10 @@ The class follows:
*/
private $dsn;
/* The database username & password. */
/* The database username, password & options. */
private $username;
private $password;
private $options;
public function __construct($info, $config) {
parent::__construct($info, $config);
......@@ -272,6 +273,12 @@ The class follows:
throw new Exception('Missing or invalid password option in config.');
}
$this->password = $config['password'];
if (isset($config['options']) {
if (!is_array($config['options])) {
throw new Exception('Missing or invalid options option in config.');
}
$this->options = $config['options'];
}
}
/**
......@@ -294,7 +301,7 @@ The class follows:
protected function login($username, $password) {
/* Connect to the database. */
$db = new PDO($this->dsn, $this->username, $this->password);
$db = new PDO($this->dsn, $this->username, $this->password, $this->options);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
/* Ensure that we are operating with UTF-8 encoding.
......
......@@ -55,9 +55,10 @@ class SQL extends Store
$dsn = $config->getString('store.sql.dsn');
$username = $config->getString('store.sql.username', null);
$password = $config->getString('store.sql.password', null);
$options = $config->getArray('store.sql.options', null);
$this->prefix = $config->getString('store.sql.prefix', 'simpleSAMLphp');
$this->pdo = new \PDO($dsn, $username, $password);
$this->pdo = new \PDO($dsn, $username, $password, $options);
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$this->driver = $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME);
......
......@@ -37,6 +37,11 @@ class sspmod_consent_Consent_Store_Database extends sspmod_consent_Store
*/
private $_password;
/**
* Options for the database;
*/
private $_options;
/**
* Table with consent.
*/
......@@ -98,6 +103,14 @@ class sspmod_consent_Consent_Store_Database extends sspmod_consent_Store
$this->_password = null;
}
if (array_key_exists('options', $config)) {
if (!is_array($config['options'])) {
throw new Exception('consent:Database - \'options\' is supposed to be an array.');
}
$this->_options = $config['options'];
} else {
$this->_options = null;
}
if (array_key_exists('table', $config)) {
if (!is_string($config['table'])) {
throw new Exception('consent:Database - \'table\' is supposed to be a string.');
......@@ -380,7 +393,7 @@ class sspmod_consent_Consent_Store_Database extends sspmod_consent_Store
// Get total number of consents
$st = $this->_execute('SELECT COUNT(*) AS no FROM '.$this->_table, array());
if ($st === false) {
return array();
}
......@@ -395,7 +408,7 @@ class sspmod_consent_Consent_Store_Database extends sspmod_consent_Store
'FROM (SELECT DISTINCT hashed_user_id FROM '.$this->_table.' ) AS foo',
array()
);
if ($st === false) {
return array();
}
......@@ -409,7 +422,7 @@ class sspmod_consent_Consent_Store_Database extends sspmod_consent_Store
'SELECT COUNT(*) AS no FROM (SELECT DISTINCT service_id FROM '.$this->_table.') AS foo',
array()
);
if ($st === false) {
return array();
}
......@@ -437,8 +450,13 @@ class sspmod_consent_Consent_Store_Database extends sspmod_consent_Store
if (isset($this->_timeout)) {
$driver_options[PDO::ATTR_TIMEOUT] = $this->_timeout;
}
if (isset($this->_options)) {
$this->_options = array_merge($driver_options, $this->_options);
} else {
$this->_options = $driver_options;
}
$this->_db = new PDO($this->_dsn, $this->_username, $this->_password, $driver_options);
$this->_db = new PDO($this->_dsn, $this->_username, $this->_password, $this->_options);
return $this->_db;
}
......
......@@ -26,6 +26,11 @@ class sspmod_sqlauth_Auth_Source_SQL extends sspmod_core_Auth_UserPassBase
*/
private $password;
/**
* The options that we should connect to the database with.
*/
private $options;
/**
* The query we should use to retrieve the attributes for the user.
*
......@@ -66,6 +71,9 @@ class sspmod_sqlauth_Auth_Source_SQL extends sspmod_core_Auth_UserPassBase
$this->username = $config['username'];
$this->password = $config['password'];
$this->query = $config['query'];
if (isset($config['options'])) {
$this->options = $config['options'];
}
}
......@@ -77,7 +85,7 @@ class sspmod_sqlauth_Auth_Source_SQL extends sspmod_core_Auth_UserPassBase
private function connect()
{
try {
$db = new PDO($this->dsn, $this->username, $this->password);
$db = new PDO($this->dsn, $this->username, $this->password, $this->options);
} catch (PDOException $e) {
throw new Exception('sqlauth:' . $this->authId . ': - Failed to connect to \'' .
$this->dsn . '\': '. $e->getMessage());
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment