diff --git a/inc/SP/Controller/MainActionController.class.php b/inc/SP/Controller/MainActionController.class.php index b60c7bae..0e13b5ea 100644 --- a/inc/SP/Controller/MainActionController.class.php +++ b/inc/SP/Controller/MainActionController.class.php @@ -58,8 +58,6 @@ class MainActionController && $hash === Config::getConfig()->getUpgradeKey() ) { $this->upgrade($dbVersion, 'db'); - - ConfigDB::setValue('version', implode(Util::getVersion(true))); } else { $controller = new MainController(); $controller->getUpgrade($dbVersion); @@ -103,7 +101,7 @@ class MainActionController private function upgrade($version, $type) { try { - Upgrade::doUpgrade($version, $type); + Upgrade::doUpgrade($version); $Config = Config::getConfig(); $Config->setMaintenance(false); diff --git a/inc/SP/Core/Installer.class.php b/inc/SP/Core/Installer.class.php index 0e007c71..3f522877 100644 --- a/inc/SP/Core/Installer.class.php +++ b/inc/SP/Core/Installer.class.php @@ -444,59 +444,43 @@ class Installer */ private function createAdminAccount() { - $GroupData = new GroupData(); - $GroupData->setUsergroupName('Admins'); - $GroupData->setUsergroupDescription('sysPass Admins'); - try { + $GroupData = new GroupData(); + $GroupData->setUsergroupName('Admins'); + $GroupData->setUsergroupDescription('sysPass Admins'); + Group::getItem($GroupData)->add(); - } catch (SPException $e) { - $this->rollback(); - throw new SPException(SPException::SP_CRITICAL, - __('Error al crear el grupo "admin"', false), - __('Informe al desarrollador', false)); - } - $ProfileData = new ProfileData(); - $ProfileData->setUserprofileName('Admin'); + $ProfileData = new ProfileData(); + $ProfileData->setUserprofileName('Admin'); - try { Profile::getItem($ProfileData)->add(); - } catch (SPException $e) { - $this->rollback(); - throw new SPException(SPException::SP_CRITICAL, - __('Error al crear el perfil "admin"', false), - __('Informe al desarrollador', false)); - } - // Datos del usuario - $UserData = new UserData(); - $UserData->setUserGroupId($GroupData->getUsergroupId()); - $UserData->setUserProfileId($ProfileData->getUserprofileId()); - $UserData->setUserLogin($this->InstallData->getAdminLogin()); - $UserData->setUserPass($this->InstallData->getAdminPass()); - $UserData->setUserName('Admin'); - $UserData->setUserIsAdminApp(1); + // Datos del usuario + $UserData = new UserData(); + $UserData->setUserGroupId($GroupData->getUsergroupId()); + $UserData->setUserProfileId($ProfileData->getUserprofileId()); + $UserData->setUserLogin($this->InstallData->getAdminLogin()); + $UserData->setUserPass($this->InstallData->getAdminPass()); + $UserData->setUserName('Admin'); + $UserData->setUserIsAdminApp(1); - try { User::getItem($UserData)->add(); + + // Guardar el hash de la clave maestra + ConfigDB::setCacheConfigValue('masterPwd', Hash::hashKey($this->InstallData->getMasterPassword())); + ConfigDB::setCacheConfigValue('lastupdatempass', time()); + ConfigDB::writeConfig(true); + + if (!UserPass::getItem($UserData)->updateUserMPass($this->InstallData->getMasterPassword())) { + throw new SPException(SPException::SP_CRITICAL, + __('Error al actualizar la clave maestra del usuario "admin"', false)); + } } catch (SPException $e) { $this->rollback(); - throw new SPException(SPException::SP_CRITICAL, - __('Error al crear el usuario "admin"', false), - __('Informe al desarrollador', false)); - } - - // Guardar el hash de la clave maestra - ConfigDB::setCacheConfigValue('masterPwd', Hash::hashKey($this->InstallData->getMasterPassword())); - ConfigDB::setCacheConfigValue('lastupdatempass', time()); - ConfigDB::writeConfig(true); - - if (!UserPass::getItem($UserData)->updateUserMPass($this->InstallData->getMasterPassword())) { - $this->rollback(); throw new SPException(SPException::SP_CRITICAL, - __('Error al actualizar la clave maestra del usuario "admin"', false), + $e->getMessage(), __('Informe al desarrollador', false)); } } diff --git a/inc/SP/Core/Upgrade/Category.class.php b/inc/SP/Core/Upgrade/Category.class.php index 9e97f103..a3d32b03 100644 --- a/inc/SP/Core/Upgrade/Category.class.php +++ b/inc/SP/Core/Upgrade/Category.class.php @@ -71,12 +71,12 @@ class Category DB::getQuery($Data); DB::endTransaction(); + + return true; } catch (SPException $e) { DB::rollbackTransaction(); return false; } - - return true; } } \ No newline at end of file diff --git a/inc/SP/Core/Upgrade/Customer.class.php b/inc/SP/Core/Upgrade/Customer.class.php index 78363878..78fbad87 100644 --- a/inc/SP/Core/Upgrade/Customer.class.php +++ b/inc/SP/Core/Upgrade/Customer.class.php @@ -71,12 +71,12 @@ class Customer DB::getQuery($Data); DB::endTransaction(); + + return true; } catch (SPException $e) { DB::rollbackTransaction(); return false; } - - return true; } } \ No newline at end of file diff --git a/inc/SP/Core/Upgrade/Group.class.php b/inc/SP/Core/Upgrade/Group.class.php index 0da1b93f..487104ad 100644 --- a/inc/SP/Core/Upgrade/Group.class.php +++ b/inc/SP/Core/Upgrade/Group.class.php @@ -63,12 +63,12 @@ class Group DB::getQuery($Data); DB::endTransaction(); + + return true; } catch (SPException $e) { DB::rollbackTransaction(); return false; } - - return true; } } \ No newline at end of file diff --git a/inc/SP/Core/Upgrade/Profile.class.php b/inc/SP/Core/Upgrade/Profile.class.php index 0a297019..af8ab20c 100644 --- a/inc/SP/Core/Upgrade/Profile.class.php +++ b/inc/SP/Core/Upgrade/Profile.class.php @@ -65,12 +65,12 @@ class Profile DB::getQuery($Data); DB::endTransaction(); + + return true; } catch (SPException $e) { DB::rollbackTransaction(); return false; } - - return true; } } \ No newline at end of file diff --git a/inc/SP/Core/Upgrade/Upgrade.class.php b/inc/SP/Core/Upgrade/Upgrade.class.php index 35e11126..0e70a3f4 100644 --- a/inc/SP/Core/Upgrade/Upgrade.class.php +++ b/inc/SP/Core/Upgrade/Upgrade.class.php @@ -52,7 +52,7 @@ defined('APP_ROOT') || die(); */ class Upgrade { - private static $dbUpgrade = [110, 1121, 1122, 1123, 11213, 11219, 11220, 12001, 12002, 1316011001, 1316020501, 1316100601, 20017011302, 20017011701, 20017012901, 20117021901]; + private static $dbUpgrade = [110, 1121, 1122, 1123, 11213, 11219, 11220, 12001, 12002, 1316011001, 1316100601, 20017011302, 20017011701, 20017012901, 20117021901]; private static $cfgUpgrade = [1124, 1316020501, 20017011202]; private static $auxUpgrade = [12001, 12002, 20017010901, 20017011202]; private static $appUpgrade = [20117021901]; @@ -61,34 +61,32 @@ class Upgrade * Inicia el proceso de actualización de la BBDD. * * @param int $version con la versión de la BBDD actual - * @param $type * @return bool * @throws SPException */ - public static function doUpgrade($version, $type) + public static function doUpgrade($version) { - if ($type === 'db') { - foreach (self::$dbUpgrade as $upgradeVersion) { - if ($version < $upgradeVersion) { - if (self::auxPreDbUpgrade($upgradeVersion) === false) { - throw new SPException(SPException::SP_CRITICAL, - __('Error al aplicar la actualización auxiliar', false), - __('Compruebe el registro de eventos para más detalles', false)); - } - - if (self::upgradeDB($upgradeVersion) === false) { - throw new SPException(SPException::SP_CRITICAL, __('Error al aplicar la actualización de la Base de Datos', false), - __('Compruebe el registro de eventos para más detalles', false)); - } - } - } - } elseif ($type === 'app') { - foreach (self::$appUpgrade as $upgradeVersion) { - if ($version < $upgradeVersion && self::appUpgrades($upgradeVersion) === false) { + foreach (self::$dbUpgrade as $upgradeVersion) { + if ($version < $upgradeVersion) { + if (self::auxPreDbUpgrade($upgradeVersion) === false) { throw new SPException(SPException::SP_CRITICAL, - __('Error al aplicar la actualización de la aplicación', false), + __('Error al aplicar la actualización auxiliar', false), __('Compruebe el registro de eventos para más detalles', false)); } + + if (self::upgradeDB($upgradeVersion) === false) { + throw new SPException(SPException::SP_CRITICAL, + __('Error al aplicar la actualización de la Base de Datos', false), + __('Compruebe el registro de eventos para más detalles', false)); + } + } + } + + foreach (self::$appUpgrade as $upgradeVersion) { + if ($version < $upgradeVersion && self::appUpgrades($upgradeVersion) === false) { + throw new SPException(SPException::SP_CRITICAL, + __('Error al aplicar la actualización de la aplicación', false), + __('Compruebe el registro de eventos para más detalles', false)); } } @@ -113,8 +111,11 @@ class Upgrade { switch ($version) { case 1316011001: + return self::upgradeDB(1300000000); + case 1316100601: return - UserUpgrade::fixUsersId(Request::analyze('userid', 1)) + Account::fixAccountsId() + && UserUpgrade::fixUsersId(Request::analyze('userid', 1)) && Group::fixGroupId(Request::analyze('groupid', 1)) && Profile::fixProfilesId(Request::analyze('profileid', 1)) && Category::fixCategoriesId(Request::analyze('categoryid', 1)) @@ -139,7 +140,7 @@ class Upgrade $queries = self::getQueriesFromFile($version); - if (count($queries) === 0) { + if (count($queries) === 0 || (int)ConfigDB::getValue('version') === $version) { $LogMessage->addDescription(__('No es necesario actualizar la Base de Datos.', false)); $Log->writeLog(); return true; @@ -162,6 +163,8 @@ class Upgrade } } + ConfigDB::setValue('version', $version); + $LogMessage->addDescription(__('Actualización de la Base de Datos realizada correctamente.', false)); $Log->writeLog(); @@ -210,9 +213,7 @@ class Upgrade $databaseVersion = (int)str_replace('.', '', ConfigDB::getValue('version')); if ($databaseVersion < $version) { - if (self::upgradeDB($version)) { - ConfigDB::setValue('version', $version); - } else { + if (!self::upgradeDB($version)) { $dbResult = false; } } diff --git a/inc/SP/Core/Upgrade/User.class.php b/inc/SP/Core/Upgrade/User.class.php index 8d0fbba6..f73345f0 100644 --- a/inc/SP/Core/Upgrade/User.class.php +++ b/inc/SP/Core/Upgrade/User.class.php @@ -93,14 +93,26 @@ class User DB::getQuery($Data); + $query = /** @lang SQL */ + 'DELETE FROM usrToGroups WHERE usertogroup_userId <> ? AND usertogroup_userId NOT IN (' . $paramsIn . ') OR usertogroup_userId IS NULL'; + $Data->setQuery($query); + + DB::getQuery($Data); + + $query = /** @lang SQL */ + 'DELETE FROM accUsers WHERE accuser_userId <> ? AND accuser_userId NOT IN (' . $paramsIn . ') OR accuser_userId IS NULL'; + $Data->setQuery($query); + + DB::getQuery($Data); + DB::endTransaction(); + + return true; } catch (SPException $e) { DB::rollbackTransaction(); return false; } - - return true; } /** diff --git a/inc/sql/20117021901.sql b/inc/sql/20117021901.sql index 4bcf2368..5986b296 100644 --- a/inc/sql/20117021901.sql +++ b/inc/sql/20117021901.sql @@ -1,5 +1,10 @@ -ALTER TABLE `accounts` CHANGE COLUMN `account_IV` `account_key` VARBINARY(500) NOT NULL ; -ALTER TABLE `accHistory` CHANGE COLUMN `acchistory_IV` `acchistory_key` VARBINARY(500) NOT NULL ; -ALTER TABLE `customFieldsData` CHANGE COLUMN `customfielddata_iv` `customfielddata_key` VARBINARY(500) NOT NULL; -ALTER TABLE `usrData` CHANGE COLUMN `user_mPass` `user_mKey` VARBINARY(500) NULL DEFAULT NULL, CHANGE COLUMN `user_mIV` `user_mKey` VARBINARY(500) NULL DEFAULT NULL; +ALTER TABLE `accounts` + CHANGE COLUMN `account_IV` `account_key` VARBINARY(1000) NOT NULL ; +ALTER TABLE `accHistory` + CHANGE COLUMN `acchistory_IV` `acchistory_key` VARBINARY(1000) NOT NULL ; +ALTER TABLE `customFieldsData` + CHANGE COLUMN `customfielddata_iv` `customfielddata_key` VARBINARY(1000) NOT NULL; +ALTER TABLE `usrData` + CHANGE COLUMN `user_mPass` `user_mPass` VARBINARY(1000) NULL DEFAULT NULL, + CHANGE COLUMN `user_mIV` `user_mKey` VARBINARY(1000) NULL DEFAULT NULL;