chg: [AppController] move the database connection setup to a dedicated function on the AppComponent

This removes a bit of clutter from the already large beforeFilter
method and allows other views to resuse the logic without having to
duplicate it.
pull/5187/head
Andreas Rammhold 2019-05-20 10:57:21 +02:00 committed by Chris Halls
parent 013b3ac619
commit 96311ef480
1 changed files with 20 additions and 11 deletions

View File

@ -124,17 +124,8 @@ class AppController extends Controller
if (!empty($this->params['named']['sql'])) {
$this->sql_dump = 1;
}
// check for a supported datasource configuration
$dataSourceConfig = ConnectionManager::getDataSource('default')->config;
if (!isset($dataSourceConfig['encoding'])) {
$db = ConnectionManager::getDataSource('default');
$db->setConfig(array('encoding' => 'utf8'));
ConnectionManager::create('default', $db->config);
}
$dataSource = $dataSourceConfig['datasource'];
if ($dataSource != 'Database/Mysql' && $dataSource != 'Database/Postgres') {
throw new Exception('datasource not supported: ' . $dataSource);
}
$this->_setupDatabaseConnection();
$this->set('ajax', $this->request->is('ajax'));
$this->set('queryVersion', $this->__queryVersion);
@ -506,6 +497,24 @@ class AppController extends Controller
$this->render('/Servers/json/simple');
}
/*
* Setup & validate the database connection configuration
* @throws Exception if the configured database is not supported.
*/
protected function _setupDatabaseConnection() {
// check for a supported datasource configuration
$dataSourceConfig = ConnectionManager::getDataSource('default')->config;
if (!isset($dataSourceConfig['encoding'])) {
$db = ConnectionManager::getDataSource('default');
$db->setConfig(array('encoding' => 'utf8'));
ConnectionManager::create('default', $db->config);
}
$dataSource = $dataSourceConfig['datasource'];
if ($dataSource != 'Database/Mysql' && $dataSource != 'Database/Postgres') {
throw new Exception('datasource not supported: ' . $dataSource);
}
}
/*
* Sanitize the configured `MISP.baseurl` and expose it to the view as `baseurl`.
*/