openrat-cms

# OpenRat Content Management System
git clone http://git.code.weiherhei.de/openrat-cms.git
Log | Files | Refs

commit f5dc11a93645cdbc7fba1726e0b5cffd2dacacae
parent 2bb3098c3277fad1aeba8c188b93b616f7cde085
Author: Jan Dankert <develop@jandankert.de>
Date:   Sat, 26 Oct 2019 01:21:45 +0200

Create database connection in dispatcher, for guests too.

Diffstat:
modules/cms-core/Dispatcher.class.php | 44+++++++++++++++++++++++++++++++++++++-------
modules/cms-core/action/LoginAction.class.php | 20+++-----------------
2 files changed, 40 insertions(+), 24 deletions(-)

diff --git a/modules/cms-core/Dispatcher.class.php b/modules/cms-core/Dispatcher.class.php @@ -23,6 +23,7 @@ use SecurityException; use Session; use Spyc; use template_engine\components\ElseComponent; +use Text; /** @@ -349,18 +350,43 @@ class Dispatcher */ private function connectToDatabase() { + $firstDbContact = ! Session::getDatabaseId() || $this->request->hasRequestVar('dbid'); + if ( $this->request->hasRequestVar('dbid') ) $dbid = $this->request->getRequestVar('dbid',OR_FILTER_ALPHANUM); elseif ( Session::getDatabaseId() ) $dbid = Session::getDatabaseId(); elseif ( isset($_COOKIE['or_dbid']) ) - $dbid = $_COOKIE['or_dbid']; + $dbid = $_COOKIE['or_dbid']; else { - //throw new LogicException('No DBID available'); - // some actions do not need a database - // f.e. the login dialog. - // so this is NOT an error. - return; + $dbids = array(); + + $databases = Conf()->get('database'); + + if ( !is_array($databases)) + throw new Exception\RuntimeException('Corrupt configuration: Database configuration must be a list'); + + foreach( $databases as $key => $dbconf ) + { + if ( !is_array($dbconf)) + throw new \LogicException("Corrupt configuration: Database configuration '".$key."' must be an array.'"); + + $dbconf += config('database-default','defaults'); // Add Default-Values + + if ( is_array($dbconf) && $dbconf['enabled'] ) // Database-Connection is enabled + $dbids[] = $key; + } + + $defaultDbId = config('database-default','default-id'); + if ( $defaultDbId && in_array($defaultDbId,$dbids) ) + // Default-Datenbankverbindung ist konfiguriert und vorhanden. + $dbid = $defaultDbId; + elseif ( count($dbids) > 0) + // Datenbankverbindungen sind vorhanden, wir nehmen die erste. + $dbid = $dbids[0]; + else + // Keine Datenbankverbindung vorhanden. Fallback: + throw new Exception\RuntimeException('No database configured'); } @@ -371,6 +397,9 @@ class Dispatcher $dbConfig = $dbConfig->subset($dbid ); + if ( ! $dbConfig->is('enabled' ) ) + throw new Exception\RuntimeException('Database connection \''.$dbid.'\' is not enabled'); + try { $key = $this->request->isAction?'write':'read'; @@ -386,7 +415,8 @@ class Dispatcher } - if ( $this->request->hasRequestVar('dbid') ) + if ( $firstDbContact ) + // Test, if we should update the database schema. $this->updateDatabase( $dbid ); } diff --git a/modules/cms-core/action/LoginAction.class.php b/modules/cms-core/action/LoginAction.class.php @@ -297,7 +297,7 @@ class LoginAction extends Action if ( is_array($dbconf) && $dbconf['enabled'] ) // Database-Connection is enabled $dbids[$dbid] = array( 'key' => $dbid, - 'value' => empty($dbconf['name']) ? $dbid : Text::maxLength($dbconf['name']), + 'value' => !$dbconf['name'] ? $dbid : Text::maxLength($dbconf['name']), 'title' => $dbconf['description'] ); } @@ -317,23 +317,9 @@ class LoginAction extends Action $this->setTemplateVar( 'dbids',$dbids ); - // Vorausgewählte Datenbank-Id ermiteln + // Database was already connected in the Dispatcher. So we MUST have a db connection here. $db = Session::getDatabase(); - if ( is_object($db) ) - // Datenbankverbindung ist noch in Sitzung, diese verwenden. - $this->setTemplateVar('dbid',$db->id); - elseif ( isset($_COOKIE['or_dbid']) && isset($dbids[$_COOKIE['or_dbid']]) ) - // DB-Id aus dem Cookie lesen. - $this->setTemplateVar('dbid',$_COOKIE['or_dbid'] ); - elseif ( ! empty($conf['database-default']['default-id']) && isset($dbids[$conf['database-default']['default-id']])) - // Default-Datenbankverbindung ist konfiguriert und vorhanden. - $this->setTemplateVar('dbid',$conf['database-default']['default-id']); - elseif ( count($dbids) > 0) - // Datenbankverbindungen sind vorhanden, wir nehmen die erste. - $this->setTemplateVar('dbid',array_keys($dbids)[0]); - else - // Keine Datenbankverbindung vorhanden. Fallback: - $this->setTemplateVar('dbid',''); + $this->setTemplateVar('dbid',$db->id); // Den Benutzernamen aus dem Client-Zertifikat lesen und in die Loginmaske eintragen.