Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixed issue #17123: Store missing state of encrypted fields for archi…
…ved tables
  • Loading branch information
ptelu committed May 6, 2021
1 parent 03ac560 commit a326ac0
Show file tree
Hide file tree
Showing 11 changed files with 425 additions and 82 deletions.
2 changes: 1 addition & 1 deletion application/config/version.php
Expand Up @@ -12,7 +12,7 @@
*/

$config['versionnumber'] = '4.6.0';
$config['dbversionnumber'] = 445;
$config['dbversionnumber'] = 446;
$config['buildnumber'] = '';
$config['updatable'] = true;
$config['templateapiversion'] = 3;
Expand Down
32 changes: 31 additions & 1 deletion application/controllers/SurveyAdministrationController.php
Expand Up @@ -1508,7 +1508,10 @@ public function actionDeactivate()
}

$survey = Survey::model()->findByPk($iSurveyID);
$date = date('YmdHis'); //'His' adds 24hours+minutes to name to allow multiple deactiviations in a day
$datestamp = time();
$date = date('YmdHis', $datestamp); //'His' adds 24hours+minutes to name to allow multiple deactiviations in a day
$DBDate = date('Y-m-d H:i:s', $datestamp);
$userID = Yii::app()->user->getId();
$aData = array();

$aData['aSurveysettings'] = getSurveyInfo($iSurveyID);
Expand Down Expand Up @@ -1558,6 +1561,15 @@ public function actionDeactivate()

Yii::app()->db->createCommand()->renameTable($toldtable, $tnewtable);

$archivedTokenSettings = new ArchivedTableSettings();
$archivedTokenSettings->survey_id = $iSurveyID;
$archivedTokenSettings->user_id = $userID;
$archivedTokenSettings->tbl_name = "old_tokens_{$iSurveyID}_{$date}";
$archivedTokenSettings->tbl_type = 'token';
$archivedTokenSettings->created = $DBDate;
$archivedTokenSettings->properties = $aData['aSurveysettings']['tokenencryptionoptions'];
$archivedTokenSettings->save();

$aData['tnewtable'] = $tnewtable;
$aData['toldtable'] = $toldtable;
}
Expand Down Expand Up @@ -1592,6 +1604,15 @@ public function actionDeactivate()

Yii::app()->db->createCommand()->renameTable($sOldSurveyTableName, $sNewSurveyTableName);

$archivedTokenSettings = new ArchivedTableSettings();
$archivedTokenSettings->survey_id = $iSurveyID;
$archivedTokenSettings->user_id = $userID;
$archivedTokenSettings->tbl_name = "old_survey_{$iSurveyID}_{$date}";
$archivedTokenSettings->tbl_type = 'response';
$archivedTokenSettings->created = $DBDate;
$archivedTokenSettings->properties = json_encode(Response::getEncryptedAttributes($iSurveyID));
$archivedTokenSettings->save();

$survey->active = 'N';
$survey->save();

Expand All @@ -1603,6 +1624,15 @@ public function actionDeactivate()
$aData['sNewTimingsTableName'] = $sNewTimingsTableName;
}

$archivedTokenSettings = new ArchivedTableSettings();
$archivedTokenSettings->survey_id = $iSurveyID;
$archivedTokenSettings->user_id = $userID;
$archivedTokenSettings->tbl_name = "old_survey_{$iSurveyID}_timings_{$date}";
$archivedTokenSettings->tbl_type = 'timings';
$archivedTokenSettings->created = $DBDate;
$archivedTokenSettings->properties = '';
$archivedTokenSettings->save();

$event = new PluginEvent('afterSurveyDeactivate');
$event->set('surveyId', $iSurveyID);
App()->getPluginManager()->dispatchEvent($event);
Expand Down
9 changes: 8 additions & 1 deletion application/controllers/admin/checkintegrity.php
Expand Up @@ -1010,7 +1010,6 @@ protected function _checkintegrity()
}
}


/**********************************************************************/
/* CHECK OLD TOKEN TABLES */
/**********************************************************************/
Expand Down Expand Up @@ -1086,6 +1085,14 @@ protected function _checkintegrity()
}
}

// delete archivedTableSettings without archived table
$archivedTableSettings = ArchivedTableSettings::model()->findAll();
foreach ($archivedTableSettings as $archivedTableSetting) {
if (Yii::app()->db->schema->getTable("{{{$archivedTableSetting->tbl_name}}}") === null) {
$archivedTableSetting->delete();
}
}

/**********************************************************************/
/* Check group sort order duplicates */
/**********************************************************************/
Expand Down
15 changes: 14 additions & 1 deletion application/controllers/admin/dataentry.php
Expand Up @@ -312,8 +312,15 @@ public function import($surveyid)
$targetSchema = SurveyDynamic::model($iSurveyId)->getTableSchema();
$sourceTable = PluginDynamic::model($_POST['table']);
$sourceSchema = $sourceTable->getTableSchema();
$encryptedAttributes = Response::getEncryptedAttributes($iSurveyId);
$tbl_name = $sourceSchema->name;
if (strpos($sourceSchema->name, Yii::app()->db->tablePrefix) === 0) {
$tbl_name = substr($sourceSchema->name, strlen(Yii::app()->db->tablePrefix));
}
$archivedTableSettings = ArchivedTableSettings::model()->findByAttributes(['tbl_name' => $tbl_name]);
$archivedEncryptedAttributes = json_decode($archivedTableSettings->properties);

$fieldMap = array();
$fieldMap = [];
$pattern = '/([\d]+)X([\d]+)X([\d]+.*)/';
foreach ($sourceSchema->getColumnNames() as $name) {
// Skip id field.
Expand Down Expand Up @@ -347,6 +354,12 @@ public function import($surveyid)

foreach ($fieldMap as $sourceField => $targetField) {
$targetResponse[$targetField] = $sourceResponse[$sourceField];
if (in_array($sourceField, $archivedEncryptedAttributes, false) && !in_array($sourceField, $encryptedAttributes, false)) {
$targetResponse[$targetField] = $sourceResponse->decryptSingle($sourceResponse[$sourceField]);
}
if (!in_array($sourceField, $archivedEncryptedAttributes, false) && in_array($sourceField, $encryptedAttributes, false)) {
$targetResponse[$targetField] = $sourceResponse->encryptSingle($sourceResponse[$sourceField]);
}
}

if (isset($targetSchema->columns['startdate']) && empty($targetResponse['startdate'])) {
Expand Down

0 comments on commit a326ac0

Please sign in to comment.