From fd7d0023a38cf4fa11c32733a70ca93d8c932d9f Mon Sep 17 00:00:00 2001 From: nuxsmin Date: Fri, 24 Feb 2017 14:11:52 +0100 Subject: [PATCH] * [MOD] Improved upgrading process. Orphaned items would be created if IDs are not set. --- inc/SP/Core/Upgrade/Category.class.php | 44 ++++++++----- inc/SP/Core/Upgrade/Customer.class.php | 45 ++++++++----- inc/SP/Core/Upgrade/Group.class.php | 41 +++++++----- inc/SP/Core/Upgrade/Profile.class.php | 36 +++++++---- inc/SP/Core/Upgrade/Upgrade.class.php | 10 +-- inc/SP/Core/Upgrade/User.class.php | 63 +++++++++++++++---- inc/sql/20117022101.sql | 3 +- inc/sql/dbstructure.sql | 18 +++--- .../material-blue/views/main/upgrade.inc | 22 ++++--- 9 files changed, 193 insertions(+), 89 deletions(-) diff --git a/inc/SP/Core/Upgrade/Category.class.php b/inc/SP/Core/Upgrade/Category.class.php index a3d32b03..cc312452 100644 --- a/inc/SP/Core/Upgrade/Category.class.php +++ b/inc/SP/Core/Upgrade/Category.class.php @@ -43,29 +43,24 @@ class Category */ public static function fixCategoriesId($categoryId) { - $Data = new QueryData(); - $Data->setQuery('SELECT category_id FROM categories ORDER BY category_id'); - - $categories = DB::getResultsArray($Data); - - $paramsIn = trim(str_repeat(',?', count($categories)), ','); - $Data->addParam($categoryId); - - foreach ($categories as $category) { - $Data->addParam($category->category_id); - } - try { DB::beginTransaction(); + if ($categoryId === 0) { + $categoryId = self::createOrphanCategory(); + } + + $Data = new QueryData(); + $Data->addParam($categoryId); + $query = /** @lang SQL */ - 'UPDATE accHistory SET acchistory_categoryId = ? WHERE acchistory_categoryId NOT IN (' . $paramsIn . ') OR acchistory_categoryId IS NULL'; + 'UPDATE accHistory SET acchistory_categoryId = ? WHERE acchistory_categoryId NOT IN (SELECT category_id FROM categories ORDER BY category_id) OR acchistory_categoryId IS NULL'; $Data->setQuery($query); DB::getQuery($Data); $query = /** @lang SQL */ - 'UPDATE accounts SET account_categoryId = ? WHERE account_categoryId NOT IN (' . $paramsIn . ') OR account_categoryId IS NULL'; + 'UPDATE accounts SET account_categoryId = ? WHERE account_categoryId NOT IN (SELECT category_id FROM categories ORDER BY category_id) OR account_categoryId IS NULL'; $Data->setQuery($query); DB::getQuery($Data); @@ -79,4 +74,25 @@ class Category return false; } } + + /** + * Crear una categoría para elementos huérfanos + * + * @return int + */ + public static function createOrphanCategory() + { + $query = /** @lang SQL */ + 'INSERT INTO categories SET + category_name = \'Orphan category\', + category_description = \'Created by the upgrade process\''; + + $Data = new QueryData(); + $Data->setQuery($query); + $Data->setOnErrorMessage(__('Error al crear la categoría', false)); + + DB::getQuery($Data); + + return DB::getLastId(); + } } \ 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 78fbad87..403e4c56 100644 --- a/inc/SP/Core/Upgrade/Customer.class.php +++ b/inc/SP/Core/Upgrade/Customer.class.php @@ -43,29 +43,24 @@ class Customer */ public static function fixCustomerId($customerId) { - $Data = new QueryData(); - $Data->setQuery('SELECT customer_id FROM customers ORDER BY customer_id'); - - $customers = DB::getResultsArray($Data); - - $paramsIn = trim(str_repeat(',?', count($customers)), ','); - $Data->addParam($customerId); - - foreach ($customers as $customer) { - $Data->addParam($customer->customer_id); - } - try { DB::beginTransaction(); + if ($customerId === 0) { + $customerId = self::createOrphanCustomer(); + } + + $Data = new QueryData(); + $Data->addParam($customerId); + $query = /** @lang SQL */ - 'UPDATE accHistory SET acchistory_customerId = ? WHERE acchistory_customerId NOT IN (' . $paramsIn . ') OR acchistory_customerId IS NULL'; + 'UPDATE accHistory SET acchistory_customerId = ? WHERE acchistory_customerId NOT IN (SELECT customer_id FROM customers ORDER BY customer_id) OR acchistory_customerId IS NULL'; $Data->setQuery($query); DB::getQuery($Data); $query = /** @lang SQL */ - 'UPDATE accounts SET account_customerId = ? WHERE account_customerId NOT IN (' . $paramsIn . ') OR account_customerId IS NULL'; + 'UPDATE accounts SET account_customerId = ? WHERE account_customerId NOT IN (SELECT customer_id FROM customers ORDER BY customer_id) OR account_customerId IS NULL'; $Data->setQuery($query); DB::getQuery($Data); @@ -79,4 +74,26 @@ class Customer return false; } } + + /** + * Crear un cliente para elementos huérfanos + * + * @return int + */ + public static function createOrphanCustomer() + { + $query = /** @lang SQL */ + 'INSERT INTO customers SET + customer_name = \'Orphan customer\', + customer_hash = MD5(\'Orphan customer\'), + customer_description = \'Created by the upgrade process\''; + + $Data = new QueryData(); + $Data->setQuery($query); + $Data->setOnErrorMessage(__('Error al crear el cliente', false)); + + DB::getQuery($Data); + + return DB::getLastId(); + } } \ 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 979d432f..954fcb5c 100644 --- a/inc/SP/Core/Upgrade/Group.class.php +++ b/inc/SP/Core/Upgrade/Group.class.php @@ -41,30 +41,22 @@ class Group */ public static function fixGroupId($groupId) { - $Data = new QueryData(); - $Data->setQuery('SELECT usergroup_id FROM usrGroups ORDER BY usergroup_id'); - - $groups = DB::getResultsArray($Data); - - $paramsIn = trim(str_repeat(',?', count($groups)), ','); - $Data->addParam($groupId); - - foreach ($groups as $group) { - $Data->addParam($group->usergroup_id); - } - try { DB::beginTransaction(); + $Data = new QueryData(); + $query = /** @lang SQL */ - 'UPDATE usrData SET user_groupId = ? WHERE user_groupId NOT IN (' . $paramsIn . ') OR user_groupId IS NULL'; + 'UPDATE usrData SET user_groupId = ? WHERE user_groupId NOT IN (SELECT usergroup_id FROM usrGroups ORDER BY usergroup_id) OR user_groupId IS NULL'; $Data->setQuery($query); + $Data->addParam($groupId); DB::getQuery($Data); $query = /** @lang SQL */ - 'DELETE FROM usrToGroups WHERE usertogroup_groupId <> ? AND usertogroup_groupId NOT IN (' . $paramsIn . ') OR usertogroup_groupId IS NULL'; + 'DELETE FROM usrToGroups WHERE usertogroup_groupId NOT IN (SELECT usergroup_id FROM usrGroups ORDER BY usergroup_id) OR usertogroup_groupId IS NULL'; $Data->setQuery($query); + $Data->setParams([]); DB::getQuery($Data); @@ -77,4 +69,25 @@ class Group return false; } } + + /** + * Crear un grupo para elementos huérfanos + * + * @return int + */ + public static function createOrphanGroup() + { + $query = /** @lang SQL */ + 'INSERT INTO usrGroups SET + usergroup_name = \'Orphan group\', + usergroup_description = \'Created by the upgrade process\''; + + $Data = new QueryData(); + $Data->setQuery($query); + $Data->setOnErrorMessage(__('Error al crear el grupo', false)); + + DB::getQuery($Data); + + return DB::getLastId(); + } } \ 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 af8ab20c..1c4cf396 100644 --- a/inc/SP/Core/Upgrade/Profile.class.php +++ b/inc/SP/Core/Upgrade/Profile.class.php @@ -25,6 +25,7 @@ namespace SP\Core\Upgrade; use SP\Core\Exceptions\SPException; +use SP\DataModel\ProfileData; use SP\Storage\DB; use SP\Storage\QueryData; @@ -44,23 +45,14 @@ class Profile public static function fixProfilesId($profileId) { $Data = new QueryData(); - $Data->setQuery('SELECT userprofile_id FROM usrProfiles ORDER BY userprofile_id'); - - $profiles = DB::getResultsArray($Data); - - $paramsIn = trim(str_repeat(',?', count($profiles)), ','); - $Data->addParam($profileId); - - foreach ($profiles as $profile) { - $Data->addParam($profile->userprofile_id); - } try { DB::beginTransaction(); $query = /** @lang SQL */ - 'UPDATE usrData SET user_profileId = ? WHERE user_profileId NOT IN (' . $paramsIn . ') OR user_profileId IS NULL'; + 'UPDATE usrData SET user_profileId = ? WHERE user_profileId NOT IN (SELECT userprofile_id FROM usrProfiles ORDER BY userprofile_id) OR user_profileId IS NULL'; $Data->setQuery($query); + $Data->addParam($profileId); DB::getQuery($Data); @@ -73,4 +65,26 @@ class Profile return false; } } + + /** + * Crear un perfil para elementos huérfanos + * + * @return int + */ + public static function createOrphanProfile() + { + $query = /** @lang SQL */ + 'INSERT INTO usrProfiles SET + userprofile_name = \'Orphan profile\', + userProfile_profile = ?'; + + $Data = new QueryData(); + $Data->setQuery($query); + $Data->addParam(serialize(new ProfileData())); + $Data->setOnErrorMessage(__('Error al crear perfil', false)); + + DB::getQuery($Data); + + return DB::getLastId(); + } } \ 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 2080ce58..5ba9c113 100644 --- a/inc/SP/Core/Upgrade/Upgrade.class.php +++ b/inc/SP/Core/Upgrade/Upgrade.class.php @@ -115,11 +115,11 @@ class Upgrade case 1316100601: return 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)) - && Customer::fixCustomerId(Request::analyze('customerid', 1)); + && UserUpgrade::fixUsersId(Request::analyze('userid', 0)) + && Group::fixGroupId(Request::analyze('groupid', 0)) + && Profile::fixProfilesId(Request::analyze('profileid', 0)) + && Category::fixCategoriesId(Request::analyze('categoryid', 0)) + && Customer::fixCustomerId(Request::analyze('customerid', 0)); } return true; diff --git a/inc/SP/Core/Upgrade/User.class.php b/inc/SP/Core/Upgrade/User.class.php index 8d32b010..b02a7485 100644 --- a/inc/SP/Core/Upgrade/User.class.php +++ b/inc/SP/Core/Upgrade/User.class.php @@ -47,21 +47,28 @@ class User */ public static function fixUsersId($userId) { - $Data = new QueryData(); - $Data->setQuery('SELECT user_id FROM usrData ORDER BY user_id'); - - $users = DB::getResultsArray($Data); - - $paramsIn = trim(str_repeat(',?', count($users)), ','); - $Data->addParam($userId); - - foreach ($users as $user) { - $Data->addParam($user->user_id); - } - try { DB::beginTransaction(); + $Data = new QueryData(); + $Data->setQuery('SELECT user_id FROM usrData ORDER BY user_id'); + + $users = DB::getResultsArray($Data); + + $paramsIn = trim(str_repeat(',?', count($users)), ','); + + if ($userId === 0) { + $groupId = Group::createOrphanGroup(); + $profileId = Profile::createOrphanProfile(); + $userId = self::createOrphanUser($groupId, $profileId); + } + + $Data->addParam($userId); + + foreach ($users as $user) { + $Data->addParam($user->user_id); + } + $query = /** @lang SQL */ 'UPDATE accounts SET account_userId = ? WHERE account_userId NOT IN (' . $paramsIn . ') OR account_userId IS NULL '; $Data->setQuery($query); @@ -114,6 +121,38 @@ class User } } + /** + * Crear un usuario para elementos huérfanos + * + * @param $groupId + * @param $profileId + * @return int + */ + public static function createOrphanUser($groupId, $profileId) + { + $query = /** @lang SQL */ + 'INSERT INTO usrData SET + user_name = \'Orphan User\', + user_login = \'orphan_user\', + user_notes = \'Created by the upgrade process\', + user_groupId = ?, + user_profileId = ?, + user_mIV = \'\', + user_isDisabled = 1, + user_pass = \'\', + user_hashSalt = \'\''; + + $Data = new QueryData(); + $Data->setQuery($query); + $Data->addParam($groupId); + $Data->addParam($profileId); + $Data->setOnErrorMessage(__('Error al crear el usuario', false)); + + DB::getQuery($Data); + + return DB::getLastId(); + } + /** * Actualizar la clave maestra * diff --git a/inc/sql/20117022101.sql b/inc/sql/20117022101.sql index cef5896c..9f6ad3c8 100644 --- a/inc/sql/20117022101.sql +++ b/inc/sql/20117022101.sql @@ -1,5 +1,6 @@ ALTER TABLE `accounts` - CHANGE COLUMN `account_IV` `account_key` VARBINARY(1000) NOT NULL ; + CHANGE COLUMN `account_pass` `account_pass` VARBINARY(1000) NOT NULL, + 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` diff --git a/inc/sql/dbstructure.sql b/inc/sql/dbstructure.sql index 7b1f4b5c..37acb7a3 100644 --- a/inc/sql/dbstructure.sql +++ b/inc/sql/dbstructure.sql @@ -65,9 +65,9 @@ CREATE TABLE `usrData` ( `user_groupId` smallint(3) unsigned NOT NULL, `user_secGroupId` smallint(3) unsigned DEFAULT NULL, `user_login` varchar(50) NOT NULL, - `user_pass` varbinary(255) NOT NULL, - `user_mPass` varbinary(255) DEFAULT NULL, - `user_mIV` varbinary(32) NOT NULL, + `user_pass` varbinary(1000) NOT NULL, + `user_mPass` varbinary(1000) DEFAULT NULL, + `user_mKey` varbinary(1000) NOT NULL, `user_email` varchar(80) DEFAULT NULL, `user_notes` text, `user_count` int(10) unsigned NOT NULL DEFAULT '0', @@ -106,8 +106,8 @@ CREATE TABLE `accounts` ( `account_categoryId` smallint(5) unsigned NOT NULL, `account_login` varchar(50) DEFAULT NULL, `account_url` varchar(255) DEFAULT NULL, - `account_pass` varbinary(255) NOT NULL, - `account_IV` varbinary(32) NOT NULL, + `account_pass` varbinary(1000) NOT NULL, + `account_key` varbinary(1000) NOT NULL, `account_notes` text, `account_countView` int(10) unsigned NOT NULL DEFAULT '0', `account_countDecrypt` int(10) unsigned NOT NULL DEFAULT '0', @@ -191,8 +191,8 @@ CREATE TABLE `accHistory` ( `acchistory_categoryId` smallint(5) unsigned NOT NULL, `acchistory_login` varchar(50) NOT NULL, `acchistory_url` varchar(255) DEFAULT NULL, - `acchistory_pass` varbinary(255) NOT NULL, - `acchistory_IV` varbinary(32) NOT NULL, + `acchistory_pass` varbinary(500) NOT NULL, + `acchistory_key` varbinary(1000) NOT NULL, `acchistory_notes` text NOT NULL, `acchistory_countView` int(10) unsigned NOT NULL DEFAULT '0', `acchistory_countDecrypt` int(10) unsigned NOT NULL DEFAULT '0', @@ -271,6 +271,8 @@ CREATE TABLE `authTokens` ( `authtoken_actionId` smallint(5) unsigned NOT NULL, `authtoken_createdBy` smallint(5) unsigned NOT NULL, `authtoken_startDate` int(10) unsigned NOT NULL, + `authtoken_vault` varbinary(2000) NULL, + `authtoken_hash` varbinary(1000) NULL, PRIMARY KEY (`authtoken_id`), UNIQUE KEY `unique_authtoken_id` (`authtoken_id`), KEY `IDX_checkToken` (`authtoken_userId`,`authtoken_actionId`,`authtoken_token`), @@ -300,7 +302,7 @@ CREATE TABLE `customFieldsData` ( `customfielddata_itemId` int(10) unsigned NOT NULL, `customfielddata_defId` int(10) unsigned NOT NULL, `customfielddata_data` longblob, - `customfielddata_iv` varbinary(128) DEFAULT NULL, + `customfielddata_key` varbinary(1000) DEFAULT NULL, PRIMARY KEY (`customfielddata_id`), KEY `IDX_DEFID` (`customfielddata_defId`), KEY `IDX_DELETE` (`customfielddata_itemId`,`customfielddata_moduleId`), diff --git a/inc/themes/material-blue/views/main/upgrade.inc b/inc/themes/material-blue/views/main/upgrade.inc index 3e21d111..a26b0ea4 100644 --- a/inc/themes/material-blue/views/main/upgrade.inc +++ b/inc/themes/material-blue/views/main/upgrade.inc @@ -33,6 +33,8 @@
  • getIconWarning()->getIcon(); ?> +
    +
  • @@ -50,9 +52,9 @@ || $checkConstraints->accountshistory_useredit > 0 ): ?>
    - + autocomplete="off" min="0" max="1000" value="0">
    @@ -62,9 +64,9 @@ || $checkConstraints->accountshistory_category > 0 ): ?>
    - + autocomplete="off" min="0" max="1000" value="0">
    @@ -74,9 +76,9 @@ || $checkConstraints->accountshistory_customer > 0 ): ?>
    - + autocomplete="off" min="0" max="1000" value="0">
    @@ -84,9 +86,9 @@ users_group > 0): ?>
    - + autocomplete="off" min="0" max="1000" value="0">
    @@ -94,9 +96,9 @@ users_profile > 0): ?>
    - + autocomplete="off" min="0" max="1000" value="0">