Skip to content

Commit

Permalink
Merge pull request #979 from benbz/fix-legacy-migration
Browse files Browse the repository at this point in the history
Fixes for migrating from legacy version
  • Loading branch information
ByteHamster authored Oct 18, 2020
2 parents 7f9c2ff + 377f608 commit ef80df9
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 32 deletions.
4 changes: 3 additions & 1 deletion Core/Frameworks/Baikal/Model/Config/Standard.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class Standard extends \Baikal\Model\Config {
"cal_enabled" => true,
"dav_auth_type" => "Digest",
"admin_passwordhash" => "",
// While not editable as will change admin & any existing user passwords,
// could be set to different value when migrating from legacy config
"auth_realm" => "BaikalDAV",
"base_uri" => ""
];
Expand Down Expand Up @@ -119,7 +121,7 @@ function set($sProp, $sValue) {
if ($sProp === "admin_passwordhash" && $sValue !== "") {
parent::set(
"admin_passwordhash",
\BaikalAdmin\Core\Auth::hashAdminPassword($sValue)
\BaikalAdmin\Core\Auth::hashAdminPassword($sValue, $this->aData["auth_realm"])
);
}

Expand Down
28 changes: 14 additions & 14 deletions Core/Frameworks/BaikalAdmin/Controller/Install/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@

namespace BaikalAdmin\Controller\Install;

use Symfony\Component\Yaml\Yaml;

class Database extends \Flake\Core\Controller {
protected $aMessages = [];
protected $oModel;
Expand All @@ -46,12 +44,6 @@ function execute() {
$this->oModel->set('mysql_username', PROJECT_DB_MYSQL_USERNAME);
$this->oModel->set('mysql_password', PROJECT_DB_MYSQL_PASSWORD);
$this->oModel->set('encryption_key', BAIKAL_ENCRYPTION_KEY);

if (defined("BAIKAL_CONFIGURED_VERSION")) {
$oStandardConfig = new \Baikal\Model\Config\Standard();
$oStandardConfig->set("configured_version", BAIKAL_CONFIGURED_VERSION);
$oStandardConfig->persist();
}
}

$this->oForm = $this->oModel->formForThisModelInstance([
Expand All @@ -68,6 +60,18 @@ function execute() {
@unlink(PROJECT_PATH_SPECIFIC . "config.system.php");
}
touch(PROJECT_PATH_SPECIFIC . '/INSTALL_DISABLED');

if (defined("BAIKAL_CONFIGURED_VERSION")) {
$oStandardConfig = new \Baikal\Model\Config\Standard();
$oStandardConfig->set("configured_version", BAIKAL_CONFIGURED_VERSION);
$oStandardConfig->persist();

# We've just rolled back the configured version, so reload so that we get to the
# version upgrade page rather than the database is configured message in render below
$sLink = PROJECT_URI . "admin/install/?/database";
\Flake\Util\Tools::redirect($sLink);
exit(0);
}
}
}
}
Expand Down Expand Up @@ -209,12 +213,8 @@ function hideMySQLFieldWhenNeeded(\Formal\Form $oForm, \Formal\Form\Morphology $
if ($oForm->submitted()) {
$bMySQL = (intval($oForm->postValue("mysql")) === 1);
} else {
try {
$configSystem = Yaml::parseFile(PROJECT_PATH_CONFIG . "baikal.yaml");
} catch (\Exception $e) {
error_log('Error reading baikal.yaml file : ' . $e->getMessage());
}
$bMySQL = $configSystem['database']['mysql'] ?? true;
// oMorpho won't have the values from the model set on it yet
$bMySQL = $this->oModel->get("mysql");
}

if ($bMySQL === true) {
Expand Down
4 changes: 4 additions & 0 deletions Core/Frameworks/BaikalAdmin/Controller/Install/Initialize.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ function execute() {
$this->oModel->set('invite_from', defined("BAIKAL_INVITE_FROM") ? BAIKAL_INVITE_FROM : "");
$this->oModel->set('dav_auth_type', BAIKAL_DAV_AUTH_TYPE);
}
if (file_exists(PROJECT_PATH_SPECIFIC . "config.system.php")) {
require_once PROJECT_PATH_SPECIFIC . "config.system.php";
$this->oModel->set('auth_realm', BAIKAL_AUTH_REALM);
}

$this->oForm = $this->oModel->formForThisModelInstance([
"close" => false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function render() {
HTML;

try {
$bSuccess = $this->upgrade($config['system']['configured_version'], BAIKAL_VERSION);
$bSuccess = $this->upgrade($config['database'], $config['system']['configured_version'], BAIKAL_VERSION);
} catch (\Exception $e) {
$bSuccess = false;
$this->aErrors[] = 'Uncaught exception during upgrade: ' . (string) $e;
Expand All @@ -88,7 +88,7 @@ function render() {
return $sHtml;
}

protected function upgrade($sVersionFrom, $sVersionTo) {
protected function upgrade($databaseConfig, $sVersionFrom, $sVersionTo) {
if (version_compare($sVersionFrom, '0.2.3', '<=')) {
throw new \Exception('This version of Baikal does not support upgrading from version 0.2.3 and older. Please request help on Github if this is a problem.');
}
Expand All @@ -99,7 +99,7 @@ protected function upgrade($sVersionFrom, $sVersionTo) {
if (version_compare($sVersionFrom, '0.3.0', '<')) {
// Upgrading from sabre/dav 1.8 schema to 3.1 schema.

if (defined("PROJECT_DB_MYSQL") && PROJECT_DB_MYSQL === true) {
if ($databaseConfig['mysql'] === true) {
// MySQL upgrade

// sabre/dav 2.0 changes
Expand Down Expand Up @@ -313,7 +313,7 @@ protected function upgrade($sVersionFrom, $sVersionTo) {
// The sqlite schema had issues with both the calendar and
// addressbooks tables. The tables didn't have a DEFAULT '1' for
// the synctoken column. So we're adding it now.
if (!defined("PROJECT_DB_MYSQL") || PROJECT_DB_MYSQL === false) {
if ($databaseConfig['mysql'] === false) {
$pdo->exec('UPDATE calendars SET synctoken = 1 WHERE synctoken IS NULL');

$tmpTable = '_' . time();
Expand Down Expand Up @@ -343,7 +343,7 @@ protected function upgrade($sVersionFrom, $sVersionTo) {
// Similar to upgrading from older than 0.4.5, there were still
// issues with a missing DEFAULT 1 for sthe synctoken field in the
// addressbook.
if (!defined("PROJECT_DB_MYSQL") || PROJECT_DB_MYSQL === false) {
if ($databaseConfig['mysql'] === false) {
$pdo->exec('UPDATE addressbooks SET synctoken = 1 WHERE synctoken IS NULL');

$tmpTable = '_' . time();
Expand All @@ -365,7 +365,7 @@ protected function upgrade($sVersionFrom, $sVersionTo) {
}
}
if (version_compare($sVersionFrom, '0.5.1', '<')) {
if (!defined("PROJECT_DB_MYSQL") || PROJECT_DB_MYSQL === false) {
if ($databaseConfig['mysql'] === false) {
$pdo->exec(<<<SQL
CREATE TABLE calendarinstances (
id integer primary key asc NOT NULL,
Expand Down
15 changes: 4 additions & 11 deletions Core/Frameworks/BaikalAdmin/Core/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@ static function authenticate() {
$sUser = \Flake\Util\Tools::POST("login");
$sPass = \Flake\Util\Tools::POST("password");

$sPassHash = self::hashAdminPassword($sPass);
try {
$config = Yaml::parseFile(PROJECT_PATH_CONFIG . "baikal.yaml");
} catch (\Exception $e) {
error_log('Error reading baikal.yaml file : ' . $e->getMessage());

return false;
}
$sPassHash = self::hashAdminPassword($sPass, $config['system']['auth_realm']);
if ($sUser === "admin" && $sPassHash === $config['system']['admin_passwordhash']) {
$_SESSION["baikaladminauth"] = md5($config['system']['admin_passwordhash']);

Expand All @@ -67,16 +69,7 @@ static function unAuthenticate() {
unset($_SESSION["baikaladminauth"]);
}

static function hashAdminPassword($sPassword) {
try {
$config = Yaml::parseFile(PROJECT_PATH_CONFIG . "baikal.yaml");
} catch (\Exception $e) {
error_log('Error reading baikal.yaml file : ' . $e->getMessage());
}

# Fallback to default value; useful when initializing App, as all constants are not set yet
$sAuthRealm = $config['system']['auth_realm'] ?? "BaikalDAV";

static function hashAdminPassword($sPassword, $sAuthRealm) {
return hash('sha256', 'admin:' . $sAuthRealm . ':' . $sPassword);
}
}

0 comments on commit ef80df9

Please sign in to comment.